Skip to content

Commit acdcb6c

Browse files
committed
Get sys attributes
1 parent dd6c047 commit acdcb6c

File tree

1 file changed

+92
-38
lines changed

1 file changed

+92
-38
lines changed

Python/initconfig.c

Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,46 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
10211021
}
10221022

10231023

1024+
static PyObject*
1025+
_PyConfig_Get(const PyConfig *config, const PyConfigSpec *spec)
1026+
{
1027+
const char *member = config_spec_get_member(spec, (PyConfig*)config);
1028+
switch (spec->type) {
1029+
case PyConfig_MEMBER_INT:
1030+
case PyConfig_MEMBER_UINT:
1031+
{
1032+
int value = *(const int*)member;
1033+
return PyLong_FromLong(value);
1034+
}
1035+
case PyConfig_MEMBER_ULONG:
1036+
{
1037+
unsigned long value = *(const unsigned long*)member;
1038+
return PyLong_FromUnsignedLong(value);
1039+
}
1040+
case PyConfig_MEMBER_WSTR:
1041+
case PyConfig_MEMBER_WSTR_OPT:
1042+
{
1043+
PyObject *obj;
1044+
if (PyConfig_GetStr(spec->name, &obj) < 0) {
1045+
return NULL;
1046+
}
1047+
return obj;
1048+
}
1049+
case PyConfig_MEMBER_WSTR_LIST:
1050+
{
1051+
PyObject *obj;
1052+
if (PyConfig_GetStrList(spec->name, &obj) < 0) {
1053+
return NULL;
1054+
}
1055+
return obj;
1056+
}
1057+
default:
1058+
break;
1059+
}
1060+
Py_UNREACHABLE();
1061+
}
1062+
1063+
10241064
PyObject *
10251065
_PyConfig_AsDict(const PyConfig *config)
10261066
{
@@ -1031,44 +1071,7 @@ _PyConfig_AsDict(const PyConfig *config)
10311071

10321072
const PyConfigSpec *spec = PYCONFIG_SPEC;
10331073
for (; spec->name != NULL; spec++) {
1034-
const char *member = config_spec_get_member(spec, (PyConfig*)config);
1035-
PyObject *obj;
1036-
switch (spec->type) {
1037-
case PyConfig_MEMBER_INT:
1038-
case PyConfig_MEMBER_UINT:
1039-
{
1040-
int value = *(const int*)member;
1041-
obj = PyLong_FromLong(value);
1042-
break;
1043-
}
1044-
case PyConfig_MEMBER_ULONG:
1045-
{
1046-
unsigned long value = *(const unsigned long*)member;
1047-
obj = PyLong_FromUnsignedLong(value);
1048-
break;
1049-
}
1050-
case PyConfig_MEMBER_WSTR:
1051-
case PyConfig_MEMBER_WSTR_OPT:
1052-
{
1053-
const wchar_t *wstr = *(const wchar_t**)member;
1054-
if (wstr != NULL) {
1055-
obj = PyUnicode_FromWideChar(wstr, -1);
1056-
}
1057-
else {
1058-
obj = Py_NewRef(Py_None);
1059-
}
1060-
break;
1061-
}
1062-
case PyConfig_MEMBER_WSTR_LIST:
1063-
{
1064-
const PyWideStringList *list = (const PyWideStringList*)member;
1065-
obj = _PyWideStringList_AsList(list);
1066-
break;
1067-
}
1068-
default:
1069-
Py_UNREACHABLE();
1070-
}
1071-
1074+
PyObject *obj = _PyConfig_Get(config, spec);
10721075
if (obj == NULL) {
10731076
Py_DECREF(dict);
10741077
return NULL;
@@ -3495,6 +3498,30 @@ PyConfig_GetStr(const char *key, PyObject **value)
34953498
return -1;
34963499
}
34973500

3501+
if (_PyRuntime.initialized) {
3502+
const char* sys_attrs[] = {
3503+
"executable",
3504+
"_base_executable",
3505+
"prefix",
3506+
"base_prefix",
3507+
"exec_prefix",
3508+
"base_exec_prefix",
3509+
"platlibdir",
3510+
"pycache_prefix",
3511+
"_stdlib_dir",
3512+
NULL,
3513+
};
3514+
for (const char **attr = sys_attrs; *attr != NULL; attr++) {
3515+
if (strcmp(key, *attr) == 0) {
3516+
*value = Py_XNewRef(PySys_GetObject(key));
3517+
if (*value == NULL) {
3518+
return -1;
3519+
}
3520+
return 0;
3521+
}
3522+
}
3523+
}
3524+
34983525
if (spec->type == PyConfig_MEMBER_WSTR
34993526
|| spec->type == PyConfig_MEMBER_WSTR_OPT) {
35003527
if (*member != NULL) {
@@ -3524,6 +3551,33 @@ PyConfig_GetStrList(const char *key, PyObject **value)
35243551
return -1;
35253552
}
35263553

3554+
if (_PyRuntime.initialized) {
3555+
const char* sys_attrs[] = {
3556+
"module_search_paths",
3557+
"argv",
3558+
"orig_argv",
3559+
"xoptions",
3560+
NULL,
3561+
};
3562+
for (const char **attr = sys_attrs; *attr != NULL; attr++) {
3563+
if (strcmp(key, *attr) == 0) {
3564+
if (strcmp(key, "module_search_paths") == 0) {
3565+
*value = Py_XNewRef(PySys_GetObject("path"));
3566+
}
3567+
else if (strcmp(key, "xoptions") == 0) {
3568+
*value = Py_XNewRef(PySys_GetObject("_xoptions"));
3569+
}
3570+
else {
3571+
*value = Py_XNewRef(PySys_GetObject(key));
3572+
}
3573+
if (*value == NULL) {
3574+
return -1;
3575+
}
3576+
return 0;
3577+
}
3578+
}
3579+
}
3580+
35273581
if (spec->type == PyConfig_MEMBER_WSTR_LIST) {
35283582
*value = _PyWideStringList_AsList(list);
35293583
if (*value == NULL) {

0 commit comments

Comments
 (0)