Skip to content

gh-145559: Add PyUnstable_DumpTraceback() and PyUnstable_DumpTracebackThreads()#145560

Open
alexmalyshev wants to merge 6 commits intopython:mainfrom
alexmalyshev:dump-traceback
Open

gh-145559: Add PyUnstable_DumpTraceback() and PyUnstable_DumpTracebackThreads()#145560
alexmalyshev wants to merge 6 commits intopython:mainfrom
alexmalyshev:dump-traceback

Conversation

@alexmalyshev
Copy link
Contributor

@alexmalyshev alexmalyshev commented Mar 5, 2026

These functions stopped being exported in #107215. However, they are the only way to print a Python stacktrace safely from a signal handler, making them very useful for extensions. Re-export them.

@bedevere-app
Copy link

bedevere-app bot commented Mar 5, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@vstinner
Copy link
Member

vstinner commented Mar 5, 2026

Rather than exporting internal C API functions, I would prefer to promote them as public or PyUnstable functions.

@bedevere-app
Copy link

bedevere-app bot commented Mar 5, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@alexmalyshev
Copy link
Contributor Author

Made them PyUnsafe_ functions. I believe that the mentions to them in the configure and emscripten files can just be deleted now that they'll be public.

@sergey-miryanov
Copy link
Contributor

I believe they should have PyUnstable_ prefix, not PyUnsafe_.

@alexmalyshev
Copy link
Contributor Author

I believe they should have PyUnstable_ prefix, not PyUnsafe_.

Ack I always make this mistake...

…functions

These functions stopped being exported in python#107215.  However, they are the only
way to print a Python stacktrace safely from a signal handler, making them very
useful for extensions.  Re-export them as PyUnstable functions.
@sergey-miryanov
Copy link
Contributor

Please, try to avoid force-push.

@freakboy3742
Copy link
Contributor

!buildbot emscripten

@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @freakboy3742 for commit 538a1cd 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F145560%2Fmerge

The command will test the builders whose names match following regular expression: emscripten

The builders matched are:

  • WASM Emscripten PR

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that PyUnstable_DumpTracebackThreads does not work well on the free-threaded build, because other threads may exit and delete their PyThreadState *. I think it's worth adding a warning for that. Here's an example:

.. warning::
    On the :term:`free-threaded build`, this function is not thread-safe. If
    another thread deletes its :term:`thread state` while this function is being
    called, the process will likely crash.

it is very limited. Strings are truncated to 100 characters and encoded to
ASCII with backslashreplace. It doesn't write the source code, only the
function name, filename and line number of each frame. Write only the first
100 frames: if the traceback is truncated, write the line " ...".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds useful to move or copy this comment into the Doc/c-api/ documentation. Same remark for PyUnstable_DumpTracebackThreads() comment.

@vstinner vstinner changed the title gh-145559: Re-export _Py_DumpTraceback and _Py_DumpTracebackThreads gh-145559: Add PyUnstable_DumpTraceback() and PyUnstable_DumpTracebackThreads() Mar 6, 2026
Comment says 100, but it appears to have been 500 since 2012,
54f939b.
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add an entry to the changelog and "What's New in Python 3.15".

For the changelog, you can use blurb_it.
For the "What's New in Python 3.15" page, please add a note under the "C API changes" section (here).

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One little issue with the blurb entry, but otherwise this looks good.

@@ -0,0 +1 @@
Rename `_Py_DumpTraceback` and `_Py_DumpTracebackThreads` to `PyUnstable_DumpTraceback` and `PyUnstable_DumpTracebackThreads`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Rename `_Py_DumpTraceback` and `_Py_DumpTracebackThreads` to `PyUnstable_DumpTraceback` and `PyUnstable_DumpTracebackThreads`.
Rename ``_Py_DumpTraceback`` and ``_Py_DumpTracebackThreads`` to :c:func:`PyUnstable_DumpTraceback` and :c:func:`PyUnstable_DumpTracebackThreads`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants