Skip to content

bpo-38787: Add PyCFunction_CheckExact() macro for exact type checks #20024

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 3 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/cpython/methodobject.h
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
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
Member

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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
Member

Choose a reason for hiding this comment

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

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
Member

Choose a reason for hiding this comment

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

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