@@ -808,18 +808,46 @@ normalize_environment(PyObject* environment) {
808
808
809
809
result = PyDict_New ();
810
810
811
- for (int i = 0 ; i < PyList_GET_SIZE (keys ); i ++ ) {
812
- if (i < 1 ) {
811
+ for (int i = 0 ; i < PyList_GET_SIZE (keys ); i ++ ) {
812
+ PyObject * key = PyList_GET_ITEM (keys , i );
813
+ PyObject * value = PyObject_GetItem (environment , key );
814
+
815
+ if (! PyUnicode_Check (key ) || ! PyUnicode_Check (value )) {
816
+ PyErr_SetString (PyExc_TypeError ,
817
+ "environment can only contain strings" );
818
+ Py_DECREF (result );
819
+ result = NULL ;
820
+ goto error ;
821
+ }
822
+ if (PyUnicode_FindChar (key , '\0' , 0 , PyUnicode_GET_LENGTH (key ), 1 ) != -1 ||
823
+ PyUnicode_FindChar (value , '\0' , 0 , PyUnicode_GET_LENGTH (value ), 1 ) != -1 )
824
+ {
825
+ PyErr_SetString (PyExc_ValueError , "embedded null character" );
826
+ Py_DECREF (result );
827
+ result = NULL ;
828
+ goto error ;
829
+ }
830
+ /* Search from index 1 because on Windows starting '=' is allowed for
831
+ defining hidden environment variables. */
832
+ if (PyUnicode_GET_LENGTH (key ) == 0 ||
833
+ PyUnicode_FindChar (key , '=' , 1 , PyUnicode_GET_LENGTH (key ), 1 ) != -1 )
834
+ {
835
+ PyErr_SetString (PyExc_ValueError , "illegal environment variable name" );
836
+ Py_DECREF (result );
837
+ result = NULL ;
838
+ goto error ;
839
+ }
840
+
841
+ if (i == 0 ) {
813
842
continue ;
814
843
}
815
- PyObject * key = PyList_GET_ITEM ( keys , i );
844
+
816
845
wchar_t * key_string = PyUnicode_AsWideCharString (key , NULL );
817
846
wchar_t * prev_key_string = PyUnicode_AsWideCharString (PyList_GET_ITEM (keys , i - 1 ), NULL );
818
847
if (CompareStringOrdinal (prev_key_string , -1 , key_string , -1 , TRUE) == CSTR_EQUAL ) {
819
848
continue ;
820
849
}
821
- PyObject * value = PyDict_GetItem (environment , key );
822
- PyDict_SetItem (result , key , value );
850
+ PyObject_SetItem (result , key , value );
823
851
}
824
852
825
853
error :
@@ -848,7 +876,7 @@ getenvironment(PyObject* env)
848
876
849
877
environment = normalize_environment (env );
850
878
if (environment == NULL ) {
851
- goto error ;
879
+ return NULL ;
852
880
}
853
881
854
882
keys = PyMapping_Keys (environment );
@@ -873,26 +901,6 @@ getenvironment(PyObject* env)
873
901
PyObject * value = PyList_GET_ITEM (values , i );
874
902
Py_ssize_t size ;
875
903
876
- if (! PyUnicode_Check (key ) || ! PyUnicode_Check (value )) {
877
- PyErr_SetString (PyExc_TypeError ,
878
- "environment can only contain strings" );
879
- goto error ;
880
- }
881
- if (PyUnicode_FindChar (key , '\0' , 0 , PyUnicode_GET_LENGTH (key ), 1 ) != -1 ||
882
- PyUnicode_FindChar (value , '\0' , 0 , PyUnicode_GET_LENGTH (value ), 1 ) != -1 )
883
- {
884
- PyErr_SetString (PyExc_ValueError , "embedded null character" );
885
- goto error ;
886
- }
887
- /* Search from index 1 because on Windows starting '=' is allowed for
888
- defining hidden environment variables. */
889
- if (PyUnicode_GET_LENGTH (key ) == 0 ||
890
- PyUnicode_FindChar (key , '=' , 1 , PyUnicode_GET_LENGTH (key ), 1 ) != -1 )
891
- {
892
- PyErr_SetString (PyExc_ValueError , "illegal environment variable name" );
893
- goto error ;
894
- }
895
-
896
904
size = PyUnicode_AsWideChar (key , NULL , 0 );
897
905
assert (size > 1 );
898
906
if (totalsize > PY_SSIZE_T_MAX - size ) {
0 commit comments