Title: [3.7] clean extern PyGC_Head *_PyGC_generation0; in Include/objimpl.h
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: egaudry, gvanrossum, pablogsal, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2019-10-21 09:43 by egaudry, last changed 2019-10-21 15:09 by egaudry. This issue is now closed.

File name Uploaded Description Edit
gc.diff egaudry, 2019-10-21 09:43 patch for exposing _PyGC_generation0 on all platforms
Messages (8)
msg355034 - (view) Author: egaudry (egaudry) * Date: 2019-10-21 09:43

I would like to be able to get an handle on PyGC_Head*_PyGC_generation0 from a CPython extension.
This is possible when building Python on a Posix host, but not on Windows because of a missing PyAPI_DATA wrapping of the said object in the objimpl.h header.

Having the possibility to inspect the PyGC_Head here can be very useful when customizing the way a garbage-collectable object created from CPython extension would be cleaned/inspected, etc.

Any chance this patch could be added to get the same capabilities on Windows and Posix platforms ?

Thanks for your feedback,
msg355036 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-10-21 10:04
This issue comes from a thread on python-ideas:

I understand that it's specific to Python 2.7.


_PyGC_generation0 variable no longer exists in Python 3.7, it has been moved into _PyRuntime.gc.generation0:

Include/internal/mem.h:146:#define _PyGC_generation0 _PyRuntime.gc.generation0

In the master branch, _PyRuntime is exported with:

Include/internal/pycore_pystate.h: PyAPI_DATA(_PyRuntimeState) _PyRuntime;

In Python 3.7, _PyRuntime is also exported, but in a different header file:

Include/internal/pystate.h: PyAPI_DATA(_PyRuntimeState) _PyRuntime;


Python 2.7:

Modules/gcmodule.c:50:PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);

Include/objimpl.h:276:extern PyGC_Head *_PyGC_generation0;
msg355058 - (view) Author: egaudry (egaudry) * Date: 2019-10-21 11:34

extern PyGC_Head *_PyGC_generation0; 
is located at line 374 of Include/objimpl.h (ifndef Py_LIMITED_API).

I used it from a CPython extension we are developing as an handle to the collection of objects being garbage collected.

From what you explained, I should use the _PyRuntime exported symbol to get the handle this PyGC_Head. I'll do this.

I guess that cleaning the Include/objimpl.h is the only remaining action to do then.

Thanks for your feedback.
msg355065 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-10-21 13:01
I think it is too late to add this feature in 2.7. See, the feature freeze was 9.5 years ago.
msg355066 - (view) Author: egaudry (egaudry) * Date: 2019-10-21 13:04
Yes, the only thing that remains to be done is cleaning objimpl.h.
msg355068 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-10-21 13:19
The master branch of Python doesn't contain _PyGC_generation0 anymore.

> Yes, the only thing that remains to be done is cleaning objimpl.h.

We don't do cleanup changes in stable branches. Only in the development branch: master. In this case, it's already fixed. So I close the issue.
msg355077 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-10-21 14:48
If it's specific to 2.7, why does the Version metadata say 3.7?
msg355078 - (view) Author: egaudry (egaudry) * Date: 2019-10-21 15:09
I originally submitted an issue asking for having the symbol exported.

But, as Victor mentioned, this part of the code was changed between 2.7 and 3.x.

I then asked if the part of the code defining the symbol (and not used anymore) in 3.7 could be removed/cleaned. This is why it was tagged back to 3.7.

As it was already removed in newer version (3.8?) and because the policy is not to clean dead-code, this issue is now closed.
Date User Action Args
2019-10-21 15:09:20egaudrysetmessages: + msg355078
2019-10-21 14:48:43gvanrossumsetnosy: + gvanrossum
messages: + msg355077
2019-10-21 13:19:58vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg355068

stage: resolved
2019-10-21 13:04:08egaudrysetversions: + Python 3.7, - Python 2.7
messages: + msg355066
title: [2.7] Expose _PyGC_generation0 for allowing internal use directly from a CPython extension -> [3.7] clean extern PyGC_Head *_PyGC_generation0; in Include/objimpl.h
2019-10-21 13:01:33serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg355065
2019-10-21 11:34:56egaudrysetmessages: + msg355058
2019-10-21 10:06:02vstinnersetnosy: + pablogsal
2019-10-21 10:04:59vstinnersetnosy: + vstinner
title: Expose _PyGC_generation0 for allowing internal use directly from a CPython extension -> [2.7] Expose _PyGC_generation0 for allowing internal use directly from a CPython extension
messages: + msg355036

versions: + Python 2.7, - Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9
2019-10-21 09:43:52egaudrycreate