Skip to content
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

Add C API for importing an attribute from a module #93741

Closed
serhiy-storchaka opened this issue Jun 12, 2022 · 2 comments · Fixed by #93742
Closed

Add C API for importing an attribute from a module #93741

serhiy-storchaka opened this issue Jun 12, 2022 · 2 comments · Fixed by #93742
Labels
expert-C-API type-feature

Comments

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Jun 12, 2022

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.

@serhiy-storchaka serhiy-storchaka added type-feature expert-C-API labels Jun 12, 2022
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jun 12, 2022
It combines PyImport_ImportModule() and PyObject_GetAttrString()
and saves 4-6 lines of code on every use.
@kumaraditya303
Copy link
Contributor

@kumaraditya303 kumaraditya303 commented Jun 13, 2022

Since this will be private API, how about using statically allocated string identifiers to avoid memory allocations?

@serhiy-storchaka
Copy link
Member Author

@serhiy-storchaka serhiy-storchaka commented Jun 13, 2022

The PR adds also function _PyImport_GetModuleAttr() which takes PyObject* arguments. You can use it with statically allocated string identifiers.

serhiy-storchaka added a commit that referenced this issue Jun 14, 2022
It combines PyImport_ImportModule() and PyObject_GetAttrString()
and saves 4-6 lines of code on every use.

Add also _PyImport_GetModuleAttr() which takes Python strings as arguments.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jun 14, 2022
…g() (pythonGH-93742)

It combines PyImport_ImportModule() and PyObject_GetAttrString()
and saves 4-6 lines of code on every use.

Add also _PyImport_GetModuleAttr() which takes Python strings as arguments.
(cherry picked from commit 6fd4c8e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jun 14, 2022
…g() (pythonGH-93742)

It combines PyImport_ImportModule() and PyObject_GetAttrString()
and saves 4-6 lines of code on every use.

Add also _PyImport_GetModuleAttr() which takes Python strings as arguments.
(cherry picked from commit 6fd4c8e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jun 14, 2022
…g() (pythonGH-93742)

It combines PyImport_ImportModule() and PyObject_GetAttrString()
and saves 4-6 lines of code on every use.

Add also _PyImport_GetModuleAttr() which takes Python strings as arguments.
(cherry picked from commit 6fd4c8e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Jun 14, 2022
…H-93742) (GH-93792)

It combines PyImport_ImportModule() and PyObject_GetAttrString()
and saves 4-6 lines of code on every use.

Add also _PyImport_GetModuleAttr() which takes Python strings as arguments.
(cherry picked from commit 6fd4c8e)
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 16, 2022
…g() (pythonGH-93742) (pythonGH-93792)

It combines PyImport_ImportModule() and PyObject_GetAttrString()
and saves 4-6 lines of code on every use.

Add also _PyImport_GetModuleAttr() which takes Python strings as arguments.
(cherry picked from commit 6fd4c8e)
(cherry picked from commit d42b368)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington added a commit that referenced this issue Jun 16, 2022
…H-93742) (GH-93792)

It combines PyImport_ImportModule() and PyObject_GetAttrString()
and saves 4-6 lines of code on every use.

Add also _PyImport_GetModuleAttr() which takes Python strings as arguments.
(cherry picked from commit 6fd4c8e)
(cherry picked from commit d42b368)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expert-C-API type-feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants