fix ticket #576: builtin 'str' type must be 'unicode' type in -3 mode
authorStefan Behnel <scoder@users.berlios.de>
Thu, 16 Dec 2010 19:46:07 +0000 (20:46 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Thu, 16 Dec 2010 19:46:07 +0000 (20:46 +0100)
Cython/Compiler/Symtab.py
tests/run/cython3.pyx

index 4573b3c38281a587b957b4ecaa7b2a1fccf4452d..c868349903cd79836129386050ed4feb50fad005 100644 (file)
@@ -733,6 +733,13 @@ class BuiltinScope(Scope):
             cname, type = definition
             self.declare_var(name, type, None, cname)
 
+    def lookup(self, name, language_level=None):
+        # 'language_level' is passed by ModuleScope
+        if language_level == 3:
+            if name == 'str':
+                name = 'unicode'
+        return Scope.lookup(self, name)
+
     def declare_builtin(self, name, pos):
         if not hasattr(builtins, name):
             if self.outer_scope is not None:
@@ -882,6 +889,12 @@ class ModuleScope(Scope):
     def global_scope(self):
         return self
 
+    def lookup(self, name):
+        entry = self.lookup_here(name)
+        if entry is not None:
+            return entry
+        return self.outer_scope.lookup(name, language_level = self.context.language_level)
+
     def declare_builtin(self, name, pos):
         if not hasattr(builtins, name) and name != 'xrange':
             # 'xrange' is special cased in Code.py
index 0d10bdbde82e530249f266770987bc708939192f..d3f416e8384524a9e0e8b9da8759e00880835feb 100644 (file)
@@ -66,6 +66,19 @@ def unicode_literals():
     print(isinstance(ustring, unicode) or type(ustring))
     return ustring
 
+def str_type_is_unicode():
+    """
+    >>> str_type, s = str_type_is_unicode()
+    >>> isinstance(s, type(ustring)) or (s, str_type)
+    True
+    >>> isinstance(s, str_type) or (s, str_type)
+    True
+    >>> isinstance(ustring, str_type) or str_type
+    True
+    """
+    cdef str s = 'abc'
+    return str, s
+
 def list_comp():
     """
     >>> list_comp()