8000 Add sys._get_stdlib_dir(). · python/cpython@9339925 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9339925

Browse files
Add sys._get_stdlib_dir().
1 parent 225302c commit 9339925

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Lib/test/test_sys.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from test.support import os_helper
1414
from test.support.script_helper import assert_python_ok, assert_python_failure
1515
from test.support import threading_helper
16+
from test.support import import_helper
1617
import textwrap
1718
import unittest
1819
import warnings
@@ -994,6 +995,15 @@ def test_module_names(self):
994995
for name in sys.stdlib_module_names:
995996
self.assertIsInstance(name, str)
996997

998+
def test_stdlib_dir(self):
999+
os = import_helper.import_fresh_module('os')
1000+
marker = getattr(os, '__file__', None)
1001+
if marker and not os.path.exists(marker):
1002+
marker = None
1003+
expected = os.path.dirname(marker) if marker else None
1004+
actual = sys._stdlib_dir
1005+
self.assertEqual(actual, expected)
1006+
9971007

9981008
@test.support.cpython_only
9991009
class UnraisableHookTest(unittest.TestCase):

Python/sysmodule.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2974,6 +2974,14 @@ _PySys_UpdateConfig(PyThreadState *tstate)
29742974

29752975
SET_SYS("_xoptions", sys_create_xoptions_dict(config));
29762976

2977+
const wchar_t *stdlibdir = _Py_GetStdlibDir();
2978+
if (stdlibdir != NULL) {
2979+
SET_SYS_FROM_WSTR("_stdlib_dir", stdlibdir);
2980+
}
2981+
else {
2982+
PyDict_SetItemString(sysdict, "_stdlib_dir", Py_None);
2983+
}
2984+
29772985
#undef SET_SYS_FROM_WSTR
29782986
#undef COPY_LIST
29792987
#undef COPY_WSTR

0 commit comments

Comments
 (0)
0