@@ -808,18 +808,46 @@ normalize_environment(PyObject* environment) {
808808
809809 result = PyDict_New ();
810810
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 ) {
813842 continue ;
814843 }
815- PyObject * key = PyList_GET_ITEM ( keys , i );
844+
816845 wchar_t * key_string = PyUnicode_AsWideCharString (key , NULL );
817846 wchar_t * prev_key_string = PyUnicode_AsWideCharString (PyList_GET_ITEM (keys , i - 1 ), NULL );
818847 if (CompareStringOrdinal (prev_key_string , -1 , key_string , -1 , TRUE) == CSTR_EQUAL ) {
819848 continue ;
820849 }
821- PyObject * value = PyDict_GetItem (environment , key );
822- PyDict_SetItem (result , key , value );
850+ PyObject_SetItem (result , key , value );
823851 }
824852
825853error :
@@ -848,7 +876,7 @@ getenvironment(PyObject* env)
848876
849877 environment = normalize_environment (env );
850878 if (environment == NULL ) {
851- goto error ;
879+ return NULL ;
852880 }
853881
854882 keys = PyMapping_Keys (environment );
@@ -873,26 +901,6 @@ getenvironment(PyObject* env)
873901 PyObject * value = PyList_GET_ITEM (values , i );
874902 Py_ssize_t size ;
875903
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-
896904 size = PyUnicode_AsWideChar (key , NULL , 0 );
897905 assert (size > 1 );
898906 if (totalsize > PY_SSIZE_T_MAX - size ) {
0 commit comments