-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-144330: Initialize staticmethod/classmethod callables in tp_new #144364
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
Open
Aniketsy
wants to merge
8
commits into
python:main
Choose a base branch
from
Aniketsy:fix-144330
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+50
−4
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e97e3ec
Initialize staticmethod/classmethod callables in tp_new
Aniketsy a81471a
Merge branch 'main' into fix-144330
Aniketsy 623d522
Update Objects/funcobject.c
Aniketsy fc67291
Update Objects/funcobject.c
Aniketsy 56f8640
Update Lib/test/test_descr.py
Aniketsy ab784f0
Update Lib/test/test_descr.py
Aniketsy 270d183
Update Lib/test/test_descr.py
Aniketsy c2dd91b
Update Lib/test/test_descr.py
Aniketsy 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
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
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 |
|---|---|---|
|
|
@@ -1555,11 +1555,15 @@ static PyMethodDef cm_methodlist[] = { | |
| {NULL} /* Sentinel */ | ||
| }; | ||
|
|
||
| static PyObject *cm_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | ||
| static PyObject *sm_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | ||
|
|
||
| static PyObject* | ||
| cm_repr(PyObject *self) | ||
| { | ||
| classmethod *cm = _PyClassMethod_CAST(self); | ||
| return PyUnicode_FromFormat("<classmethod(%R)>", cm->cm_callable); | ||
| PyObject *callable = cm->cm_callable != NULL ? cm->cm_callable : Py_None; | ||
| return PyUnicode_FromFormat("<classmethod(%R)>", callable); | ||
| } | ||
|
|
||
| PyDoc_STRVAR(classmethod_doc, | ||
|
|
@@ -1623,7 +1627,7 @@ PyTypeObject PyClassMethod_Type = { | |
| offsetof(classmethod, cm_dict), /* tp_dictoffset */ | ||
| cm_init, /* tp_init */ | ||
| PyType_GenericAlloc, /* tp_alloc */ | ||
| PyType_GenericNew, /* tp_new */ | ||
| cm_new, /* tp_new */ | ||
| PyObject_GC_Del, /* tp_free */ | ||
| }; | ||
|
|
||
|
|
@@ -1638,6 +1642,18 @@ PyClassMethod_New(PyObject *callable) | |
| return (PyObject *)cm; | ||
| } | ||
|
|
||
| static PyObject * | ||
| cm_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||
| { | ||
| classmethod *cm = (classmethod *)PyType_GenericAlloc(type, 0); | ||
| if (cm == NULL) { | ||
| return NULL; | ||
| } | ||
| cm->cm_callable = Py_None; | ||
| cm->cm_dict = NULL; | ||
| return (PyObject *)cm; | ||
| } | ||
|
|
||
|
|
||
| /* Static method object */ | ||
|
|
||
|
|
@@ -1796,7 +1812,8 @@ static PyObject* | |
| sm_repr(PyObject *self) | ||
| { | ||
| staticmethod *sm = _PyStaticMethod_CAST(self); | ||
| return PyUnicode_FromFormat("<staticmethod(%R)>", sm->sm_callable); | ||
| PyObject *callable = sm->sm_callable != NULL ? sm->sm_callable : Py_None; | ||
| return PyUnicode_FromFormat("<staticmethod(%R)>", callable); | ||
| } | ||
|
|
||
| PyDoc_STRVAR(staticmethod_doc, | ||
|
|
@@ -1858,7 +1875,7 @@ PyTypeObject PyStaticMethod_Type = { | |
| offsetof(staticmethod, sm_dict), /* tp_dictoffset */ | ||
| sm_init, /* tp_init */ | ||
| PyType_GenericAlloc, /* tp_alloc */ | ||
| PyType_GenericNew, /* tp_new */ | ||
| sm_new, /* tp_new */ | ||
| PyObject_GC_Del, /* tp_free */ | ||
| }; | ||
|
|
||
|
|
@@ -1872,3 +1889,14 @@ PyStaticMethod_New(PyObject *callable) | |
| } | ||
| return (PyObject *)sm; | ||
| } | ||
|
|
||
| static PyObject * | ||
| sm_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||
| { | ||
| staticmethod *sm = (staticmethod *)PyType_GenericAlloc(type, 0); | ||
| if (sm == NULL) | ||
| return NULL; | ||
| sm->sm_callable = Py_NewRef(Py_None); | ||
| sm->sm_dict = NULL; | ||
| return (PyObject *)sm; | ||
| } | ||
|
Member
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. Move sm_new() before PyStaticMethod_Type. |
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move cm_new() before PyClassMethod_Type, so you don't need to declare it in advance (remove forward declaration line 1558).