Skip to content

gh-155742: Check singletons consistency at Python exit - #157572

Merged
vstinner merged 12 commits into
python:mainfrom
vstinner:global_obj_fini_check
Sep 17, 2026
Merged

vstinner merged 12 commits into
python:mainfrom
vstinner:global_obj_fini_check

Conversation

@vstinner

@vstinner vstinner commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

In debug mode at Python exit, check if immutable singleton objects have been modified by mistake to detect bugs in C extensions.

Add tests corrupting bytes, str, bool and int singleton objects.

In debug mode at Python exit, check if immutable singleton objects
have been modified by mistake to detect bugs in C extensions.

Add tests corrupting bytes, str, bool and int singleton objects.
if (!_Py_IsImmortal(obj)) {
fprintf(stderr, "Immortal Object has less refcnt than expected.\n");
PyObject_Dump(obj);
_PyStaticObject_CheckSingleton(PyObject *obj, PyTypeObject *type)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems weird to have this much hand-written code in *_generated.h, which is hidden by default in GitHub reviews.
Did you consider moving the supporting functions to pycore_global_objects.h for example?

* Add a comment explaining why the bytes empty string doesn't need
  _PyBytes_CheckOverflow().
* Share code for unicode checks.
@vstinner

Copy link
Copy Markdown
Member Author

@encukou: I addressed your review.

  • I added a comment to explain why _PyBytes_CheckOverflow() is omitted on the empty string.
  • I add a sub-function to share unicode object checks.
  • Change _PyStaticObject_CheckUnicodeCharSingleton() to hardcode the size of 1 character.
It's fine to call it on an empty string.
@vstinner

Copy link
Copy Markdown
Member Author

I simplified _PyStaticObject_CheckBytesSingleton() to always call _PyBytes_CheckOverflow().

Comment thread Lib/test/test_capi/test_misc.py Resolved
@encukou

encukou commented Sep 17, 2026

Copy link
Copy Markdown
Member

Thanks. I sent another simplification as vstinner#17
Please do consider moving the hand-written functions to a file that's shown in reviews by default.

@vstinner
vstinner requested a review from itamaro as a code owner September 17, 2026 12:31
@vstinner

Copy link
Copy Markdown
Member Author

@encukou:

Please do consider moving the hand-written functions to a file that's shown in reviews by default.

Oh right, pycore_global_objects_fini_generated.h contains hand written code and generated code, it's not new in my PR. But yeah, I was also annoyed by that: I just made a change adding pycore_global_objects_fini.h for hand written code.

I also updated pycore_runtime_init_generated.h to fully generate the file by script.

I left pycore_global_strings.h and pycore_unicodeobject_generated.h as they are (hand written code + generated code), someone else can update them if they want :-) I prefer to not make too many changes in this PR.

@vstinner
vstinner merged commit 8bcbcf8 into python:main Sep 17, 2026
54 checks passed
@vstinner
vstinner deleted the global_obj_fini_check branch September 17, 2026 14:03
@vstinner

Copy link
Copy Markdown
Member Author

Merged. Thanks for your reviews and contribution @encukou.

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

Labels

None yet

2 participants