pa...">

Add C API for importing an attribute from a module · Issue #93741 · python/cpython (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

@serhiy-storchaka

Description

@serhiy-storchaka

It is common (more than 30 cases) in the C code to access a function or a variable from other module. For example:

functools = PyImport_ImportModule("functools");
if (!functools)
    goto error;
st->partial = PyObject_GetAttrString(functools, "partial");
Py_CLEAR(functools);

I propose to add a private helper function which combines PyImport_ImportModule() and PyObject_GetAttrString().

st->partial = _PyImport_GetModuleAttrString("functools", "partial");

It will save 4-6 lines of code and a variable on every use.