bpo-34170: Add _PyCoreConfig.isolated#8417
bpo-34170: Add _PyCoreConfig.isolated#8417vstinner merged 2 commits intopython:masterfrom vstinner:isolated
Conversation
* _PyCoreConfig: add isolated and site_import attributes * Replace Py_IgnoreEnvironment with config->ignore_environment when reading the current configuration * _PyCoreConfig_Read() now sets ignore_environment, utf8_mode, isolated and site_import from Py_IgnoreEnvironment, Py_UTF8Mode, Py_IsolatedFlag and Py_NoSiteFlag * _Py_InitializeCore() now sets Py_xxx flags from the configuration * pymain_read_conf() now uses _PyCoreConfig_Copy() to save/restore the configuration.
|
@ncoghlan, @ericsnowcurrently, @emilyemorehouse: OK, this PR should be big enhancement for the PEP 432 :-) Previously, _PyCoreConfig "tried" to be isolated from Py_xxx global configuration variables like Py_IsolatedFlag. In practice, it already contained ignore_environment and utf8_mode which duplicated Py_IgnoreEnvironment and Py_UTF8Mode global configuration which can lead to inconsistency. With this change, the priority becomes more explicit: _PyCoreConfig has now the priority over Py_xxx. To get a smooth transition, _PyCoreConfig are initialized to -1 which means "unset": in that case, the fields are initialized from Py_xxx. Core config has the priority: Backward compatibility: This change simplify and clarify main.c:
|
Include/pystate.h
Outdated
| wchar_t *dll_path; /* Windows DLL path */ | ||
| #endif | ||
|
|
||
| /* If greater than 0, enable isolated mode sys.path contains |
There was a problem hiding this comment.
oops, colon is missing: "enable isolated mode: sys.path contains ..."
Include/pystate.h
Outdated
| Ignored if set to -1. | ||
|
|
||
| Related to Py_IsolatedFlag global configuration variable and -I command | ||
| line option. */ |
There was a problem hiding this comment.
Maybe "Initialized from Py_IsolatedFlag global configuration variable and set to 1 by -I command line option"?
Include/pystate.h
Outdated
| Ignored if set to -1. | ||
|
|
||
| Related to Py_NoSiteFlag global configuration variable and -S command | ||
| line option. */ |
There was a problem hiding this comment.
Maybe "Initialized from Py_NoSiteFlag global configuration variable and set to 0 by -S command line option"?
|
This PR also removes the ugly hack added by the commit f2626ce: two It was a temporary hack until I make this more brave change. |
Include/pystate.h
Outdated
|
|
||
| /* If greater than 0, enable isolated mode sys.path contains | ||
| neither the script's directory nor the user's site-packages directory. | ||
| Ignored if set to -1. |
There was a problem hiding this comment.
What if it's set to 0? Is it ignored in that case as well? (the comment covers >1 and -1 but not 0)
| { | ||
| assert(core_config != NULL); | ||
|
|
||
| PyInterpreterState *interp; |
There was a problem hiding this comment.
This is just a question regarding the preferred C style for my learning -- from the code I've read, I noticed that most variable declarations are at the top of the function, but you've moved them to just before they are used (I assume for locality while reading). Is there a preference I should stick to?
There was a problem hiding this comment.
CPython is 28+ years old and has been written for ISO C90. Since Python 3.7, we are allowed to move variables declaration in the middle of functions: ISO C99 is allowed!!!
I don't recall why I decided to move these variables in this PR 😁 In general, we reject PR which are only coding style changes.
* Rename _disable_importlib of _PyCoreConfig to _install_importlib * _PyCoreConfig_SetGlobalConfig() now also set Py_HashRandomizationFlag * Replace !Py_NoSiteFlag with core_config->site_import
ncoghlan
left a comment
There was a problem hiding this comment.
The changes themselves look good to me (with one minor fix to a comment), but it would be good to add a test case that ensures the settings in the config struct override the preconfigured ones in the environment (or not, as the case may be).
My suggestion would be to have a test embed helper command that runs 3 cases:
- global variables set to 1, core config fields set to -1 -> sys.flags reports 1
- global variables set to 1, core config fields set to 0 -> sys.flags reports 0
- global variables set to 0, core config fields set to 1 -> sys.flags reports 1
The embedding helper would just run import sys; print(sys.flags); sys.stdout.flush(), and then the Python test case would handle checking the output flags matched what the test excepted for each case.
| site.main() if you want them to be triggered). | ||
|
|
||
| Set to 0 by the -S command line option. If set to -1 (default), set to | ||
| the negative value of Py_NoSiteFlag. */ |
There was a problem hiding this comment.
This comment isn't right, as the field gets set to the result of !Py_NoSiteFlag
There was a problem hiding this comment.
I wrote "negative value of !Py_NoSiteFlag". Is that wrong?
There was a problem hiding this comment.
"negative value" is ambiguous here because we're dealing with C integers rather than strict booleans, and arithmetic negation (-value) and logical negation (!value) do different things. Since this is C code, and the English spelling is ambiguous, just including the exact C expression in the comment is clearer than trying to express the operation as words.
|
Write more tests is a good idea! But this change is already big enough, I will write them in my following change. I plan to move all "cmdline" fields into the core config. |
reading the current configuration
isolated and site_import from Py_IgnoreEnvironment, Py_UTF8Mode,
Py_IsolatedFlag and Py_NoSiteFlag
the configuration.
https://bugs.python.org/issue34170