-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
[PEP 741] gh-107954: Add PyConfig_Get() function #112609
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| """ | ||
| Tests on PyConfig API (PEP 587). | ||
| """ | ||
| import os | ||
| import sys | ||
| import unittest | ||
| from test import support | ||
| try: | ||
| import _testcapi | ||
| except ImportError: | ||
| _testcapi = None | ||
|
|
||
|
|
||
| @unittest.skipIf(_testcapi is None, 'need _testcapi') | ||
| class CAPITests(unittest.TestCase): | ||
| def check_config_get(self, get_func): | ||
| # write_bytecode is read from sys.dont_write_bytecode as int | ||
| with support.swap_attr(sys, "dont_write_bytecode", 0): | ||
| self.assertEqual(get_func('write_bytecode'), 1) | ||
| with support.swap_attr(sys, "dont_write_bytecode", "yes"): | ||
| self.assertEqual(get_func('write_bytecode'), 0) | ||
| with support.swap_attr(sys, "dont_write_bytecode", []): | ||
| self.assertEqual(get_func('write_bytecode'), 1) | ||
|
|
||
| # non-existent config option name | ||
| NONEXISTENT_KEY = 'NONEXISTENT_KEY' | ||
| err_msg = f'unknown config option name: {NONEXISTENT_KEY}' | ||
| with self.assertRaisesRegex(ValueError, err_msg): | ||
| get_func('NONEXISTENT_KEY') | ||
|
|
||
| def test_config_get(self): | ||
| config_get = _testcapi.config_get | ||
|
|
||
| self.check_config_get(config_get) | ||
|
|
||
| for name, config_type, expected in ( | ||
| ('verbose', int, sys.flags.verbose), # PyConfig_MEMBER_INT | ||
| ('isolated', int, sys.flags.isolated), # PyConfig_MEMBER_UINT | ||
| ('platlibdir', str, sys.platlibdir), # PyConfig_MEMBER_WSTR | ||
| ('argv', list, sys.argv), # PyConfig_MEMBER_WSTR_LIST | ||
| ('xoptions', dict, sys._xoptions), # xoptions dict | ||
| ): | ||
| with self.subTest(name=name): | ||
| value = config_get(name) | ||
| self.assertEqual(type(value), config_type) | ||
| self.assertEqual(value, expected) | ||
|
|
||
| # PyConfig_MEMBER_ULONG type | ||
| hash_seed = config_get('hash_seed') | ||
| self.assertIsInstance(hash_seed, int) | ||
| self.assertGreaterEqual(hash_seed, 0) | ||
|
|
||
| # PyConfig_MEMBER_WSTR_OPT type | ||
| if 'PYTHONDUMPREFSFILE' not in os.environ: | ||
| self.assertIsNone(config_get('dump_refs_file')) | ||
|
|
||
| # attributes read from sys | ||
| value_str = "TEST_MARKER_STR" | ||
| value_list = ["TEST_MARKER_STRLIST"] | ||
| value_dict = {"x": "value", "y": True} | ||
| for name, sys_name, value in ( | ||
| ("base_exec_prefix", None, value_str), | ||
| ("base_prefix", None, value_str), | ||
| ("exec_prefix", None, value_str), | ||
| ("executable", None, value_str), | ||
| ("platlibdir", None, value_str), | ||
| ("prefix", None, value_str), | ||
| ("pycache_prefix", None, value_str), | ||
| ("base_executable", "_base_executable", value_str), | ||
| ("stdlib_dir", "_stdlib_dir", value_str), | ||
| ("argv", None, value_list), | ||
| ("orig_argv", None, value_list), | ||
| ("warnoptions", None, value_list), | ||
| ("module_search_paths", "path", value_list), | ||
| ("xoptions", "_xoptions", value_dict), | ||
| ): | ||
| with self.subTest(name=name): | ||
| if sys_name is None: | ||
| sys_name = name | ||
| with support.swap_attr(sys, sys_name, value): | ||
| self.assertEqual(config_get(name), value) | ||
|
|
||
| def test_config_getint(self): | ||
| config_getint = _testcapi.config_getint | ||
|
|
||
| self.check_config_get(config_getint) | ||
|
|
||
| # PyConfig_MEMBER_INT type | ||
| self.assertEqual(config_getint('verbose'), sys.flags.verbose) | ||
|
|
||
| # PyConfig_MEMBER_UINT type | ||
| self.assertEqual(config_getint('isolated'), sys.flags.isolated) | ||
|
|
||
| # PyConfig_MEMBER_ULONG type | ||
| hash_seed = config_getint('hash_seed') | ||
| self.assertIsInstance(hash_seed, int) | ||
| self.assertGreaterEqual(hash_seed, 0) | ||
|
|
||
| # platlibdir is a str | ||
| with self.assertRaises(TypeError): | ||
| config_getint('platlibdir') | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
Misc/NEWS.d/next/C API/2023-12-01-21-13-32.gh-issue-107954.GO4oND.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Add functions to get the current Python configuration: | ||
|
|
||
| * :c:func:`PyConfig_Get` | ||
| * :c:func:`PyConfig_GetInt` | ||
|
|
||
| Patch by Victor Stinner. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #include "parts.h" | ||
|
|
||
|
|
||
| static PyObject * | ||
| _testcapi_config_get(PyObject *module, PyObject *name_obj) | ||
| { | ||
| const char *name; | ||
| if (PyArg_Parse(name_obj, "s", &name) < 0) { | ||
| return NULL; | ||
| } | ||
|
|
||
| return PyConfig_Get(name); | ||
| } | ||
|
|
||
|
|
||
| static PyObject * | ||
| _testcapi_config_getint(PyObject *module, PyObject *name_obj) | ||
| { | ||
| const char *name; | ||
| if (PyArg_Parse(name_obj, "s", &name) < 0) { | ||
| return NULL; | ||
| } | ||
|
|
||
| int value; | ||
| if (PyConfig_GetInt(name, &value) < 0) { | ||
| return NULL; | ||
| } | ||
| return PyLong_FromLong(value); | ||
| } | ||
|
|
||
|
|
||
| static PyMethodDef test_methods[] = { | ||
| {"config_get", _testcapi_config_get, METH_O}, | ||
| {"config_getint", _testcapi_config_getint, METH_O}, | ||
| {NULL} | ||
| }; | ||
|
|
||
| int _PyTestCapi_Init_Config(PyObject *mod) | ||
| { | ||
| if (PyModule_AddFunctions(mod, test_methods) < 0) { | ||
| return -1; | ||
| } | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.