File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -244,12 +244,12 @@ process and user.
244244.. function :: getlogin()
245245
246246 Return the name of the user logged in on the controlling terminal of the
247- process. For most purposes, it is more useful to use the environment variable
248- :envvar: `LOGNAME ` to find out who the user is, or
247+ process. For most purposes, it is more useful to use the environment variables
248+ :envvar: `LOGNAME ` or :envvar: ` USERNAME ` to find out who the user is, or
249249 ``pwd.getpwuid(os.getuid())[0] `` to get the login name of the currently
250250 effective user id.
251251
252- Availability: Unix.
252+ Availability: Unix, Windows .
253253
254254
255255.. function :: getpgid(pid)
Original file line number Diff line number Diff line change @@ -1202,6 +1202,13 @@ def test_getppid(self):
12021202 self .assertEqual (int (stdout ), os .getpid ())
12031203
12041204
1205+ @unittest .skipUnless (hasattr (os , 'getlogin' ), "test needs os.getlogin" )
1206+ class LoginTests (unittest .TestCase ):
1207+ def test_getlogin (self ):
1208+ user_name = os .getlogin ()
1209+ self .assertNotEqual (len (user_name ), 0 )
1210+
1211+
12051212def test_main ():
12061213 support .run_unittest (
12071214 FileTests ,
@@ -1220,6 +1227,7 @@ def test_main():
12201227 Win32SymlinkTests ,
12211228 FSEncodingTests ,
12221229 PidTests ,
1230+ LoginTests ,
12231231 )
12241232
12251233if __name__ == "__main__" :
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ What's New in Python 3.2 Alpha 3?
1010Core and Builtins
1111-----------------
1212
13+ - Issue #9808: Implement os.getlogin for Windows. Patch by Jon Anglin.
14+
1315- Issue #9901: Destroying the GIL in Py_Finalize() can fail if some other
1416 threads are still running. Instead, reinitialize the GIL on a second call to
1517 Py_Initialize().
Original file line number Diff line number Diff line change @@ -122,6 +122,7 @@ corresponding Unix manual entries for more information on calls.");
122122#ifdef _MSC_VER /* Microsoft compiler */
123123#define HAVE_GETCWD 1
124124#define HAVE_GETPPID 1
125+ #define HAVE_GETLOGIN 1
125126#define HAVE_SPAWNV 1
126127#define HAVE_EXECV 1
127128#define HAVE_PIPE 1
@@ -276,6 +277,7 @@ extern int lstat(const char *, struct stat *);
276277#include <malloc.h>
277278#include <windows.h>
278279#include <shellapi.h> /* for ShellExecute() */
280+ #include <lmcons.h> /* for UNLEN */
279281#endif /* _MSC_VER */
280282
281283#if defined(PYCC_VACPP ) && defined(PYOS_OS2 )
@@ -4380,6 +4382,17 @@ static PyObject *
43804382posix_getlogin (PyObject * self , PyObject * noargs )
43814383{
43824384 PyObject * result = NULL ;
4385+ #ifdef MS_WINDOWS
4386+ wchar_t user_name [UNLEN + 1 ];
4387+ DWORD num_chars = sizeof (user_name )/sizeof (user_name [0 ]);
4388+
4389+ if (GetUserNameW (user_name , & num_chars )) {
4390+ /* num_chars is the number of unicode chars plus null terminator */
4391+ result = PyUnicode_FromWideChar (user_name , num_chars - 1 );
4392+ }
4393+ else
4394+ result = PyErr_SetFromWindowsErr (GetLastError ());
4395+ #else
43834396 char * name ;
43844397 int old_errno = errno ;
43854398
@@ -4394,10 +4407,10 @@ posix_getlogin(PyObject *self, PyObject *noargs)
43944407 else
43954408 result = PyUnicode_DecodeFSDefault (name );
43964409 errno = old_errno ;
4397-
4410+ #endif
43984411 return result ;
43994412}
4400- #endif
4413+ #endif /* HAVE_GETLOGIN */
44014414
44024415#ifdef HAVE_GETUID
44034416PyDoc_STRVAR (posix_getuid__doc__ ,
You can’t perform that action at this time.
0 commit comments