blob: 3a931cae1fc2eec6b3e58f5e197750511ad6fb65 [file] [log] [blame]
Victor Stinner23bace22019-04-18 11:37:26 +02001/*
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 Stinner331a6a52019-05-27 16:39:22 +020012#include "pycore_initconfig.h"
Victor Stinner23bace22019-04-18 11:37:26 +020013
14
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -070015#ifdef MS_WINDOWS
16#include <windows.h>
17
18static 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 Stinner23bace22019-04-18 11:37:26 +020050static PyObject *
51get_configs(PyObject *self, PyObject *Py_UNUSED(args))
52{
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -070053 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 Stinner23bace22019-04-18 11:37:26 +020062}
63
64
65static PyMethodDef TestMethods[] = {
66 {"get_configs", get_configs, METH_NOARGS},
67 {NULL, NULL} /* sentinel */
68};
69
70
71static 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
84PyMODINIT_FUNC
85PyInit__testinternalcapi(void)
86{
87 return PyModule_Create(&_testcapimodule);
88}