@@ -33,9 +33,9 @@ config_get_int(const PyConfig *config, const PyConfigSpec *spec,
33
33
static int
34
34
config_get_str (const PyConfig * config , const PyConfigSpec * spec ,
35
35
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 );
39
39
40
40
/* --- PyConfig spec ---------------------------------------------- */
41
41
@@ -1061,47 +1061,6 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
1061
1061
}
1062
1062
1063
1063
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
-
1105
1064
PyObject *
1106
1065
_PyConfig_AsDict (const PyConfig * config )
1107
1066
{
@@ -1112,7 +1071,7 @@ _PyConfig_AsDict(const PyConfig *config)
1112
1071
1113
1072
const PyConfigSpec * spec = PYCONFIG_SPEC ;
1114
1073
for (; spec -> name != NULL ; spec ++ ) {
1115
- PyObject * obj = _PyConfig_Get (config , spec );
1074
+ PyObject * obj = config_get (config , spec , 0 );
1116
1075
if (obj == NULL ) {
1117
1076
Py_DECREF (dict );
1118
1077
return NULL ;
@@ -3672,7 +3631,6 @@ config_get_str(const PyConfig *config, const PyConfigSpec *spec,
3672
3631
}
3673
3632
}
3674
3633
3675
- wchar_t * * member = config_spec_get_member (spec , config );
3676
3634
if (spec -> type != PyConfig_MEMBER_WSTR
3677
3635
&& spec -> type != PyConfig_MEMBER_WSTR_OPT )
3678
3636
{
@@ -3681,6 +3639,7 @@ config_get_str(const PyConfig *config, const PyConfigSpec *spec,
3681
3639
return -1 ;
3682
3640
}
3683
3641
3642
+ wchar_t * * member = config_spec_get_member (spec , config );
3684
3643
if (* member != NULL ) {
3685
3644
* value = PyUnicode_FromWideChar (* member , -1 );
3686
3645
if (* value == NULL ) {
@@ -3745,13 +3704,25 @@ config_dict_as_str_list(PyObject *dict)
3745
3704
}
3746
3705
3747
3706
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 )
3751
3710
{
3752
3711
if (use_sys && !_PyRuntime .initialized ) {
3753
3712
use_sys = 0 ;
3754
3713
}
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
+ }
3755
3726
if (use_sys ) {
3756
3727
const char * sys_attrs [] = {
3757
3728
"argv" ,
@@ -3765,43 +3736,93 @@ config_get_str_list(const PyConfig *config, const PyConfigSpec *spec,
3765
3736
for (const char * * attr = sys_attrs ; * attr != NULL ; attr ++ ) {
3766
3737
if (strcmp (name , * attr ) == 0 ) {
3767
3738
if (strcmp (name , "module_search_paths" ) == 0 ) {
3768
- * value = Py_XNewRef (PySys_GetObject ("path" ));
3739
+ return Py_XNewRef (PySys_GetObject ("path" ));
3769
3740
}
3770
3741
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" ));
3772
3743
}
3773
3744
else {
3774
- * value = Py_XNewRef (PySys_GetObject (name ));
3745
+ return Py_XNewRef (PySys_GetObject (name ));
3775
3746
}
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 ));
3778
3774
}
3779
- return 0 ;
3780
3775
}
3781
3776
}
3782
3777
}
3783
3778
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 );
3788
3786
}
3789
3787
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 ;
3794
3815
}
3795
- return 0 ;
3796
3816
}
3797
3817
3798
- int
3799
- PyConfig_GetStrList (const char * name , PyObject * * value )
3818
+
3819
+ PyObject *
3820
+ PyConfig_Get (const char * name )
3800
3821
{
3801
3822
const PyConfigSpec * spec = config_prepare_get (name );
3802
3823
if (spec == NULL ) {
3803
- return -1 ;
3824
+ return NULL ;
3804
3825
}
3805
3826
const PyConfig * config = _Py_GetConfig ();
3806
- return config_get_str_list (config , spec , value , 1 );
3827
+ return config_get (config , spec , 1 );
3807
3828
}
0 commit comments