GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [C API] Add PyImport_ImportModuleAttr(mod_name, attr_name) helper function · Issue #128911 · python/cpython

[C API] Add PyImport_ImportModuleAttr(mod_name, attr_name) helper function #128911

New issue
Copy link
New issue
Copy link
Closed
Closed
[C API] Add PyImport_ImportModuleAttr(mod_name, attr_name) helper function#128911
Copy link
Labels
topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

Description

@vstinner
vstinner
opened on Jan 16, 2025
Issue body actions

Feature or enhancement

Proposal:

Python has an internal _PyImport_GetModuleAttrString(mod_name, attr_name) helper function to import a module and get a module attribute. I propose to make this function public to be able to use it outside Python.

UPDATE: Function renamed to PyImport_ImportModuleAttrString().

The function is convenient to use and is used by the following files in Python:

  • Modules/arraymodule.c
  • Modules/cjkcodecs/cjkcodecs.h
  • Modules/_ctypes/callbacks.c
  • Modules/_datetimemodule.c
  • Modules/_decimal/_decimal.c
  • Modules/_elementtree.c
  • Modules/faulthandler.c
  • Modules/_lsprof.c
  • Modules/_operator.c
  • Modules/_pickle.c
  • Modules/posixmodule.c
  • Modules/selectmodule.c
  • Modules/_sqlite/connection.c
  • Modules/_sqlite/module.c
  • Modules/_sre/sre.c
  • Modules/timemodule.c
  • Modules/_zoneinfo.c
  • Objects/abstract.c
  • Objects/fileobject.c
  • Objects/memoryobject.c
  • Parser/pegen.c
  • Parser/tokenizer/file_tokenizer.c
  • Python/import.c
  • Python/pylifecycle.c

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-128911: Add PyImport_ImportModuleAttr() function #128912
  • gh-128911: Add tests on the PyImport C API #128915
  • [3.13] gh-128911: Add tests on the PyImport C API (#128915) #128960
  • [3.12] gh-128911: Add tests on the PyImport C API (GH-128915) (GH-128960) #128989
  • gh-128911: Use PyImport_ImportModuleAttr() function #129657

Activity

  1. vstinner
    added
    topic-C-API
    type-featureA feature request or enhancementA feature request or enhancement
    on Jan 16, 2025
  2. vstinner
    added a commit that references this issue on Jan 16, 2025

    pythongh-128911: Add PyImport_GetModuleAttrString() function

    5f320d1
  3. encukou commented on Jan 16, 2025

    @encukou
    encukou
    on Jan 16, 2025
    Member
    More actions

    IMO, the *String functions should generally have variants that take PyObject*.
    So, if we add this we should also add PyImport_GetModuleAttr.

    Reacted by Erlend E. Aasland and Bénédikt Tran
  4. vstinner
    added 2 commits that reference this issue on Jan 16, 2025

    pythongh-128911: Add tests on the PyImport C API

    1d8342b

    pythongh-128911: Add tests on the PyImport C API

    214b751
  5. vstinner commented on Jan 16, 2025

    @vstinner
    vstinner
    on Jan 16, 2025
    MemberAuthor
    More actions

    So, if we add this we should also add PyImport_GetModuleAttr.

    Ok, I added PyImport_GetModuleAttr() to my PR.

  6. vstinner
    added 4 commits that reference this issue on Jan 17, 2025

    gh-128911: Add tests on the PyImport C API (#128915)

    d95ba9f

    pythongh-128911: Add tests on the PyImport C API (python#128915)

    a53147b

    pythongh-128911: Add tests on the PyImport C API (python#128915)

    4f21755

    [3.13] gh-128911: Add tests on the PyImport C API (#128915) (#128960)

    34ded1a
  7. serhiy-storchaka
    added a commit that references this issue on Jan 18, 2025

    [3.12] [3.13] pythongh-128911: Add tests on the PyImport C API (pytho…

    96e0bc4
  8. vstinner
    added a commit that references this issue on Jan 19, 2025

    [3.12] gh-128911: Add tests on the PyImport C API (GH-128915) (GH-128960

    83de72e
  9. malemburg commented on Jan 20, 2025

    @malemburg
    malemburg
    on Jan 20, 2025
    Member
    More actions

    This sounds like a useful addition. The fact that the internal API is already used a lot in CPython is evidence enough 😄

    And BTW: I like that you're starting to approach C API design from the perspective of a C programmer again.

    Rich C APIs make it a joy programming against them, while minimalist APIs only result in others creating additional layers on top, which adds fragmentation, overall increased maintenance and loss of design control. 👍

  10. srinivasreddy
    added a commit that references this issue on Jan 21, 2025

    pythongh-128911: Add tests on the PyImport C API (python#128915)

    d42d098
  11. vstinner
    changed the title [-][C API] Add PyImport_GetModuleAttrString(mod_name, attr_name) helper function[/-] [+][C API] Add PyImport_ImportModuleAttr(mod_name, attr_name) helper function[/+] on Jan 26, 2025
  12. vstinner commented on Jan 27, 2025

    @vstinner
    vstinner
    on Jan 27, 2025
    MemberAuthor
    More actions

    See also #129367 which proposes adding PySys_GetAttr().

  13. vstinner
    added a commit that references this issue on Jan 30, 2025

    gh-128911: Add PyImport_ImportModuleAttr() function (#128912)

    3bebe46
  14. vstinner
    closed this as completedon Jan 30, 2025
  15. vstinner
    added 2 commits that reference this issue on Feb 4, 2025

    pythongh-128911: Use PyImport_ImportModuleAttr() function

    76ba47e

    gh-128911: Use PyImport_ImportModuleAttr() function (#129657)

    dc804ff
  16. srinivasreddy
    added 2 commits that reference this issue on Feb 7, 2025

    pythongh-128911: Add PyImport_ImportModuleAttr() function (python#128912

    2721adc

    pythongh-128911: Use PyImport_ImportModuleAttr() function (python#129657

    92260e3
  17. cmaloney
    added a commit that references this issue on Feb 8, 2025

    pythongh-128911: Use PyImport_ImportModuleAttr() function (python#129657

    8ed58cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.