[pypy-svn] r15186 - in pypy/dist/pypy: interpreter interpreter/test module/__builtin__

arigo at codespeak.net arigo at codespeak.net
Wed Jul 27 14:48:26 CEST 2005


Author: arigo
Date: Wed Jul 27 14:48:21 2005
New Revision: 15186

Modified:
   pypy/dist/pypy/interpreter/pycompiler.py
   pypy/dist/pypy/interpreter/test/test_compiler.py
   pypy/dist/pypy/module/__builtin__/compiling.py
Log:
Properly implemented the built-in compile() on unicode sources.
(The pyparser works fine on encoded strings, processing the
-*- coding -*- declaration.)



Modified: pypy/dist/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycompiler.py	(original)
+++ pypy/dist/pypy/interpreter/pycompiler.py	Wed Jul 27 14:48:21 2005
@@ -178,11 +178,6 @@
          the whole source after having only added a new '\n')
     """
     def compile(self, source, filename, mode, flags):
-        assert isinstance(source, str) # xxx__builtin__.compile is cheating in the unicode case
-                                       # we need to do something about that
-                                       # CPython encode unicode for compilation into utf-8
-                                       # and use a special internal flag to control behavior!
-    
         from pyparser.error import ParseError
         from pyparser.pythonutil import internal_pypy_parse
         flags |= __future__.generators.compiler_flag   # always on (2.2 compat)

Modified: pypy/dist/pypy/interpreter/test/test_compiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_compiler.py	(original)
+++ pypy/dist/pypy/interpreter/test/test_compiler.py	Wed Jul 27 14:48:21 2005
@@ -17,6 +17,10 @@
         w_res = code.exec_code(space, space.newdict([]), space.newdict([]))
         assert space.int_w(w_res) == 42
 
+    def test_eval_unicode(self):
+        assert (eval(unicode('u"\xc3\xa5"', 'utf8')) ==
+                unicode('\xc3\xa5', 'utf8'))
+
     def test_compile_command(self):
         c0 = self.compiler.compile_command('\t # hello\n ', '?', 'exec', 0)
         c1 = self.compiler.compile_command('print 6*7', '?', 'exec', 0)

Modified: pypy/dist/pypy/module/__builtin__/compiling.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/compiling.py	(original)
+++ pypy/dist/pypy/module/__builtin__/compiling.py	Wed Jul 27 14:48:21 2005
@@ -9,7 +9,11 @@
 
 def compile(space, w_source, filename, mode, flags=0, dont_inherit=0):
     if space.is_true(space.isinstance(w_source, space.w_unicode)):
-        str_ = space.unwrap(w_source) # Bad exposing of unicode internals
+        # hack: encode the unicode string as UTF-8 and attach a 'coding'
+        # declaration at the start
+        w_source = space.call_method(w_source, 'encode', space.wrap('utf-8'))
+        str_ = space.str_w(w_source)
+        str_ = "# -*- coding: utf-8 -*-\n" + str_
     else:
         str_ = space.str_w(w_source)
 



More information about the Pypy-commit mailing list