Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Provide feedback

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

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bpo-42100: Add _PyType_GetModuleByDef#22835

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
encukou merged 1 commit intopython:masterfromencukou:pep-573-walker
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes fromall 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
Add _PyType_GetModuleByDef
  • Loading branch information
@encukou
encukou committedOct 20, 2020
commit0b0d93c320684f9448b6c59f3e13e3ad7fd96d41
2 changes: 2 additions & 0 deletionsInclude/cpython/object.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -296,6 +296,8 @@ PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *);
PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
struct PyModuleDef;
PyAPI_FUNC(PyObject *) _PyType_GetModuleByDef(PyTypeObject *, struct PyModuleDef *);

struct _Py_Identifier;
PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
Expand Down
8 changes: 7 additions & 1 deletionModules/_testmultiphase.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -121,24 +121,30 @@ static PyType_Spec Example_Type_spec = {
};


static PyModuleDef def_meth_state_access;

/*[clinic input]
_testmultiphase.StateAccessType.get_defining_module

cls: defining_class

Return the module of the defining class.

Also tests that result of _PyType_GetModuleByDef matches defining_class's
module.
[clinic start generated code]*/

static PyObject *
_testmultiphase_StateAccessType_get_defining_module_impl(StateAccessTypeObject *self,
PyTypeObject *cls)
/*[clinic end generated code: output=ba2a14284a5d0921 input=946149f91cf72c0d]*/
/*[clinic end generated code: output=ba2a14284a5d0921 input=356f999fc16e0933]*/
{
PyObject *retval;
retval = PyType_GetModule(cls);
if (retval == NULL) {
return NULL;
}
assert(_PyType_GetModuleByDef(Py_TYPE(self), &def_meth_state_access) == retval);
Py_INCREF(retval);
return retval;
}
Expand Down
7 changes: 5 additions & 2 deletionsModules/clinic/_testmultiphase.c.h
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

38 changes: 38 additions & 0 deletionsObjects/typeobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3155,6 +3155,44 @@ PyType_GetModuleState(PyTypeObject *type)
return PyModule_GetState(m);
}


/* Get the module of the first superclass where the module has the
* given PyModuleDef.
* Implemented by walking the MRO, is relatively slow.
*
* This is internal API for experimentation within stdlib. Discussion:
* https://mail.python.org/archives/list/capi-sig@python.org/thread/T3P2QNLNLBRFHWSKYSTPMVEIL2EEKFJU/
*/
PyObject *
_PyType_GetModuleByDef(PyTypeObject *type, struct PyModuleDef *def)
{
assert(PyType_Check(type));
assert(type->tp_mro);
int i;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
int i;

for (i = 0; i < PyTuple_GET_SIZE(type->tp_mro); i++) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for (i=0;i<PyTuple_GET_SIZE(type->tp_mro);i++) {
for (inti=0;i<PyTuple_GET_SIZE(type->tp_mro);i++) {

PyObject *super = PyTuple_GET_ITEM(type->tp_mro, i);
if (!PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) {
/* Currently, there's no way for static types to inherit
* from heap types, but to allow that possibility,
* we `continue` rather than `break`.
* We'll just potentially loop a few more times before throwing
* the error.
*/
continue;
}
PyHeapTypeObject *ht = (PyHeapTypeObject*)super;
if (ht->ht_module && PyModule_GetDef(ht->ht_module) == def) {
return ht->ht_module;
}
}
PyErr_Format(
PyExc_TypeError,
"_PyType_GetModuleByDef: No superclass of '%s' has the given module",
type->tp_name);
return NULL;
}


/* Internal API to look for a name through the MRO, bypassing the method cache.
This returns a borrowed reference, and might set an exception.
'error' is set to: -1: error with exception; 1: error without exception; 0: ok */
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp