FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[WIP] gh-107954: Add _PyConfig_Parse() by vstinner · Pull Request #110145 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (1) .py  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 6 additions & 0 deletions Include/internal/pycore_initconfig.h
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
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
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,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
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
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

Back | FazBrowse Home | New Git URL