@@ -1900,16 +1900,7 @@ static struct PyModuleDef sysmodule = {
1900
1900
NULL
1901
1901
};
1902
1902
1903
- PyObject *
1904
- _PySys_Init (void )
1905
- {
1906
- PyObject * m , * sysdict , * version_info ;
1907
- int res ;
1908
-
1909
- m = PyModule_Create (& sysmodule );
1910
- if (m == NULL )
1911
- return NULL ;
1912
- sysdict = PyModule_GetDict (m );
1903
+ /* Updating the sys namespace, returning NULL pointer on error */
1913
1904
#define SET_SYS_FROM_STRING_BORROW (key , value ) \
1914
1905
do { \
1915
1906
PyObject *v = (value); \
@@ -1932,6 +1923,17 @@ _PySys_Init(void)
1932
1923
} \
1933
1924
} while (0)
1934
1925
1926
+ PyObject *
1927
+ _PySys_BeginInit (void )
1928
+ {
1929
+ PyObject * m , * sysdict , * version_info ;
1930
+ int res ;
1931
+
1932
+ m = PyModule_Create (& sysmodule );
1933
+ if (m == NULL )
1934
+ return NULL ;
1935
+ sysdict = PyModule_GetDict (m );
1936
+
1935
1937
/* Check that stdin is not a directory
1936
1938
Using shell redirection, you can redirect stdin to a directory,
1937
1939
crashing the Python interpreter. Catch this common mistake here
@@ -1963,25 +1965,12 @@ _PySys_Init(void)
1963
1965
SET_SYS_FROM_STRING ("_git" ,
1964
1966
Py_BuildValue ("(szz)" , "CPython" , _Py_gitidentifier (),
1965
1967
_Py_gitversion ()));
1966
- SET_SYS_FROM_STRING ("dont_write_bytecode" ,
1967
- PyBool_FromLong (Py_DontWriteBytecodeFlag ));
1968
1968
SET_SYS_FROM_STRING ("api_version" ,
1969
1969
PyLong_FromLong (PYTHON_API_VERSION ));
1970
1970
SET_SYS_FROM_STRING ("copyright" ,
1971
1971
PyUnicode_FromString (Py_GetCopyright ()));
1972
1972
SET_SYS_FROM_STRING ("platform" ,
1973
1973
PyUnicode_FromString (Py_GetPlatform ()));
1974
- SET_SYS_FROM_STRING ("executable" ,
1975
- PyUnicode_FromWideChar (
1976
- Py_GetProgramFullPath (), -1 ));
1977
- SET_SYS_FROM_STRING ("prefix" ,
1978
- PyUnicode_FromWideChar (Py_GetPrefix (), -1 ));
1979
- SET_SYS_FROM_STRING ("exec_prefix" ,
1980
- PyUnicode_FromWideChar (Py_GetExecPrefix (), -1 ));
1981
- SET_SYS_FROM_STRING ("base_prefix" ,
1982
- PyUnicode_FromWideChar (Py_GetPrefix (), -1 ));
1983
- SET_SYS_FROM_STRING ("base_exec_prefix" ,
1984
- PyUnicode_FromWideChar (Py_GetExecPrefix (), -1 ));
1985
1974
SET_SYS_FROM_STRING ("maxsize" ,
1986
1975
PyLong_FromSsize_t (PY_SSIZE_T_MAX ));
1987
1976
SET_SYS_FROM_STRING ("float_info" ,
@@ -2017,17 +2006,6 @@ _PySys_Init(void)
2017
2006
SET_SYS_FROM_STRING ("abiflags" ,
2018
2007
PyUnicode_FromString (ABIFLAGS ));
2019
2008
#endif
2020
- if (warnoptions == NULL ) {
2021
- warnoptions = PyList_New (0 );
2022
- if (warnoptions == NULL )
2023
- return NULL ;
2024
- }
2025
- else {
2026
- Py_INCREF (warnoptions );
2027
- }
2028
- SET_SYS_FROM_STRING_BORROW ("warnoptions" , warnoptions );
2029
-
2030
- SET_SYS_FROM_STRING_BORROW ("_xoptions" , get_xoptions ());
2031
2009
2032
2010
/* version_info */
2033
2011
if (VersionInfoType .tp_name == NULL ) {
@@ -2052,13 +2030,8 @@ _PySys_Init(void)
2052
2030
if (PyStructSequence_InitType2 (& FlagsType , & flags_desc ) < 0 )
2053
2031
return NULL ;
2054
2032
}
2033
+ /* Set flags to their default values */
2055
2034
SET_SYS_FROM_STRING ("flags" , make_flags ());
2056
- /* prevent user from creating new instances */
2057
- FlagsType .tp_init = NULL ;
2058
- FlagsType .tp_new = NULL ;
2059
- res = PyDict_DelItemString (FlagsType .tp_dict , "__new__" );
2060
- if (res < 0 && PyErr_ExceptionMatches (PyExc_KeyError ))
2061
- PyErr_Clear ();
2062
2035
2063
2036
#if defined(MS_WINDOWS )
2064
2037
/* getwindowsversion */
@@ -2095,13 +2068,89 @@ _PySys_Init(void)
2095
2068
}
2096
2069
}
2097
2070
2098
- #undef SET_SYS_FROM_STRING
2099
- #undef SET_SYS_FROM_STRING_BORROW
2100
2071
if (PyErr_Occurred ())
2101
2072
return NULL ;
2102
2073
return m ;
2103
2074
}
2104
2075
2076
+ #undef SET_SYS_FROM_STRING
2077
+ #undef SET_SYS_FROM_STRING_BORROW
2078
+
2079
+ /* Updating the sys namespace, returning integer error codes */
2080
+ #define SET_SYS_FROM_STRING_BORROW_INT_RESULT (key , value ) \
2081
+ do { \
2082
+ PyObject *v = (value); \
2083
+ if (v == NULL) \
2084
+ return -1; \
2085
+ res = PyDict_SetItemString(sysdict, key, v); \
2086
+ if (res < 0) { \
2087
+ return res; \
2088
+ } \
2089
+ } while (0)
2090
+ #define SET_SYS_FROM_STRING_INT_RESULT (key , value ) \
2091
+ do { \
2092
+ PyObject *v = (value); \
2093
+ if (v == NULL) \
2094
+ return -1; \
2095
+ res = PyDict_SetItemString(sysdict, key, v); \
2096
+ Py_DECREF(v); \
2097
+ if (res < 0) { \
2098
+ return res; \
2099
+ } \
2100
+ } while (0)
2101
+
2102
+ int
2103
+ _PySys_EndInit (PyObject * sysdict )
2104
+ {
2105
+ int res ;
2106
+
2107
+ /* Set flags to their final values */
2108
+ SET_SYS_FROM_STRING_INT_RESULT ("flags" , make_flags ());
2109
+ /* prevent user from creating new instances */
2110
+ FlagsType .tp_init = NULL ;
2111
+ FlagsType .tp_new = NULL ;
2112
+ res = PyDict_DelItemString (FlagsType .tp_dict , "__new__" );
2113
+ if (res < 0 ) {
2114
+ if (!PyErr_ExceptionMatches (PyExc_KeyError )) {
2115
+ return res ;
2116
+ }
2117
+ PyErr_Clear ();
2118
+ }
2119
+
2120
+ SET_SYS_FROM_STRING_INT_RESULT ("dont_write_bytecode" ,
2121
+ PyBool_FromLong (Py_DontWriteBytecodeFlag ));
2122
+ SET_SYS_FROM_STRING_INT_RESULT ("executable" ,
2123
+ PyUnicode_FromWideChar (
2124
+ Py_GetProgramFullPath (), -1 ));
2125
+ SET_SYS_FROM_STRING_INT_RESULT ("prefix" ,
2126
+ PyUnicode_FromWideChar (Py_GetPrefix (), -1 ));
2127
+ SET_SYS_FROM_STRING_INT_RESULT ("exec_prefix" ,
2128
+ PyUnicode_FromWideChar (Py_GetExecPrefix (), -1 ));
2129
+ SET_SYS_FROM_STRING_INT_RESULT ("base_prefix" ,
2130
+ PyUnicode_FromWideChar (Py_GetPrefix (), -1 ));
2131
+ SET_SYS_FROM_STRING_INT_RESULT ("base_exec_prefix" ,
2132
+ PyUnicode_FromWideChar (Py_GetExecPrefix (), -1 ));
2133
+
2134
+ if (warnoptions == NULL ) {
2135
+ warnoptions = PyList_New (0 );
2136
+ if (warnoptions == NULL )
2137
+ return -1 ;
2138
+ }
2139
+ else {
2140
+ Py_INCREF (warnoptions );
2141
+ }
2142
+ SET_SYS_FROM_STRING_BORROW_INT_RESULT ("warnoptions" , warnoptions );
2143
+
2144
+ SET_SYS_FROM_STRING_BORROW_INT_RESULT ("_xoptions" , get_xoptions ());
2145
+
2146
+ if (PyErr_Occurred ())
2147
+ return -1 ;
2148
+ return 0 ;
2149
+ }
2150
+
2151
+ #undef SET_SYS_FROM_STRING_INT_RESULT
2152
+ #undef SET_SYS_FROM_STRING_BORROW_INT_RESULT
2153
+
2105
2154
static PyObject *
2106
2155
makepathobject (const wchar_t * path , wchar_t delim )
2107
2156
{
0 commit comments