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

PyConfig.stdlib_dir does not affect sys._stdlib_dir #106718

Closed
Yhg1s opened this issue Jul 13, 2023 · 3 comments
Closed

PyConfig.stdlib_dir does not affect sys._stdlib_dir #106718

Yhg1s opened this issue Jul 13, 2023 · 3 comments
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@Yhg1s
Copy link
Member

Yhg1s commented Jul 13, 2023

In Python 3.11, PyConfig gained a stdlib_dir attribute which is, as far as I can tell, supposed to override sys._stdlib_dir (which is used by importlib). No matter what I set it to or what else I set, however, it seems to be ignored. For example:

#include <Python.h>
#include <stdio.h>

extern wchar_t *_Py_GetStdlibDir(void);

int main(int argc, char **argv)
{
    PyStatus status;
    PyConfig config;
    PyConfig_InitPythonConfig(&config);
    PyConfig_SetString(&config, &config.stdlib_dir, L"/my/stdlib/dir");
    Py_InitializeFromConfig(&config);
    if (PyStatus_Exception(status)) {
        Py_ExitStatusException(status);
    }
    PyConfig_Clear(&config);
    fprintf(stderr, "stdlib_dir = '%ls'\n", _Py_GetStdlibDir());
    Py_Finalize();
}
% gcc -Wall $(/python/3.12-opt/bin/python3-config --cflags) -o testprogram testprogram.c $(/python/3.12-opt/bin/python3-config --ldflags) -lpython3.12
# Look ma, no warnings.
% ./testprogram
stdlib_dir = '/python/3.12-opt/lib/python3.12'

It doesn't seem to matter whether PyConfig.stdlib_dir is set to an existing directory either (although for my use-case, it must be a non-existant one; we're getting the stdlib from embedded data, and the incorrect setting of sys._stdlib_dir means importlib's FrozenImporter sets the wrong __file__ attribute on frozen/deepfrozen modules, like os.)

Linked PRs

@Yhg1s Yhg1s added type-bug An unexpected behavior, bug, or error 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes labels Jul 13, 2023
@vstinner
Copy link
Member

I wasn't involved in these changes. I only designed the original PyConfig structure.

Maybe @zooba or @ncoghlan know about this?

@vstinner vstinner removed their assignment Jul 13, 2023
@zooba
Copy link
Member

zooba commented Jul 13, 2023

Looking at getpath.py, it doesn't try to read stdlib_dir out of the config when starting, and I assume never has (there are no checks later on to see whether it's already been set).

It looks like the stdlib_dir attribute was added around the same time I was working on porting getpath.c to Python, so it may just have been a successful merge and nobody noticed that it wasn't being read? That PR took a long time to get in.

I won't be able to get to this in time for 3.12.0rc1, but the change ought to be entirely in Python starting from getpath.py where I linked above, and it's relatively easy to test (the test suite exec's the source file, rather than needing a rebuild)

@yilei
Copy link
Contributor

yilei commented Aug 31, 2023

I'll try to send a PR for this. I'm not familiar with the code so I may need additional pointers later.

zooba pushed a commit that referenced this issue Nov 1, 2023
@zooba zooba closed this as completed Nov 1, 2023
FullteaR pushed a commit to FullteaR/cpython that referenced this issue Nov 3, 2023
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants
@vstinner @yilei @ncoghlan @zooba @Yhg1s and others