-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [new] Add new CI features and keyboard interrupt handling * [docs] Update README and docstrings * [test] Added tests for many features
- Loading branch information
1 parent
fc02327
commit 32d59cc
Showing
6 changed files
with
426 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: lint | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- "**.py" | ||
push: | ||
paths: | ||
- "**.py" | ||
|
||
jobs: | ||
flake8: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout source | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.8 | ||
architecture: x64 | ||
|
||
- name: Run flake8 | ||
uses: py-actions/flake8@v2 | ||
with: | ||
max-line-length: "90" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,82 @@ | ||
# pyterminate | ||
|
||
Reliably run cleanup upon program termination. | ||
Reliably run cleanup code upon program termination. | ||
|
||
## Table of Contents | ||
|
||
- [Why does this exist?](#why-does-this-exist?) | ||
- [What can it do?](#what-can-it-do?) | ||
- [Quickstart](#quickstart) | ||
|
||
## Why does this exist? | ||
|
||
There are currently two builtin modules for handling termination behavior | ||
in Python: [`atexit`](https://docs.python.org/3/library/atexit.html) and | ||
[`signal`](https://docs.python.org/3/library/signal.html). However, using them | ||
directly leads to a lot of repeated boilerplate code, and some non-obvious | ||
behaviors that can be easy to accidentally get wrong, which is why I wrote this | ||
package. | ||
|
||
The `atexit` module is currently insufficient since it fails to handle signals. | ||
The `signal` module is currently insufficient since it fails to handle normal | ||
or exception-caused exits. | ||
|
||
Typical approaches would include frequently repeated code registering a | ||
function both with `atexit` and on desired signals. However, extra care | ||
sometimes needs to be taken to ensure the function doesn't run twice (or is | ||
idempotent), and that a previously registered signal handler gets called. | ||
|
||
## What can it do? | ||
|
||
This packages does or allows the following behavior: | ||
|
||
- Register a function to be called on program termination | ||
- Always on normal or exception-caused termination: `@pyterminate.register` | ||
- Configurable for any desired signals:<br/> | ||
`@pyterminate.register(signals=(signal.SIGINT, signal.SIGABRT))` | ||
|
||
- Allows multiple functions to be registered | ||
|
||
- Will call previous registered signal handlers | ||
|
||
- Allows zero or non-zero exit codes on captured signals:<br/> | ||
`@pyterminate.register(successful_exit=True)` | ||
|
||
- Allows suppressing or throwing of `KeyboardInterrupt` on `SIGINT`:<br/> | ||
`@pyterminate.register(keyboard_interrupt_on_sigint=True)` | ||
- You may want to throw a `KeyboardInterrupt` if there is additional | ||
exception handling defined. | ||
|
||
- Allows functions to be unregistered: `pyterminate.unregister(func)` | ||
|
||
- Ignore multiple repeated signals to allow the registered functions to | ||
complete | ||
- However, it can be canceled upon receipt of another signal. Desired | ||
behavior could vary application to application, but this feels appropriate | ||
for the most common known use cases. | ||
|
||
## Quickstart | ||
|
||
```bash | ||
python3 -m pip install pyterminate | ||
``` | ||
|
||
```python3 | ||
import signal | ||
|
||
import pyterminate | ||
|
||
@pyterminate.register(signals=(signal.SIGINT, signal.SIGTERM)) | ||
def cleanup(): | ||
@pyterminate.register( | ||
args=(None,), | ||
kwargs={"b": 42}, | ||
signals=(signal.SIGINT, signal.SIGTERM), | ||
successful_exit=True, | ||
keyboard_interrupt_on_sigint=True | ||
) | ||
def cleanup(*args, **kwargs): | ||
... | ||
|
||
# or | ||
|
||
def cleanup(a, b): | ||
... | ||
|
||
pyterminate.register(cleanup, args=(None, 42)) | ||
pyterminate.register(cleanup, ...) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,11 @@ | |
|
||
setuptools.setup( | ||
name='pyterminate', | ||
version='0.0.1', | ||
version='0.0.2', | ||
url='https://github.com/jeremyephron/pyterminate', | ||
author='Jeremy Ephron', | ||
author_email='[email protected]', | ||
description='Exit programs gracefully', | ||
description='Exit programs gracefully.', | ||
long_description=open('README.md').read(), | ||
long_description_content_type='text/markdown', | ||
packages=setuptools.find_packages(), | ||
|
@@ -15,7 +15,11 @@ | |
'Development Status :: 3 - Alpha', | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 3', | ||
'Programming Language :: Python :: 3.6', | ||
'Programming Language :: Python :: 3.7', | ||
'Programming Language :: Python :: 3.8', | ||
'Programming Language :: Python :: 3.9', | ||
'Programming Language :: Python :: 3.10', | ||
'License :: OSI Approved :: MIT License', | ||
'Operating System :: MacOS', | ||
'Operating System :: Unix', | ||
|
Oops, something went wrong.