[pypy-commit] pypy more-rposix: Lots of translation fixes for Windows

amauryfa noreply at buildbot.pypy.org
Fri Nov 7 23:58:28 CET 2014


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: more-rposix
Changeset: r74395:ab68fdb236c0
Date: 2014-11-07 23:57 +0100
http://bitbucket.org/pypy/pypy/changeset/ab68fdb236c0/

Log:	Lots of translation fixes for Windows

diff --git a/pypy/module/posix/interp_posix.py b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -431,11 +431,11 @@
     try:
         if space.isinstance_w(w_path, space.w_unicode):
             path = FileEncoder(space, w_path)
-            fullpath = rposix._getfullpathname(path)
+            fullpath = rposix.getfullpathname(path)
             w_fullpath = space.wrap(fullpath)
         else:
             path = space.str0_w(w_path)
-            fullpath = rposix._getfullpathname(path)
+            fullpath = rposix.getfullpathname(path)
             w_fullpath = space.wrap(fullpath)
     except OSError, e:
         raise wrap_oserror2(space, e, w_path)
diff --git a/rpython/flowspace/objspace.py b/rpython/flowspace/objspace.py
--- a/rpython/flowspace/objspace.py
+++ b/rpython/flowspace/objspace.py
@@ -18,7 +18,7 @@
     if func.func_code.co_cellvars:
         raise ValueError(
 """RPython functions cannot create closures
-Possible casues:
+Possible causes:
     Function is inner function
     Function uses generator expressions
     Lambda expressions
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -3,6 +3,7 @@
 import errno
 from rpython.rtyper.lltypesystem.rffi import CConstant, CExternVariable, INT
 from rpython.rtyper.lltypesystem import lltype, ll2ctypes, rffi
+from rpython.rtyper.module.support import StringTraits, UnicodeTraits
 from rpython.rtyper.tool import rffi_platform
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 from rpython.rlib.rarithmetic import intmask, widen
@@ -234,6 +235,8 @@
 # Returns True when the unicode function should be called:
 # - on Windows
 # - if the path is Unicode.
+unicode_traits = UnicodeTraits()
+string_traits = StringTraits()
 if _WIN32:
     @specialize.argtype(0)
     def _prefer_unicode(path):
@@ -246,11 +249,10 @@
 
     @specialize.argtype(0)
     def _preferred_traits(path):
-        from rpython.rtyper.module.support import StringTraits, UnicodeTraits
         if _prefer_unicode(path):
-            return UnicodeTraits()
+            return unicode_traits
         else:
-            return StringTraits()
+            return string_traits
 else:
     @specialize.argtype(0)
     def _prefer_unicode(path):
@@ -258,8 +260,7 @@
 
     @specialize.argtype(0)
     def _preferred_traits(path):
-        from rpython.rtyper.module.support import StringTraits
-        return StringTraits()
+        return string_traits
     
 @specialize.argtype(0)
 def stat(path):
@@ -311,12 +312,6 @@
 def symlink(src, dest):
     os.symlink(_as_bytes(src), _as_bytes(dest))
 
-if os.name == 'nt':
-    import nt
-    @specialize.argtype(0)
-    def _getfullpathname(path):
-        return nt._getfullpathname(_as_bytes(path))
-
 @specialize.argtype(0, 1)
 def putenv(name, value):
     os.environ[_as_bytes(name)] = _as_bytes(value)
@@ -464,14 +459,16 @@
 @specialize.argtype(0)
 def getfullpathname(path):
     length = rwin32.MAX_PATH + 1
-    traits = _get_preferred_traits(path)
-    with traits.scoped_alloc_buffer(count) as buf:
+    traits = _preferred_traits(path)
+    from rpython.rlib.rwin32file import make_win32_traits
+    win32traits = make_win32_traits(traits)
+    with traits.scoped_alloc_buffer(length) as buf:
         res = win32traits.GetFullPathName(
-            path, rffi.cast(rwin32.DWORD, length),
+            traits.as_str0(path), rffi.cast(rwin32.DWORD, length),
             buf.raw, lltype.nullptr(win32traits.LPSTRP.TO))
         if res == 0:
             raise rwin32.lastWindowsError("_getfullpathname failed")
-        return buf.str()
+        return buf.str(intmask(res))
 
 c_getcwd = external(UNDERSCORE_ON_WIN32 + 'getcwd',
                     [rffi.CCHARP, rffi.SIZE_T], rffi.CCHARP)
@@ -506,7 +503,7 @@
         res = c_wgetcwd(buf, bufsize)
         if res:
             break   # ok
-        error = rposix.get_errno()
+        error = get_errno()
         lltype.free(buf, flavor='raw')
         if error != errno.ERANGE:
             raise OSError(error, "getcwd failed")
@@ -612,7 +609,7 @@
 c_spawnv = external('spawnv',
                     [rffi.INT, rffi.CCHARP, rffi.CCHARPP], rffi.INT)
 c_spawnve = external('spawnve',
-                    [rffi.INT, rffi.CCHARP, rffi.CCHARPP, rffi.CCHARP],
+                    [rffi.INT, rffi.CCHARP, rffi.CCHARPP, rffi.CCHARPP],
                      rffi.INT)
 
 @replace_os_function('execv')
@@ -829,7 +826,7 @@
                 lltype.free(l_utimbuf, flavor='raw')
         handle_posix_error('utime', error)
     else:  # _WIN32 case
-        from rpython.rlib.rwin32file import make_win32_traits
+        from rpython.rlib.rwin32file import make_win32_traits, time_t_to_FILE_TIME
         traits = _preferred_traits(path)
         win32traits = make_win32_traits(traits)
         path = traits.as_str0(path)
@@ -844,7 +841,7 @@
         atime = lltype.malloc(rwin32.FILETIME, flavor='raw')
         mtime = lltype.malloc(rwin32.FILETIME, flavor='raw')
         try:
-            if tp is None:
+            if times is None:
                 now = lltype.malloc(rwin32.SYSTEMTIME, flavor='raw')
                 try:
                     GetSystemTime(now)
@@ -854,7 +851,7 @@
                 finally:
                     lltype.free(now, flavor='raw')
             else:
-                actime, modtime = tp
+                actime, modtime = times
                 time_t_to_FILE_TIME(actime, atime)
                 time_t_to_FILE_TIME(modtime, mtime)
             if not SetFileTime(hFile, ctime, atime, mtime):
@@ -1125,22 +1122,23 @@
 def chroot(path):
     handle_posix_error('chroot', c_chroot(_as_bytes0(path)))
 
-CHARARRAY1 = lltype.FixedSizeArray(lltype.Char, 1)
-class CConfig:
-    _compilation_info_ = ExternalCompilationInfo(
-        includes = ['sys/utsname.h']
-    )
-    UTSNAME = rffi_platform.Struct('struct utsname', [
-        ('sysname',  CHARARRAY1),
-        ('nodename', CHARARRAY1),
-        ('release',  CHARARRAY1),
-        ('version',  CHARARRAY1),
-        ('machine',  CHARARRAY1)])
-config = rffi_platform.configure(CConfig)
-UTSNAMEP = lltype.Ptr(config['UTSNAME'])
+if not _WIN32:
+    CHARARRAY1 = lltype.FixedSizeArray(lltype.Char, 1)
+    class CConfig:
+        _compilation_info_ = ExternalCompilationInfo(
+            includes = ['sys/utsname.h']
+        )
+        UTSNAME = rffi_platform.Struct('struct utsname', [
+            ('sysname',  CHARARRAY1),
+            ('nodename', CHARARRAY1),
+            ('release',  CHARARRAY1),
+            ('version',  CHARARRAY1),
+            ('machine',  CHARARRAY1)])
+    config = rffi_platform.configure(CConfig)
+    UTSNAMEP = lltype.Ptr(config['UTSNAME'])
 
-c_uname = external('uname', [UTSNAMEP], rffi.INT,
-                    compilation_info=CConfig._compilation_info_)
+    c_uname = external('uname', [UTSNAMEP], rffi.INT,
+                        compilation_info=CConfig._compilation_info_)
 
 @replace_os_function('uname')
 def uname():
diff --git a/rpython/rlib/rwin32file.py b/rpython/rlib/rwin32file.py
--- a/rpython/rlib/rwin32file.py
+++ b/rpython/rlib/rwin32file.py
@@ -5,8 +5,9 @@
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 from rpython.rtyper.tool import rffi_platform as platform
+from rpython.rlib.objectmodel import specialize
 
-
+ at specialize.memo()
 def make_win32_traits(traits):
     from rpython.rlib import rwin32
 
@@ -190,3 +191,20 @@
             rwin32.BOOL)
 
     return Win32Traits
+
+def make_longlong(high, low):
+    return (rffi.r_longlong(high) << 32) + rffi.r_longlong(low)
+
+# Seconds between 1.1.1601 and 1.1.1970
+secs_between_epochs = rffi.r_longlong(11644473600)
+
+def FILE_TIME_to_time_t_float(filetime):
+    ft = make_longlong(filetime.c_dwHighDateTime, filetime.c_dwLowDateTime)
+    # FILETIME is in units of 100 nsec
+    return float(ft) * (1.0 / 10000000.0) - secs_between_epochs
+
+def time_t_to_FILE_TIME(time, filetime):
+    ft = rffi.r_longlong((time + secs_between_epochs) * 10000000)
+    filetime.c_dwHighDateTime = rffi.r_uint(ft >> 32)
+    filetime.c_dwLowDateTime = rffi.r_uint(ft)    # masking off high bits
+
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
@@ -20,7 +20,7 @@
 from rpython.rtyper.lltypesystem import rffi
 from rpython.rtyper.lltypesystem import lltype
 from rpython.rtyper.tool import rffi_platform as platform
-from rpython.rlib import rposix
+from rpython.rlib import rposix, rwin32
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 from rpython.rlib.objectmodel import specialize
 from rpython.translator import cdir
diff --git a/rpython/rtyper/module/support.py b/rpython/rtyper/module/support.py
--- a/rpython/rtyper/module/support.py
+++ b/rpython/rtyper/module/support.py
@@ -3,6 +3,7 @@
 from rpython.annotator import model as annmodel
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.rlib.objectmodel import specialize
+from rpython.rlib import rstring
 
 _WIN32 = sys.platform.startswith('win')
 UNDERSCORE_ON_WIN32 = '_' if _WIN32 else ''
@@ -64,19 +65,24 @@
     def ll_os_name(name):
         return 'll_os.ll_os_' + name
 
-    @classmethod
-    @specialize.argtype(1)
-    def as_str(cls, path):
+    @staticmethod
+    @specialize.argtype(0)
+    def as_str(path):
         assert path is not None
-        if isinstance(path, unicode):
+        if isinstance(path, str):
             return path
+        elif isinstance(path, unicode):
+            # This never happens in PyPy's Python interpreter!
+            # Only in raw RPython code that uses unicode strings.
+            # We implement python2 behavior: silently convert to ascii.
+            return path.encode('ascii')
         else:
-            return path.as_unicode()
-    
-    @classmethod
-    @specialize.argtype(1)
-    def as_str0(cls, path):
-        res = cls.as_str(path)
+            return path.as_bytes()    
+
+    @staticmethod
+    @specialize.argtype(0)
+    def as_str0(path):
+        res = StringTraits.as_str(path)
         rstring.check_str0(res)
         return res
 
@@ -97,25 +103,22 @@
         return UNDERSCORE_ON_WIN32 + 'w' + name
 
     @staticmethod
+    @specialize.argtype(0)
     def ll_os_name(name):
         return 'll_os.ll_os_w' + name
 
-    @classmethod
-    def as_str(cls, path):
+    @staticmethod
+    @specialize.argtype(0)
+    def as_str(path):
         assert path is not None
-        if isinstance(path, str):
+        if isinstance(path, unicode):
             return path
-        elif isinstance(path, unicode):
-            # This never happens in PyPy's Python interpreter!
-            # Only in raw RPython code that uses unicode strings.
-            # We implement python2 behavior: silently convert to ascii.
-            return path.encode('ascii')
         else:
-            return path.as_bytes()
+            return path.as_unicode()
     
-    @classmethod
-    def as_str0(cls, path):
-        res = cls.as_str(path)
+    @staticmethod
+    def as_str0(path):
+        res = UnicodeTraits.as_str(path)
         rstring.check_str0(res)
         return res
 


More information about the pypy-commit mailing list