Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
tried to clean up language
Source Link
Eli Collins
  • 8.6k
  • 2
  • 36
  • 38

Out of the box, atexit is unfortunately not oriented towards doingquite suited for what you want... to do: it's use is primarily used for resource cleanup at the very last moment, as things are shutting down and exiting. By analogy, it's the "finally" of a try/fexceptexcept, whereas what you want is the "else" of a try/except.

The simplest way would be to continueI can think of is continuing to use atexit, but create a global flag somewhere which you set only when your script "succeeds"... and then have all the functions you attach to atexitatexit check that flag, and do nothing unless it's been set.

Eg:

_success = False def atsuccess(func, *args, **kwds): def wrapper(): if _success: func(*args,**kwds) atexit(wrapper) def succeededset_success(): global _success _success = True # then call atsuccess() to attach your callbacks, # and call succeededset_success() before your script returns 

One limitation is if you have internalany code callingwhich calls sys.exit(0) before setting the success flag. ItSuch code should probably(probably) be refactored to return to the main function instead of callingfirst, so that you call set_success and sys.exit, but to make things still work in only one place. Failing that, you'dyou'll need to doadd something like the following wrapper around the main entry point in your script:

try: main() except SystemExit, err: if err.code == 0: succeededset_success() raise 

atexit is unfortunately not oriented towards doing what you want... it's use is primarily resource cleanup at the very last moment, as things are shutting down and exiting. By analogy, it's the "finally" of a try/fexcept, whereas what you want is the "else" of a try/except.

The simplest way would be to continue to use atexit, but create a global flag somewhere which you set only when your script "succeeds"... and then have all the functions you attach to atexit check that flag, and do nothing unless it's been set.

Eg:

_success = False def atsuccess(func, *args, **kwds): def wrapper(): if _success: func(*args,**kwds) atexit(wrapper) def succeeded(): global _success _success = True # then call atsuccess() to attach your callbacks, # and call succeeded() before your script returns 

One limitation is if you have internal code calling sys.exit(0) before setting the success flag. It should probably be refactored to return to the main function instead of calling sys.exit, but to make things still work, you'd need to do something like the following around the main entry point in your script:

try: main() except SystemExit, err: if err.code == 0: succeeded() raise 

Out of the box, atexit is not quite suited for what you want to do: it's primarily used for resource cleanup at the very last moment, as things are shutting down and exiting. By analogy, it's the "finally" of a try/except, whereas what you want is the "else" of a try/except.

The simplest way I can think of is continuing to create a global flag which you set only when your script "succeeds"... and then have all the functions you attach to atexit check that flag, and do nothing unless it's been set.

Eg:

_success = False def atsuccess(func, *args, **kwds): def wrapper(): if _success: func(*args,**kwds) atexit(wrapper) def set_success(): global _success _success = True # then call atsuccess() to attach your callbacks, # and call set_success() before your script returns 

One limitation is if you have any code which calls sys.exit(0) before setting the success flag. Such code should (probably) be refactored to return to the main function first, so that you call set_success and sys.exit in only one place. Failing that, you'll need add something like the following wrapper around the main entry point in your script:

try: main() except SystemExit, err: if err.code == 0: set_success() raise 
added 25 characters in body
Source Link
Eli Collins
  • 8.6k
  • 2
  • 36
  • 38

atexit is unfortunately not oriented towards doing what you want... it's use is primarily resource cleanup at the very last moment, as things are shutting down and exiting. By analogy, it's the "finally" of a try/fexcept, whereas what you want is the "else" of a try/except.

The simplest way would be to continue to use atexit, but create a global flag somewhere which you set only when your script "succeeds"... and then have all the functions you attach to atexit check that flag, and do nothing unless it's been set.

Eg:

_success = False def atsuccess(func, *args, **kwds): def wrapper(): if _success: func(*args,**kwds) atexit(wrapper) def succeeded(): global _success _success = True # then call atsuccess() to attach your callbacks, # and call succeeded() before your script returns 

 

Another way to do thisOne limitation is to isolate the executing part of your script inside aif you have internal code calling mainsys.exit(0) before setting the success flag. It should probably be refactored to return to the main function or equivalentinstead of calling sys.exit, create a global registry inside it's module where code can attach callbacksbut to make things still work, and then run those atyou'd need to do something like the end offollowing around the main() method. Thus, they won't get called if an exception is raised. But that's more complex to code probably. entry point in your script:

try: main() except SystemExit, err: if err.code == 0: succeeded() raise 

atexit is unfortunately not oriented towards doing what you want... it's use is primarily resource cleanup at the very last moment, as things are shutting down and exiting. By analogy, it's the "finally" of a try/fexcept, whereas what you want is the "else" of a try/except.

The simplest way would be to continue to use atexit, but create a global flag somewhere which you set only when your script "succeeds"... and then have all the functions you attach to atexit check that flag, and do nothing unless it's been set.

Eg:

_success = False def atsuccess(func, *args, **kwds): def wrapper(): if _success: func(*args,**kwds) atexit(wrapper) def succeeded(): global _success _success = True # then call atsuccess() to attach your callbacks, # and call succeeded() before your script returns 

 

Another way to do this is to isolate the executing part of your script inside a main() function or equivalent, create a global registry inside it's module where code can attach callbacks, and then run those at the end of the main() method. Thus, they won't get called if an exception is raised. But that's more complex to code probably.

atexit is unfortunately not oriented towards doing what you want... it's use is primarily resource cleanup at the very last moment, as things are shutting down and exiting. By analogy, it's the "finally" of a try/fexcept, whereas what you want is the "else" of a try/except.

The simplest way would be to continue to use atexit, but create a global flag somewhere which you set only when your script "succeeds"... and then have all the functions you attach to atexit check that flag, and do nothing unless it's been set.

Eg:

_success = False def atsuccess(func, *args, **kwds): def wrapper(): if _success: func(*args,**kwds) atexit(wrapper) def succeeded(): global _success _success = True # then call atsuccess() to attach your callbacks, # and call succeeded() before your script returns 

One limitation is if you have internal code calling sys.exit(0) before setting the success flag. It should probably be refactored to return to the main function instead of calling sys.exit, but to make things still work, you'd need to do something like the following around the main entry point in your script:

try: main() except SystemExit, err: if err.code == 0: succeeded() raise 
added 25 characters in body
Source Link
Eli Collins
  • 8.6k
  • 2
  • 36
  • 38

atexit is unfortunately not designed to dooriented towards doing what you want. It's.. it's use is primarily resource cleanup at the very last moment, as things are shutting down and exiting. This matches underlying C functionality which does the same. By analogy, it's the "finally" of a try/fexcept, whereas what you want is the "else" of a try/except.

The simplest way would be to continue to use atexit, but create a global flag somewhere which you set only when your script "succeeds"... and then have all the functions you attach to atexit check that flag, and do nothing unless it's been set.

Eg:

_success = False def atsuccess(func, *args, **kwds): def wrapper(): if _success: func(*args,**kwds) atexit(wrapper) def succeeded(): global _success _success = True # then call atsuccess() to attach your callbacks, # and call succeeded() before your script returns 

Another way to do this is to isolate the executing part of your script inside a main() function or equivalent, create a global registry inside it's module where code can attach callbacks, and then run those at the end of the main() method. Thus, they won't get called if an exception is raised. But that's more complex to code probably.

atexit is unfortunately not designed to do what you want. It's use is primarily resource cleanup at the very last moment, as things are shutting down and exiting. This matches underlying C functionality which does the same. By analogy, it's the "finally" of a try/fexcept, whereas what you want is the "else" of a try/except.

The simplest way would be to continue to use atexit, but create a global flag somewhere which you set only when your script "succeeds"... and then have all the functions you attach to atexit check that flag, and do nothing unless it's been set.

Eg:

_success = False def atsuccess(func, *args, **kwds): def wrapper(): if _success: func(*args,**kwds) atexit(wrapper) def succeeded(): _success = True # then call atsuccess() to attach your callbacks, # and call succeeded() before your script returns 

Another way to do this is to isolate the executing part of your script inside a main() function or equivalent, create a global registry inside it's module where code can attach callbacks, and then run those at the end of the main() method. Thus, they won't get called if an exception is raised. But that's more complex to code probably.

atexit is unfortunately not oriented towards doing what you want... it's use is primarily resource cleanup at the very last moment, as things are shutting down and exiting. By analogy, it's the "finally" of a try/fexcept, whereas what you want is the "else" of a try/except.

The simplest way would be to continue to use atexit, but create a global flag somewhere which you set only when your script "succeeds"... and then have all the functions you attach to atexit check that flag, and do nothing unless it's been set.

Eg:

_success = False def atsuccess(func, *args, **kwds): def wrapper(): if _success: func(*args,**kwds) atexit(wrapper) def succeeded(): global _success _success = True # then call atsuccess() to attach your callbacks, # and call succeeded() before your script returns 

Another way to do this is to isolate the executing part of your script inside a main() function or equivalent, create a global registry inside it's module where code can attach callbacks, and then run those at the end of the main() method. Thus, they won't get called if an exception is raised. But that's more complex to code probably.

changed mind, made 2nd idea the primary suggestion
Source Link
Eli Collins
  • 8.6k
  • 2
  • 36
  • 38
Loading
Source Link
Eli Collins
  • 8.6k
  • 2
  • 36
  • 38
Loading