◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
15 changes: 15 additions & 0 deletions Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ extern "C" {

#include <locale.h> /* struct lconv */

PyAPI_DATA(int) _Py_HasFileSystemDefaultEncodeErrors;

PyAPI_FUNC(int) _Py_DecodeUTF8Ex(
@@ -33,6 +39,9 @@ PyAPI_FUNC(wchar_t*) _Py_DecodeUTF8_surrogateescape(
Py_ssize_t arglen,
size_t *wlen);

PyAPI_FUNC(int) _Py_GetForceASCII(void);

/* Reset "force ASCII" mode (if it was initialized).
Expand Up @@ -65,6 +74,12 @@ extern int _Py_EncodeNonUnicodeWchar_InPlace(
Py_ssize_t size);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
55 changes: 29 additions & 26 deletions PC/getpathp.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#include "Python.h"
#include "pycore_initconfig.h" // PyStatus
#include "pycore_pathconfig.h" // _PyPathConfig
#include "osdefs.h" // SEP, ALTSEP
#include <wchar.h>

Expand Up @@ -115,10 +116,6 @@
* with a semicolon separated path prior to calling Py_Initialize.
*/

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

#define INIT_ERR_BUFFER_OVERFLOW() _PyStatus_ERR("buffer overflow")


Expand Down Expand Up @@ -216,7 +213,7 @@ exists(const wchar_t *filename)
Assumes 'filename' MAXPATHLEN+1 bytes long -
may extend 'filename' by one character. */
static int
ismodule(wchar_t *filename, int update_filename)
{
size_t n;

Expand All @@ -231,9 +228,8 @@ ismodule(wchar_t *filename, int update_filename)
filename[n] = L'c';
filename[n + 1] = L'\0';
exist = exists(filename);
if (!update_filename) {
filename[n] = L'\0';
}
return exist;
}
return 0;
@@ -253,7 +249,7 @@ ismodule(wchar_t *filename, int update_filename)
static void
join(wchar_t *buffer, const wchar_t *stuff)
{
if (FAILED(PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
Py_FatalError("buffer overflow in getpathp.c's join()");
}
}
Expand All @@ -273,30 +269,37 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
return _PyStatus_OK();
}


/* gotlandmark only called by search_for_prefix, which ensures
'prefix' is null terminated in bounds. join() ensures
'landmark' can not overflow prefix if too long. */
static int
gotlandmark(const wchar_t *prefix, const wchar_t *landmark)
{
wchar_t filename[MAXPATHLEN+1];
memset(filename, 0, sizeof(filename));
wcscpy_s(filename, Py_ARRAY_LENGTH(filename), prefix);
join(filename, landmark);
return ismodule(filename, FALSE);
}


/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
assumption provided by only caller, calculate_path() */
static int
search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
{
/* Search from argv0_path, until landmark is found */
wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
do {
if (gotlandmark(prefix, landmark)) {
return 1;
}
reduce(prefix);
Expand Up @@ -758,7 +761,7 @@ calculate_home_prefix(PyCalculatePath *calculate,
reduce(prefix);
calculate->home = prefix;
}
else if (search_for_prefix(prefix, argv0_path, LANDMARK)) {
calculate->home = prefix;
}
else {
Expand Up @@ -936,7 +939,7 @@ calculate_module_search_path(PyCalculatePath *calculate,
lookBuf[nchars] = L'\0';
/* Up one level to the parent */
reduce(lookBuf);
if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
break;
}
/* If we are out of paths to search - give up */
Expand Down
Loading
Toggle all file notes Toggle all file annotations