@@ -187,27 +187,31 @@ pwd_getpwuid(PyObject *module, PyObject *uidobj)
187187 p = getpwuid (uid );
188188#endif
189189 if (p == NULL ) {
190- #ifndef HAVE_GETPWUID_R
191- PyMutex_Unlock (& pwd_db_mutex );
192- #endif
193- PyMem_RawFree (buf );
194190 if (nomem == 1 ) {
195- return PyErr_NoMemory ();
191+ retval = PyErr_NoMemory ();
192+ }
193+ else if (errno == 0 ) {
194+ PyObject * uid_obj = _PyLong_FromUid (uid );
195+ if (uid_obj == NULL ) {
196+ retval = NULL ;
197+ }
198+ else {
199+ retval = PyErr_Format (PyExc_KeyError ,
200+ "getpwuid(): uid not found: %S" , uid_obj );
201+ Py_DECREF (uid_obj );
202+ }
203+ }
204+ else {
205+ retval = PyErr_SetFromErrno (PyExc_OSError );
196206 }
197- PyObject * uid_obj = _PyLong_FromUid (uid );
198- if (uid_obj == NULL )
199- return NULL ;
200- PyErr_Format (PyExc_KeyError ,
201- "getpwuid(): uid not found: %S" , uid_obj );
202- Py_DECREF (uid_obj );
203- return NULL ;
204207 }
205- retval = mkpwent ( module , p );
206- #ifdef HAVE_GETPWUID_R
207- PyMem_RawFree ( buf );
208- #else
208+ else {
209+ retval = mkpwent ( module , p );
210+ }
211+ #ifndef HAVE_GETPWUID_R
209212 PyMutex_Unlock (& pwd_db_mutex );
210213#endif
214+ PyMem_RawFree (buf );
211215 return retval ;
212216}
213217
@@ -278,19 +282,20 @@ pwd_getpwnam_impl(PyObject *module, PyObject *name)
278282 p = getpwnam (name_chars );
279283#endif
280284 if (p == NULL ) {
281- #ifndef HAVE_GETPWNAM_R
282- PyMutex_Unlock (& pwd_db_mutex );
283- #endif
284285 if (nomem == 1 ) {
285- PyErr_NoMemory ();
286+ retval = PyErr_NoMemory ();
287+ }
288+ else if (errno == 0 ) {
289+ retval = PyErr_Format (PyExc_KeyError ,
290+ "getpwnam(): name not found: %R" , name );
286291 }
287292 else {
288- PyErr_Format (PyExc_KeyError ,
289- "getpwnam(): name not found: %R" , name );
293+ retval = PyErr_SetFromErrno (PyExc_OSError );
290294 }
291- goto out ;
292295 }
293- retval = mkpwent (module , p );
296+ else {
297+ retval = mkpwent (module , p );
298+ }
294299#ifndef HAVE_GETPWNAM_R
295300 PyMutex_Unlock (& pwd_db_mutex );
296301#endif
0 commit comments