@@ -33,9 +33,9 @@ config_get_int(const PyConfig *config, const PyConfigSpec *spec,
3333static int
3434config_get_str (const PyConfig * config , const PyConfigSpec * spec ,
3535 PyObject * * value , int use_sys );
36- static int
37- config_get_str_list (const PyConfig * config , const PyConfigSpec * spec ,
38- PyObject * * value , int use_sys );
36+ static PyObject *
37+ config_get (const PyConfig * config , const PyConfigSpec * spec ,
38+ int use_sys );
3939
4040/* --- PyConfig spec ---------------------------------------------- */
4141
@@ -1061,47 +1061,6 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
10611061}
10621062
10631063
1064- static PyObject *
1065- _PyConfig_Get (const PyConfig * config , const PyConfigSpec * spec )
1066- {
1067- switch (spec -> type ) {
1068- case PyConfig_MEMBER_INT :
1069- case PyConfig_MEMBER_UINT :
1070- case PyConfig_MEMBER_ULONG :
1071- {
1072- int64_t value ;
1073- if (config_get_int (config , spec , & value , 0 ) < 0 ) {
1074- return NULL ;
1075- }
1076-
1077- Py_BUILD_ASSERT (sizeof (value ) == sizeof (long long ));
1078- long long llvalue = (long long )value ;
1079- return PyLong_FromLongLong (llvalue );
1080- }
1081- case PyConfig_MEMBER_WSTR :
1082- case PyConfig_MEMBER_WSTR_OPT :
1083- {
1084- PyObject * obj ;
1085- if (config_get_str (config , spec , & obj , 0 ) < 0 ) {
1086- return NULL ;
1087- }
1088- return obj ;
1089- }
1090- case PyConfig_MEMBER_WSTR_LIST :
1091- {
1092- PyObject * obj ;
1093- if (config_get_str_list (config , spec , & obj , 0 ) < 0 ) {
1094- return NULL ;
1095- }
1096- return obj ;
1097- }
1098- default :
1099- break ;
1100- }
1101- Py_UNREACHABLE ();
1102- }
1103-
1104-
11051064PyObject *
11061065_PyConfig_AsDict (const PyConfig * config )
11071066{
@@ -1112,7 +1071,7 @@ _PyConfig_AsDict(const PyConfig *config)
11121071
11131072 const PyConfigSpec * spec = PYCONFIG_SPEC ;
11141073 for (; spec -> name != NULL ; spec ++ ) {
1115- PyObject * obj = _PyConfig_Get (config , spec );
1074+ PyObject * obj = config_get (config , spec , 0 );
11161075 if (obj == NULL ) {
11171076 Py_DECREF (dict );
11181077 return NULL ;
@@ -3672,7 +3631,6 @@ config_get_str(const PyConfig *config, const PyConfigSpec *spec,
36723631 }
36733632 }
36743633
3675- wchar_t * * member = config_spec_get_member (spec , config );
36763634 if (spec -> type != PyConfig_MEMBER_WSTR
36773635 && spec -> type != PyConfig_MEMBER_WSTR_OPT )
36783636 {
@@ -3681,6 +3639,7 @@ config_get_str(const PyConfig *config, const PyConfigSpec *spec,
36813639 return -1 ;
36823640 }
36833641
3642+ wchar_t * * member = config_spec_get_member (spec , config );
36843643 if (* member != NULL ) {
36853644 * value = PyUnicode_FromWideChar (* member , -1 );
36863645 if (* value == NULL ) {
@@ -3745,13 +3704,25 @@ config_dict_as_str_list(PyObject *dict)
37453704}
37463705
37473706
3748- static int
3749- config_get_str_list (const PyConfig * config , const PyConfigSpec * spec ,
3750- PyObject * * value , int use_sys )
3707+ static PyObject *
3708+ config_get (const PyConfig * config , const PyConfigSpec * spec ,
3709+ int use_sys )
37513710{
37523711 if (use_sys && !_PyRuntime .initialized ) {
37533712 use_sys = 0 ;
37543713 }
3714+ if (use_sys ) {
3715+ if (strcmp (spec -> name , "write_bytecode" ) == 0 ) {
3716+ PyObject * attr = PySys_GetObject ("dont_write_bytecode" );
3717+ if (attr != NULL ) {
3718+ int is_true = PyObject_IsTrue (attr );
3719+ if (is_true < 0 ) {
3720+ return NULL ;
3721+ }
3722+ return PyLong_FromLong (!is_true );
3723+ }
3724+ }
3725+ }
37553726 if (use_sys ) {
37563727 const char * sys_attrs [] = {
37573728 "argv" ,
@@ -3765,43 +3736,93 @@ config_get_str_list(const PyConfig *config, const PyConfigSpec *spec,
37653736 for (const char * * attr = sys_attrs ; * attr != NULL ; attr ++ ) {
37663737 if (strcmp (name , * attr ) == 0 ) {
37673738 if (strcmp (name , "module_search_paths" ) == 0 ) {
3768- * value = Py_XNewRef (PySys_GetObject ("path" ));
3739+ return Py_XNewRef (PySys_GetObject ("path" ));
37693740 }
37703741 else if (strcmp (name , "xoptions" ) == 0 ) {
3771- * value = config_dict_as_str_list (PySys_GetObject ("_xoptions" ));
3742+ return config_dict_as_str_list (PySys_GetObject ("_xoptions" ));
37723743 }
37733744 else {
3774- * value = Py_XNewRef (PySys_GetObject (name ));
3745+ return Py_XNewRef (PySys_GetObject (name ));
37753746 }
3776- if (* value == NULL ) {
3777- return -1 ;
3747+ }
3748+ }
3749+ }
3750+ if (use_sys ) {
3751+ const char * sys_attrs [] = {
3752+ "base_exec_prefix" ,
3753+ "base_executable" ,
3754+ "base_prefix" ,
3755+ "exec_prefix" ,
3756+ "executable" ,
3757+ "platlibdir" ,
3758+ "prefix" ,
3759+ "pycache_prefix" ,
3760+ "stdlib_dir" ,
3761+ NULL ,
3762+ };
3763+ const char * name = spec -> name ;
3764+ for (const char * * attr = sys_attrs ; * attr != NULL ; attr ++ ) {
3765+ if (strcmp (name , * attr ) == 0 ) {
3766+ if (strcmp (name , "stdlib_dir" ) == 0 ) {
3767+ return Py_XNewRef (PySys_GetObject ("_stdlib_dir" ));
3768+ }
3769+ else if (strcmp (name , "base_executable" ) == 0 ) {
3770+ return Py_XNewRef (PySys_GetObject ("_base_executable" ));
3771+ }
3772+ else {
3773+ return Py_XNewRef (PySys_GetObject (name ));
37783774 }
3779- return 0 ;
37803775 }
37813776 }
37823777 }
37833778
3784- if (spec -> type != PyConfig_MEMBER_WSTR_LIST ) {
3785- PyErr_Format (PyExc_TypeError ,
3786- "config option %s is not a strings list" , spec -> name );
3787- return -1 ;
3779+ char * member = config_spec_get_member (spec , config );
3780+ switch (spec -> type ) {
3781+ case PyConfig_MEMBER_INT :
3782+ case PyConfig_MEMBER_UINT :
3783+ {
3784+ int value = * (int * )member ;
3785+ return PyLong_FromLong (value );
37883786 }
37893787
3790- const PyWideStringList * list = config_spec_get_member (spec , config );
3791- * value = _PyWideStringList_AsList (list );
3792- if (* value == NULL ) {
3793- return -1 ;
3788+ case PyConfig_MEMBER_ULONG :
3789+ {
3790+ unsigned long value = * (unsigned long * )member ;
3791+ return PyLong_FromUnsignedLong (value );
3792+ }
3793+
3794+ case PyConfig_MEMBER_WSTR :
3795+ case PyConfig_MEMBER_WSTR_OPT :
3796+ {
3797+ wchar_t * wstr = * (wchar_t * * )member ;
3798+ if (wstr != NULL ) {
3799+ return PyUnicode_FromWideChar (wstr , -1 );
3800+ }
3801+ else {
3802+ return Py_NewRef (Py_None );
3803+ }
3804+ }
3805+
3806+ case PyConfig_MEMBER_WSTR_LIST :
3807+ {
3808+ const PyWideStringList * list = (const PyWideStringList * )member ;
3809+ return _PyWideStringList_AsList (list );
3810+ }
3811+ default :
3812+ PyErr_Format (PyExc_TypeError ,
3813+ "config option %s is not a strings list" , spec -> name );
3814+ return NULL ;
37943815 }
3795- return 0 ;
37963816}
37973817
3798- int
3799- PyConfig_GetStrList (const char * name , PyObject * * value )
3818+
3819+ PyObject *
3820+ PyConfig_Get (const char * name )
38003821{
38013822 const PyConfigSpec * spec = config_prepare_get (name );
38023823 if (spec == NULL ) {
3803- return -1 ;
3824+ return NULL ;
38043825 }
38053826 const PyConfig * config = _Py_GetConfig ();
3806- return config_get_str_list (config , spec , value , 1 );
3827+ return config_get (config , spec , 1 );
38073828}
0 commit comments