[pypy-svn] r48490 - in pypy/branch/unicode-objspace/pypy: interpreter module/sys module/sys/test objspace/std

cfbolz at codespeak.net cfbolz at codespeak.net
Sat Nov 10 02:13:47 CET 2007


Author: cfbolz
Date: Sat Nov 10 02:13:44 2007
New Revision: 48490

Added:
   pypy/branch/unicode-objspace/pypy/module/sys/interp_encoding.py   (contents, props changed)
Modified:
   pypy/branch/unicode-objspace/pypy/interpreter/baseobjspace.py
   pypy/branch/unicode-objspace/pypy/module/sys/__init__.py
   pypy/branch/unicode-objspace/pypy/module/sys/app.py
   pypy/branch/unicode-objspace/pypy/module/sys/test/test_sysmodule.py
   pypy/branch/unicode-objspace/pypy/objspace/std/unicodetype.py
Log:
attach the default-encoding to the space for easier accessibility. not sure
this is a very nice solution, but I think it is way better than a global in an
applevel implementation module in the sys mixed-module.


Modified: pypy/branch/unicode-objspace/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/branch/unicode-objspace/pypy/interpreter/baseobjspace.py	Sat Nov 10 02:13:44 2007
@@ -188,6 +188,7 @@
         self.interned_strings = {}
         self.pending_actions = []
         self.setoptions(**kw)
+        self.defaultencoding = "ascii"
 
 #        if self.config.objspace.logbytecodes:
 #            self.bytecodecounts = {}

Modified: pypy/branch/unicode-objspace/pypy/module/sys/__init__.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/module/sys/__init__.py	(original)
+++ pypy/branch/unicode-objspace/pypy/module/sys/__init__.py	Sat Nov 10 02:13:44 2007
@@ -64,6 +64,9 @@
         'path_hooks'            : 'space.wrap([])',
         'path_importer_cache'   : 'space.wrap({})',
         #'subversion'           : added in Python 2.5
+        
+        'getdefaultencoding'    : 'interp_encoding.getdefaultencoding', 
+        'setdefaultencoding'    : 'interp_encoding.setdefaultencoding', 
 }
     appleveldefs = {
         #'displayhook'           : 'app.displayhook', 
@@ -75,8 +78,6 @@
         'pypy__exithandlers__'  : 'app.pypy__exithandlers__',  # internal
         'getfilesystemencoding' : 'app.getfilesystemencoding', 
         'callstats'             : 'app.callstats',
-        'getdefaultencoding'    : 'app.getdefaultencoding', 
-        'setdefaultencoding'    : 'app.setdefaultencoding', 
     }
 
     def setbuiltinmodule(self, w_module, name): 

Modified: pypy/branch/unicode-objspace/pypy/module/sys/app.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/module/sys/app.py	(original)
+++ pypy/branch/unicode-objspace/pypy/module/sys/app.py	Sat Nov 10 02:13:44 2007
@@ -46,17 +46,3 @@
     """Not implemented."""
     return None
 
-defaultencoding = 'ascii'
-
-def getdefaultencoding():
-    """Return the current default string encoding used by the Unicode 
-implementation."""
-    return defaultencoding
-
-def setdefaultencoding(encoding):
-    """Set the current default string encoding used by the Unicode 
-implementation."""
-    global defaultencoding
-    import codecs
-    codecs.lookup(encoding)
-    defaultencoding = encoding

Added: pypy/branch/unicode-objspace/pypy/module/sys/interp_encoding.py
==============================================================================
--- (empty file)
+++ pypy/branch/unicode-objspace/pypy/module/sys/interp_encoding.py	Sat Nov 10 02:13:44 2007
@@ -0,0 +1,15 @@
+
+def getdefaultencoding(space):
+    """Return the current default string encoding used by the Unicode 
+implementation."""
+    return space.wrap(space.defaultencoding)
+
+def setdefaultencoding(space, w_encoding):
+    """Set the current default string encoding used by the Unicode 
+implementation."""
+    encoding = space.str_w(w_encoding)
+    mod = space.getbuiltinmodule("_codecs")
+    w_lookup = space.getattr(mod, space.wrap("lookup"))
+    # check whether the encoding is there
+    space.call_function(w_lookup, w_encoding)
+    space.defaultencoding = encoding

Modified: pypy/branch/unicode-objspace/pypy/module/sys/test/test_sysmodule.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/module/sys/test/test_sysmodule.py	(original)
+++ pypy/branch/unicode-objspace/pypy/module/sys/test/test_sysmodule.py	Sat Nov 10 02:13:44 2007
@@ -246,6 +246,16 @@
         raises(TypeError, sys.getdefaultencoding, 42)
         # can't check more than the type, as the user might have changed it
         assert isinstance(sys.getdefaultencoding(), str)
+
+    def test_getdefaultencoding(self):
+        encoding = sys.getdefaultencoding()
+        sys.setdefaultencoding("ascii")
+        try:
+            assert sys.getdefaultencoding() == 'ascii'
+            raises(UnicodeDecodeError, unicode, '\x80')
+        finally:
+            sys.setdefaultencoding(encoding)
+
             
     # testing sys.settrace() is done in test_trace.py
     # testing sys.setprofile() is done in test_profile.py

Modified: pypy/branch/unicode-objspace/pypy/objspace/std/unicodetype.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/objspace/std/unicodetype.py	(original)
+++ pypy/branch/unicode-objspace/pypy/objspace/std/unicodetype.py	Sat Nov 10 02:13:44 2007
@@ -184,16 +184,15 @@
     s = space.str_w(w_str)
     try:
         return W_UnicodeObject(s.decode("ascii"))
-    except UnicodeDecodeErrors:
-        # raising UnicodeDecodeErrors is messy, "please crash for me"
+    except UnicodeDecodeError:
+        # raising UnicodeDecodeError is messy, "please crash for me"
         return unicode_from_object(space, w_str)
 
 
-def descr__new__(space, w_unicodetype, w_string='', w_encoding=None, w_errors=None):
-    # NB. the default value of w_string is really a *wrapped* empty string:
+def descr__new__(space, w_unicodetype, w_obj='', w_encoding=None, w_errors=None):
+    # NB. the default value of w_obj is really a *wrapped* empty string:
     #     there is gateway magic at work
     from pypy.objspace.std.unicodeobject import W_UnicodeObject
-    w_obj = w_string
     w_obj_type = space.type(w_obj)
     
     if space.is_w(w_obj_type, space.w_unicode):



More information about the Pypy-commit mailing list