[pypy-commit] pypy default: Try to fix an import cycle on Windows.

mjacob pypy.commits at gmail.com
Tue Feb 23 06:57:27 EST 2016


Author: Manuel Jacob <me at manueljacob.de>
Branch: 
Changeset: r82431:0116c45060a7
Date: 2016-02-23 12:50 +0100
http://bitbucket.org/pypy/pypy/changeset/0116c45060a7/

Log:	Try to fix an import cycle on Windows.

diff --git a/rpython/rlib/_os_support.py b/rpython/rlib/_os_support.py
new file mode 100644
--- /dev/null
+++ b/rpython/rlib/_os_support.py
@@ -0,0 +1,109 @@
+import sys
+
+from rpython.annotator.model import s_Str0, s_Unicode0
+from rpython.rlib import rstring
+from rpython.rlib.objectmodel import specialize
+from rpython.rtyper.lltypesystem import rffi
+
+
+_CYGWIN = sys.platform == 'cygwin'
+_WIN32 = sys.platform.startswith('win')
+UNDERSCORE_ON_WIN32 = '_' if _WIN32 else ''
+_MACRO_ON_POSIX = True if not _WIN32 else None
+
+
+class StringTraits(object):
+    str = str
+    str0 = s_Str0
+    CHAR = rffi.CHAR
+    CCHARP = rffi.CCHARP
+    charp2str = staticmethod(rffi.charp2str)
+    charpsize2str = staticmethod(rffi.charpsize2str)
+    scoped_str2charp = staticmethod(rffi.scoped_str2charp)
+    str2charp = staticmethod(rffi.str2charp)
+    free_charp = staticmethod(rffi.free_charp)
+    scoped_alloc_buffer = staticmethod(rffi.scoped_alloc_buffer)
+
+    @staticmethod
+    @specialize.argtype(0)
+    def as_str(path):
+        assert path is not None
+        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_bytes()
+
+    @staticmethod
+    @specialize.argtype(0)
+    def as_str0(path):
+        res = StringTraits.as_str(path)
+        rstring.check_str0(res)
+        return res
+
+
+class UnicodeTraits(object):
+    str = unicode
+    str0 = s_Unicode0
+    CHAR = rffi.WCHAR_T
+    CCHARP = rffi.CWCHARP
+    charp2str = staticmethod(rffi.wcharp2unicode)
+    charpsize2str = staticmethod(rffi.wcharpsize2unicode)
+    str2charp = staticmethod(rffi.unicode2wcharp)
+    scoped_str2charp = staticmethod(rffi.scoped_unicode2wcharp)
+    free_charp = staticmethod(rffi.free_wcharp)
+    scoped_alloc_buffer = staticmethod(rffi.scoped_alloc_unicodebuffer)
+
+    @staticmethod
+    @specialize.argtype(0)
+    def as_str(path):
+        assert path is not None
+        if isinstance(path, unicode):
+            return path
+        else:
+            return path.as_unicode()
+
+    @staticmethod
+    @specialize.argtype(0)
+    def as_str0(path):
+        res = UnicodeTraits.as_str(path)
+        rstring.check_str0(res)
+        return res
+
+
+string_traits = StringTraits()
+unicode_traits = UnicodeTraits()
+
+
+# Returns True when the unicode function should be called:
+# - on Windows
+# - if the path is Unicode.
+if _WIN32:
+    @specialize.argtype(0)
+    def _prefer_unicode(path):
+        assert path is not None
+        if isinstance(path, str):
+            return False
+        elif isinstance(path, unicode):
+            return True
+        else:
+            return path.is_unicode
+
+    @specialize.argtype(0)
+    def _preferred_traits(path):
+        if _prefer_unicode(path):
+            return unicode_traits
+        else:
+            return string_traits
+else:
+    @specialize.argtype(0)
+    def _prefer_unicode(path):
+        return False
+
+    @specialize.argtype(0)
+    def _preferred_traits(path):
+        return string_traits
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -1,26 +1,22 @@
 import os
 import sys
 import errno
+from rpython.annotator.model import s_Str0
 from rpython.rtyper.lltypesystem.rffi import CConstant, CExternVariable, INT
 from rpython.rtyper.lltypesystem import lltype, ll2ctypes, rffi
 from rpython.rtyper.tool import rffi_platform
-from rpython.tool.sourcetools import func_renamer
-from rpython.translator.tool.cbuild import ExternalCompilationInfo
-from rpython.rlib.rarithmetic import intmask, widen
+from rpython.rlib import debug, jit, rstring, rthread, types
+from rpython.rlib._os_support import (
+    _CYGWIN, _MACRO_ON_POSIX, UNDERSCORE_ON_WIN32, _WIN32,
+    _prefer_unicode, _preferred_traits)
 from rpython.rlib.objectmodel import (
     specialize, enforceargs, register_replacement_for, NOT_CONSTANT)
+from rpython.rlib.rarithmetic import intmask, widen
 from rpython.rlib.signature import signature
-from rpython.rlib import types
-from rpython.annotator.model import s_Str0, s_Unicode0
-from rpython.rlib import jit
+from rpython.tool.sourcetools import func_renamer
 from rpython.translator.platform import platform
-from rpython.rlib import rstring
-from rpython.rlib import debug, rthread
+from rpython.translator.tool.cbuild import ExternalCompilationInfo
 
-_WIN32 = sys.platform.startswith('win')
-_CYGWIN = sys.platform == 'cygwin'
-UNDERSCORE_ON_WIN32 = '_' if _WIN32 else ''
-_MACRO_ON_POSIX = True if not _WIN32 else None
 
 if _WIN32:
     from rpython.rlib import rwin32
@@ -118,7 +114,6 @@
     with the flag llexternal(..., save_err=rffi.RFFI_SAVE_ERRNO).
     Functions without that flag don't change the saved errno.
     """
-    from rpython.rlib import rthread
     return intmask(rthread.tlfield_rpy_errno.getraw())
 
 def set_saved_errno(errno):
@@ -129,7 +124,6 @@
     zero; for that case, use llexternal(..., save_err=RFFI_ZERO_ERRNO_BEFORE)
     and then you don't need set_saved_errno(0).
     """
-    from rpython.rlib import rthread
     rthread.tlfield_rpy_errno.setraw(rffi.cast(INT, errno))
 
 def get_saved_alterrno():
@@ -138,7 +132,6 @@
     with the flag llexternal(..., save_err=rffi.RFFI_SAVE_ERRNO | rffl.RFFI_ALT_ERRNO).
     Functions without that flag don't change the saved errno.
     """
-    from rpython.rlib import rthread
     return intmask(rthread.tlfield_alt_errno.getraw())
 
 def set_saved_alterrno(errno):
@@ -149,7 +142,6 @@
     zero; for that case, use llexternal(..., save_err=RFFI_ZERO_ERRNO_BEFORE)
     and then you don't need set_saved_errno(0).
     """
-    from rpython.rlib import rthread
     rthread.tlfield_alt_errno.setraw(rffi.cast(INT, errno))
 
 
@@ -157,7 +149,6 @@
 @specialize.call_location()
 def _errno_before(save_err):
     if save_err & rffi.RFFI_READSAVED_ERRNO:
-        from rpython.rlib import rthread
         if save_err & rffi.RFFI_ALT_ERRNO:
             _set_errno(rthread.tlfield_alt_errno.getraw())
         else:
@@ -165,7 +156,6 @@
     elif save_err & rffi.RFFI_ZERO_ERRNO_BEFORE:
         _set_errno(rffi.cast(rffi.INT, 0))
     if _WIN32 and (save_err & rffi.RFFI_READSAVED_LASTERROR):
-        from rpython.rlib import rthread, rwin32
         if save_err & rffi.RFFI_ALT_ERRNO:
             err = rthread.tlfield_alt_lasterror.getraw()
         else:
@@ -179,7 +169,6 @@
 def _errno_after(save_err):
     if _WIN32:
         if save_err & rffi.RFFI_SAVE_LASTERROR:
-            from rpython.rlib import rthread, rwin32
             err = rwin32._GetLastError()
             # careful, setraw() overwrites GetLastError.
             # We must read it first, before the errno handling.
@@ -188,14 +177,13 @@
             else:
                 rthread.tlfield_rpy_lasterror.setraw(err)
         elif save_err & rffi.RFFI_SAVE_WSALASTERROR:
-            from rpython.rlib import rthread, _rsocket_rffi
+            from rpython.rlib import _rsocket_rffi
             err = _rsocket_rffi._WSAGetLastError()
             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:
             rthread.tlfield_alt_errno.setraw(_get_errno())
         else:
@@ -341,101 +329,6 @@
     rstring.check_str0(res)
     return res
 
-
-class StringTraits:
-    str = str
-    str0 = s_Str0
-    CHAR = rffi.CHAR
-    CCHARP = rffi.CCHARP
-    charp2str = staticmethod(rffi.charp2str)
-    charpsize2str = staticmethod(rffi.charpsize2str)
-    scoped_str2charp = staticmethod(rffi.scoped_str2charp)
-    str2charp = staticmethod(rffi.str2charp)
-    free_charp = staticmethod(rffi.free_charp)
-    scoped_alloc_buffer = staticmethod(rffi.scoped_alloc_buffer)
-
-    @staticmethod
-    @specialize.argtype(0)
-    def as_str(path):
-        assert path is not None
-        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_bytes()    
-
-    @staticmethod
-    @specialize.argtype(0)
-    def as_str0(path):
-        res = StringTraits.as_str(path)
-        rstring.check_str0(res)
-        return res
-
-
-class UnicodeTraits:
-    str = unicode
-    str0 = s_Unicode0
-    CHAR = rffi.WCHAR_T
-    CCHARP = rffi.CWCHARP
-    charp2str = staticmethod(rffi.wcharp2unicode)
-    charpsize2str = staticmethod(rffi.wcharpsize2unicode)
-    str2charp = staticmethod(rffi.unicode2wcharp)
-    scoped_str2charp = staticmethod(rffi.scoped_unicode2wcharp)
-    free_charp = staticmethod(rffi.free_wcharp)
-    scoped_alloc_buffer = staticmethod(rffi.scoped_alloc_unicodebuffer)
-
-    @staticmethod
-    @specialize.argtype(0)
-    def as_str(path):
-        assert path is not None
-        if isinstance(path, unicode):
-            return path
-        else:
-            return path.as_unicode()
-    
-    @staticmethod
-    @specialize.argtype(0)
-    def as_str0(path):
-        res = UnicodeTraits.as_str(path)
-        rstring.check_str0(res)
-        return res
-
-
-# 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):
-        assert path is not None
-        if isinstance(path, str):
-            return False
-        elif isinstance(path, unicode):
-            return True
-        else:
-            return path.is_unicode
-
-    @specialize.argtype(0)
-    def _preferred_traits(path):
-        if _prefer_unicode(path):
-            return unicode_traits
-        else:
-            return string_traits
-else:
-    @specialize.argtype(0)
-    def _prefer_unicode(path):
-        return False
-
-    @specialize.argtype(0)
-    def _preferred_traits(path):
-        return string_traits
-
 @specialize.argtype(0, 1)
 def putenv(name, value):
     os.environ[_as_bytes(name)] = _as_bytes(value)
diff --git a/rpython/rlib/rposix_environ.py b/rpython/rlib/rposix_environ.py
--- a/rpython/rlib/rposix_environ.py
+++ b/rpython/rlib/rposix_environ.py
@@ -1,8 +1,9 @@
 import os
 import sys
 from rpython.annotator import model as annmodel
+from rpython.rlib._os_support import _WIN32, StringTraits, UnicodeTraits
 from rpython.rlib.objectmodel import enforceargs
-from rpython.rlib.rposix import _WIN32, StringTraits, UnicodeTraits
+# importing rposix here creates a cycle on Windows
 from rpython.rtyper.controllerentry import Controller
 from rpython.rtyper.extfunc import register_external
 from rpython.rtyper.lltypesystem import rffi, lltype
diff --git a/rpython/rlib/rposix_stat.py b/rpython/rlib/rposix_stat.py
--- a/rpython/rlib/rposix_stat.py
+++ b/rpython/rlib/rposix_stat.py
@@ -16,13 +16,13 @@
 from rpython.rtyper.rint import IntegerRepr
 from rpython.rtyper.error import TyperError
 
+from rpython.rlib._os_support import _preferred_traits, string_traits
 from rpython.rlib.objectmodel import specialize
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 from rpython.rlib.rarithmetic import intmask
 from rpython.rlib.rposix import (
-    replace_os_function, handle_posix_error, _as_bytes0,
-    _preferred_traits, string_traits)
+    replace_os_function, handle_posix_error, _as_bytes0)
 
 _WIN32 = sys.platform.startswith('win')
 _LINUX = sys.platform.startswith('linux')


More information about the pypy-commit mailing list