Author: mattip <[email protected]>
Branch: alt_errno
Changeset: r75987:6ecc88245506
Date: 2015-02-18 21:50 +0200
http://bitbucket.org/pypy/pypy/changeset/6ecc88245506/

Log:    make alt_lasterror for win32

diff --git a/pypy/module/_cffi_backend/cerrno.py 
b/pypy/module/_cffi_backend/cerrno.py
--- a/pypy/module/_cffi_backend/cerrno.py
+++ b/pypy/module/_cffi_backend/cerrno.py
@@ -23,8 +23,8 @@
 
 @unwrap_spec(code=int)
 def getwinerror(space, code=-1):
-    from rpython.rlib.rwin32 import GetLastError_saved, FormatError
+    from rpython.rlib.rwin32 import GetLastError_alt_saved, FormatError
     if code == -1:
-        code = GetLastError_saved()
+        code = GetLastError_alt_saved()
     message = FormatError(code)
     return space.newtuple([space.wrap(code), space.wrap(message)])
diff --git a/pypy/module/_rawffi/interp_rawffi.py 
b/pypy/module/_rawffi/interp_rawffi.py
--- a/pypy/module/_rawffi/interp_rawffi.py
+++ b/pypy/module/_rawffi/interp_rawffi.py
@@ -617,10 +617,10 @@
     # see also
     # https://bitbucket.org/pypy/pypy/issue/1944/ctypes-on-windows-getlasterror
     def get_last_error(space):
-        return space.wrap(rwin32.GetLastError_saved())
+        return space.wrap(rwin32.GetLastError_alt_saved())
     @unwrap_spec(error=int)
     def set_last_error(space, error):
-        rwin32.SetLastError_saved(error)
+        rwin32.SetLastError_alt_saved(error)
 else:
     # always have at least a dummy version of these functions
     # (https://bugs.pypy.org/issue1242)
diff --git a/rpython/jit/backend/llsupport/llerrno.py 
b/rpython/jit/backend/llsupport/llerrno.py
--- a/rpython/jit/backend/llsupport/llerrno.py
+++ b/rpython/jit/backend/llsupport/llerrno.py
@@ -47,6 +47,13 @@
     else:
         return 5 * WORD
 
+def get_alt_lasterror_offset(cpu):
+    if cpu.translate_support_code:
+        from rpython.rlib import rthread
+        return rthread.tlfield_alt_lasterror.getoffset()
+    else:
+        return 6 * WORD
+
 
 def _fetch_addr_errno():
     eci = ExternalCompilationInfo(
diff --git a/rpython/jit/backend/llsupport/llmodel.py 
b/rpython/jit/backend/llsupport/llmodel.py
--- a/rpython/jit/backend/llsupport/llmodel.py
+++ b/rpython/jit/backend/llsupport/llmodel.py
@@ -63,7 +63,7 @@
                                               ad.lendescr, FLAG_FLOAT)
         self.setup()
         self._debug_errno_container = lltype.malloc(
-            rffi.CArray(lltype.Signed), 6, flavor='raw', zero=True,
+            rffi.CArray(lltype.Signed), 7, flavor='raw', zero=True,
             track_allocation=False)
 
     def getarraydescr_for_frame(self, type):
diff --git a/rpython/jit/backend/x86/callbuilder.py 
b/rpython/jit/backend/x86/callbuilder.py
--- a/rpython/jit/backend/x86/callbuilder.py
+++ b/rpython/jit/backend/x86/callbuilder.py
@@ -196,10 +196,13 @@
             SetLastError_addr = self.asm.cpu.cast_adr_to_int(adr)
             assert isinstance(self, CallBuilder32)    # Windows 32-bit only
             #
-            rpy_lasterror = llerrno.get_rpy_lasterror_offset(self.asm.cpu)
+            if save_err & rffi.RFFI_ALT_ERRNO:
+                lasterror = llerrno.get_alt_lasterror_offset(self.asm.cpu)
+            else:
+                lasterror = llerrno.get_rpy_lasterror_offset(self.asm.cpu)
             tlofsreg = self.get_tlofs_reg()    # => esi, callee-saved
             self.save_stack_position()         # => edi, callee-saved
-            mc.PUSH_m((tlofsreg.value, rpy_lasterror))
+            mc.PUSH_m((tlofsreg.value, lasterror))
             mc.CALL(imm(SetLastError_addr))
             # restore the stack position without assuming a particular
             # calling convention of _SetLastError()
@@ -262,13 +265,16 @@
             GetLastError_addr = self.asm.cpu.cast_adr_to_int(adr)
             assert isinstance(self, CallBuilder32)    # Windows 32-bit only
             #
-            rpy_lasterror = llerrno.get_rpy_lasterror_offset(self.asm.cpu)
+            if save_err & rffi.RFFI_ALT_ERRNO:
+                lasterror = llerrno.get_alt_lasterror_offset(self.asm.cpu)
+            else:
+                lasterror = llerrno.get_rpy_lasterror_offset(self.asm.cpu)
             self.save_result_value(save_edx=True)   # save eax/edx/xmm0
             self.result_value_saved_early = True
             mc.CALL(imm(GetLastError_addr))
             #
             tlofsreg = self.get_tlofs_reg()    # => esi (possibly reused)
-            mc.MOV32_mr((tlofsreg.value, rpy_lasterror), eax.value)
+            mc.MOV32_mr((tlofsreg.value, lasterror), eax.value)
 
     def move_real_result_and_call_reacqgil_addr(self, fastgil):
         from rpython.jit.backend.x86 import rx86
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -150,7 +150,10 @@
         _set_errno(rffi.cast(rffi.INT, 0))
     if WIN32 and (save_err & rffi.RFFI_READSAVED_LASTERROR):
         from rpython.rlib import rthread, rwin32
-        err = rthread.tlfield_rpy_lasterror.getraw()
+        if save_err & rffi.RFFI_ALT_ERRNO:
+            err = rthread.tlfield_alt_lasterror.getraw()
+        else:
+            err = rthread.tlfield_rpy_lasterror.getraw()
         # careful, getraw() overwrites GetLastError.
         # We must assign it with _SetLastError() as the last
         # operation, i.e. after the errno handling.
@@ -164,11 +167,17 @@
             err = rwin32._GetLastError()
             # careful, setraw() overwrites GetLastError.
             # We must read it first, before the errno handling.
-            rthread.tlfield_rpy_lasterror.setraw(err)
+            if save_err & rffi.RFFI_ALT_ERRNO:
+                rthread.tlfield_alt_lasterror.setraw(err)
+            else:
+                rthread.tlfield_rpy_lasterror.setraw(err)
         elif save_err & rffi.RFFI_SAVE_WSALASTERROR:
             from rpython.rlib import rthread, _rsocket_rffi
             err = _rsocket_rffi._WSAGetLastError()
-            rthread.tlfield_rpy_lasterror.setraw(err)
+            if save_err & rffi.RFFI_ALT_ERRNO:
+                rthread.tlfield_alt_lasterror.setraw(err)
+            else:
+                rthread.tlfield_rpy_lasterror.setraw(err)
     if save_err & rffi.RFFI_SAVE_ERRNO:
         from rpython.rlib import rthread
         if save_err & rffi.RFFI_ALT_ERRNO:
diff --git a/rpython/rlib/rthread.py b/rpython/rlib/rthread.py
--- a/rpython/rlib/rthread.py
+++ b/rpython/rlib/rthread.py
@@ -374,6 +374,7 @@
 if sys.platform == "win32":
     from rpython.rlib import rwin32
     tlfield_rpy_lasterror = ThreadLocalField(rwin32.DWORD, "rpy_lasterror")
+    tlfield_alt_lasterror = ThreadLocalField(rwin32.DWORD, "alt_lasterror")
 
 def _threadlocalref_seeme(field):
     "NOT_RPYTHON"
diff --git a/rpython/rlib/rwin32.py b/rpython/rlib/rwin32.py
--- a/rpython/rlib/rwin32.py
+++ b/rpython/rlib/rwin32.py
@@ -145,6 +145,29 @@
         from rpython.rlib import rthread
         rthread.tlfield_rpy_lasterror.setraw(rffi.cast(DWORD, err))
 
+    def GetLastError_alt_saved():
+        """Return the value of the "saved alt LastError".
+        The C-level GetLastError() is saved there after a call to a C
+        function, if that C function was declared with the flag
+        llexternal(..., save_err=RFFI_SAVE_LASTERROR | RFFI_ALT_ERRNO).
+        Functions without that flag don't change the saved LastError.
+        Alternatively, if the function was declared 
+        RFFI_SAVE_WSALASTERROR | RFFI_ALT_ERRNO,
+        then the value of the C-level WSAGetLastError() is saved instead
+        (into the same "saved alt LastError" variable).
+        """
+        from rpython.rlib import rthread
+        return rffi.cast(lltype.Signed, rthread.tlfield_alt_lasterror.getraw())
+
+    def SetLastError_alt_saved(err):
+        """Set the value of the saved alt LastError.  This value will be used 
in
+        a call to the C-level SetLastError() just before calling the
+        following C function, provided it was declared
+        llexternal(..., save_err=RFFI_READSAVED_LASTERROR | RFFI_ALT_ERRNO).
+        """
+        from rpython.rlib import rthread
+        rthread.tlfield_alt_lasterror.setraw(rffi.cast(DWORD, err))
+
     # In tests, the first call to _GetLastError() is always wrong,
     # because error is hidden by operations in ll2ctypes.  Call it now.
     _GetLastError()
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to