Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r51469:8b47ef4c3162
Date: 2012-01-18 22:09 +0100
http://bitbucket.org/pypy/pypy/changeset/8b47ef4c3162/

Log:    Fixes for the struct module: the builtin part is now named _struct.

diff --git a/lib_pypy/struct.py b/lib_pypy/_struct.py
rename from lib_pypy/struct.py
rename to lib_pypy/_struct.py
--- a/lib_pypy/struct.py
+++ b/lib_pypy/_struct.py
@@ -51,7 +51,7 @@
     bytes = [b for b in data[index:index+size]]
     if le == 'little':
         bytes.reverse()
-    number = 0L
+    number = 0
     for b in bytes:
         number = number << 8 | b
     return int(number)
@@ -415,3 +415,7 @@
         raise error("unpack_from requires a buffer of at least %d bytes"
                     % (size,))
     return unpack(fmt, data)
+
+def _clearcache():
+    "Clear the internal cache."
+    # No cache in this implementation
diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1280,11 +1280,14 @@
         return self.str_w(w_obj)
 
     def str_w(self, w_obj):
-        try:
-            return self.unicode_w(w_obj).encode('ascii')
-        except UnicodeEncodeError:
-            w_bytes = self.call_method(w_obj, 'encode', self.wrap('utf-8'))
-            return self.bytes_w(w_bytes)
+        if self.isinstance_w(w_obj, self.w_unicode):
+            try:
+                return self.unicode_w(w_obj).encode('ascii')
+            except UnicodeEncodeError:
+                w_bytes = self.call_method(w_obj, 'encode', self.wrap('utf-8'))
+                return self.bytes_w(w_bytes)
+        else:
+            return w_obj.bytes_w(self)
 
     def bytes_w(self, w_obj):
         return w_obj.bytes_w(self)
diff --git a/pypy/module/struct/__init__.py b/pypy/module/struct/__init__.py
--- a/pypy/module/struct/__init__.py
+++ b/pypy/module/struct/__init__.py
@@ -45,10 +45,13 @@
 
 The variable struct.error is an exception raised on errors."""
 
+    applevel_name = '_struct'
+
     interpleveldefs = {
         'calcsize': 'interp_struct.calcsize',
         'pack': 'interp_struct.pack',
         'unpack': 'interp_struct.unpack',
+        '_clearcache': 'interp_struct.clearcache',
         }
 
     appleveldefs = {
diff --git a/pypy/module/struct/app_struct.py b/pypy/module/struct/app_struct.py
--- a/pypy/module/struct/app_struct.py
+++ b/pypy/module/struct/app_struct.py
@@ -2,7 +2,7 @@
 """
 Application-level definitions for the struct module.
 """
-import struct
+import _struct as struct
 
 class error(Exception):
     """Exception raised on various occasions; argument is a string
@@ -15,7 +15,7 @@
 
 # XXX inefficient
 def unpack_from(fmt, buf, offset=0):
-    size = struct.calcsize(fmt)
+    size = _struct.calcsize(fmt)
     data = buffer(buf)[offset:offset+size]
     if len(data) != size:
         raise error("unpack_from requires a buffer of at least %d bytes"
diff --git a/pypy/module/struct/interp_struct.py 
b/pypy/module/struct/interp_struct.py
--- a/pypy/module/struct/interp_struct.py
+++ b/pypy/module/struct/interp_struct.py
@@ -33,3 +33,7 @@
     except StructError, e:
         raise e.at_applevel(space)
     return space.newtuple(fmtiter.result_w[:])
+
+def clearcache(space):
+    "Clear the internal cache."
+    # No cache in this implementation
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to