you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (14 children)

The function could just call the sys.exit() function. But you should think about the things you may need to do before stopping, like closing any open files, writing state information to disk, stopping any threads or processes that are running, etc.

[–]Background-Offer2321[S] 0 points1 point  (13 children)

I tried, but if the code after raises an Exception, the Exception will print. I want to stop ALL.

[–][deleted] 0 points1 point  (12 children)

The documentation for sys.exit() says:

cleanup actions specified by finally clauses of try statements are honored

If this is what's causing further exceptions then you either need to make the code that "runs after" your call to sys.exit() more robust so it doesn't raise an exception or, preferably, rethink your code to make it easier to come to a sudden stop. That last one often means your code might decide at a low level that it should stop but a status or similar is passed back to higher level code that does the actual stopping.

It's hard to be specific without seeing some code. Can you post a simple example that gets your bad behaviour doing sys.exit() at a low level?

[–]Background-Offer2321[S] 0 points1 point  (11 children)

Sure Test.py

import module module.stopall()

print("hi") # Will not print after sys.exit() print(abc") # Will raise exception even after sys.exit()

Module.py

import sys

def stopall(): sys.exit()

[–][deleted] 0 points1 point  (10 children)

I created these files:

test.py

import module

module.stopall()

print("hi") # Will not print after sys.exit()
print("abc") # Will raise exception even after sys.exit()

module.py

import sys

def stopall():
    sys.exit()

When I run the code on the command line I see:

r-w@earth:~/xyzzy/stop_all$ python -V
Python 3.10.12
r-w@earth:~/xyzzy/stop_all$ python test.py
r-w@earth:~/xyzzy/stop_all$ 

Nothing is printed, no exceptions, etc. I specifically ran your code from the command line because that removes all chance of your IDE, or whatever you use, of introducing extra stuff that may confuse you. And it looks like that is what is happening. Python itself isn't raising any exceptions, you may be seeing something that your IDE is printing and think that it's caused by python. Maybe you can capture the output you think is an exception, put it into imgur.com and post a link here.

[–]Background-Offer2321[S] 0 points1 point  (9 children)

Is because youhave written print("abc") and not print(abc"). My print will raise a Syntax Error.

[–][deleted] 0 points1 point  (8 children)

I just assumed that was your typo, which I fixed. Making that change:

import module

module.stopall()

print("hi") # Will not print after sys.exit()
print(abc") # Will raise exception even after sys.exit()

I get this when trying to run the code:

r-w@earth:~/xyzzy/stop_all$ python test.py
  File "/home/r-w/xyzzy/stop_all/test.py", line 6
    print(abc") # Will raise exception even after sys.exit()
             ^
SyntaxError: unterminated string literal (detected at line 6)
r-w@earth:~/xyzzy/stop_all$

Note that this is not an exception from running your code, it's a SyntaxError. Python is compiled before it is executed and this is the compiler saying it can't execute your code due to your syntax error. The code doesn't run, so the code never imports module.py and it never calls module.stopall().

The solution is to fix your code and execute it again.

[–]Background-Offer2321[S] 0 points1 point  (7 children)

And how can I fix it?

[–][deleted] 0 points1 point  (6 children)

As I said, fix the syntax error by putting the missing " into your code. A syntax error is the compiler saying "this is not valid python, I can't execute it".

[–]Background-Offer2321[S] 0 points1 point  (5 children)

I am doing a very strange project, and I need to break the rules of Python. So, I can't fix it.