|
1 | 1 | #include "Python.h" |
2 | 2 | #include "osdefs.h" /* DELIM */ |
3 | | -#include "pycore_initconfig.h" |
4 | 3 | #include "pycore_fileutils.h" |
5 | 4 | #include "pycore_getopt.h" |
| 5 | +#include "pycore_initconfig.h" |
| 6 | +#include "pycore_pathconfig.h" |
| 7 | +#include "pycore_pyerrors.h" |
6 | 8 | #include "pycore_pylifecycle.h" |
7 | 9 | #include "pycore_pymem.h" |
8 | 10 | #include "pycore_pystate.h" /* _PyRuntime */ |
9 | | -#include "pycore_pathconfig.h" |
10 | 11 | #include <locale.h> /* setlocale() */ |
11 | 12 | #ifdef HAVE_LANGINFO_H |
12 | 13 | # include <langinfo.h> /* nl_langinfo(CODESET) */ |
@@ -2539,3 +2540,97 @@ _Py_GetConfigsAsDict(void) |
2539 | 2540 | Py_XDECREF(dict); |
2540 | 2541 | return NULL; |
2541 | 2542 | } |
| 2543 | + |
| 2544 | + |
| 2545 | +static void |
| 2546 | +init_dump_ascii_wstr(const wchar_t *str) |
| 2547 | +{ |
| 2548 | + if (str == NULL) { |
| 2549 | + PySys_WriteStderr("(not set)"); |
| 2550 | + } |
| 2551 | + |
| 2552 | + PySys_WriteStderr("'"); |
| 2553 | + for (; *str != L'\0'; str++) { |
| 2554 | + wchar_t ch = *str; |
| 2555 | + if (ch == L'\'') { |
| 2556 | + PySys_WriteStderr("\\'"); |
| 2557 | + } else if (0x20 <= ch && ch < 0x7f) { |
| 2558 | + PySys_WriteStderr("%lc", ch); |
| 2559 | + } |
| 2560 | + else if (ch <= 0xff) { |
| 2561 | + PySys_WriteStderr("\\x%02x", ch); |
| 2562 | + } |
| 2563 | +#if SIZEOF_WCHAR_T > 2 |
| 2564 | + else if (ch > 0xffff) { |
| 2565 | + PySys_WriteStderr("\\U%08x", ch); |
| 2566 | + } |
| 2567 | +#endif |
| 2568 | + else { |
| 2569 | + PySys_WriteStderr("\\u%04x", ch); |
| 2570 | + } |
| 2571 | + } |
| 2572 | + PySys_WriteStderr("'"); |
| 2573 | +} |
| 2574 | + |
| 2575 | + |
| 2576 | +/* Dump the Python path configuration into sys.stderr */ |
| 2577 | +void |
| 2578 | +_Py_DumpPathConfig(PyThreadState *tstate) |
| 2579 | +{ |
| 2580 | + PyObject *exc_type, *exc_value, *exc_tb; |
| 2581 | + _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb); |
| 2582 | + |
| 2583 | + PySys_WriteStderr("Python path configuration:\n"); |
| 2584 | + |
| 2585 | +#define DUMP_CONFIG(NAME, FIELD) \ |
| 2586 | + do { \ |
| 2587 | + PySys_WriteStderr(" " NAME " = "); \ |
| 2588 | + init_dump_ascii_wstr(config->FIELD); \ |
| 2589 | + PySys_WriteStderr("\n"); \ |
| 2590 | + } while (0) |
| 2591 | + |
| 2592 | + PyConfig *config = &tstate->interp->config; |
| 2593 | + DUMP_CONFIG("PYTHONHOME", home); |
| 2594 | + DUMP_CONFIG("PYTHONPATH", pythonpath_env); |
| 2595 | + DUMP_CONFIG("program name", program_name); |
| 2596 | + PySys_WriteStderr(" isolated = %i\n", config->isolated); |
| 2597 | + PySys_WriteStderr(" environment = %i\n", config->use_environment); |
| 2598 | + PySys_WriteStderr(" user site = %i\n", config->user_site_directory); |
| 2599 | + PySys_WriteStderr(" import site = %i\n", config->site_import); |
| 2600 | +#undef DUMP_CONFIG |
| 2601 | + |
| 2602 | +#define DUMP_SYS(NAME) \ |
| 2603 | + do { \ |
| 2604 | + obj = PySys_GetObject(#NAME); \ |
| 2605 | + PySys_FormatStderr(" sys.%s = ", #NAME); \ |
| 2606 | + if (obj != NULL) { \ |
| 2607 | + PySys_FormatStderr("%A", obj); \ |
| 2608 | + } \ |
| 2609 | + else { \ |
| 2610 | + PySys_WriteStderr("(not set)"); \ |
| 2611 | + } \ |
| 2612 | + PySys_FormatStderr("\n"); \ |
| 2613 | + } while (0) |
| 2614 | + |
| 2615 | + PyObject *obj; |
| 2616 | + DUMP_SYS(_base_executable); |
| 2617 | + DUMP_SYS(base_prefix); |
| 2618 | + DUMP_SYS(base_exec_prefix); |
| 2619 | + DUMP_SYS(executable); |
| 2620 | + DUMP_SYS(prefix); |
| 2621 | + DUMP_SYS(exec_prefix); |
| 2622 | +#undef DUMP_SYS |
| 2623 | + |
| 2624 | + PyObject *sys_path = PySys_GetObject("path"); /* borrowed reference */ |
| 2625 | + if (sys_path != NULL && PyList_Check(sys_path)) { |
| 2626 | + PySys_WriteStderr(" sys.path = [\n"); |
| 2627 | + Py_ssize_t len = PyList_GET_SIZE(sys_path); |
| 2628 | + for (Py_ssize_t i=0; i < len; i++) { |
| 2629 | + PyObject *path = PyList_GET_ITEM(sys_path, i); |
| 2630 | + PySys_FormatStderr(" %A,\n", path); |
| 2631 | + } |
| 2632 | + PySys_WriteStderr(" ]\n"); |
| 2633 | + } |
| 2634 | + |
| 2635 | + _PyErr_Restore(tstate, exc_type, exc_value, exc_tb); |
| 2636 | +} |
0 commit comments