• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef Py_INTERNAL_CORECONFIG_H
2 #define Py_INTERNAL_CORECONFIG_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #ifndef Py_BUILD_CORE
8 #  error "this header requires Py_BUILD_CORE define"
9 #endif
10 
11 /* Forward declaration */
12 struct pyruntimestate;
13 
14 /* --- PyStatus ----------------------------------------------- */
15 
16 /* Almost all errors causing Python initialization to fail */
17 #ifdef _MSC_VER
18    /* Visual Studio 2015 doesn't implement C99 __func__ in C */
19 #  define _PyStatus_GET_FUNC() __FUNCTION__
20 #else
21 #  define _PyStatus_GET_FUNC() __func__
22 #endif
23 
24 #define _PyStatus_OK() \
25     (PyStatus){._type = _PyStatus_TYPE_OK}
26     /* other fields are set to 0 */
27 #define _PyStatus_ERR(ERR_MSG) \
28     (PyStatus){ \
29         ._type = _PyStatus_TYPE_ERROR, \
30         .func = _PyStatus_GET_FUNC(), \
31         .err_msg = (ERR_MSG)}
32         /* other fields are set to 0 */
33 #define _PyStatus_NO_MEMORY_ERRMSG "memory allocation failed"
34 #define _PyStatus_NO_MEMORY() _PyStatus_ERR(_PyStatus_NO_MEMORY_ERRMSG)
35 #define _PyStatus_EXIT(EXITCODE) \
36     (PyStatus){ \
37         ._type = _PyStatus_TYPE_EXIT, \
38         .exitcode = (EXITCODE)}
39 #define _PyStatus_IS_ERROR(err) \
40     ((err)._type == _PyStatus_TYPE_ERROR)
41 #define _PyStatus_IS_EXIT(err) \
42     ((err)._type == _PyStatus_TYPE_EXIT)
43 #define _PyStatus_EXCEPTION(err) \
44     ((err)._type != _PyStatus_TYPE_OK)
45 #define _PyStatus_UPDATE_FUNC(err) \
46     do { (err).func = _PyStatus_GET_FUNC(); } while (0)
47 
48 // Export for '_testinternalcapi' shared extension
49 PyAPI_FUNC(void) _PyErr_SetFromPyStatus(PyStatus status);
50 
51 
52 /* --- PyWideStringList ------------------------------------------------ */
53 
54 #define _PyWideStringList_INIT (PyWideStringList){.length = 0, .items = NULL}
55 
56 #ifndef NDEBUG
57 extern int _PyWideStringList_CheckConsistency(const PyWideStringList *list);
58 #endif
59 extern void _PyWideStringList_Clear(PyWideStringList *list);
60 extern int _PyWideStringList_Copy(PyWideStringList *list,
61     const PyWideStringList *list2);
62 extern PyStatus _PyWideStringList_Extend(PyWideStringList *list,
63     const PyWideStringList *list2);
64 extern PyObject* _PyWideStringList_AsList(const PyWideStringList *list);
65 
66 
67 /* --- _PyArgv ---------------------------------------------------- */
68 
69 typedef struct _PyArgv {
70     Py_ssize_t argc;
71     int use_bytes_argv;
72     char * const *bytes_argv;
73     wchar_t * const *wchar_argv;
74 } _PyArgv;
75 
76 extern PyStatus _PyArgv_AsWstrList(const _PyArgv *args,
77     PyWideStringList *list);
78 
79 
80 /* --- Helper functions ------------------------------------------- */
81 
82 extern int _Py_str_to_int(
83     const char *str,
84     int *result);
85 extern const wchar_t* _Py_get_xoption(
86     const PyWideStringList *xoptions,
87     const wchar_t *name);
88 extern const char* _Py_GetEnv(
89     int use_environment,
90     const char *name);
91 extern void _Py_get_env_flag(
92     int use_environment,
93     int *flag,
94     const char *name);
95 
96 /* Py_GetArgcArgv() helper */
97 extern void _Py_ClearArgcArgv(void);
98 
99 
100 /* --- _PyPreCmdline ------------------------------------------------- */
101 
102 typedef struct {
103     PyWideStringList argv;
104     PyWideStringList xoptions;     /* "-X value" option */
105     int isolated;             /* -I option */
106     int use_environment;      /* -E option */
107     int dev_mode;             /* -X dev and PYTHONDEVMODE */
108     int warn_default_encoding;     /* -X warn_default_encoding and PYTHONWARNDEFAULTENCODING */
109 } _PyPreCmdline;
110 
111 #define _PyPreCmdline_INIT \
112     (_PyPreCmdline){ \
113         .use_environment = -1, \
114         .isolated = -1, \
115         .dev_mode = -1}
116 /* Note: _PyPreCmdline_INIT sets other fields to 0/NULL */
117 
118 extern void _PyPreCmdline_Clear(_PyPreCmdline *cmdline);
119 extern PyStatus _PyPreCmdline_SetArgv(_PyPreCmdline *cmdline,
120     const _PyArgv *args);
121 extern PyStatus _PyPreCmdline_SetConfig(
122     const _PyPreCmdline *cmdline,
123     PyConfig *config);
124 extern PyStatus _PyPreCmdline_Read(_PyPreCmdline *cmdline,
125     const PyPreConfig *preconfig);
126 
127 
128 /* --- PyPreConfig ----------------------------------------------- */
129 
130 // Export for '_testembed' program
131 PyAPI_FUNC(void) _PyPreConfig_InitCompatConfig(PyPreConfig *preconfig);
132 
133 extern void _PyPreConfig_InitFromConfig(
134     PyPreConfig *preconfig,
135     const PyConfig *config);
136 extern PyStatus _PyPreConfig_InitFromPreConfig(
137     PyPreConfig *preconfig,
138     const PyPreConfig *config2);
139 extern PyObject* _PyPreConfig_AsDict(const PyPreConfig *preconfig);
140 extern void _PyPreConfig_GetConfig(PyPreConfig *preconfig,
141     const PyConfig *config);
142 extern PyStatus _PyPreConfig_Read(PyPreConfig *preconfig,
143     const _PyArgv *args);
144 extern PyStatus _PyPreConfig_Write(const PyPreConfig *preconfig);
145 
146 
147 /* --- PyConfig ---------------------------------------------- */
148 
149 typedef enum {
150     /* Py_Initialize() API: backward compatibility with Python 3.6 and 3.7 */
151     _PyConfig_INIT_COMPAT = 1,
152     _PyConfig_INIT_PYTHON = 2,
153     _PyConfig_INIT_ISOLATED = 3
154 } _PyConfigInitEnum;
155 
156 typedef enum {
157     /* For now, this means the GIL is enabled.
158 
159        gh-116329: This will eventually change to "the GIL is disabled but can
160        be reenabled by loading an incompatible extension module." */
161     _PyConfig_GIL_DEFAULT = -1,
162 
163     /* The GIL has been forced off or on, and will not be affected by module loading. */
164     _PyConfig_GIL_DISABLE = 0,
165     _PyConfig_GIL_ENABLE = 1,
166 } _PyConfigGILEnum;
167 
168 // Export for '_testembed' program
169 PyAPI_FUNC(void) _PyConfig_InitCompatConfig(PyConfig *config);
170 
171 extern PyStatus _PyConfig_Copy(
172     PyConfig *config,
173     const PyConfig *config2);
174 extern PyStatus _PyConfig_InitPathConfig(
175     PyConfig *config,
176     int compute_path_config);
177 extern PyStatus _PyConfig_InitImportConfig(PyConfig *config);
178 extern PyStatus _PyConfig_Read(PyConfig *config, int compute_path_config);
179 extern PyStatus _PyConfig_Write(const PyConfig *config,
180     struct pyruntimestate *runtime);
181 extern PyStatus _PyConfig_SetPyArgv(
182     PyConfig *config,
183     const _PyArgv *args);
184 
185 
186 extern void _Py_DumpPathConfig(PyThreadState *tstate);
187 
188 
189 /* --- Function used for testing ---------------------------------- */
190 
191 // Export these functions for '_testinternalcapi' shared extension
192 PyAPI_FUNC(PyObject*) _PyConfig_AsDict(const PyConfig *config);
193 PyAPI_FUNC(int) _PyConfig_FromDict(PyConfig *config, PyObject *dict);
194 PyAPI_FUNC(PyObject*) _Py_Get_Getpath_CodeObject(void);
195 PyAPI_FUNC(PyObject*) _Py_GetConfigsAsDict(void);
196 
197 #ifdef __cplusplus
198 }
199 #endif
200 #endif /* !Py_INTERNAL_CORECONFIG_H */
201