[pypy-svn] r9914 - pypy/dist/pypy/module/builtin

arigo at codespeak.net arigo at codespeak.net
Sun Mar 20 01:17:42 CET 2005


Author: arigo
Date: Sun Mar 20 01:17:41 2005
New Revision: 9914

Added:
   pypy/dist/pypy/module/builtin/app_buffer.py   (contents, props changed)
Modified:
   pypy/dist/pypy/module/builtin/__init__.py
   pypy/dist/pypy/module/builtin/app_misc.py
Log:
A limited implementation of buffer().


Modified: pypy/dist/pypy/module/builtin/__init__.py
==============================================================================
--- pypy/dist/pypy/module/builtin/__init__.py	(original)
+++ pypy/dist/pypy/module/builtin/__init__.py	Sun Mar 20 01:17:41 2005
@@ -51,7 +51,7 @@
 
         'intern'        : 'app_misc.intern',
         'unichr'        : 'app_misc.unichr',
-        'buffer'        : 'app_misc.buffer',
+        'buffer'        : 'app_buffer.buffer',
         'reload'        : 'app_misc.reload',
     }
 

Added: pypy/dist/pypy/module/builtin/app_buffer.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/builtin/app_buffer.py	Sun Mar 20 01:17:41 2005
@@ -0,0 +1,46 @@
+# Might probably be deprecated in Python at some point.
+
+class buffer(object):
+    """buffer(object [, offset[, size]])
+
+Create a new buffer object which references the given object.
+The buffer will reference a slice of the target object from the
+start of the object (or at the specified offset). The slice will
+extend to the end of the target object (or with the specified size).
+"""
+
+    def __init__(self, object, offset=0, size=None):
+        if isinstance(object, str):
+            pass
+        elif isinstance(object, buffer):
+            object = object.buf
+        else:
+            # XXX check for more types
+            raise TypeError, "buffer object expected"
+        if size is None:
+            self.buf = object[offset:]
+        else:
+            self.buf = object[offset:offset+size]
+
+    def __str__(self):
+        return self.buf
+
+    def __add__(self, other):
+        return self.buf + buffer(other).buf
+
+    def __mul__(self, count):
+        return self.buf * count
+
+    __rmul__ = __mul__
+
+    def __cmp__(self, other):
+        return cmp(self.buf, buffer(other).buf)
+
+    def __getitem__(self, index_or_slice):
+        return self.buf[index_or_slice]
+
+    def __hash__(self):
+        return hash(self.buf)
+
+    def __len__(self):
+        return len(self.buf)

Modified: pypy/dist/pypy/module/builtin/app_misc.py
==============================================================================
--- pypy/dist/pypy/module/builtin/app_misc.py	(original)
+++ pypy/dist/pypy/module/builtin/app_misc.py	Sun Mar 20 01:17:41 2005
@@ -18,11 +18,6 @@
     return unicode('\\U%08x' %(code), 'unicode-escape')
 
 
-class buffer(object):
-    def __init__(self, object, offset=None, size=None):
-        raise NotImplementedError, "XXX nobody needs this anyway"
-
-
 def reload(module):
     import imp, sys, errno
 



More information about the Pypy-commit mailing list