Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "pycore_coreconfig.h" |
| 3 | |
| 4 | |
| 5 | #define DECODE_LOCALE_ERR(NAME, LEN) \ |
| 6 | (((LEN) == -2) \ |
| 7 | ? _Py_INIT_USER_ERR("cannot decode " NAME) \ |
| 8 | : _Py_INIT_NO_MEMORY()) |
| 9 | |
| 10 | |
| 11 | /* --- File system encoding/errors -------------------------------- */ |
| 12 | |
| 13 | /* The filesystem encoding is chosen by config_init_fs_encoding(), |
| 14 | see also initfsencoding(). */ |
| 15 | const char *Py_FileSystemDefaultEncoding = NULL; |
| 16 | int Py_HasFileSystemDefaultEncoding = 0; |
| 17 | const char *Py_FileSystemDefaultEncodeErrors = NULL; |
| 18 | int _Py_HasFileSystemDefaultEncodeErrors = 0; |
| 19 | |
| 20 | void |
| 21 | _Py_ClearFileSystemEncoding(void) |
| 22 | { |
| 23 | if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) { |
| 24 | PyMem_RawFree((char*)Py_FileSystemDefaultEncoding); |
| 25 | Py_FileSystemDefaultEncoding = NULL; |
| 26 | } |
| 27 | if (!_Py_HasFileSystemDefaultEncodeErrors && Py_FileSystemDefaultEncodeErrors) { |
| 28 | PyMem_RawFree((char*)Py_FileSystemDefaultEncodeErrors); |
| 29 | Py_FileSystemDefaultEncodeErrors = NULL; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors |
| 35 | global configuration variables. */ |
| 36 | int |
| 37 | _Py_SetFileSystemEncoding(const char *encoding, const char *errors) |
| 38 | { |
| 39 | char *encoding2 = _PyMem_RawStrdup(encoding); |
| 40 | if (encoding2 == NULL) { |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | char *errors2 = _PyMem_RawStrdup(errors); |
| 45 | if (errors2 == NULL) { |
| 46 | PyMem_RawFree(encoding2); |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | _Py_ClearFileSystemEncoding(); |
| 51 | |
| 52 | Py_FileSystemDefaultEncoding = encoding2; |
| 53 | Py_HasFileSystemDefaultEncoding = 0; |
| 54 | |
| 55 | Py_FileSystemDefaultEncodeErrors = errors2; |
| 56 | _Py_HasFileSystemDefaultEncodeErrors = 0; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /* --- _PyArgv ---------------------------------------------------- */ |
| 62 | |
| 63 | _PyInitError |
| 64 | _PyArgv_Decode(const _PyArgv *args, wchar_t*** argv_p) |
| 65 | { |
| 66 | wchar_t** argv; |
| 67 | if (args->use_bytes_argv) { |
| 68 | /* +1 for a the NULL terminator */ |
| 69 | size_t size = sizeof(wchar_t*) * (args->argc + 1); |
| 70 | argv = (wchar_t **)PyMem_RawMalloc(size); |
| 71 | if (argv == NULL) { |
| 72 | return _Py_INIT_NO_MEMORY(); |
| 73 | } |
| 74 | |
| 75 | for (int i = 0; i < args->argc; i++) { |
| 76 | size_t len; |
| 77 | wchar_t *arg = Py_DecodeLocale(args->bytes_argv[i], &len); |
| 78 | if (arg == NULL) { |
| 79 | _Py_wstrlist_clear(i, argv); |
| 80 | return DECODE_LOCALE_ERR("command line arguments", |
| 81 | (Py_ssize_t)len); |
| 82 | } |
| 83 | argv[i] = arg; |
| 84 | } |
| 85 | argv[args->argc] = NULL; |
| 86 | } |
| 87 | else { |
| 88 | argv = args->wchar_argv; |
| 89 | } |
| 90 | *argv_p = argv; |
| 91 | return _Py_INIT_OK(); |
| 92 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame^] | 93 | |
| 94 | |
| 95 | /* --- _PyPreConfig ----------------------------------------------- */ |
| 96 | |
| 97 | void |
| 98 | _PyPreConfig_Clear(_PyPreConfig *config) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | |
| 103 | int |
| 104 | _PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2) |
| 105 | { |
| 106 | _PyPreConfig_Clear(config); |
| 107 | |
| 108 | #define COPY_ATTR(ATTR) config->ATTR = config2->ATTR |
| 109 | |
| 110 | COPY_ATTR(isolated); |
| 111 | COPY_ATTR(use_environment); |
| 112 | |
| 113 | #undef COPY_ATTR |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void |
| 119 | _PyPreConfig_GetGlobalConfig(_PyPreConfig *config) |
| 120 | { |
| 121 | #define COPY_FLAG(ATTR, VALUE) \ |
| 122 | if (config->ATTR == -1) { \ |
| 123 | config->ATTR = VALUE; \ |
| 124 | } |
| 125 | #define COPY_NOT_FLAG(ATTR, VALUE) \ |
| 126 | if (config->ATTR == -1) { \ |
| 127 | config->ATTR = !(VALUE); \ |
| 128 | } |
| 129 | |
| 130 | COPY_FLAG(isolated, Py_IsolatedFlag); |
| 131 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
| 132 | |
| 133 | #undef COPY_FLAG |
| 134 | #undef COPY_NOT_FLAG |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void |
| 139 | _PyPreConfig_SetGlobalConfig(const _PyPreConfig *config) |
| 140 | { |
| 141 | #define COPY_FLAG(ATTR, VAR) \ |
| 142 | if (config->ATTR != -1) { \ |
| 143 | VAR = config->ATTR; \ |
| 144 | } |
| 145 | #define COPY_NOT_FLAG(ATTR, VAR) \ |
| 146 | if (config->ATTR != -1) { \ |
| 147 | VAR = !config->ATTR; \ |
| 148 | } |
| 149 | |
| 150 | COPY_FLAG(isolated, Py_IsolatedFlag); |
| 151 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
| 152 | |
| 153 | #undef COPY_FLAG |
| 154 | #undef COPY_NOT_FLAG |
| 155 | } |
| 156 | |
| 157 | |
| 158 | _PyInitError |
| 159 | _PyPreConfig_Read(_PyPreConfig *config) |
| 160 | { |
| 161 | _PyPreConfig_GetGlobalConfig(config); |
| 162 | |
| 163 | if (config->isolated > 0) { |
| 164 | config->use_environment = 0; |
| 165 | } |
| 166 | |
| 167 | /* Default values */ |
| 168 | if (config->use_environment < 0) { |
| 169 | config->use_environment = 0; |
| 170 | } |
| 171 | |
| 172 | assert(config->use_environment >= 0); |
| 173 | |
| 174 | return _Py_INIT_OK(); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | int |
| 179 | _PyPreConfig_AsDict(const _PyPreConfig *config, PyObject *dict) |
| 180 | { |
| 181 | #define SET_ITEM(KEY, EXPR) \ |
| 182 | do { \ |
| 183 | PyObject *obj = (EXPR); \ |
| 184 | if (obj == NULL) { \ |
| 185 | goto fail; \ |
| 186 | } \ |
| 187 | int res = PyDict_SetItemString(dict, (KEY), obj); \ |
| 188 | Py_DECREF(obj); \ |
| 189 | if (res < 0) { \ |
| 190 | goto fail; \ |
| 191 | } \ |
| 192 | } while (0) |
| 193 | #define SET_ITEM_INT(ATTR) \ |
| 194 | SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR)) |
| 195 | |
| 196 | SET_ITEM_INT(isolated); |
| 197 | SET_ITEM_INT(use_environment); |
| 198 | return 0; |
| 199 | |
| 200 | fail: |
| 201 | return -1; |
| 202 | |
| 203 | #undef SET_ITEM |
| 204 | #undef SET_ITEM_INT |
| 205 | } |