Skip to content

[WIP] gh-107954: Add _PyConfig_Parse() #110145

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
wants to merge 1 commit into from
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
6 changes: 6 additions & 0 deletions Include/internal/pycore_initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ PyAPI_FUNC(int) _PyConfig_FromDict(PyConfig *config, PyObject *dict);
PyAPI_FUNC(PyObject*) _Py_Get_Getpath_CodeObject(void);
PyAPI_FUNC(PyObject*) _Py_GetConfigsAsDict(void);

PyAPI_FUNC(int) _PyConfig_Parse(
PyObject *config_dict,
// UTF-8 encoded string
const char *config_str
);

#ifdef __cplusplus
}
#endif
Expand Down
84 changes: 84 additions & 0 deletions Lib/test/test_initconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import textwrap
import unittest
from test.support import import_helper

_testinternalcapi = import_helper.import_module('_testinternalcapi')


class InitConfigTests(unittest.TestCase):
def check_parse(self, text, expected):
text = textwrap.dedent(text)
config = _testinternalcapi.parse_config_text(text)
self.assertEqual(config, expected)

def test_parse(self):
self.check_parse('verbose=1', {'verbose': 1})

# strip spaces
self.check_parse(' \tverbose = 1 ', {'verbose': 1})

# comments
self.check_parse('''
verbose=1
# ignored comment
verbose=2 # more #hashtag comment
''', dict(
verbose = 2,
))
self.check_parse('verbose=1', {'verbose': 1})

# types
self.check_parse('''
# int
int_max_str_digits = -123

# uint
isolated = 0

# ulong
hash_seed = 555

# wstr
filesystem_encoding = 'with spaces'
filesystem_errors = "double quotes"
program_name = "key=value"

# wstr optional
pycache_prefix =

# == wstr list ==

argv = ['python', '-c', 'pass']

# accept spaces before/after strings and commas
orig_argv = [ 'python' , '-c' , 'accept spaces, and commas' ]

# empty list
xoptions = []
''', dict(
int_max_str_digits = -123,
isolated = 0,
hash_seed = 555,
filesystem_encoding = "with spaces",
filesystem_errors = "double quotes",
program_name = "key=value",
pycache_prefix = None,
argv = ['python', '-c', 'pass'],
orig_argv = ['python', '-c', 'accept spaces, and commas'],
xoptions = [],
))

def check_error(self, text):
text = textwrap.dedent(text)
with self.assertRaises(ValueError):
_testinternalcapi.parse_config_text(text)

def test_parse_invalid(self):
# no value
self.check_error('isolated=')
# no key
self.check_error('= 123')


if __name__ == "__main__":
unittest.main()
73 changes: 73 additions & 0 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,77 @@ test_set_config(PyObject *Py_UNUSED(self), PyObject *dict)
}


static PyObject *
test_parse_config_text(PyObject *Py_UNUSED(self), PyObject *config_obj)
{
const char *config_utf8 = PyUnicode_AsUTF8(config_obj);
if (config_utf8 == NULL) {
return NULL;
}

PyObject *config_dict = PyDict_New();
if (config_dict == NULL) {
return NULL;
}

if (_PyConfig_Parse(config_dict, config_utf8) < 0) {
Py_DECREF(config_dict);
return NULL;
}
return config_dict;
}


static PyObject *
test_set_config_text(PyObject *Py_UNUSED(self), PyObject *config_obj)
{
if (!PyUnicode_Check(config_obj)) {
PyErr_Format(PyExc_TypeError, "expected str, got %s",
Py_TYPE(config_obj)->tp_name);
return NULL;
}

PyConfig config;
PyConfig_InitIsolatedConfig(&config);
PyObject *config_dict = NULL;

// Set config from the dict
if (_PyInterpreterState_GetConfigCopy(&config) < 0) {
goto error;
}
config_dict = _PyConfig_AsDict(&config);
if (config_dict == NULL) {
goto error;
}

// Parse config text
const char *config_utf8 = PyUnicode_AsUTF8(config_obj);
if (config_utf8 == NULL) {
goto error;
}
if (_PyConfig_Parse(config_dict, config_utf8) < 0) {
goto error;
}

// Set config from dict
if (_PyConfig_FromDict(&config, config_dict) < 0) {
goto error;
}
if (_PyInterpreterState_SetConfig(&config) < 0) {
goto error;
}

PyConfig_Clear(&config);
Py_DECREF(config_dict);
Py_RETURN_NONE;

error:
PyConfig_Clear(&config);
Py_DECREF(config_dict);
return NULL;
}


static PyObject *
test_reset_path_config(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(arg))
{
Expand Down Expand Up @@ -1536,6 +1607,8 @@ static PyMethodDef module_functions[] = {
{"test_hashtable", test_hashtable, METH_NOARGS},
{"get_config", test_get_config, METH_NOARGS},
{"set_config", test_set_config, METH_O},
{"parse_config_text", test_parse_config_text, METH_O},
{"set_config_text", test_set_config_text, METH_O},
{"reset_path_config", test_reset_path_config, METH_NOARGS},
{"test_edit_cost", test_edit_cost, METH_NOARGS},
{"test_bytes_find", test_bytes_find, METH_NOARGS},
Expand Down
Loading