← 返回首页
bpo-38787: Add PyCFunction_CheckExact() macro for exact type checks by scoder · Pull Request #20024 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (2) .rst  (1) All 3 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
3 changes: 3 additions & 0 deletions Include/cpython/methodobject.h
Show comments View file Edit file Delete file Open in desktop
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
Expand Up @@ -4,6 +4,9 @@

PyAPI_DATA(PyTypeObject) PyCMethod_Type;

#define PyCMethod_CheckExact(op) Py_IS_TYPE(op, &PyCMethod_Type)
#define PyCMethod_Check(op) PyObject_TypeCheck(op, &PyCMethod_Type)

/* Macros for direct access to these values. Type checks are *not*
done, so use with care. */
#define PyCFunction_GET_FUNCTION(func) \
Expand Down
3 changes: 2 additions & 1 deletion Include/methodobject.h
Show comments View file Edit file Delete file Open in desktop
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
Expand Up @@ -13,7 +13,8 @@ extern "C" {

PyAPI_DATA(PyTypeObject) PyCFunction_Type;

#define PyCFunction_Check(op) (Py_IS_TYPE(op, &PyCFunction_Type) || (PyType_IsSubtype(Py_TYPE(op), &PyCFunction_Type)))
#define PyCFunction_CheckExact(op) Py_IS_TYPE(op, &PyCFunction_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.

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

I would prefer to exclude PyCFunction_CheckExact() from the limited C API.

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 Hide comment

I'm not so sure. We are changing the semantics of PyCFunction_Check() by adding a subtype that it covers. The macro is a part of the limited C-API and thus, the semantic change is a problem all by itself. Either we accept that, then we should add the "obvious" related CheckExact macro, also to the limited API. Or, we reject that change all together. I don't think the latter is an option, though.

Personally, I'd say, since it's a macro, it doesn't hurt, since it has no impact at all on the ABI. Everyone can safely copy and use that macro also on their side, so why not just add it?

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 Hide comment

I will not hold this PR just for that. We can should PyCFunction_CheckExact() as it is in Python 3.9, and revisit the issue later. I created https://bugs.python.org/issue40601 to explain my concern: I plan to work on that in Python 3.10, it's too late for Python 3.9.

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 Hide comment

Do note that PyCMethod is a bit of a hack in that PyCFunction is still not an "acceptable base type". You can't subclasstype(print), but static types happen to bypass the Py_TPFLAGS_BASETYPE check. This is one more thing to be resolved if we want all types can become heap types.

#define PyCFunction_Check(op) PyObject_TypeCheck(op, &PyCFunction_Type)

typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
typedef PyObject *(*_PyCFunctionFast) (PyObject *, PyObject *const *, Py_ssize_t);
Expand Down
Show comments View file Edit file Delete file Open in desktop
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,2 @@
Add PyCFunction_CheckExact() macro for exact type checks now that we allow subtypes of PyCFunction,
as well as PyCMethod_CheckExact() and PyCMethod_Check() for the new PyCMethod subtype.
2 changes: 1 addition & 1 deletion Objects/abstract.c
Show comments View file Edit file Delete file Open in desktop
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
Expand Up @@ -900,7 +900,7 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
Py_DECREF(result);

if (op_slot == NB_SLOT(nb_rshift) &&
PyCFunction_Check(v) &&
PyCFunction_CheckExact(v) &&
strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
{
PyErr_Format(PyExc_TypeError,
Expand Down
4 changes: 2 additions & 2 deletions Python/ceval.c
Show comments View file Edit file Delete file Open in desktop
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
Expand Up @@ -5054,7 +5054,7 @@ trace_call_function(PyThreadState *tstate,
PyObject *kwnames)
{
PyObject *x;
if (PyCFunction_Check(func)) {
if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
return x;
}
Expand Down Expand Up @@ -5115,7 +5115,7 @@ do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject
{
PyObject *result;

if (PyCFunction_Check(func)) {
if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
C_TRACE(result, PyObject_Call(func, callargs, kwdict));
return result;
}
Expand Down
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.