Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
bpo-38787: C API for module state access from extension methods (PEP 573)#19936
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
e47e21e0741598d7c3551856f0a33053c42c807aef3e1aa3da17a565f105affFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -147,23 +147,56 @@ Implementing functions and methods | ||
| value of the function as exposed in Python. The function must return a new | ||
| reference. | ||
| The function signature is:: | ||
| PyObject *PyCFunction(PyObject *self, | ||
| PyObject *const *args); | ||
| .. c:type:: PyCFunctionWithKeywords | ||
| Type of the functions used to implement Python callables in C | ||
| with signature :const:`METH_VARARGS | METH_KEYWORDS`. | ||
| The function signature is:: | ||
| PyObject *PyCFunctionWithKeywords(PyObject *self, | ||
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. Dittio. | ||
| PyObject *const *args, | ||
| PyObject *kwargs); | ||
| .. c:type:: _PyCFunctionFast | ||
| Type of the functions used to implement Python callables in C | ||
| with signature :const:`METH_FASTCALL`. | ||
| The function signature is:: | ||
| PyObject *_PyCFunctionFast(PyObject *self, | ||
| PyObject *const *args, | ||
| Py_ssize_t nargs); | ||
| .. c:type:: _PyCFunctionFastWithKeywords | ||
| Type of the functions used to implement Python callables in C | ||
| with signature :const:`METH_FASTCALL | METH_KEYWORDS`. | ||
| The function signature is:: | ||
| PyObject *_PyCFunctionFastWithKeywords(PyObject *self, | ||
| PyObject *const *args, | ||
| Py_ssize_t nargs, | ||
| PyObject *kwnames); | ||
| .. c:type:: PyCMethod | ||
| Type of the functions used to implement Python callables in C | ||
| with signature :const:`METH_METHOD | METH_FASTCALL | METH_KEYWORDS`. | ||
| The function signature is:: | ||
| PyObject *PyCMethod(PyObject *self, | ||
| PyTypeObject *defining_class, | ||
| PyObject *const *args, | ||
| Py_ssize_t nargs, | ||
| PyObject *kwnames) | ||
| .. versionadded:: 3.9 | ||
| .. c:type:: PyMethodDef | ||
| @@ -197,9 +230,7 @@ The :attr:`ml_flags` field is a bitfield which can include the following flags. | ||
| The individual flags indicate either a calling convention or a binding | ||
| convention. | ||
| There are four basic calling conventions for positional arguments | ||
| and two of them can be combined with :const:`METH_KEYWORDS` to support | ||
| also keyword arguments. So there are a total of 6 calling conventions: | ||
| There are these calling conventions: | ||
| .. data:: METH_VARARGS | ||
| @@ -250,6 +281,19 @@ also keyword arguments. So there are a total of 6 calling conventions: | ||
| .. versionadded:: 3.7 | ||
| .. data:: METH_METHOD | METH_FASTCALL | METH_KEYWORDS | ||
| Extension of :const:`METH_FASTCALL | METH_KEYWORDS` supporting the *defining | ||
| class*, that is, the class that contains the method in question. | ||
| The defining class might be a superclass of ``Py_TYPE(self)``. | ||
| The method needs to be of type :c:type:`PyCMethod`, the same as for | ||
| ``METH_FASTCALL | METH_KEYWORDS`` with ``defining_class`` argument added after | ||
| ``self``. | ||
| .. versionadded:: 3.9 | ||
| .. data:: METH_NOARGS | ||
| Methods without parameters don't need to check whether arguments are given if | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #ifndef Py_CPYTHON_METHODOBJECT_H | ||
| # error "this header file must not be included directly" | ||
| #endif | ||
| PyAPI_DATA(PyTypeObject) PyCMethod_Type; | ||
| /* Macros for direct access to these values. Type checks are *not* | ||
| done, so use with care. */ | ||
| #define PyCFunction_GET_FUNCTION(func) \ | ||
| (((PyCFunctionObject *)func) -> m_ml -> ml_meth) | ||
| #define PyCFunction_GET_SELF(func) \ | ||
| (((PyCFunctionObject *)func) -> m_ml -> ml_flags & METH_STATIC ? \ | ||
| NULL : ((PyCFunctionObject *)func) -> m_self) | ||
| #define PyCFunction_GET_FLAGS(func) \ | ||
| (((PyCFunctionObject *)func) -> m_ml -> ml_flags) | ||
| #define PyCFunction_GET_CLASS(func) \ | ||
| (((PyCFunctionObject *)func) -> m_ml -> ml_flags & METH_METHOD ? \ | ||
| ((PyCMethodObject *)func) -> mm_class : NULL) | ||
| typedef struct{ | ||
| PyObject_HEAD | ||
| PyMethodDef *m_ml; /* Description of the C function to call */ | ||
| PyObject *m_self; /* Passed as 'self' arg to the C func, can be NULL */ | ||
| PyObject *m_module; /* The __module__ attribute, can be anything */ | ||
| PyObject *m_weakreflist; /* List of weak references */ | ||
| vectorcallfunc vectorcall; | ||
| } PyCFunctionObject; | ||
| typedef struct{ | ||
| PyCFunctionObject func; | ||
| PyTypeObject *mm_class; /* Class that defines this method */ | ||
| } PyCMethodObject; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -13,7 +13,7 @@ extern "C"{ | ||
| PyAPI_DATA(PyTypeObject) PyCFunction_Type; | ||
| #define PyCFunction_Check(op) Py_IS_TYPE(op, &PyCFunction_Type) | ||
| #define PyCFunction_Check(op) (Py_IS_TYPE(op, &PyCFunction_Type) || (PyType_IsSubtype(Py_TYPE(op), &PyCFunction_Type))) | ||
Contributor 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. This should use the Contributor 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. BTW, I think we need a That probably means that we'll have to look through all usages in CPython to see which is intended. 8-] Also, the usual pattern for Contributor 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. Looks like there weren't all that many such type checks. I pushed #20024. MemberAuthor 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.
No. Subclasses of | ||
| typedef PyObject *(*PyCFunction)(PyObject *, PyObject *); | ||
| typedef PyObject *(*_PyCFunctionFast) (PyObject *, PyObject *const *, Py_ssize_t); | ||
| @@ -22,21 +22,13 @@ typedef PyObject *(*PyCFunctionWithKeywords)(PyObject *, PyObject *, | ||
| typedef PyObject *(*_PyCFunctionFastWithKeywords) (PyObject *, | ||
| PyObject *const *, Py_ssize_t, | ||
| PyObject *); | ||
| typedef PyObject *(*PyCMethod)(PyObject *, PyTypeObject *, PyObject *const *, | ||
| size_t, PyObject *); | ||
| PyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *); | ||
| PyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *); | ||
| PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *); | ||
| /* Macros for direct access to these values. Type checks are *not* | ||
| done, so use with care. */ | ||
| #ifndef Py_LIMITED_API | ||
| #define PyCFunction_GET_FUNCTION(func) \ | ||
| (((PyCFunctionObject *)func) -> m_ml -> ml_meth) | ||
| #define PyCFunction_GET_SELF(func) \ | ||
| (((PyCFunctionObject *)func) -> m_ml -> ml_flags & METH_STATIC ? \ | ||
| NULL : ((PyCFunctionObject *)func) -> m_self) | ||
| #define PyCFunction_GET_FLAGS(func) \ | ||
| (((PyCFunctionObject *)func) -> m_ml -> ml_flags) | ||
| #endif | ||
| Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *); | ||
| struct PyMethodDef{ | ||
| @@ -52,6 +44,13 @@ typedef struct PyMethodDef PyMethodDef; | ||
| PyAPI_FUNC(PyObject *) PyCFunction_NewEx(PyMethodDef *, PyObject *, | ||
| PyObject *); | ||
| #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 | ||
| #define PyCFunction_NewEx(ML, SELF, MOD) PyCMethod_New((ML), (SELF), (MOD), NULL) | ||
| PyAPI_FUNC(PyObject *) PyCMethod_New(PyMethodDef *, PyObject *, | ||
| PyObject *, PyTypeObject *); | ||
| #endif | ||
| /* Flag passed to newmethodobject */ | ||
| /* #define METH_OLDARGS 0x0000 -- unsupported now */ | ||
| #define METH_VARARGS 0x0001 | ||
| @@ -84,15 +83,24 @@ PyAPI_FUNC(PyObject *) PyCFunction_NewEx(PyMethodDef *, PyObject *, | ||
| #define METH_STACKLESS 0x0000 | ||
| #endif | ||
| /* METH_METHOD means the function stores an | ||
| * additional reference to the class that defines it; | ||
| * both self and class are passed to it. | ||
| * It uses PyCMethodObject instead of PyCFunctionObject. | ||
| * May not be combined with METH_NOARGS, METH_O, METH_CLASS or METH_STATIC. | ||
| */ | ||
| #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 | ||
| #define METH_METHOD 0x0200 | ||
| #endif | ||
| #ifndef Py_LIMITED_API | ||
| typedef struct{ | ||
| PyObject_HEAD | ||
| PyMethodDef *m_ml; /* Description of the C function to call */ | ||
| PyObject *m_self; /* Passed as 'self' arg to the C func, can be NULL */ | ||
| PyObject *m_module; /* The __module__ attribute, can be anything */ | ||
| PyObject *m_weakreflist; /* List of weak references */ | ||
| vectorcallfunc vectorcall; | ||
| } PyCFunctionObject; | ||
| #define Py_CPYTHON_METHODOBJECT_H | ||
| #include "cpython/methodobject.h" | ||
| #undef Py_CPYTHON_METHODOBJECT_H | ||
| #endif | ||
| #ifdef __cplusplus | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Module C state is now accessible from C-defined heap type methods (:pep:`573`). | ||
| Patch by Marcel Plch and Petr Viktorin. |
Uh oh!
There was an error while loading. Please reload this page.
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.
Inconsistent argument type compared to
typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);https://github.com/python/cpython/blob/master/Include/methodobject.h#L18