Skip to content

bpo-45211: Add _Py_IsStdlibDir(). #28585

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Include/internal/pycore_pathconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include <stdbool.h>

typedef struct _PyPathConfig {
/* Full path to the Python program */
wchar_t *program_full_path;
Expand Down Expand Up @@ -67,6 +69,11 @@ extern PyStatus _PyConfig_WritePathConfig(const PyConfig *config);
extern void _Py_DumpPathConfig(PyThreadState *tstate);
extern PyObject* _PyPathConfig_AsDict(void);


/* locating the stdlib */

extern bool _Py_IsStdlibDir(const wchar_t *stdlibdir, bool checkpyc);

#ifdef __cplusplus
}
#endif
Expand Down
32 changes: 1 addition & 31 deletions Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ extern "C" {
#error "PREFIX, EXEC_PREFIX, VERSION and VPATH macros must be defined"
#endif

#ifndef LANDMARK
#define LANDMARK L"os.py"
#endif

#define BUILD_LANDMARK L"Modules/Setup.local"

#define PATHLEN_ERR() _PyStatus_ERR("path configuration: path too long")
Expand Down Expand Up @@ -325,33 +321,7 @@ absolutize(wchar_t **path_p)
static PyStatus
ismodule(const wchar_t *path, int *result)
{
wchar_t *filename = joinpath2(path, LANDMARK);
if (filename == NULL) {
return _PyStatus_NO_MEMORY();
}

if (isfile(filename)) {
PyMem_RawFree(filename);
*result = 1;
return _PyStatus_OK();
}

/* Check for the compiled version of prefix. */
size_t len = wcslen(filename);
wchar_t *pyc = PyMem_RawMalloc((len + 2) * sizeof(wchar_t));
if (pyc == NULL) {
PyMem_RawFree(filename);
return _PyStatus_NO_MEMORY();
}

memcpy(pyc, filename, len * sizeof(wchar_t));
pyc[len] = L'c';
pyc[len + 1] = L'\0';
*result = isfile(pyc);

PyMem_RawFree(filename);
PyMem_RawFree(pyc);

*result = _Py_IsStdlibDir(path, true) ? 1 : 0;
return _PyStatus_OK();
}

Expand Down
42 changes: 2 additions & 40 deletions PC/getpathp.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#include "pycore_fileutils.h" // _Py_add_relfile()
#include "osdefs.h" // SEP, ALTSEP
#include <wchar.h>
#include <stdbool.h>

#ifndef MS_WINDOWS
#error getpathp.c should only be built on Windows
Expand Down Expand Up @@ -209,33 +210,6 @@ exists(const wchar_t *filename)
}


/* Is module -- check for .pyc too.
Assumes 'filename' MAXPATHLEN+1 bytes long -
may extend 'filename' by one character. */
static int
ismodule(wchar_t *filename)
{
size_t n;

if (exists(filename)) {
return 1;
}

/* Check for the compiled version of prefix. */
n = wcsnlen_s(filename, MAXPATHLEN+1);
if (n < MAXPATHLEN) {
int exist = 0;
filename[n] = L'c';
filename[n + 1] = L'\0';
exist = exists(filename);
// Drop the 'c' we just added.
filename[n] = L'\0';
return exist;
}
return 0;
}


/* Add a path component, by appending stuff to buffer.
buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
NUL-terminated string with no more than MAXPATHLEN characters (not counting
Expand Down Expand Up @@ -269,18 +243,6 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
return _PyStatus_OK();
}

static int
is_stdlibdir(wchar_t *stdlibdir)
{
wchar_t *filename = stdlibdir;
#ifndef LANDMARK
# define LANDMARK L"os.py"
#endif
/* join() ensures 'landmark' can not overflow prefix if too long. */
join(filename, LANDMARK);
return ismodule(filename);
}

/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
assumption provided by only caller, calculate_path() */
static int
Expand All @@ -299,7 +261,7 @@ search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path)
/* Due to reduce() and our initial value, this result
is guaranteed to fit. */
wcscpy(&stdlibdir[wcslen(prefix) + 1], L"lib");
if (is_stdlibdir(stdlibdir)) {
if (_Py_IsStdlibDir(stdlibdir, true)) {
return 1;
}
reduce(prefix);
Expand Down
50 changes: 50 additions & 0 deletions Python/pathconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "pycore_fileutils.h"
#include "pycore_pathconfig.h"
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
#include <stdbool.h>
#include <wchar.h>
#ifdef MS_WINDOWS
# include <windows.h> // GetFullPathNameW(), MAX_PATH
Expand Down Expand Up @@ -813,6 +814,55 @@ _Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
return _PyStatus_OK();
}


/* locating the stdlib */

static int
file_exists(const wchar_t *filename)
{
#ifdef MS_WINDOWS
return GetFileAttributesW(filename) != 0xFFFFFFFF;
#else
struct stat buf;
return _Py_wstat(filename, &buf) == 0 && S_ISREG(buf.st_mode);
#endif
}

/* Decide if the given directory hold the stdlib. */
bool
_Py_IsStdlibDir(const wchar_t *stdlibdir, bool checkpyc)
{
// Build the filename to look for.
wchar_t filename[MAXPATHLEN + 1];
assert(stdlibdir != NULL);
#ifdef MS_WINDOWS
wcscpy_s(filename, MAXPATHLEN + 1, stdlibdir);
#else
wcscpy(filename, stdlibdir);
#endif
#ifndef LANDMARK
#define LANDMARK L"os.py"
#endif
_Py_add_relfile(filename, LANDMARK, MAXPATHLEN + 1);

// Check if the file exists.
if (file_exists(filename)) {
return true;
}

// Check for the compiled version. */
size_t len = wcslen(filename);
if (checkpyc && len < MAXPATHLEN) {
filename[len] = 'c';
filename[len + 1] = '\0';
if (file_exists(filename)) {
return true;
}
}

return false;
}

#ifdef __cplusplus
}
#endif