Uncategorized

PEP 741: Python Configuration C API – PEPs



Read the PEP 741: Python Configuration C API.

Abstract

Add a C API to the limited C API to configure the Python preinitialization and initialization, and to get the current configuration. It can be used with the stable ABI.

Add sys.get_config(name) function to get the current value of a configuration option.

Allow setting custom configuration options, not used by Python but by third-party code. Options are referred to by their name as a string.

PEP 587 “Python Initialization Configuration” unified all the ways to configure the Python initialization. This PEP unifies also the configuration of the Python preinitialization and the Python initialization in a single API.

Out of scope: set config at runtime

There is no API to set a configuration option while Python is running. Only to set the initialization configuration. Some people discussed it, but I didn’t see a clear willingness to have this feature.

Technically, it can be implemented. If we decide to add such API, I would suggest to make most options read-only, and add callbacks on others to validate values (reject invalid values) and execute code when an option is modified (ex: update/invalidate caches).

For example, do you want to be able to change bytes_warning option at runtime? I’m not sure that the Python ecosystem is ready to see this option changing between two lines of code.

It may be interesting to have a Python API (like sys.set_config()) to set some options at Python startup.



1 Like

I think this is a good idea and the API looks generally well thought.

I would suggest a few changes:

  • Instead of PyInitConfig_SetStr taking a locale-encoded string, I would suggest PyInitConfig_SetUtf8 taking a UTF8 string (probably the preferred choice for most people?) and PyInitConfig_SetLocaleStr taking a locale-encoded string;
  • PyInitConfig_Exception would probably be less confusingly named PyInitConfig_HasError (we aren’t talking about proper Python exceptions, are we?);
  • PyInitConfig_GetError could probably return a const char* directly, rather than have a separate int return code that only duplicates the information.

Also, I do not understand what a config exit code is.

I’m not sure the allow_custom_options special config entry is really useful. Instead, you could arrange that values starting with some well-known prefix such as X- or vendor. are freely accepted.

Finally, it seems that PyConfig_Get and PyConfig_GetInt will only work if interpreter initialization was successful, since they set an exception on error? This means it’s not possible to inspect the current configuration while building it.

Are the configuration options themselves also a stable API?

The pep allows setting custom options. IMHO it would be better to specify that all names without a colon are reserved for CPython configuration and will be ignored as custom option. This would avoid breakage when adding new options in future Python versions.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *