Skip to content

_PyImport_SwapPackageContext would be helpful if exposed #139227

@ZeroIntensity

Description

@ZeroIntensity

Feature or enhancement

In Nuitka, we have our own meta-path-based loader, which loads packages that contain extension modules with custom code that emulates the load process. For an extension module in a package, we used to set _Py_PackageContext, which has since been removed. Currently, _PyRuntime.imports.pkgcontext is not used because we fall back to a thread-local pkgcontext variable, which Nuitka cannot access on Windows or where static linking to Python is not available.

This leads to some issues when Nuitka goes to load extensions that are stored in a submodule or create submodules, because the package context is not correct. For example, an extension module under x.y will create submodule z as y.z rather than x.y.z. There are currently some workarounds to repair the damage, but this is tedious and sometimes unreliable. If _PyImport_SwapPackageContext were exposed, Nuitka could just call that and make PyModule_Create know what package it lives in.

Nuitka doesn't need _PyImport_SwapPackageContext to be exposed as public or PyUnstable, we would just like to have it available in the ABI under PyAPI_FUNC. It would be very helpful if this could happen before 3.14.0, but that's up to Hugo.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixestopic-C-APItype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions