-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-40217: Clean code in PyType_FromSpec_Alloc and add NEWS entry #19733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
Misc/NEWS.d/next/C API/2020-04-27-14-00-38.bpo-40217.sgn6c8.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Ensure that instances of types created with | ||
:c:func:`PyType_FromSpecWithBases` will visit its class object when | ||
traversing references in the garbage collector (implemented as an extension | ||
of the provided :c:member:`~PyTypeObject.tp_traverse`). Patch by Pablo | ||
Galindo | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1031,25 +1031,29 @@ PyType_FromSpec_Alloc(PyTypeObject *type, Py_ssize_t nitems) | |
/* note that we need to add one, for the sentinel and space for the | ||
provided tp-traverse: See bpo-40217 for more details */ | ||
|
||
if (PyType_IS_GC(type)) | ||
if (PyType_IS_GC(type)) { | ||
obj = _PyObject_GC_Malloc(size); | ||
else | ||
} | ||
else { | ||
obj = (PyObject *)PyObject_MALLOC(size); | ||
} | ||
|
||
if (obj == NULL) | ||
if (obj == NULL) { | ||
return PyErr_NoMemory(); | ||
|
||
obj = obj; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind to keep this instruction? I love it :-D (I'm kidding you, please remove it!) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should add some |
||
} | ||
|
||
memset(obj, '\0', size); | ||
|
||
if (type->tp_itemsize == 0) | ||
if (type->tp_itemsize == 0) { | ||
(void)PyObject_INIT(obj, type); | ||
else | ||
} | ||
else { | ||
(void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); | ||
} | ||
|
||
if (PyType_IS_GC(type)) | ||
if (PyType_IS_GC(type)) { | ||
_PyObject_GC_TRACK(obj); | ||
} | ||
return obj; | ||
} | ||
|
||
|
@@ -3066,7 +3070,11 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) | |
* | ||
* We store the user-provided traverse function at the end of the type | ||
* (we have allocated space for it) so we can call it from our | ||
* PyType_FromSpec_tp_traverse wrapper. */ | ||
* PyType_FromSpec_tp_traverse wrapper. | ||
* | ||
* Check bpo-40217 for more information and rationale about this issue. | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* */ | ||
|
||
type->tp_traverse = PyType_FromSpec_tp_traverse; | ||
size_t _offset = _PyObject_VAR_SIZE(&PyType_Type, nmembers+1); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.