Skip to content

Commit eef175c

Browse files
committed
use sys._stdlib_dir
1 parent 3cc5694 commit eef175c

File tree

1 file changed

+39
-55
lines changed

1 file changed

+39
-55
lines changed

Python/codecs.c

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,69 +1416,53 @@ static PyObject *surrogateescape_errors(PyObject *self, PyObject *exc)
14161416
static int
14171417
_set_encodings_path(PyObject *mod) {
14181418
int rc = -1;
1419-
PyObject *path = PyList_New(0);
1420-
if (path == NULL) {
1419+
PyObject *path = NULL;
1420+
PyObject *encodings_dir = NULL;
1421+
1422+
// borrowed ref
1423+
PyObject *stdlib_dir = PySys_GetObject("_stdlib_dir");
1424+
if (stdlib_dir == NULL) {
1425+
rc = 0;
14211426
goto exit;
14221427
}
1423-
const PyConfig *config = _Py_GetConfig();
1424-
if (config->stdlib_dir != NULL) {
1425-
// standard library directory
14261428

1427-
// os.path.join(stdlib_dir, "encodings")
1428-
wchar_t *encodings_dirw = _Py_join_relfile(
1429-
config->stdlib_dir, L"encodings");
1430-
if (encodings_dirw == NULL) {
1431-
PyErr_NoMemory();
1432-
goto exit;
1433-
}
1434-
PyObject *encodings_dir = PyUnicode_FromWideChar(encodings_dirw, -1);
1435-
PyMem_RawFree(encodings_dirw);
1436-
if (encodings_dir == NULL) {
1437-
goto exit;
1438-
}
1439-
if (PyList_Append(path, encodings_dir) < 0) {
1440-
goto exit;
1441-
}
1442-
} else if (config->module_search_paths_set) {
1443-
// No stdlib_dir, search module_search paths
1444-
for (Py_ssize_t i = 0; i < config->module_search_paths.length; ++i) {
1445-
wchar_t *encodings_dirw = _Py_join_relfile(
1446-
config->module_search_paths.items[i], L"encodings");
1447-
if (encodings_dirw == NULL) {
1448-
PyErr_NoMemory();
1449-
goto exit;
1450-
}
1451-
#ifdef MS_WINDOWS
1452-
DWORD attr = GetFileAttributesW(encodings_dirw);
1453-
int isdir = (attr != INVALID_FILE_ATTRIBUTES) &&
1454-
(attr & FILE_ATTRIBUTE_DIRECTORY);
1455-
#else
1456-
struct stat st;
1457-
int isdir = (_Py_wstat(encodings_dirw, &st) == 0) && S_ISDIR(st.st_mode);
1458-
#endif
1459-
if (!isdir) {
1460-
PyMem_RawFree(encodings_dirw);
1461-
continue;
1462-
}
1463-
PyObject *encodings_dir = PyUnicode_FromWideChar(encodings_dirw, -1);
1464-
PyMem_RawFree(encodings_dirw);
1465-
if (encodings_dir == NULL) {
1466-
goto exit;
1467-
}
1468-
if (PyList_Append(path, encodings_dir) < 0) {
1469-
goto exit;
1470-
}
1471-
}
1429+
Py_ssize_t size;
1430+
wchar_t *stdlib_dirw = PyUnicode_AsWideCharString(stdlib_dir, &size);
1431+
if (stdlib_dirw == NULL) {
1432+
goto exit;
14721433
}
1473-
if (PyObject_IsTrue(path)) {
1474-
// set and override __path__
1475-
if (PyObject_SetAttrString(mod, "__path__", path) < 0) {
1476-
goto exit;
1477-
}
1434+
1435+
// encodings_dir = os.path.join(sys._stdlib_dir, "encodings")
1436+
wchar_t *encodings_dirw = _Py_join_relfile(stdlib_dirw, L"encodings");
1437+
PyMem_Free((void *)stdlib_dirw);
1438+
if (encodings_dirw == NULL) {
1439+
PyErr_NoMemory();
1440+
goto exit;
1441+
}
1442+
1443+
encodings_dir = PyUnicode_FromWideChar(encodings_dirw, -1);
1444+
PyMem_RawFree((void *)encodings_dirw);
1445+
if (encodings_dir == NULL) {
1446+
goto exit;
1447+
}
1448+
1449+
path = PyList_New(0);
1450+
if (path == NULL) {
1451+
goto exit;
1452+
}
1453+
if (PyList_Append(path, encodings_dir) < 0) {
1454+
goto exit;
14781455
}
1456+
// encodings.__path__ = [encodings_dir]
1457+
if (PyObject_SetAttrString(mod, "__path__", path) < 0) {
1458+
goto exit;
1459+
}
1460+
14791461
rc = 0;
1462+
14801463
exit:
14811464
Py_XDECREF(path);
1465+
Py_XDECREF(encodings_dir);
14821466
return rc;
14831467
}
14841468

0 commit comments

Comments
 (0)