Victor Stinner | 23bace2 | 2019-04-18 11:37:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * C Extension module to test Python internal C APIs (Include/internal). |
| 3 | */ |
| 4 | |
| 5 | #if !defined(Py_BUILD_CORE_BUILTIN) && !defined(Py_BUILD_CORE_MODULE) |
| 6 | # error "Py_BUILD_CORE_BUILTIN or Py_BUILD_CORE_MODULE must be defined" |
| 7 | #endif |
| 8 | |
| 9 | #define PY_SSIZE_T_CLEAN |
| 10 | |
| 11 | #include "Python.h" |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 12 | #include "pycore_initconfig.h" |
Victor Stinner | 23bace2 | 2019-04-18 11:37:26 +0200 | [diff] [blame] | 13 | |
| 14 | |
Miss Islington (bot) | aa7f775 | 2020-07-06 10:12:16 -0700 | [diff] [blame] | 15 | #ifdef MS_WINDOWS |
| 16 | #include <windows.h> |
| 17 | |
| 18 | static int |
| 19 | _add_windows_config(PyObject *configs) |
| 20 | { |
| 21 | HMODULE hPython3; |
| 22 | wchar_t py3path[MAX_PATH]; |
| 23 | PyObject *dict = PyDict_New(); |
| 24 | PyObject *obj = NULL; |
| 25 | if (!dict) { |
| 26 | return -1; |
| 27 | } |
| 28 | |
| 29 | hPython3 = GetModuleHandleW(PY3_DLLNAME); |
| 30 | if (hPython3 && GetModuleFileNameW(hPython3, py3path, MAX_PATH)) { |
| 31 | obj = PyUnicode_FromWideChar(py3path, -1); |
| 32 | } else { |
| 33 | obj = Py_None; |
| 34 | Py_INCREF(obj); |
| 35 | } |
| 36 | if (obj && |
| 37 | !PyDict_SetItemString(dict, "python3_dll", obj) && |
| 38 | !PyDict_SetItemString(configs, "windows", dict)) { |
| 39 | Py_DECREF(obj); |
| 40 | Py_DECREF(dict); |
| 41 | return 0; |
| 42 | } |
| 43 | Py_DECREF(obj); |
| 44 | Py_DECREF(dict); |
| 45 | return -1; |
| 46 | } |
| 47 | #endif |
| 48 | |
| 49 | |
Victor Stinner | 23bace2 | 2019-04-18 11:37:26 +0200 | [diff] [blame] | 50 | static PyObject * |
| 51 | get_configs(PyObject *self, PyObject *Py_UNUSED(args)) |
| 52 | { |
Miss Islington (bot) | aa7f775 | 2020-07-06 10:12:16 -0700 | [diff] [blame] | 53 | PyObject *dict = _Py_GetConfigsAsDict(); |
| 54 | #ifdef MS_WINDOWS |
| 55 | if (dict) { |
| 56 | if (_add_windows_config(dict) < 0) { |
| 57 | Py_CLEAR(dict); |
| 58 | } |
| 59 | } |
| 60 | #endif |
| 61 | return dict; |
Victor Stinner | 23bace2 | 2019-04-18 11:37:26 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
| 65 | static PyMethodDef TestMethods[] = { |
| 66 | {"get_configs", get_configs, METH_NOARGS}, |
| 67 | {NULL, NULL} /* sentinel */ |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | static struct PyModuleDef _testcapimodule = { |
| 72 | PyModuleDef_HEAD_INIT, |
| 73 | "_testinternalcapi", |
| 74 | NULL, |
| 75 | -1, |
| 76 | TestMethods, |
| 77 | NULL, |
| 78 | NULL, |
| 79 | NULL, |
| 80 | NULL |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | PyMODINIT_FUNC |
| 85 | PyInit__testinternalcapi(void) |
| 86 | { |
| 87 | return PyModule_Create(&_testcapimodule); |
| 88 | } |