FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-28254: Add a C-API for controlling the state of the garbage collector by scoder · Pull Request #25687 · python/cpython · GitHub

Repository navigation

bpo-28254: Add a C-API for controlling the state of the garbage collector - #25687

Merged
vstinner merged 17 commits into
python:masterfrom
scoder:bpo-28254_gc_capi
Apr 28, 2021
Merged

vstinner merged 17 commits into
python:masterfrom
scoder:bpo-28254_gc_capi

Conversation

scoder commented Apr 28, 2021 •
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Member

Looks good, I will review it in more detail today, but please, add an entry to the what's new document for 3.10 in the meantime.

Comment thread Doc/whatsnew/3.10.rst Outdated
pablogsal and others added 2 commits April 28, 2021 14:44
…ions to make it easy for users to ('atomically') know whether they actually changed something and what state to go back to later. Also, returning an "int" may make it easier to add error handling later, if that becomes needed at some point.
Comment thread Modules/_testcapimodule.c Outdated
Comment thread Modules/_testcapimodule.c Outdated
Comment thread Modules/_testcapimodule.c Outdated

Copy link
Copy Markdown
Member

Thanks for the PR @scoder! Make sure to merge it before end of the week 😉

scoder commented Apr 28, 2021

Copy link
Copy Markdown
Contributor Author

I changed the functions to return the previous state. That's a common idiom in C, and I think a useful one in this case.

Comment thread Doc/whatsnew/3.10.rst
Comment thread Modules/_testcapimodule.c
int old_state;

old_state = PyGC_Enable();
msg = "Enable(1)";

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Bugs are unlikely. IMO using assert() is good enough for _testcapi tests. You can leave the code as it is ;-)

See for example test_refcount_funcs() which uses assert().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I was thinking that assertions can be compiled out, which is decidedly not intended here.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

In general yes, but _testcapimodule.c starts with:

/* Always enable assertions */
#undef NDEBUG

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good to know! I'd keep the test as it is though. It doesn't really hurt, I think.

scoder commented Apr 28, 2021

Copy link
Copy Markdown
Contributor Author

Are the C functions prototypes defined in the right place? Limited API, header file hierarchy, etc.?

Copy link
Copy Markdown
Member

Are the C functions prototypes defined in the right place? Limited API, header file hierarchy, etc.?

You added them to the stable ABI and so you should run "regen-limited-abi".

scoder commented Apr 28, 2021

Copy link
Copy Markdown
Contributor Author

Are the C functions prototypes defined in the right place? Limited API, header file hierarchy, etc.?

You added them to the stable ABI and so you should run "regen-limited-abi".

That added more than the three. I only committed the ones I added here in order to keep the PR clean.
I'll check where the others came from.

Comment thread Doc/c-api/gcsupport.rst Outdated
Comment thread Doc/c-api/gcsupport.rst
If the garbage collector is disabled or already running,
returns ``0`` immediately.
Errors during garbage collection are ignored and printed.
This function does not raise exceptions.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@pablogsal: Do we want to document that it's safe to call this function with an exception raised, and that the exception is saved and then restored?

Comment thread Doc/c-api/gcsupport.rst Outdated
.. c:function:: Py_ssize_t PyGC_Collect(void)

Performs a garbage collection, if the garbage collector is enabled.
Returns the number of collected + uncollectable objects.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Hum, uncollectable objects are stored in gc.garbage no? Shoud we document that?

The C code uses "unreachable objects that couldn't be collected" in a comment instead of "uncollectable objects". Maybe it's a more explicit definition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't think this C-API function is the right place to document these details. That's what the gc module is for.

Comment thread Doc/c-api/gcsupport.rst Outdated
Comment thread Doc/c-api/gcsupport.rst Outdated
Comment thread Doc/c-api/gcsupport.rst

Copy link
Copy Markdown
Member

That added more than the three. I only committed the ones I added here in order to keep the PR clean.
I'll check where the others came from.

Right, thanks: https://bugs.python.org/issue43795#msg392212

Comment thread Doc/c-api/gcsupport.rst Outdated
Comment thread Doc/c-api/gcsupport.rst Outdated
Comment thread Doc/c-api/gcsupport.rst Outdated
Co-authored-by: Victor Stinner <vstinner@python.org>

vstinner left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM but please fix the Sphinx markup.

Comment thread Doc/c-api/gcsupport.rst Outdated
Comment thread Doc/c-api/gcsupport.rst Outdated
Returns the previous state, 0 for disabled and 1 for enabled.

.. versionchanged:: 3.10
.. versionadded:: 3.10

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please fix also the indentation.

vstinner left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM.

vstinner merged commit 3cc481b into python:master Apr 28, 2021
scoder deleted the bpo-28254_gc_capi branch April 28, 2021 16:12

Copy link
Copy Markdown
Member

Merged, thanks @scoder!

I'm not really excited by adding a C API for each Python function, but this one seems common enough to justify adding an API. Also, @pablogsal liked the idea ;-)

Copy link
Copy Markdown
Member

Merged, thanks @scoder!

I'm not really excited by adding a C API for each Python function, but this one seems common enough to justify adding an API. Also, @pablogsal liked the idea ;-)

The GC gods we all serve are pleased with the new API ;)

scoder commented Apr 28, 2021

Copy link
Copy Markdown
Contributor Author

Thanks Victor and Pablo!

Copy link
Copy Markdown
Member

I created PR #25693 which shows how useful are these new C API functions ;-) Ok, maybe, it makes the C code a little bit simpler ;-)

scoder commented Apr 28, 2021

Copy link
Copy Markdown
Contributor Author

35 lines added, 116 lines saved. Seems a good deal.

Copy link
Copy Markdown
Member

FYI, this has broken CI
Example: https://github.com/python/cpython/pull/25719/checks?check_run_id=2466874649
Proposed fix:
#25716

encukou commented Apr 29, 2021 •
edited
Loading

Copy link
Copy Markdown
Member

Yes, this adds things to Doc/data/stable_abi.dat but not PC/python3dll.c, which is quite wrong.
Meanwhile, I merged the part of PEP-652 that will prevent these from going out of sync (by generating them both from a single manifest), but the CI on this PR ran before that and the merge happened after.

From now on, CI will be fail on the pull request when issues like this happen, but this one's timing was unfortunate.

Copy link
Copy Markdown
Member

Ok, then let's merge #25720 to unblock this

This file contains hidden or 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants


Back | FazBrowse Home | New Git URL