◐ Shell
reader mode source ↗
Skip to content

bpo-34170: Add _PyCoreConfig.isolated#8417

Merged
vstinner merged 2 commits into
python:masterfrom
vstinner:isolated
Jul 24, 2018
Merged

bpo-34170: Add _PyCoreConfig.isolated#8417
vstinner merged 2 commits into
python:masterfrom
vstinner:isolated

Conversation

@vstinner

@vstinner vstinner commented Jul 23, 2018

Copy link
Copy Markdown
Member
  • _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.

https://bugs.python.org/issue34170

* _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.
@vstinner

Copy link
Copy Markdown
Member Author

@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:

_PyCoreConfig config = _PyCoreConfig_INIT;
Py_UTF8Mode = 0;   /* just for the example */
config.utf8_mode = 1;  /* override Py_UTF8Mode */
Py_Initialize(&config);
# UTF-8 Mode enabled from config.utf8_mode

Backward compatibility:

_PyCoreConfig config = _PyCoreConfig_INIT;
Py_UTF8Mode = 1;
Py_Initialize(config);
# UTF-8 Mode enabled from Py_UTF8Mode

This change simplify and clarify main.c:

  • Py_xxx variables are no longer modified when reading the configuration. Reading the configuration must no longer modify Python global variables: only local variables like pymain, config and cmdline
  • Unset fields (set to -1) of config are now always initialized from Py_xxx
  • Python initialization now always override Py_xxx from the config <= this part is now better defined

@vstinner

vstinner commented Jul 23, 2018

Copy link
Copy Markdown
Member Author

This PR also removes the ugly hack added by the commit f2626ce: two int* pointers added to _PyCoreConfig_Read().

+PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(
+    _PyCoreConfig *config,
+    int *isolated,
+    int *no_site_import);

It was a temporary hack until I make this more brave change.

* Rename _disable_importlib of _PyCoreConfig to _install_importlib
* _PyCoreConfig_SetGlobalConfig() now also set
  Py_HashRandomizationFlag
* Replace !Py_NoSiteFlag with core_config->site_import

@ncoghlan ncoghlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide 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.

@vstinner

Copy link
Copy Markdown
Member Author

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.

@vstinner vstinner merged commit d19d8d5 into python:master Jul 24, 2018
@vstinner vstinner deleted the isolated branch July 24, 2018 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants