Hey, Mom! The Explanation.

Here's the permanent dedicated link to my first Hey, Mom! post and the explanation of the feature it contains.

Wednesday, October 17, 2018

A Sense of Doubt blog post #1335 - Coding Tip: Python Fault Handler

A Sense of Doubt blog post #1335 - Coding Tip: Python Fault Handler


Okay, so because of Grading Hell, I have yet again fallen behind, this time three days, as it's Saturday while I type these words. So prepare for three quickly made, share-centric posts simply to catch up and then two more original posts for Saturday and Sunday. I may skip Musical Monday this week and roll out  a new one this coming Monday.

This post started with one of the tips and tricks emails from Dan Bader over at Real Python.

I have not been able to secure any time for coding recently in my schedule, but maybe this post will inspire me.

Possibly, some people find this tip useful. I know I will when coding in Python.

# Python 3.3+ has a std
# lib module for displaying
# tracebacks even when Python
# "dies", e.g with a segfault:

import faulthandler
faulthandler.enable()

# Can also be enabled with
# "python -X faulthandler"
# from the command line.

# Learn more here:
# https://docs.python.org/3/library/faulthandler.html

faulthandler — Dump the Python traceback

New in version 3.3.

This module contains functions to dump Python tracebacks explicitly, on a fault, after a timeout, or on a user signal. Call faulthandler.enable() to install fault handlers for the SIGSEGVSIGFPESIGABRTSIGBUS, and SIGILL signals. You can also enable them at startup by setting the PYTHONFAULTHANDLER environment variable or by using the -X faulthandler command line option.

The fault handler is compatible with system fault handlers like Apport or the Windows fault handler. The module uses an alternative stack for signal handlers if the sigaltstack() function is available. This allows it to dump the traceback even on a stack overflow.

Example

Example of a segmentation fault on Linux with and without enabling the fault handler:
$ python3 -c "import ctypes; ctypes.string_at(0)"
Segmentation fault

$ python3 -q -X faulthandler
>>> import ctypes
>>> ctypes.string_at(0)
Fatal Python error: Segmentation fault

Current thread 0x00007fb899f39700 (most recent call first):
  File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
  File "", line 1 in 
Segmentation fault


If you think your friends would find this tip useful, please share it with them—I’d really appreciate it. Here's the link: View in browser

To make sure you keep getting these emails, please add info@realpython.com to your address book or whitelist us. Want out of the loop? Unsubscribe.

308 E. 5th Ave, Vancouver BC V5T 1H4

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


- Bloggery committed by chris tower - 1810.17 - 10:10

- Days ago = 1201 days ago

- New note - On 1807.06, I ceased daily transmission of my Hey Mom feature after three years of daily conversations. I plan to continue Hey Mom posts at least twice per week but will continue to post the days since ("Days Ago") count on my blog each day. The blog entry numbering in the title has changed to reflect total Sense of Doubt posts since I began the blog on 0705.04, which include Hey Mom posts, Daily Bowie posts, and Sense of Doubt posts. Hey Mom posts will still be numbered sequentially. New Hey Mom posts will use the same format as all the other Hey Mom posts; all other posts will feature this format seen here.

No comments: