| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,10 +16,11 @@ PyAPI_DATA(int) _Py_UnhandledKeyboardInterrupt; | |||
| 16 | 16 | ||
| 17 | 17 | PyAPI_FUNC(int) _Py_UnixMain(int argc, char **argv); | |
| 18 | 18 | ||
| 19 | - PyAPI_FUNC(int) _Py_SetFileSystemEncoding( | ||
| 19 | + extern int _Py_SetFileSystemEncoding( | ||
| 20 | 20 | const char *encoding, | |
| 21 | 21 | const char *errors); | |
| 22 | - PyAPI_FUNC(void) _Py_ClearFileSystemEncoding(void); | ||
| 22 | + extern void _Py_ClearFileSystemEncoding(void); | ||
| 23 | + extern _PyInitError _PyUnicode_InitEncodings(PyInterpreterState *interp); | ||
| 23 | 24 | ||
| 24 | 25 | PyAPI_FUNC(void) _Py_ClearStandardStreamEncoding(void); | |
| 25 | 26 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,6 +42,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |||
| 42 | 42 | #include "Python.h" | |
| 43 | 43 | #include "pycore_fileutils.h" | |
| 44 | 44 | #include "pycore_object.h" | |
| 45 | + #include "pycore_pylifecycle.h" | ||
| 45 | 46 | #include "pycore_pystate.h" | |
| 46 | 47 | #include "ucnhash.h" | |
| 47 | 48 | #include "bytes_methods.h" | |
@@ -15574,6 +15575,102 @@ PyUnicode_AsUnicodeCopy(PyObject *unicode) | |||
| 15574 | 15575 | } | |
| 15575 | 15576 | ||
| 15576 | 15577 | ||
| 15578 | + static char* | ||
| 15579 | + get_codec_name(const char *encoding) | ||
| 15580 | + { | ||
| 15581 | + PyObject *codec, *name_obj = NULL; | ||
| 15582 | + | ||
| 15583 | + codec = _PyCodec_Lookup(encoding); | ||
| 15584 | + if (!codec) | ||
| 15585 | + goto error; | ||
| 15586 | + | ||
| 15587 | + name_obj = PyObject_GetAttrString(codec, "name"); | ||
| 15588 | + Py_CLEAR(codec); | ||
| 15589 | + if (!name_obj) { | ||
| 15590 | + goto error; | ||
| 15591 | + } | ||
| 15592 | + | ||
| 15593 | + const char *name_utf8 = PyUnicode_AsUTF8(name_obj); | ||
| 15594 | + if (name_utf8 == NULL) { | ||
| 15595 | + goto error; | ||
| 15596 | + } | ||
| 15597 | + | ||
| 15598 | + char *name = _PyMem_RawStrdup(name_utf8); | ||
| 15599 | + Py_DECREF(name_obj); | ||
| 15600 | + if (name == NULL) { | ||
| 15601 | + PyErr_NoMemory(); | ||
| 15602 | + return NULL; | ||
| 15603 | + } | ||
| 15604 | + return name; | ||
| 15605 | + | ||
| 15606 | + error: | ||
| 15607 | + Py_XDECREF(codec); | ||
| 15608 | + Py_XDECREF(name_obj); | ||
| 15609 | + return NULL; | ||
| 15610 | + } | ||
| 15611 | + | ||
| 15612 | + | ||
| 15613 | + static _PyInitError | ||
| 15614 | + init_stdio_encoding(PyInterpreterState *interp) | ||
| 15615 | + { | ||
| 15616 | + _PyCoreConfig *config = &interp->core_config; | ||
| 15617 | + | ||
| 15618 | + char *codec_name = get_codec_name(config->stdio_encoding); | ||
| 15619 | + if (codec_name == NULL) { | ||
| 15620 | + return _Py_INIT_ERR("failed to get the Python codec name " | ||
| 15621 | + "of the stdio encoding"); | ||
| 15622 | + } | ||
| 15623 | + PyMem_RawFree(config->stdio_encoding); | ||
| 15624 | + config->stdio_encoding = codec_name; | ||
| 15625 | + return _Py_INIT_OK(); | ||
| 15626 | + } | ||
| 15627 | + | ||
| 15628 | + | ||
| 15629 | + static _PyInitError | ||
| 15630 | + init_fs_encoding(PyInterpreterState *interp) | ||
| 15631 | + { | ||
| 15632 | + _PyCoreConfig *config = &interp->core_config; | ||
| 15633 | + | ||
| 15634 | + char *encoding = get_codec_name(config->filesystem_encoding); | ||
| 15635 | + if (encoding == NULL) { | ||
| 15636 | + /* Such error can only occurs in critical situations: no more | ||
| 15637 | + memory, import a module of the standard library failed, etc. */ | ||
| 15638 | + return _Py_INIT_ERR("failed to get the Python codec " | ||
| 15639 | + "of the filesystem encoding"); | ||
| 15640 | + } | ||
| 15641 | + | ||
| 15642 | + /* Update the filesystem encoding to the normalized Python codec name. | ||
| 15643 | + For example, replace "ANSI_X3.4-1968" (locale encoding) with "ascii" | ||
| 15644 | + (Python codec name). */ | ||
| 15645 | + PyMem_RawFree(config->filesystem_encoding); | ||
| 15646 | + config->filesystem_encoding = encoding; | ||
| 15647 | + | ||
| 15648 | + /* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors | ||
| 15649 | + global configuration variables. */ | ||
| 15650 | + if (_Py_SetFileSystemEncoding(config->filesystem_encoding, | ||
| 15651 | + config->filesystem_errors) < 0) { | ||
| 15652 | + return _Py_INIT_NO_MEMORY(); | ||
| 15653 | + } | ||
| 15654 | + | ||
| 15655 | + /* PyUnicode can now use the Python codec rather than C implementation | ||
| 15656 | + for the filesystem encoding */ | ||
| 15657 | + interp->fscodec_initialized = 1; | ||
| 15658 | + return _Py_INIT_OK(); | ||
| 15659 | + } | ||
| 15660 | + | ||
| 15661 | + | ||
| 15662 | + _PyInitError | ||
| 15663 | + _PyUnicode_InitEncodings(PyInterpreterState *interp) | ||
| 15664 | + { | ||
| 15665 | + _PyInitError err = init_fs_encoding(interp); | ||
| 15666 | + if (_Py_INIT_FAILED(err)) { | ||
| 15667 | + return err; | ||
| 15668 | + } | ||
| 15669 | + | ||
| 15670 | + return init_stdio_encoding(interp); | ||
| 15671 | + } | ||
| 15672 | + | ||
| 15673 | + | ||
| 15577 | 15674 | void | |
| 15578 | 15675 | _PyUnicode_Fini(void) | |
| 15579 | 15676 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,10 +59,9 @@ extern grammar _PyParser_Grammar; /* From graminit.c */ | |||
| 59 | 59 | ||
| 60 | 60 | /* Forward */ | |
| 61 | 61 | static _PyInitError add_main_module(PyInterpreterState *interp); | |
| 62 | - static _PyInitError initfsencoding(PyInterpreterState *interp); | ||
| 63 | - static _PyInitError initsite(void); | ||
| 62 | + static _PyInitError init_import_size(void); | ||
| 64 | 63 | static _PyInitError init_sys_streams(PyInterpreterState *interp); | |
| 65 | - static _PyInitError initsigs(void); | ||
| 64 | + static _PyInitError init_signals(void); | ||
| 66 | 65 | static void call_py_exitfuncs(PyInterpreterState *); | |
| 67 | 66 | static void wait_for_thread_shutdown(void); | |
| 68 | 67 | static void call_ll_exitfuncs(_PyRuntimeState *runtime); | |
@@ -144,42 +143,8 @@ Py_IsInitialized(void) | |||
| 144 | 143 | ||
| 145 | 144 | */ | |
| 146 | 145 | ||
| 147 | - static char* | ||
| 148 | - get_codec_name(const char *encoding) | ||
| 149 | - { | ||
| 150 | - const char *name_utf8; | ||
| 151 | - char *name_str; | ||
| 152 | - PyObject *codec, *name = NULL; | ||
| 153 | - | ||
| 154 | - codec = _PyCodec_Lookup(encoding); | ||
| 155 | - if (!codec) | ||
| 156 | - goto error; | ||
| 157 | - | ||
| 158 | - name = _PyObject_GetAttrId(codec, &PyId_name); | ||
| 159 | - Py_CLEAR(codec); | ||
| 160 | - if (!name) | ||
| 161 | - goto error; | ||
| 162 | - | ||
| 163 | - name_utf8 = PyUnicode_AsUTF8(name); | ||
| 164 | - if (name_utf8 == NULL) | ||
| 165 | - goto error; | ||
| 166 | - name_str = _PyMem_RawStrdup(name_utf8); | ||
| 167 | - Py_DECREF(name); | ||
| 168 | - if (name_str == NULL) { | ||
| 169 | - PyErr_NoMemory(); | ||
| 170 | - return NULL; | ||
| 171 | - } | ||
| 172 | - return name_str; | ||
| 173 | - | ||
| 174 | - error: | ||
| 175 | - Py_XDECREF(codec); | ||
| 176 | - Py_XDECREF(name); | ||
| 177 | - return NULL; | ||
| 178 | - } | ||
| 179 | - | ||
| 180 | - | ||
| 181 | 146 | static _PyInitError | |
| 182 | - initimport(PyInterpreterState *interp, PyObject *sysmod) | ||
| 147 | + init_importlib(PyInterpreterState *interp, PyObject *sysmod) | ||
| 183 | 148 | { | |
| 184 | 149 | PyObject *importlib; | |
| 185 | 150 | PyObject *impmod; | |
@@ -229,7 +194,7 @@ initimport(PyInterpreterState *interp, PyObject *sysmod) | |||
| 229 | 194 | } | |
| 230 | 195 | ||
| 231 | 196 | static _PyInitError | |
| 232 | - initexternalimport(PyInterpreterState *interp) | ||
| 197 | + init_importlib_external(PyInterpreterState *interp) | ||
| 233 | 198 | { | |
| 234 | 199 | PyObject *value; | |
| 235 | 200 | value = PyObject_CallMethod(interp->importlib, | |
@@ -661,7 +626,7 @@ pycore_init_import_warnings(PyInterpreterState *interp, PyObject *sysmod) | |||
| 661 | 626 | ||
| 662 | 627 | /* This call sets up builtin and frozen import support */ | |
| 663 | 628 | if (interp->core_config._install_importlib) { | |
| 664 | - err = initimport(interp, sysmod); | ||
| 629 | + err = init_importlib(interp, sysmod); | ||
| 665 | 630 | if (_Py_INIT_FAILED(err)) { | |
| 666 | 631 | return err; | |
| 667 | 632 | } | |
@@ -940,7 +905,7 @@ _Py_InitializeMainInterpreter(_PyRuntimeState *runtime, | |||
| 940 | 905 | return _Py_INIT_ERR("can't finish initializing sys"); | |
| 941 | 906 | } | |
| 942 | 907 | ||
| 943 | - _PyInitError err = initexternalimport(interp); | ||
| 908 | + _PyInitError err = init_importlib_external(interp); | ||
| 944 | 909 | if (_Py_INIT_FAILED(err)) { | |
| 945 | 910 | return err; | |
| 946 | 911 | } | |
@@ -951,13 +916,13 @@ _Py_InitializeMainInterpreter(_PyRuntimeState *runtime, | |||
| 951 | 916 | return err; | |
| 952 | 917 | } | |
| 953 | 918 | ||
| 954 | - err = initfsencoding(interp); | ||
| 919 | + err = _PyUnicode_InitEncodings(interp); | ||
| 955 | 920 | if (_Py_INIT_FAILED(err)) { | |
| 956 | 921 | return err; | |
| 957 | 922 | } | |
| 958 | 923 | ||
| 959 | 924 | if (core_config->install_signal_handlers) { | |
| 960 | - err = initsigs(); /* Signal handling stuff, including initintr() */ | ||
| 925 | + err = init_signals(); | ||
| 961 | 926 | if (_Py_INIT_FAILED(err)) { | |
| 962 | 927 | return err; | |
| 963 | 928 | } | |
@@ -992,7 +957,7 @@ _Py_InitializeMainInterpreter(_PyRuntimeState *runtime, | |||
| 992 | 957 | runtime->initialized = 1; | |
| 993 | 958 | ||
| 994 | 959 | if (core_config->site_import) { | |
| 995 | - err = initsite(); /* Module site */ | ||
| 960 | + err = init_import_size(); /* Module site */ | ||
| 996 | 961 | if (_Py_INIT_FAILED(err)) { | |
| 997 | 962 | return err; | |
| 998 | 963 | } | |
@@ -1497,17 +1462,17 @@ new_interpreter(PyThreadState **tstate_p) | |||
| 1497 | 1462 | return err; | |
| 1498 | 1463 | } | |
| 1499 | 1464 | ||
| 1500 | - err = initimport(interp, sysmod); | ||
| 1465 | + err = init_importlib(interp, sysmod); | ||
| 1501 | 1466 | if (_Py_INIT_FAILED(err)) { | |
| 1502 | 1467 | return err; | |
| 1503 | 1468 | } | |
| 1504 | 1469 | ||
| 1505 | - err = initexternalimport(interp); | ||
| 1470 | + err = init_importlib_external(interp); | ||
| 1506 | 1471 | if (_Py_INIT_FAILED(err)) { | |
| 1507 | 1472 | return err; | |
| 1508 | 1473 | } | |
| 1509 | 1474 | ||
| 1510 | - err = initfsencoding(interp); | ||
| 1475 | + err = _PyUnicode_InitEncodings(interp); | ||
| 1511 | 1476 | if (_Py_INIT_FAILED(err)) { | |
| 1512 | 1477 | return err; | |
| 1513 | 1478 | } | |
@@ -1523,7 +1488,7 @@ new_interpreter(PyThreadState **tstate_p) | |||
| 1523 | 1488 | } | |
| 1524 | 1489 | ||
| 1525 | 1490 | if (core_config->site_import) { | |
| 1526 | - err = initsite(); | ||
| 1491 | + err = init_import_size(); | ||
| 1527 | 1492 | if (_Py_INIT_FAILED(err)) { | |
| 1528 | 1493 | return err; | |
| 1529 | 1494 | } | |
@@ -1649,42 +1614,10 @@ add_main_module(PyInterpreterState *interp) | |||
| 1649 | 1614 | return _Py_INIT_OK(); | |
| 1650 | 1615 | } | |
| 1651 | 1616 | ||
| 1652 | - static _PyInitError | ||
| 1653 | - initfsencoding(PyInterpreterState *interp) | ||
| 1654 | - { | ||
| 1655 | - _PyCoreConfig *config = &interp->core_config; | ||
| 1656 | - | ||
| 1657 | - char *encoding = get_codec_name(config->filesystem_encoding); | ||
| 1658 | - if (encoding == NULL) { | ||
| 1659 | - /* Such error can only occurs in critical situations: no more | ||
| 1660 | - memory, import a module of the standard library failed, etc. */ | ||
| 1661 | - return _Py_INIT_ERR("failed to get the Python codec " | ||
| 1662 | - "of the filesystem encoding"); | ||
| 1663 | - } | ||
| 1664 | - | ||
| 1665 | - /* Update the filesystem encoding to the normalized Python codec name. | ||
| 1666 | - For example, replace "ANSI_X3.4-1968" (locale encoding) with "ascii" | ||
| 1667 | - (Python codec name). */ | ||
| 1668 | - PyMem_RawFree(config->filesystem_encoding); | ||
| 1669 | - config->filesystem_encoding = encoding; | ||
| 1670 | - | ||
| 1671 | - /* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors | ||
| 1672 | - global configuration variables. */ | ||
| 1673 | - if (_Py_SetFileSystemEncoding(config->filesystem_encoding, | ||
| 1674 | - config->filesystem_errors) < 0) { | ||
| 1675 | - return _Py_INIT_NO_MEMORY(); | ||
| 1676 | - } | ||
| 1677 | - | ||
| 1678 | - /* PyUnicode can now use the Python codec rather than C implementation | ||
| 1679 | - for the filesystem encoding */ | ||
| 1680 | - interp->fscodec_initialized = 1; | ||
| 1681 | - return _Py_INIT_OK(); | ||
| 1682 | - } | ||
| 1683 | - | ||
| 1684 | 1617 | /* Import the site module (not into __main__ though) */ | |
| 1685 | 1618 | ||
| 1686 | 1619 | static _PyInitError | |
| 1687 | - initsite(void) | ||
| 1620 | + init_import_size(void) | ||
| 1688 | 1621 | { | |
| 1689 | 1622 | PyObject *m; | |
| 1690 | 1623 | m = PyImport_ImportModule("site"); | |
@@ -1880,14 +1813,6 @@ init_sys_streams(PyInterpreterState *interp) | |||
| 1880 | 1813 | } | |
| 1881 | 1814 | #endif | |
| 1882 | 1815 | ||
| 1883 | - char *codec_name = get_codec_name(config->stdio_encoding); | ||
| 1884 | - if (codec_name == NULL) { | ||
| 1885 | - return _Py_INIT_ERR("failed to get the Python codec name " | ||
| 1886 | - "of the stdio encoding"); | ||
| 1887 | - } | ||
| 1888 | - PyMem_RawFree(config->stdio_encoding); | ||
| 1889 | - config->stdio_encoding = codec_name; | ||
| 1890 | - | ||
| 1891 | 1816 | /* Hack to avoid a nasty recursion issue when Python is invoked | |
| 1892 | 1817 | in verbose mode: pre-import the Latin-1 and UTF-8 codecs */ | |
| 1893 | 1818 | if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) { | |
@@ -2287,7 +2212,7 @@ Py_Exit(int sts) | |||
| 2287 | 2212 | } | |
| 2288 | 2213 | ||
| 2289 | 2214 | static _PyInitError | |
| 2290 | - initsigs(void) | ||
| 2215 | + init_signals(void) | ||
| 2291 | 2216 | { | |
| 2292 | 2217 | #ifdef SIGPIPE | |
| 2293 | 2218 | PyOS_setsig(SIGPIPE, SIG_IGN); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments