Skip to content

[WIP] bpo-35265: Add _PyConfigCtx #10574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
42 changes: 30 additions & 12 deletions Include/coreconfig.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#ifndef Py_PYCORECONFIG_H
#define Py_PYCORECONFIG_H
#ifndef Py_LIMITED_API

#ifdef __cplusplus
extern "C" {
#endif


#ifndef Py_LIMITED_API
typedef struct {
const char *prefix;
const char *msg;
Expand All @@ -32,10 +33,31 @@ typedef struct {
#define _Py_INIT_FAILED(err) \
(err.msg != NULL)

#endif /* !defined(Py_LIMITED_API) */

typedef struct {
/* Enable UTF-8 mode?
Set by -X utf8 command line option and PYTHONUTF8 environment variable.
If set to -1 (default), inherit Py_UTF8Mode value. */
int utf8_mode;

PyMemAllocatorEx raw_alloc;
} _PyConfigCtx;

PyAPI_FUNC(void) _PyConfigCtx_Init(_PyConfigCtx *ctx);


/* Functions implemented in obmalloc.c */
PyAPI_FUNC(void *) _PyMem_RawMallocCtx(const _PyConfigCtx *ctx, size_t size);
PyAPI_FUNC(void *) _PyMem_RawCallocCtx(const _PyConfigCtx *ctx, size_t nelem, size_t elsize);
PyAPI_FUNC(void *) _PyMem_RawReallocCtx(const _PyConfigCtx *ctx, void *ptr, size_t new_size);
PyAPI_FUNC(void) _PyMem_RawFreeCtx(const _PyConfigCtx *ctx, void *ptr);
PyAPI_FUNC(char *) _PyMem_RawStrdupCtx(const _PyConfigCtx *ctx, const char *str);
PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const _PyConfigCtx *ctx, const wchar_t *str);


typedef struct {
_PyConfigCtx ctx;

/* Install signal handlers? Yes by default. */
int install_signal_handlers;

Expand Down Expand Up @@ -102,11 +124,6 @@ typedef struct {
char *filesystem_encoding;
char *filesystem_errors;

/* Enable UTF-8 mode?
Set by -X utf8 command line option and PYTHONUTF8 environment variable.
If set to -1 (default), inherit Py_UTF8Mode value. */
int utf8_mode;

wchar_t *pycache_prefix; /* PYTHONPYCACHEPREFIX, -X pycache_prefix=PATH */

wchar_t *program_name; /* Program name, see also Py_GetProgramName() */
Expand Down Expand Up @@ -309,15 +326,15 @@ typedef struct {
# define _PyCoreConfig_WINDOWS_INIT
#endif

#define _PyCoreConfig_INIT \
/* _PyCoreConfig_STATIC_INIT must not be used: use _PyCoreConfig_Init() */
#define _PyCoreConfig_STATIC_INIT \
(_PyCoreConfig){ \
.install_signal_handlers = 1, \
.use_environment = -1, \
.use_hash_seed = -1, \
.faulthandler = -1, \
.tracemalloc = -1, \
.coerce_c_locale = -1, \
.utf8_mode = -1, \
.argc = -1, \
.nmodule_search_path = -1, \
.isolated = -1, \
Expand All @@ -336,10 +353,11 @@ typedef struct {
._install_importlib = 1, \
._check_hash_pycs_mode = "default", \
._frozen = -1}
/* Note: _PyCoreConfig_INIT sets other fields to 0/NULL */
/* Note: _PyCoreConfig_STATIC_INIT sets other fields to 0/NULL */

PyAPI_FUNC(void) _PyCoreConfig_Init(_PyCoreConfig *config);


#ifndef Py_LIMITED_API
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config);
PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);
PyAPI_FUNC(int) _PyCoreConfig_Copy(
Expand All @@ -362,9 +380,9 @@ PyAPI_FUNC(int) _PyCoreConfig_GetEnvDup(
/* Used by _testcapi.get_global_config() and _testcapi.get_core_config() */
PyAPI_FUNC(PyObject *) _Py_GetGlobalVariablesAsDict(void);
PyAPI_FUNC(PyObject *) _PyCoreConfig_AsDict(const _PyCoreConfig *config);
#endif

#ifdef __cplusplus
}
#endif
#endif /* !Py_LIMITED_API */
#endif /* !Py_PYCORECONFIG_H */
4 changes: 4 additions & 0 deletions Include/fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ typedef enum {

PyAPI_FUNC(_Py_error_handler) _Py_GetErrorHandler(const char *errors);


PyAPI_FUNC(int) _Py_DecodeLocaleEx(
const _PyConfigCtx *ctx,
const char *arg,
wchar_t **wstr,
size_t *wlen,
Expand All @@ -43,6 +45,7 @@ PyAPI_FUNC(int) _Py_DecodeLocaleEx(
_Py_error_handler errors);

PyAPI_FUNC(int) _Py_EncodeLocaleEx(
const _PyConfigCtx *ctx,
const wchar_t *text,
char **str,
size_t *error_pos,
Expand All @@ -51,6 +54,7 @@ PyAPI_FUNC(int) _Py_EncodeLocaleEx(
_Py_error_handler errors);
#endif


#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _Py_device_encoding(int);

Expand Down
7 changes: 7 additions & 0 deletions Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ extern "C" {
# error "Py_BUILD_CORE must be defined to include this header"
#endif

PyAPI_FUNC(wchar_t*) _Py_DecodeLocaleCtx(
const _PyConfigCtx *ctx,
const char* arg,
size_t *wlen);

PyAPI_FUNC(int) _Py_DecodeUTF8Ex(
const _PyConfigCtx *ctx,
const char *arg,
Py_ssize_t arglen,
wchar_t **wstr,
Expand All @@ -17,6 +23,7 @@ PyAPI_FUNC(int) _Py_DecodeUTF8Ex(
_Py_error_handler errors);

PyAPI_FUNC(int) _Py_EncodeUTF8Ex(
const _PyConfigCtx *ctx,
const wchar_t *text,
char **str,
size_t *error_pos,
Expand Down
23 changes: 16 additions & 7 deletions Include/internal/pycore_pathconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ extern "C" {
#endif

PyAPI_FUNC(void) _Py_wstrlist_clear(
const _PyConfigCtx *ctx,
int len,
wchar_t **list);
PyAPI_FUNC(wchar_t**) _Py_wstrlist_copy(
const _PyConfigCtx *ctx,
int len,
wchar_t **list);
PyAPI_FUNC(_PyInitError) _Py_wstrlist_append(
const _PyConfigCtx *ctx,
int *len,
wchar_t ***list,
const wchar_t *str);
Expand All @@ -23,6 +26,8 @@ PyAPI_FUNC(PyObject*) _Py_wstrlist_as_pylist(
wchar_t **list);

typedef struct _PyPathConfig {
_PyConfigCtx ctx;

/* Full path to the Python program */
wchar_t *program_full_path;
wchar_t *prefix;
Expand All @@ -44,17 +49,21 @@ typedef struct _PyPathConfig {
int site_import;
} _PyPathConfig;

#define _PyPathConfig_INIT \
{.module_search_path = NULL, \
.isolated = -1, \
.site_import = -1}
/* Note: _PyPathConfig_INIT sets other fields to 0/NULL */
#define _PyPathConfig_STATIC_INIT \
(_PyPathConfig){ \
.module_search_path = NULL, \
.isolated = -1, \
.site_import = -1}
/* Note: _PyPathConfig_STATIC_INIT sets other fields to 0/NULL */

PyAPI_DATA(_PyPathConfig) _Py_path_config;
PyAPI_FUNC(void) _PyPathConfig_StaticInit(
_PyPathConfig *config);

PyAPI_FUNC(void) _PyPathConfig_ClearGlobal(void);
PyAPI_FUNC(_PyInitError) _PyPathConfig_SetGlobal(
const struct _PyPathConfig *config);
const _PyPathConfig *config);

PyAPI_FUNC(_PyPathConfig*) _PyPathConfig_GetGlobal(void);

PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate_impl(
_PyPathConfig *config,
Expand Down
4 changes: 1 addition & 3 deletions Include/pymem.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);

/* strdup() using PyMem_Malloc() */
PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);

/* wcsdup() using PyMem_RawMalloc() */
PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str);
#endif

/* Macros. */
Expand Down Expand Up @@ -198,6 +195,7 @@ PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
#endif /* Py_LIMITED_API */


/* bpo-35053: expose _Py_tracemalloc_config for performance:
_Py_NewReference() needs an efficient check to test if tracemalloc is
tracing.
Expand Down
6 changes: 4 additions & 2 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4622,7 +4622,8 @@ encode_locale_ex(PyObject *self, PyObject *args)
char *str = NULL;
size_t error_pos;
const char *reason = NULL;
int ret = _Py_EncodeLocaleEx(wstr,
PyInterpreterState *interp = _PyInterpreterState_Get();
int ret = _Py_EncodeLocaleEx(&interp->core_config.ctx, wstr,
&str, &error_pos, &reason,
current_locale, error_handler);
PyMem_Free(wstr);
Expand Down Expand Up @@ -4666,7 +4667,8 @@ decode_locale_ex(PyObject *self, PyObject *args)
wchar_t *wstr = NULL;
size_t wlen = 0;
const char *reason = NULL;
int ret = _Py_DecodeLocaleEx(str,
PyInterpreterState *interp = _PyInterpreterState_Get();
int ret = _Py_DecodeLocaleEx(&interp->core_config.ctx, str,
&wstr, &wlen, &reason,
current_locale, error_handler);

Expand Down
Loading