Add a field to PyConfig¶
See also Embed Python in an application.
Documentation¶
Main files¶
Python/preconfig.c
: _PyPreConfig_Read()Python/initconfig.c
: PyConfig_Read()Include/cpython/initconfig.h
: PyConfig structureInclude/internal/pycore_initconfig.h
Python initialization and finalization:
Modules/main.c
:pymain_main()
pymain_free()
Py_BytesMain()
Py_RunMain()
Python/pylifecycle.c
:Py_InitializeFromConfig()
Py_PreInitialize()
Py_FinalizeEx()
Add a field to PyConfig¶
Include/cpython/initconfig.h
: Add a new field intoPyConfig
structurePython/initconfig.c
:Initialize the field in
_PyConfig_InitCompatConfig()
if the default is not zero_PyConfig_Copy()
: addCOPY_ATTR(field)
config_as_dict()
: addSET_ITEM_INT(field)
PyConfig_Read()
: if needed, add an assertion at the function exit to validate that the field is valid.
Lib/test/test_embed.py
: Add field default value toInitConfigTests.CONFIG_COMPAT
Doc/c-api/init_config.rst
: Document the new field.
Add a field to PyPreConfig¶
Include/cpython/initconfig.h
: Add a new field intoPyPreConfig
structurePython/preconfig.c
:Initialize the field in
_PyPreConfig_InitCompatConfig()
if the default is not zeropreconfig_copy()
: addCOPY_ATTR(field)
_PyPreConfig_AsDict()
: addSET_ITEM_INT(field)
preconfig_read()
: if needed, add an assertion at the function exit to validate that the field is valid.
Lib/test/test_embed.py
: Add field default value toInitConfigTests.PRE_CONFIG_COMPAT
Doc/c-api/init_config.rst
: Document the new field.
Add an unit test for new PyPreConfig or PyConfig field¶
Programs/_testembed.c
,test_init_from_config()
: Set the field to a value different than the default.Lib/test/test_embed.py
: Updatetest_init_from_config()
ofInitConfigTests
.
Add a new command line option¶
Python/getopt.c
: UpdateSHORT_OPTS
or_PyOS_LongOption
Python/initconfig.c
: Modify config_parse_cmdline()Lib/subprocess.py
: Update_args_from_interpreter_flags()
and_optim_args_from_interpreter_flags()
if needed.Document the new env option
Python/initconfig.c
: Update one of theusage_xxx
variablesDoc/using/cmdline.rst
Misc/python.man
Example of Python/initconfig.c
usage:
-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Example of Doc/using/cmdline.rst
entry:
.. cmdoption:: -d
Turn on parser debugging output (for expert only, depending on compilation
options). See also :envvar:`PYTHONDEBUG`.
Add an unit test.
Lib/test/test_cmd_line.py
,Lib/test/test_embed.py
(InitConfigTests
) or another test depending on how the value is exposed in Python.
Add an environment variable¶
Python/initconfig.c
:Modify the appropriate helper function of
PyConfig_Read()
to read the environment variable.Update
usage_xxx
to document the new env var
Example:
_Py_get_env_flag(use_env, &config->parser_debug, "PYTHONDEBUG");
Doc/using/cmdline.rst
: Document the new env varMisc/python.man
: Document the new env varPrograms/_testembed.c
: Updatetest_init_from_config()
andset_most_env_vars()
to set the env var,PyConfig
must have the priority.Lib/test/test_embed.py
: UpdateInitConfigTests
.
Note: environment variable with a name starting with PYTHON
are ignored
when using -E
or -I
command line options.