Commit 418e3897 authored by Brian Curtin's avatar Brian Curtin

#9808. Implement os.getlogin for Windows, completed by Jon Anglin.

The test is semi-dumb, it just makes sure something comes back since we
don't have a solid source to validate the returned login. We can't be 100%
sure that the USERNAME env var will always match what os.getlogin() returns,
so we don't make any specific assertion there.
parent cd4df6b1
...@@ -244,12 +244,12 @@ process and user. ...@@ -244,12 +244,12 @@ process and user.
.. function:: getlogin() .. function:: getlogin()
Return the name of the user logged in on the controlling terminal of the Return the name of the user logged in on the controlling terminal of the
process. For most purposes, it is more useful to use the environment variable process. For most purposes, it is more useful to use the environment variables
:envvar:`LOGNAME` to find out who the user is, or :envvar:`LOGNAME` or :envvar:`USERNAME` to find out who the user is, or
``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently
effective user id. effective user id.
Availability: Unix. Availability: Unix, Windows.
.. function:: getpgid(pid) .. function:: getpgid(pid)
......
...@@ -1202,6 +1202,13 @@ class PidTests(unittest.TestCase): ...@@ -1202,6 +1202,13 @@ class PidTests(unittest.TestCase):
self.assertEqual(int(stdout), os.getpid()) self.assertEqual(int(stdout), os.getpid())
@unittest.skipUnless(hasattr(os, 'getlogin'), "test needs os.getlogin")
class LoginTests(unittest.TestCase):
def test_getlogin(self):
user_name = os.getlogin()
self.assertNotEqual(len(user_name), 0)
def test_main(): def test_main():
support.run_unittest( support.run_unittest(
FileTests, FileTests,
...@@ -1220,6 +1227,7 @@ def test_main(): ...@@ -1220,6 +1227,7 @@ def test_main():
Win32SymlinkTests, Win32SymlinkTests,
FSEncodingTests, FSEncodingTests,
PidTests, PidTests,
LoginTests,
) )
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -10,6 +10,8 @@ What's New in Python 3.2 Alpha 3? ...@@ -10,6 +10,8 @@ What's New in Python 3.2 Alpha 3?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #9808: Implement os.getlogin for Windows. Patch by Jon Anglin.
- Issue #9901: Destroying the GIL in Py_Finalize() can fail if some other - Issue #9901: Destroying the GIL in Py_Finalize() can fail if some other
threads are still running. Instead, reinitialize the GIL on a second call to threads are still running. Instead, reinitialize the GIL on a second call to
Py_Initialize(). Py_Initialize().
......
...@@ -122,6 +122,7 @@ corresponding Unix manual entries for more information on calls."); ...@@ -122,6 +122,7 @@ corresponding Unix manual entries for more information on calls.");
#ifdef _MSC_VER /* Microsoft compiler */ #ifdef _MSC_VER /* Microsoft compiler */
#define HAVE_GETCWD 1 #define HAVE_GETCWD 1
#define HAVE_GETPPID 1 #define HAVE_GETPPID 1
#define HAVE_GETLOGIN 1
#define HAVE_SPAWNV 1 #define HAVE_SPAWNV 1
#define HAVE_EXECV 1 #define HAVE_EXECV 1
#define HAVE_PIPE 1 #define HAVE_PIPE 1
...@@ -276,6 +277,7 @@ extern int lstat(const char *, struct stat *); ...@@ -276,6 +277,7 @@ extern int lstat(const char *, struct stat *);
#include <malloc.h> #include <malloc.h>
#include <windows.h> #include <windows.h>
#include <shellapi.h> /* for ShellExecute() */ #include <shellapi.h> /* for ShellExecute() */
#include <lmcons.h> /* for UNLEN */
#endif /* _MSC_VER */ #endif /* _MSC_VER */
#if defined(PYCC_VACPP) && defined(PYOS_OS2) #if defined(PYCC_VACPP) && defined(PYOS_OS2)
...@@ -4380,6 +4382,17 @@ static PyObject * ...@@ -4380,6 +4382,17 @@ static PyObject *
posix_getlogin(PyObject *self, PyObject *noargs) posix_getlogin(PyObject *self, PyObject *noargs)
{ {
PyObject *result = NULL; PyObject *result = NULL;
#ifdef MS_WINDOWS
wchar_t user_name[UNLEN + 1];
DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
if (GetUserNameW(user_name, &num_chars)) {
/* num_chars is the number of unicode chars plus null terminator */
result = PyUnicode_FromWideChar(user_name, num_chars - 1);
}
else
result = PyErr_SetFromWindowsErr(GetLastError());
#else
char *name; char *name;
int old_errno = errno; int old_errno = errno;
...@@ -4394,10 +4407,10 @@ posix_getlogin(PyObject *self, PyObject *noargs) ...@@ -4394,10 +4407,10 @@ posix_getlogin(PyObject *self, PyObject *noargs)
else else
result = PyUnicode_DecodeFSDefault(name); result = PyUnicode_DecodeFSDefault(name);
errno = old_errno; errno = old_errno;
#endif
return result; return result;
} }
#endif #endif /* HAVE_GETLOGIN */
#ifdef HAVE_GETUID #ifdef HAVE_GETUID
PyDoc_STRVAR(posix_getuid__doc__, PyDoc_STRVAR(posix_getuid__doc__,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment