Skip to content

Commit 0044636

Browse files
committed
udapy should end with a non-zero exit code (1) in case of exceptions
fixes #127
1 parent 4c08d40 commit 0044636

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

bin/udapy

+21-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ else:
7171
logging.basicConfig(format='%(asctime)-15s [%(levelname)7s] %(funcName)s - %(message)s',
7272
level=level)
7373

74+
# Global flag to track if an unhandled exception occurred
75+
_unhandled_exception_occurred = False
76+
77+
def _custom_excepthook(exc_type, exc_value, traceback):
78+
global _unhandled_exception_occurred
79+
_unhandled_exception_occurred = True
80+
81+
# Call the default excepthook to allow normal error reporting
82+
sys.__excepthook__(exc_type, exc_value, traceback)
83+
84+
# Override the default excepthook
85+
sys.excepthook = _custom_excepthook
86+
87+
7488
# Process and provide the scenario.
7589
if __name__ == "__main__":
7690

@@ -86,7 +100,13 @@ if __name__ == "__main__":
86100
# Udapi documents have a many cyclic references, so running GC is quite slow.
87101
if not args.gc:
88102
gc.disable()
89-
atexit.register(os._exit, 0)
103+
# When an exception/error has happened, udapy should exit with a non-zero exit code,
104+
# so that users can use `udapy ... || echo "Error detected"` (or Makefile reports errors).
105+
# However, we cannot use `atexit.register(lambda: os._exit(1 if sys.exc_info()[0] else 0))`
106+
# because the Python has already exited the exception-handling block
107+
# (the exception/error has been already reported and sys.exc_info()[0] is None).
108+
# We thus keep record whether _unhandled_exception_occurred.
109+
atexit.register(lambda: os._exit(1 if _unhandled_exception_occurred else 0))
90110
atexit.register(sys.stderr.flush)
91111
if args.save:
92112
args.scenario = args.scenario + ['write.Conllu']

0 commit comments

Comments
 (0)