Author: Armin Rigo <ar...@tunes.org>
Branch: errno-again
Changeset: r75394:944e6eb53a35
Date: 2015-01-16 18:01 +0100
http://bitbucket.org/pypy/pypy/changeset/944e6eb53a35/

Log:    in-progress: windows

diff --git a/pypy/module/_multiprocessing/interp_connection.py 
b/pypy/module/_multiprocessing/interp_connection.py
--- a/pypy/module/_multiprocessing/interp_connection.py
+++ b/pypy/module/_multiprocessing/interp_connection.py
@@ -441,7 +441,7 @@
                                   lltype.nullptr(rwin32.LPDWORD.TO),
                                   lltype.nullptr(rwin32.LPDWORD.TO),
                                   left_ptr):
-                raise wrap_windowserror(space, rwin32.lastWindowsError())
+                raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
 
             length = intmask(read_ptr[0] + left_ptr[0])
             if length > maxlength: # bad message, close connection
@@ -460,7 +460,7 @@
                                read_ptr, rffi.NULL)
             if not result:
                 rffi.free_charp(newbuf)
-                raise wrap_windowserror(space, rwin32.lastWindowsError())
+                raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
 
             assert read_ptr[0] == left_ptr[0]
             return length, newbuf
@@ -480,7 +480,7 @@
                                   lltype.nullptr(rwin32.LPDWORD.TO),
                                   bytes_ptr,
                                   lltype.nullptr(rwin32.LPDWORD.TO)):
-                raise wrap_windowserror(space, rwin32.lastWindowsError())
+                raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
             bytes = bytes_ptr[0]
         finally:
             lltype.free(bytes_ptr, flavor='raw')
@@ -506,7 +506,8 @@
                                       lltype.nullptr(rwin32.LPDWORD.TO),
                                       bytes_ptr,
                                       lltype.nullptr(rwin32.LPDWORD.TO)):
-                    raise wrap_windowserror(space, rwin32.lastWindowsError())
+                    raise wrap_windowserror(space,
+                                            rwin32.lastSavedWindowsError())
                 bytes = bytes_ptr[0]
             finally:
                 lltype.free(bytes_ptr, flavor='raw')
diff --git a/pypy/module/_multiprocessing/interp_semaphore.py 
b/pypy/module/_multiprocessing/interp_semaphore.py
--- a/pypy/module/_multiprocessing/interp_semaphore.py
+++ b/pypy/module/_multiprocessing/interp_semaphore.py
@@ -31,7 +31,8 @@
         rwin32.BOOL, releasegil=False)
     _ReleaseSemaphore = rwin32.winexternal(
         'ReleaseSemaphore', [rwin32.HANDLE, rffi.LONG, rffi.LONGP],
-        rwin32.BOOL)
+        rwin32.BOOL,
+        save_err=rffi.RFFI_SAVE_LASTERROR)
 
 else:
     from rpython.rlib import rposix
@@ -296,7 +297,7 @@
     def semlock_release(self, space):
         if not _ReleaseSemaphore(self.handle, 1,
                                  lltype.nullptr(rffi.LONGP.TO)):
-            err = rwin32.GetLastError()
+            err = rwin32.GetLastError_saved()
             if err == 0x0000012a: # ERROR_TOO_MANY_POSTS
                 raise OperationError(
                     space.w_ValueError,
@@ -310,7 +311,7 @@
         previous_ptr = lltype.malloc(rffi.LONGP.TO, 1, flavor='raw')
         try:
             if not _ReleaseSemaphore(self.handle, 1, previous_ptr):
-                raise rwin32.lastWindowsError("ReleaseSemaphore")
+                raise rwin32.lastSavedWindowsError("ReleaseSemaphore")
             return previous_ptr[0] + 1
         finally:
             lltype.free(previous_ptr, flavor='raw')
diff --git a/pypy/module/_multiprocessing/interp_win32.py 
b/pypy/module/_multiprocessing/interp_win32.py
--- a/pypy/module/_multiprocessing/interp_win32.py
+++ b/pypy/module/_multiprocessing/interp_win32.py
@@ -41,20 +41,24 @@
         rwin32.DWORD, rwin32.DWORD, rwin32.DWORD,
         rwin32.DWORD, rwin32.DWORD, rwin32.DWORD,
         rffi.VOIDP],
-    rwin32.HANDLE)
+    rwin32.HANDLE,
+    save_err=rffi.RFFI_SAVE_LASTERROR)
 
 _ConnectNamedPipe = rwin32.winexternal(
-    'ConnectNamedPipe', [rwin32.HANDLE, rffi.VOIDP], rwin32.BOOL)
+    'ConnectNamedPipe', [rwin32.HANDLE, rffi.VOIDP], rwin32.BOOL,
+    save_err=rffi.RFFI_SAVE_LASTERROR)
 
 _SetNamedPipeHandleState = rwin32.winexternal(
     'SetNamedPipeHandleState', [
         rwin32.HANDLE,
         rwin32.LPDWORD, rwin32.LPDWORD, rwin32.LPDWORD],
-    rwin32.BOOL)
+    rwin32.BOOL,
+    save_err=rffi.RFFI_SAVE_LASTERROR)
 
 _WaitNamedPipe = rwin32.winexternal(
     'WaitNamedPipeA', [rwin32.LPCSTR, rwin32.DWORD],
-    rwin32.BOOL)
+    rwin32.BOOL,
+    save_err=rffi.RFFI_SAVE_LASTERROR)
 
 _PeekNamedPipe = rwin32.winexternal(
     'PeekNamedPipe', [
@@ -62,31 +66,36 @@
         rffi.VOIDP,
         rwin32.DWORD,
         rwin32.LPDWORD, rwin32.LPDWORD, rwin32.LPDWORD],
-    rwin32.BOOL) 
+    rwin32.BOOL,
+    save_err=rffi.RFFI_SAVE_LASTERROR)
 
 _CreateFile = rwin32.winexternal(
     'CreateFileA', [
         rwin32.LPCSTR,
         rwin32.DWORD, rwin32.DWORD, rffi.VOIDP,
         rwin32.DWORD, rwin32.DWORD, rwin32.HANDLE],
-    rwin32.HANDLE)
+    rwin32.HANDLE,
+    save_err=rffi.RFFI_SAVE_LASTERROR)
 
 _WriteFile = rwin32.winexternal(
     'WriteFile', [
         rwin32.HANDLE,
         rffi.VOIDP, rwin32.DWORD,
         rwin32.LPDWORD, rffi.VOIDP],
-    rwin32.BOOL)
+    rwin32.BOOL,
+    save_err=rffi.RFFI_SAVE_LASTERROR)
 
 _ReadFile = rwin32.winexternal(
     'ReadFile', [
         rwin32.HANDLE,
         rffi.VOIDP, rwin32.DWORD,
         rwin32.LPDWORD, rffi.VOIDP],
-    rwin32.BOOL)
+    rwin32.BOOL,
+    save_err=rffi.RFFI_SAVE_LASTERROR)
 
 _ExitProcess = rwin32.winexternal(
-    'ExitProcess', [rffi.UINT], lltype.Void)
+    'ExitProcess', [rffi.UINT], lltype.Void,
+    save_err=rffi.RFFI_SAVE_LASTERROR)
 
 _GetTickCount = rwin32.winexternal(
     'GetTickCount', [], rwin32.DWORD)
@@ -97,10 +106,10 @@
 def CloseHandle(space, w_handle):
     handle = handle_w(space, w_handle)
     if not rwin32.CloseHandle(handle):
-        raise wrap_windowserror(space, rwin32.lastWindowsError())
+        raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
 
 def GetLastError(space):
-    return space.wrap(rwin32.GetLastError())
+    return space.wrap(rwin32.GetLastError_saved())
 
 # __________________________________________________________
 # functions for the "win32" namespace
@@ -118,7 +127,7 @@
         outputsize, inputsize, timeout, rffi.NULL)
 
     if handle == rwin32.INVALID_HANDLE_VALUE:
-        raise wrap_windowserror(space, rwin32.lastWindowsError())
+        raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
 
     return w_handle(space, handle)
 
@@ -129,7 +138,7 @@
         raise OperationError(space.w_NotImplementedError,
                              space.wrap("expected a NULL pointer"))
     if not _ConnectNamedPipe(handle, rffi.NULL):
-        raise wrap_windowserror(space, rwin32.lastWindowsError())
+        raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
 
 def SetNamedPipeHandleState(space, w_handle, w_pipemode, w_maxinstances,
                             w_timeout):
@@ -149,7 +158,7 @@
             statep[2] = rffi.ptradd(state, 2)
         if not _SetNamedPipeHandleState(handle, statep[0], statep[1],
                                         statep[2]):
-            raise wrap_windowserror(space, rwin32.lastWindowsError())
+            raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
     finally:
         lltype.free(state, flavor='raw')
         lltype.free(statep, flavor='raw')
@@ -158,7 +167,7 @@
 def WaitNamedPipe(space, name, timeout):
     # Careful: zero means "default value specified by CreateNamedPipe()"
     if not _WaitNamedPipe(name, timeout):
-        raise wrap_windowserror(space, rwin32.lastWindowsError())
+        raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
 
 @unwrap_spec(filename=str, access=r_uint, share=r_uint,
              disposition=r_uint, flags=r_uint)
@@ -174,7 +183,7 @@
                          disposition, flags, rwin32.NULL_HANDLE)
 
     if handle == rwin32.INVALID_HANDLE_VALUE:
-        raise wrap_windowserror(space, rwin32.lastWindowsError())
+        raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
 
     return w_handle(space, handle)
 
diff --git a/pypy/module/time/interp_time.py b/pypy/module/time/interp_time.py
--- a/pypy/module/time/interp_time.py
+++ b/pypy/module/time/interp_time.py
@@ -62,7 +62,8 @@
     _setCtrlHandlerRoutine = rffi.llexternal(
         'pypy_timemodule_setCtrlHandler',
         [rwin32.HANDLE], rwin32.BOOL,
-        compilation_info=eci)
+        compilation_info=eci,
+        save_err=rffi.RFFI_SAVE_LASTERROR)
 
     class GlobalState:
         def __init__(self):
@@ -79,8 +80,8 @@
             except WindowsError, e:
                 raise wrap_windowserror(space, e)
             if not _setCtrlHandlerRoutine(globalState.interrupt_event):
-                raise wrap_windowserror(
-                    space, rwin32.lastWindowsError("SetConsoleCtrlHandler"))
+                raise wrap_windowserror(space,
+                    rwin32.lastSavedWindowsError("SetConsoleCtrlHandler"))
 
     globalState = GlobalState()
 
diff --git a/rpython/rlib/rdynload.py b/rpython/rlib/rdynload.py
--- a/rpython/rlib/rdynload.py
+++ b/rpython/rlib/rdynload.py
@@ -145,7 +145,7 @@
         # mode is unused on windows, but a consistant signature
         res = rwin32.LoadLibrary(name)
         if not res:
-            err = rwin32.GetLastError()
+            err = rwin32.GetLastError_saved()
             raise DLOpenError(rwin32.FormatError(err))
         return res
 
diff --git a/rpython/rlib/rmmap.py b/rpython/rlib/rmmap.py
--- a/rpython/rlib/rmmap.py
+++ b/rpython/rlib/rmmap.py
@@ -201,11 +201,20 @@
     SYSTEM_INFO_P = lltype.Ptr(SYSTEM_INFO)
 
     GetSystemInfo, _ = winexternal('GetSystemInfo', [SYSTEM_INFO_P], 
lltype.Void)
-    GetFileSize, _ = winexternal('GetFileSize', [HANDLE, LPDWORD], DWORD)
+    GetFileSize, _ = winexternal('GetFileSize', [HANDLE, LPDWORD], DWORD,
+                                 save_err=rffi.RFFI_SAVE_LASTERROR)
     GetCurrentProcess, _ = winexternal('GetCurrentProcess', [], HANDLE)
-    DuplicateHandle, _ = winexternal('DuplicateHandle', [HANDLE, HANDLE, 
HANDLE, LPHANDLE, DWORD, BOOL, DWORD], BOOL)
-    CreateFileMapping, _ = winexternal('CreateFileMappingA', [HANDLE, 
rwin32.LPSECURITY_ATTRIBUTES, DWORD, DWORD, DWORD, LPCSTR], HANDLE)
-    MapViewOfFile, _ = winexternal('MapViewOfFile', [HANDLE, DWORD, DWORD, 
DWORD, SIZE_T], LPCSTR)##!!LPVOID)
+    DuplicateHandle, _ = winexternal('DuplicateHandle',
+                                     [HANDLE, HANDLE, HANDLE, LPHANDLE, DWORD,
+                                      BOOL, DWORD], BOOL,
+                                     save_err=rffi.RFFI_SAVE_LASTERROR)
+    CreateFileMapping, _ = winexternal('CreateFileMappingA',
+                                       [HANDLE, rwin32.LPSECURITY_ATTRIBUTES,
+                                        DWORD, DWORD, DWORD, LPCSTR], HANDLE,
+                                       save_err=rffi.RFFI_SAVE_LASTERROR)
+    MapViewOfFile, _ = winexternal('MapViewOfFile', [HANDLE, DWORD, DWORD,
+                                                     DWORD, SIZE_T], LPCSTR,
+                                   save_err=rffi.RFFI_SAVE_LASTERROR) 
##!!LPVOID
     _, UnmapViewOfFile_safe = winexternal('UnmapViewOfFile', [LPCSTR], BOOL)
     FlushViewOfFile, _ = winexternal('FlushViewOfFile', [LPCSTR, SIZE_T], BOOL)
     SetFilePointer, _ = winexternal('SetFilePointer', [HANDLE, LONG, PLONG, 
DWORD], DWORD)
@@ -255,7 +264,7 @@
             # so we need to check the last error also
             INVALID_FILE_SIZE = -1
             if low == INVALID_FILE_SIZE:
-                err = rwin32.GetLastError()
+                err = rwin32.GetLastError_saved()
                 if err:
                     raise WindowsError(err, "mmap")
             return low, high
@@ -328,10 +337,10 @@
                 self.unmap()
                 self.setdata(NODATA, 0)
             if self.map_handle != INVALID_HANDLE:
-                rwin32.CloseHandle(self.map_handle)
+                rwin32.CloseHandle_no_err(self.map_handle)
                 self.map_handle = INVALID_HANDLE
             if self.file_handle != INVALID_HANDLE:
-                rwin32.CloseHandle(self.file_handle)
+                rwin32.CloseHandle_no_err(self.file_handle)
                 self.file_handle = INVALID_HANDLE
         elif _POSIX:
             self.closed = True
@@ -536,7 +545,7 @@
         elif _MS_WINDOWS:
             # disconnect the mapping
             self.unmap()
-            rwin32.CloseHandle(self.map_handle)
+            rwin32.CloseHandle_no_err(self.map_handle)
 
             # move to the desired EOF position
             if _64BIT:
@@ -573,9 +582,9 @@
                     charp = rffi.cast(LPCSTR, data)
                     self.setdata(charp, newsize)
                     return
-            winerror = rwin32.lastWindowsError()
+            winerror = rwin32.lastSavedWindowsError()
             if self.map_handle:
-                rwin32.CloseHandle(self.map_handle)
+                rwin32.CloseHandle_no_err(self.map_handle)
             self.map_handle = INVALID_HANDLE
             raise winerror
 
@@ -812,7 +821,7 @@
                                       False, # inherited by child procs?
                                       DUPLICATE_SAME_ACCESS) # options
                 if not res:
-                    raise rwin32.lastWindowsError()
+                    raise rwin32.lastSavedWindowsError()
                 m.file_handle = handle_ref[0]
             finally:
                 lltype.free(handle_ref, flavor='raw')
@@ -855,9 +864,9 @@
                 charp = rffi.cast(LPCSTR, data)
                 m.setdata(charp, map_size)
                 return m
-        winerror = rwin32.lastWindowsError()
+        winerror = rwin32.lastSavedWindowsError()
         if m.map_handle:
-            rwin32.CloseHandle(m.map_handle)
+            rwin32.CloseHandle_no_err(m.map_handle)
         m.map_handle = INVALID_HANDLE
         raise winerror
 
diff --git a/rpython/rlib/rthread.py b/rpython/rlib/rthread.py
--- a/rpython/rlib/rthread.py
+++ b/rpython/rlib/rthread.py
@@ -371,6 +371,7 @@
                                    loop_invariant=True)
 tlfield_rpy_errno = ThreadLocalField(rffi.INT, "rpy_errno")
 if sys.platform == "win32":
+    from rpython.rlib import rwin32
     tlfield_rpy_lasterror = ThreadLocalField(rwin32.DWORD, "rpy_lasterror")
 
 def _threadlocalref_seeme(field):
diff --git a/rpython/rlib/runicode.py b/rpython/rlib/runicode.py
--- a/rpython/rlib/runicode.py
+++ b/rpython/rlib/runicode.py
@@ -1597,7 +1597,8 @@
                                            rwin32.LPCSTR, rffi.INT,
                                            rffi.CWCHARP, rffi.INT],
                                           rffi.INT,
-                                          calling_conv='win')
+                                          calling_conv='win',
+                                          save_err=rffi.RFFI_SAVE_LASTERROR)
 
     WideCharToMultiByte = rffi.llexternal('WideCharToMultiByte',
                                           [rffi.UINT, rwin32.DWORD,
@@ -1605,19 +1606,20 @@
                                            rwin32.LPCSTR, rffi.INT,
                                            rwin32.LPCSTR, BOOLP],
                                           rffi.INT,
-                                          calling_conv='win')
+                                          calling_conv='win',
+                                          save_err=rffi.RFFI_SAVE_LASTERROR)
 
     def is_dbcs_lead_byte(c):
         # XXX don't know how to test this
         return False
 
     def _decode_mbcs_error(s, errorhandler):
-        if rwin32.GetLastError() == rwin32.ERROR_NO_UNICODE_TRANSLATION:
+        if rwin32.GetLastError_saved() == rwin32.ERROR_NO_UNICODE_TRANSLATION:
             msg = ("No mapping for the Unicode character exists in the target "
                    "multi-byte code page.")
             errorhandler('strict', 'mbcs', msg, s, 0, 0)
         else:
-            raise rwin32.lastWindowsError()
+            raise rwin32.lastSavedWindowsError()
 
     def str_decode_mbcs(s, size, errors, final=False, errorhandler=None,
                         force_ignore=True):
@@ -1684,7 +1686,7 @@
                                                dataptr, size, None, 0,
                                                None, used_default_p)
                 if mbcssize == 0:
-                    raise rwin32.lastWindowsError()
+                    raise rwin32.lastSavedWindowsError()
                 # If we used a default char, then we failed!
                 if (used_default_p and
                     rffi.cast(lltype.Bool, used_default_p[0])):
@@ -1696,7 +1698,7 @@
                     if WideCharToMultiByte(CP_ACP, flags,
                                            dataptr, size, buf.raw, mbcssize,
                                            None, used_default_p) == 0:
-                        raise rwin32.lastWindowsError()
+                        raise rwin32.lastSavedWindowsError()
                     if (used_default_p and
                         rffi.cast(lltype.Bool, used_default_p[0])):
                         errorhandler('strict', 'mbcs', "invalid character",
diff --git a/rpython/rlib/rurandom.py b/rpython/rlib/rurandom.py
--- a/rpython/rlib/rurandom.py
+++ b/rpython/rlib/rurandom.py
@@ -35,14 +35,16 @@
          rwin32.LPCSTR, rwin32.LPCSTR, rwin32.DWORD, rwin32.DWORD],
         rwin32.BOOL,
         calling_conv='win',
-        compilation_info=eci)
+        compilation_info=eci,
+        save_err=rffi.RFFI_SAVE_LASTERROR)
 
     CryptGenRandom = rffi.llexternal(
         'CryptGenRandom',
         [HCRYPTPROV, rwin32.DWORD, rffi.CArrayPtr(rwin32.BYTE)],
         rwin32.BOOL,
         calling_conv='win',
-        compilation_info=eci)
+        compilation_info=eci,
+        save_err=rffi.RFFI_SAVE_LASTERROR)
 
     def init_urandom():
         """NOT_RPYTHON
@@ -60,14 +62,14 @@
             if not CryptAcquireContext(
                 context, None, None,
                 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT):
-                raise rwin32.lastWindowsError("CryptAcquireContext")
+                raise rwin32.lastSavedWindowsError("CryptAcquireContext")
             provider = context[0]
         # TODO(win64) This is limited to 2**31
         with lltype.scoped_alloc(rffi.CArray(rwin32.BYTE), n,
                                  zero=True, # zero seed
                                  ) as buf:
             if not CryptGenRandom(provider, n, buf):
-                raise rwin32.lastWindowsError("CryptGenRandom")
+                raise rwin32.lastSavedWindowsError("CryptGenRandom")
 
             return rffi.charpsize2str(rffi.cast(rffi.CCHARP, buf), n)
 
diff --git a/rpython/rlib/rwin32.py b/rpython/rlib/rwin32.py
--- a/rpython/rlib/rwin32.py
+++ b/rpython/rlib/rwin32.py
@@ -131,19 +131,23 @@
         from rpython.rlib import rthread
         rthread.tlfield_rpy_lasterror.setraw(rffi.cast(DWORD, err))
 
-    # In tests, the first call to GetLastError_real() is always wrong,
+    # In tests, the first call to _GetLastError() is always wrong,
     # because error is hidden by operations in ll2ctypes.  Call it now.
-    GetLastError_real()
+    _GetLastError()
 
     GetModuleHandle = winexternal('GetModuleHandleA', [rffi.CCHARP], HMODULE)
-    LoadLibrary = winexternal('LoadLibraryA', [rffi.CCHARP], HMODULE)
+    LoadLibrary = winexternal('LoadLibraryA', [rffi.CCHARP], HMODULE,
+                              save_err=rffi.RFFI_SAVE_LASTERROR)
     GetProcAddress = winexternal('GetProcAddress',
                                  [HMODULE, rffi.CCHARP],
                                  rffi.VOIDP)
     FreeLibrary = winexternal('FreeLibrary', [HMODULE], BOOL, releasegil=False)
 
     LocalFree = winexternal('LocalFree', [HLOCAL], DWORD)
-    CloseHandle = winexternal('CloseHandle', [HANDLE], BOOL, releasegil=False)
+    CloseHandle = winexternal('CloseHandle', [HANDLE], BOOL, releasegil=False,
+                              save_err=rffi.RFFI_SAVE_LASTERROR)
+    CloseHandle_no_err = winexternal('CloseHandle', [HANDLE], BOOL,
+                                     releasegil=False)
 
     FormatMessage = winexternal(
         'FormatMessageA',
@@ -260,7 +264,7 @@
     def fake_FormatError(code):
         return 'Windows Error %d' % (code,)
 
-    def lastWindowsError(context="Windows Error"):
+    def lastSavedWindowsError(context="Windows Error"):
         code = GetLastError_saved()
         return WindowsError(code, context)
 
@@ -285,7 +289,8 @@
 
     _GetVersionEx = winexternal('GetVersionExA',
                                 [lltype.Ptr(OSVERSIONINFOEX)],
-                                DWORD)
+                                DWORD,
+                                save_err=rffi.RFFI_SAVE_LASTERROR)
 
     @jit.dont_look_inside
     def GetVersionEx():
@@ -294,7 +299,7 @@
                          rffi.sizeof(OSVERSIONINFOEX))
         try:
             if not _GetVersionEx(info):
-                raise lastWindowsError()
+                raise lastSavedWindowsError()
             return (rffi.cast(lltype.Signed, info.c_dwMajorVersion),
                     rffi.cast(lltype.Signed, info.c_dwMinorVersion),
                     rffi.cast(lltype.Signed, info.c_dwBuildNumber),
@@ -309,7 +314,8 @@
             lltype.free(info, flavor='raw')
 
     _WaitForSingleObject = winexternal(
-        'WaitForSingleObject', [HANDLE, DWORD], DWORD)
+        'WaitForSingleObject', [HANDLE, DWORD], DWORD,
+        save_err=rffi.RFFI_SAVE_LASTERROR)
 
     def WaitForSingleObject(handle, timeout):
         """Return values:
@@ -317,12 +323,13 @@
         - WAIT_TIMEOUT when the timeout elapsed"""
         res = _WaitForSingleObject(handle, timeout)
         if res == rffi.cast(DWORD, -1):
-            raise lastWindowsError("WaitForSingleObject")
+            raise lastSavedWindowsError("WaitForSingleObject")
         return res
 
     _WaitForMultipleObjects = winexternal(
         'WaitForMultipleObjects', [
-            DWORD, rffi.CArrayPtr(HANDLE), BOOL, DWORD], DWORD)
+            DWORD, rffi.CArrayPtr(HANDLE), BOOL, DWORD], DWORD,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
     def WaitForMultipleObjects(handles, waitall=False, timeout=INFINITE):
         """Return values:
@@ -336,24 +343,26 @@
                 handle_array[i] = handles[i]
             res = _WaitForMultipleObjects(nb, handle_array, waitall, timeout)
             if res == rffi.cast(DWORD, -1):
-                raise lastWindowsError("WaitForMultipleObjects")
+                raise lastSavedWindowsError("WaitForMultipleObjects")
             return res
         finally:
             lltype.free(handle_array, flavor='raw')
 
     _CreateEvent = winexternal(
-        'CreateEventA', [rffi.VOIDP, BOOL, BOOL, LPCSTR], HANDLE)
+        'CreateEventA', [rffi.VOIDP, BOOL, BOOL, LPCSTR], HANDLE,
+        save_err=rffi.RFFI_SAVE_LASTERROR)
     def CreateEvent(*args):
         handle = _CreateEvent(*args)
         if handle == NULL_HANDLE:
-            raise lastWindowsError("CreateEvent")
+            raise lastSavedWindowsError("CreateEvent")
         return handle
     SetEvent = winexternal(
         'SetEvent', [HANDLE], BOOL)
     ResetEvent = winexternal(
         'ResetEvent', [HANDLE], BOOL)
     _OpenProcess = winexternal(
-        'OpenProcess', [DWORD, BOOL, DWORD], HANDLE)
+        'OpenProcess', [DWORD, BOOL, DWORD], HANDLE,
+        save_err=rffi.RFFI_SAVE_LASTERROR)
     def OpenProcess(*args):
         ''' OpenProcess( dwDesiredAccess, bInheritHandle, dwProcessId)
         where dwDesiredAccess is a combination of the flags:
@@ -379,12 +388,14 @@
         '''
         handle = _OpenProcess(*args)
         if handle == NULL_HANDLE:
-            raise lastWindowsError("OpenProcess")
+            raise lastSavedWindowsError("OpenProcess")
         return handle
     TerminateProcess = winexternal(
-        'TerminateProcess', [HANDLE, rffi.UINT], BOOL)
+        'TerminateProcess', [HANDLE, rffi.UINT], BOOL,
+        save_err=rffi.RFFI_SAVE_LASTERROR)
     GenerateConsoleCtrlEvent = winexternal(
-        'GenerateConsoleCtrlEvent', [DWORD, DWORD], BOOL)
+        'GenerateConsoleCtrlEvent', [DWORD, DWORD], BOOL,
+        save_err=rffi.RFFI_SAVE_LASTERROR)
     _GetCurrentProcessId = winexternal(
         'GetCurrentProcessId', [], DWORD)
     def GetCurrentProcessId():
@@ -400,14 +411,15 @@
     def os_kill(pid, sig):
         if sig == CTRL_C_EVENT or sig == CTRL_BREAK_EVENT:
             if GenerateConsoleCtrlEvent(sig, pid) == 0:
-                raise lastWindowsError('os_kill failed generating event')
+                raise lastSavedWindowsError('os_kill failed generating event')
             return
         handle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
         if handle == NULL_HANDLE:
-            raise lastWindowsError('os_kill failed opening process')
+            raise lastSavedWindowsError('os_kill failed opening process')
         try:
             if TerminateProcess(handle, sig) == 0:
-                raise lastWindowsError('os_kill failed to terminate process')
+                raise lastSavedWindowsError(
+                    'os_kill failed to terminate process')
         finally:
             CloseHandle(handle)
 
diff --git a/rpython/rlib/rwinreg.py b/rpython/rlib/rwinreg.py
--- a/rpython/rlib/rwinreg.py
+++ b/rpython/rlib/rwinreg.py
@@ -146,17 +146,18 @@
 _ExpandEnvironmentStringsW = external(
     'ExpandEnvironmentStringsW',
     [rffi.CWCHARP, rffi.CWCHARP, rwin32.DWORD],
-    rwin32.DWORD)
+    rwin32.DWORD,
+    save_err=rffi.RFFI_SAVE_LASTERROR)
 
 def ExpandEnvironmentStrings(source):
     with rffi.scoped_unicode2wcharp(source) as src_buf:
         size = _ExpandEnvironmentStringsW(src_buf,
                                           lltype.nullptr(rffi.CWCHARP.TO), 0)
         if size == 0:
-            raise rwin32.lastWindowsError("ExpandEnvironmentStrings")
+            raise rwin32.lastSavedWindowsError("ExpandEnvironmentStrings")
         size = intmask(size)
         with rffi.scoped_alloc_unicodebuffer(size) as dest_buf:
             if _ExpandEnvironmentStringsW(src_buf,
                                           dest_buf.raw, size) == 0:
-                raise rwin32.lastWindowsError("ExpandEnvironmentStrings")
+                raise rwin32.lastSavedWindowsError("ExpandEnvironmentStrings")
             return dest_buf.str(size - 1) # remove trailing \0
diff --git a/rpython/rlib/streamio.py b/rpython/rlib/streamio.py
--- a/rpython/rlib/streamio.py
+++ b/rpython/rlib/streamio.py
@@ -188,7 +188,8 @@
 
 
 if sys.platform == "win32":
-    from rpython.rlib.rwin32 import BOOL, HANDLE, get_osfhandle, GetLastError
+    from rpython.rlib.rwin32 import BOOL, HANDLE, get_osfhandle
+    from rpython.rlib.rwin32 import GetLastError_saved
     from rpython.translator.tool.cbuild import ExternalCompilationInfo
     from rpython.rtyper.lltypesystem import rffi
 
@@ -196,7 +197,8 @@
     _setmode = rffi.llexternal('_setmode', [rffi.INT, rffi.INT], rffi.INT,
                                compilation_info=_eci)
     SetEndOfFile = rffi.llexternal('SetEndOfFile', [HANDLE], BOOL,
-                                   compilation_info=_eci)
+                                   compilation_info=_eci,
+                                   save_err=rffi.RFFI_SAVE_LASTERROR)
 
     def _setfd_binary(fd):
         # Allow this to succeed on invalid fd's
@@ -211,7 +213,7 @@
             # Truncate.  Note that this may grow the file!
             handle = get_osfhandle(fd)
             if not SetEndOfFile(handle):
-                raise OSError(GetLastError(),
+                raise OSError(GetLastError_saved(),
                                    "Could not truncate file")
         finally:
             # we restore the file pointer position in any case
diff --git a/rpython/rtyper/lltypesystem/module/ll_math.py 
b/rpython/rtyper/lltypesystem/module/ll_math.py
--- a/rpython/rtyper/lltypesystem/module/ll_math.py
+++ b/rpython/rtyper/lltypesystem/module/ll_math.py
@@ -43,10 +43,10 @@
     return rffi.llexternal(name, ARGS, RESULT, compilation_info=eci,
                            sandboxsafe=True, **kwargs)
 
-def math_llexternal(name, ARGS, RESULT):
+def math_llexternal(name, ARGS, RESULT, **kwargs):
     return rffi.llexternal(math_prefix + name, ARGS, RESULT,
                            compilation_info=math_eci,
-                           sandboxsafe=True)
+                           sandboxsafe=True, **kwargs)
 
 math_fabs = llexternal('fabs', [rffi.DOUBLE], rffi.DOUBLE)
 math_log = llexternal('log', [rffi.DOUBLE], rffi.DOUBLE)
diff --git a/rpython/rtyper/module/ll_os.py b/rpython/rtyper/module/ll_os.py
--- a/rpython/rtyper/module/ll_os.py
+++ b/rpython/rtyper/module/ll_os.py
@@ -1339,7 +1339,8 @@
                                                         rwin32.LPHANDLE,
                                                         rffi.VOIDP,
                                                         rwin32.DWORD],
-                                         rwin32.BOOL)
+                                         rwin32.BOOL,
+                                         save_err=rffi.RFFI_SAVE_LASTERROR)
             _open_osfhandle = self.llexternal('_open_osfhandle', 
[rffi.INTPTR_T,
                                                                   rffi.INT],
                                               rffi.INT)
@@ -1352,7 +1353,7 @@
                 if ok:
                     error = 0
                 else:
-                    error = rwin32.GetLastError()
+                    error = rwin32.GetLastError_saved()
                 hread = rffi.cast(rffi.INTPTR_T, pread[0])
                 hwrite = rffi.cast(rffi.INTPTR_T, pwrite[0])
                 lltype.free(pwrite, flavor='raw')
@@ -1564,7 +1565,7 @@
             @func_renamer('unlink_llimpl_%s' % traits.str.__name__)
             def unlink_llimpl(path):
                 if not win32traits.DeleteFile(path):
-                    raise rwin32.lastWindowsError()
+                    raise rwin32.lastSavedWindowsError()
 
         return extdef([traits.str0], s_None, llimpl=unlink_llimpl,
                       export_name=traits.ll_os_name('unlink'))
@@ -1601,7 +1602,7 @@
             @func_renamer('mkdir_llimpl_%s' % traits.str.__name__)
             def os_mkdir_llimpl(path, mode):
                 if not win32traits.CreateDirectory(path, None):
-                    raise rwin32.lastWindowsError()
+                    raise rwin32.lastSavedWindowsError()
         else:
             def os_mkdir_llimpl(pathname, mode):
                 res = os_mkdir(pathname, mode)
@@ -1677,7 +1678,7 @@
             @func_renamer('rename_llimpl_%s' % traits.str.__name__)
             def rename_llimpl(oldpath, newpath):
                 if not win32traits.MoveFile(oldpath, newpath):
-                    raise rwin32.lastWindowsError()
+                    raise rwin32.lastSavedWindowsError()
 
         return extdef([traits.str0, traits.str0], s_None, llimpl=rename_llimpl,
                       export_name=traits.ll_os_name('rename'))
diff --git a/rpython/rtyper/module/ll_os_environ.py 
b/rpython/rtyper/module/ll_os_environ.py
--- a/rpython/rtyper/module/ll_os_environ.py
+++ b/rpython/rtyper/module/ll_os_environ.py
@@ -124,7 +124,8 @@
     _wgetenv = rffi.llexternal('_wgetenv', [rffi.CWCHARP], rffi.CWCHARP,
                                compilation_info=eci, releasegil=False)
     _wputenv = rffi.llexternal('_wputenv', [rffi.CWCHARP], rffi.INT,
-                               compilation_info=eci)
+                               compilation_info=eci,
+                               save_err=rffi.RFFI_SAVE_LASTERROR)
 
 class EnvKeepalive:
     pass
@@ -144,7 +145,7 @@
         traits = UnicodeTraits()
         get_environ, getenv, putenv = get__wenviron, _wgetenv, _wputenv
         byname, eq = envkeepalive.bywname, u'='
-        from rpython.rlib.rwin32 import lastWindowsError as last_error
+        from rpython.rlib.rwin32 import lastSavedWindowsError as last_error
 
     def envitems_llimpl():
         environ = get_environ()
diff --git a/rpython/rtyper/module/ll_os_stat.py 
b/rpython/rtyper/module/ll_os_stat.py
--- a/rpython/rtyper/module/ll_os_stat.py
+++ b/rpython/rtyper/module/ll_os_stat.py
@@ -523,11 +523,11 @@
         try:
             l_path = traits.str2charp(path)
             res = win32traits.GetFileAttributesEx(l_path, 
win32traits.GetFileExInfoStandard, data)
-            errcode = rwin32.GetLastError()
+            errcode = rwin32.GetLastError_saved()
             if res == 0:
                 if errcode == win32traits.ERROR_SHARING_VIOLATION:
                     res = attributes_from_dir(l_path, data)
-                    errcode = rwin32.GetLastError()
+                    errcode = rwin32.GetLastError_saved()
             traits.free_charp(l_path)
             if res == 0:
                 raise WindowsError(errcode, "os_stat failed")
@@ -549,7 +549,7 @@
                                      0, 0, 0, 0, 0,
                                      0, 0, 0, 0))
         elif filetype == win32traits.FILE_TYPE_UNKNOWN:
-            error = rwin32.GetLastError()
+            error = rwin32.GetLastError_saved()
             if error != 0:
                 raise WindowsError(error, "os_fstat failed")
             # else: unknown but valid file
@@ -560,7 +560,8 @@
         try:
             res = win32traits.GetFileInformationByHandle(handle, info)
             if res == 0:
-                raise WindowsError(rwin32.GetLastError(), "os_fstat failed")
+                raise WindowsError(rwin32.GetLastError_saved(),
+                                   "os_fstat failed")
             return by_handle_info_to_stat(info)
         finally:
             lltype.free(info, flavor='raw')
diff --git a/rpython/rtyper/module/ll_win32file.py 
b/rpython/rtyper/module/ll_win32file.py
--- a/rpython/rtyper/module/ll_win32file.py
+++ b/rpython/rtyper/module/ll_win32file.py
@@ -113,10 +113,12 @@
 
         FindFirstFile = external('FindFirstFile' + suffix,
                                  [traits.CCHARP, LPWIN32_FIND_DATA],
-                                 rwin32.HANDLE)
+                                 rwin32.HANDLE,
+                                 save_err=rffi.RFFI_SAVE_LASTERROR)
         FindNextFile = external('FindNextFile' + suffix,
                                 [rwin32.HANDLE, LPWIN32_FIND_DATA],
-                                rwin32.BOOL)
+                                rwin32.BOOL,
+                                save_err=rffi.RFFI_SAVE_LASTERROR)
         FindClose = external('FindClose',
                              [rwin32.HANDLE],
                              rwin32.BOOL)
@@ -124,28 +126,33 @@
         GetFileAttributes = external(
             'GetFileAttributes' + suffix,
             [traits.CCHARP],
-            rwin32.DWORD)
+            rwin32.DWORD,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         SetFileAttributes = external(
             'SetFileAttributes' + suffix,
             [traits.CCHARP, rwin32.DWORD],
-            rwin32.BOOL)
+            rwin32.BOOL,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         GetFileAttributesEx = external(
             'GetFileAttributesEx' + suffix,
             [traits.CCHARP, GET_FILEEX_INFO_LEVELS,
              lltype.Ptr(WIN32_FILE_ATTRIBUTE_DATA)],
-            rwin32.BOOL)
+            rwin32.BOOL,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         GetFileInformationByHandle = external(
             'GetFileInformationByHandle',
             [rwin32.HANDLE, lltype.Ptr(BY_HANDLE_FILE_INFORMATION)],
-            rwin32.BOOL)
+            rwin32.BOOL,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         GetFileType = external(
             'GetFileType',
             [rwin32.HANDLE],
-            rwin32.DWORD)
+            rwin32.DWORD,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         LPSTRP = rffi.CArrayPtr(traits.CCHARP)
 
@@ -153,45 +160,52 @@
             'GetFullPathName' + suffix,
             [traits.CCHARP, rwin32.DWORD,
              traits.CCHARP, LPSTRP],
-            rwin32.DWORD)
+            rwin32.DWORD,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         GetCurrentDirectory = external(
             'GetCurrentDirectory' + suffix,
             [rwin32.DWORD, traits.CCHARP],
-            rwin32.DWORD)
+            rwin32.DWORD,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         SetCurrentDirectory = external(
             'SetCurrentDirectory' + suffix,
             [traits.CCHARP],
-            rwin32.BOOL)
+            rwin32.BOOL,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         CreateDirectory = external(
             'CreateDirectory' + suffix,
             [traits.CCHARP, rffi.VOIDP],
             rwin32.BOOL,
-            XXX)   # save_err=rffi.RFFI_SAVE_LASTERROR
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         SetEnvironmentVariable = external(
             'SetEnvironmentVariable' + suffix,
             [traits.CCHARP, traits.CCHARP],
-            rwin32.BOOL)
+            rwin32.BOOL,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         CreateFile = external(
             'CreateFile' + apisuffix,
             [traits.CCHARP, rwin32.DWORD, rwin32.DWORD,
              rwin32.LPSECURITY_ATTRIBUTES, rwin32.DWORD, rwin32.DWORD,
              rwin32.HANDLE],
-            rwin32.HANDLE)
+            rwin32.HANDLE,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         DeleteFile = external(
             'DeleteFile' + suffix,
             [traits.CCHARP],
-            rwin32.BOOL)
+            rwin32.BOOL,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
         MoveFile = external(
             'MoveFile' + suffix,
             [traits.CCHARP, traits.CCHARP],
-            rwin32.BOOL)
+            rwin32.BOOL,
+            save_err=rffi.RFFI_SAVE_LASTERROR)
 
     return Win32Traits
 
@@ -227,7 +241,7 @@
             result = []
             hFindFile = win32traits.FindFirstFile(mask, filedata)
             if hFindFile == rwin32.INVALID_HANDLE_VALUE:
-                error = rwin32.GetLastError()
+                error = rwin32.GetLastError_saved()
                 if error == win32traits.ERROR_FILE_NOT_FOUND:
                     return result
                 else:
@@ -241,7 +255,7 @@
                     break
             # FindNextFile sets error to ERROR_NO_MORE_FILES if
             # it got to the end of the directory
-            error = rwin32.GetLastError()
+            error = rwin32.GetLastError_saved()
             win32traits.FindClose(hFindFile)
             if error == win32traits.ERROR_NO_MORE_FILES:
                 return result
@@ -279,14 +293,14 @@
         the per-drive current directory, which are of the form =<drive>:
         """
         if not win32traits.SetCurrentDirectory(path):
-            raise rwin32.lastWindowsError()
+            raise rwin32.lastSavedWindowsError()
         MAX_PATH = rwin32.MAX_PATH
         assert MAX_PATH > 0
 
         with traits.scoped_alloc_buffer(MAX_PATH) as path:
             res = win32traits.GetCurrentDirectory(MAX_PATH + 1, path.raw)
             if not res:
-                raise rwin32.lastWindowsError()
+                raise rwin32.lastSavedWindowsError()
             res = rffi.cast(lltype.Signed, res)
             assert res > 0
             if res <= MAX_PATH + 1:
@@ -295,14 +309,14 @@
                 with traits.scoped_alloc_buffer(res) as path:
                     res = win32traits.GetCurrentDirectory(res, path.raw)
                     if not res:
-                        raise rwin32.lastWindowsError()
+                        raise rwin32.lastSavedWindowsError()
                     res = rffi.cast(lltype.Signed, res)
                     assert res > 0
                     new_path = path.str(res)
         if isUNC(new_path):
             return
         if not win32traits.SetEnvironmentVariable(magic_envvar(new_path), 
new_path):
-            raise rwin32.lastWindowsError()
+            raise rwin32.lastSavedWindowsError()
 
     return chdir_llimpl
 
@@ -317,13 +331,13 @@
     def chmod_llimpl(path, mode):
         attr = win32traits.GetFileAttributes(path)
         if attr == win32traits.INVALID_FILE_ATTRIBUTES:
-            raise rwin32.lastWindowsError()
+            raise rwin32.lastSavedWindowsError()
         if mode & 0200: # _S_IWRITE
             attr &= ~win32traits.FILE_ATTRIBUTE_READONLY
         else:
             attr |= win32traits.FILE_ATTRIBUTE_READONLY
         if not win32traits.SetFileAttributes(path, attr):
-            raise rwin32.lastWindowsError()
+            raise rwin32.lastSavedWindowsError()
 
     return chmod_llimpl
 
@@ -343,7 +357,7 @@
                 path, rffi.cast(rwin32.DWORD, nBufferLength),
                 lpBuffer, lltype.nullptr(win32traits.LPSTRP.TO))
             if res == 0:
-                raise rwin32.lastWindowsError("_getfullpathname failed")
+                raise rwin32.lastSavedWindowsError("_getfullpathname failed")
             result = traits.charp2str(lpBuffer)
             return result
         finally:
@@ -360,7 +374,8 @@
         'GetSystemTime',
         [lltype.Ptr(rwin32.SYSTEMTIME)],
         lltype.Void,
-        calling_conv='win')
+        calling_conv='win',
+        save_err=rffi.RFFI_SAVE_LASTERROR)
 
     SystemTimeToFileTime = rffi.llexternal(
         'SystemTimeToFileTime',
@@ -376,7 +391,8 @@
          lltype.Ptr(rwin32.FILETIME),
          lltype.Ptr(rwin32.FILETIME)],
         rwin32.BOOL,
-        calling_conv = 'win')
+        calling_conv = 'win',
+        save_err=rffi.RFFI_SAVE_LASTERROR)
 
     @specialize.argtype(1)
     def os_utime_llimpl(path, tp):
@@ -386,7 +402,7 @@
                            win32traits.FILE_FLAG_BACKUP_SEMANTICS,
                            rwin32.NULL_HANDLE)
         if hFile == rwin32.INVALID_HANDLE_VALUE:
-            raise rwin32.lastWindowsError()
+            raise rwin32.lastSavedWindowsError()
         ctime = lltype.nullptr(rwin32.FILETIME)
         atime = lltype.malloc(rwin32.FILETIME, flavor='raw')
         mtime = lltype.malloc(rwin32.FILETIME, flavor='raw')
@@ -397,7 +413,7 @@
                     GetSystemTime(now)
                     if (not SystemTimeToFileTime(now, atime) or
                         not SystemTimeToFileTime(now, mtime)):
-                        raise rwin32.lastWindowsError()
+                        raise rwin32.lastSavedWindowsError()
                 finally:
                     lltype.free(now, flavor='raw')
             else:
@@ -405,7 +421,7 @@
                 time_t_to_FILE_TIME(actime, atime)
                 time_t_to_FILE_TIME(modtime, mtime)
             if not SetFileTime(hFile, ctime, atime, mtime):
-                raise rwin32.lastWindowsError()
+                raise rwin32.lastSavedWindowsError()
         finally:
             rwin32.CloseHandle(hFile)
             lltype.free(atime, flavor='raw')
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to