[pypy-svn] r38567 - in pypy/dist/pypy: interpreter module/posix module/posix/test module/rctime module/rctime/test

arigo at codespeak.net arigo at codespeak.net
Mon Feb 12 15:49:54 CET 2007


Author: arigo
Date: Mon Feb 12 15:49:52 2007
New Revision: 38567

Modified:
   pypy/dist/pypy/interpreter/mixedmodule.py
   pypy/dist/pypy/module/posix/app_posix.py
   pypy/dist/pypy/module/posix/test/test_posix2.py
   pypy/dist/pypy/module/rctime/app_time.py
   pypy/dist/pypy/module/rctime/test/test_rctime.py
Log:
Use _structseq for stat_result and struct_time.  Add pickling tests.
Remove the interp-level import of the app_* files that mixedmodule
is doing for a reason that is no longer obvious (and which fails if
the app_* file is trying to import something that implicitly comes
from pypy/lib/).


Modified: pypy/dist/pypy/interpreter/mixedmodule.py
==============================================================================
--- pypy/dist/pypy/interpreter/mixedmodule.py	(original)
+++ pypy/dist/pypy/interpreter/mixedmodule.py	Mon Feb 12 15:49:52 2007
@@ -174,17 +174,20 @@
     # hum, it's a bit more involved, because we usually 
     # want the import at applevel
     modname, attrname = spec.split('.')
-    impbase = pkgroot + '.' + modname 
-    mod = __import__(impbase, None, None, ['attrname'])
+    impbase = pkgroot + '.' + modname
     try:
-        app = applevelcache[mod]
+        app = applevelcache[impbase]
     except KeyError:
-        source = inspect.getsource(mod) 
-        fn = mod.__file__
+        import imp
+        pkg = __import__(pkgroot, None, None, ['__doc__'])
+        file, fn, (suffix, mode, typ) = imp.find_module(modname, pkg.__path__)
+        assert typ == imp.PY_SOURCE
+        source = file.read()
+        file.close()
         if fn.endswith('.pyc') or fn.endswith('.pyo'):
             fn = fn[:-1]
         app = gateway.applevel(source, filename=fn)
-        applevelcache[mod] = app
+        applevelcache[impbase] = app
 
     def afileloader(space): 
         return app.wget(space, attrname)

Modified: pypy/dist/pypy/module/posix/app_posix.py
==============================================================================
--- pypy/dist/pypy/module/posix/app_posix.py	(original)
+++ pypy/dist/pypy/module/posix/app_posix.py	Mon Feb 12 15:49:52 2007
@@ -1,30 +1,25 @@
 # NOT_RPYTHON
 
+from _structseq import structseqtype, structseqfield
+
 error = OSError
 
 
-def tuple_item_getter(n):   # helper to make properties
-    def getter(self):
-        return self[n]
-    return property(getter)
-
-
-class stat_result(tuple):
-    __slots__ = []
-
-    st_mode  = tuple_item_getter(0)
-    st_ino   = tuple_item_getter(1)
-    st_dev   = tuple_item_getter(2)
-    st_nlink = tuple_item_getter(3)
-    st_uid   = tuple_item_getter(4)
-    st_gid   = tuple_item_getter(5)
-    st_size  = tuple_item_getter(6)
-    st_atime = tuple_item_getter(7)
-    st_mtime = tuple_item_getter(8)
-    st_ctime = tuple_item_getter(9)
-    n_sequence_fields = 10
-    n_fields          = 10             # no extra attributes for now
-    n_unnamed_fields  = 0
+class stat_result:
+    __metaclass__ = structseqtype
+
+    st_mode  = structseqfield(0, "protection bits")
+    st_ino   = structseqfield(1, "inode")
+    st_dev   = structseqfield(2, "device")
+    st_nlink = structseqfield(3, "number of hard links")
+    st_uid   = structseqfield(4, "user ID of owner")
+    st_gid   = structseqfield(5, "group ID of owner")
+    st_size  = structseqfield(6, "total size, in bytes")
+    st_atime = structseqfield(7, "time of last access (XXX as an int)")
+    st_mtime = structseqfield(8, "time of last modification (XXX as an int)")
+    st_ctime = structseqfield(9, "time of last change (XXX as an int)")
+    # XXX no extra fields for now
+
 
 def fdopen(fd, mode='r', buffering=-1):
     """fdopen(fd [, mode='r' [, buffering]]) -> file_object

Modified: pypy/dist/pypy/module/posix/test/test_posix2.py
==============================================================================
--- pypy/dist/pypy/module/posix/test/test_posix2.py	(original)
+++ pypy/dist/pypy/module/posix/test/test_posix2.py	Mon Feb 12 15:49:52 2007
@@ -40,6 +40,16 @@
         posix.close(fd2)
         posix.close(fd)
 
+    def test_pickle(self):
+        import pickle, os
+        st = self.posix.stat(os.curdir)
+        print type(st).__module__
+        s = pickle.dumps(st)
+        print repr(s)
+        new = pickle.loads(s)
+        assert new == st
+        assert type(new) is type(st)
+
     def test_open_exception(self): 
         posix = self.posix
         try: 

Modified: pypy/dist/pypy/module/rctime/app_time.py
==============================================================================
--- pypy/dist/pypy/module/rctime/app_time.py	(original)
+++ pypy/dist/pypy/module/rctime/app_time.py	Mon Feb 12 15:49:52 2007
@@ -1,41 +1,23 @@
+# NOT_RPYTHON
+
 import os
+from _structseq import structseqtype, structseqfield
 
 _POSIX = os.name == "posix"
 
-class struct_time(object):
-    def __init__(self, tup):
-        if len(tup) != 9:
-            raise TypeError, "time.struct_time() takes a 9-sequence"
-
-        self._tup = tup
-        self.tm_year = self._tup[0]
-        self.tm_mon = self._tup[1]
-        self.tm_mday = self._tup[2]
-        self.tm_hour = self._tup[3]
-        self.tm_min = self._tup[4]
-        self.tm_sec = self._tup[5]
-        self.tm_wday = self._tup[6]
-        self.tm_yday = self._tup[7]
-        self.tm_isdst = self._tup[8]
-
-    def __repr__(self):
-        return "(%d, %d, %d, %d, %d, %d, %d, %d, %d)" %\
-               (self.tm_year, self.tm_mon, self.tm_mday, self.tm_hour,
-                self.tm_min, self.tm_sec, self.tm_wday, self.tm_yday,
-                self.tm_isdst)
-
-    def __len__(self):
-        return 9
-
-    def __getitem__(self, key):
-        if not isinstance(key, (int, slice)):
-            raise TypeError, "sequence index must be integer"
-        return self._tup[key]
-
-    def __cmp__(self, other):
-        if isinstance(other, struct_time):
-            return cmp(self._tup, other._tup)
-        return cmp(self._tup, other)
+class struct_time:
+    __metaclass__ = structseqtype
+    __module__ = 'time'
+
+    tm_year   = structseqfield(0)
+    tm_mon    = structseqfield(1)
+    tm_mday   = structseqfield(2)
+    tm_hour   = structseqfield(3)
+    tm_min    = structseqfield(4)
+    tm_sec    = structseqfield(5)
+    tm_wday   = structseqfield(6)
+    tm_yday   = structseqfield(7)
+    tm_isdst  = structseqfield(8)
 
 
 if _POSIX:

Modified: pypy/dist/pypy/module/rctime/test/test_rctime.py
==============================================================================
--- pypy/dist/pypy/module/rctime/test/test_rctime.py	(original)
+++ pypy/dist/pypy/module/rctime/test/test_rctime.py	Mon Feb 12 15:49:52 2007
@@ -264,3 +264,11 @@
                           'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
             format = ' %' + directive
             rctime.strptime(rctime.strftime(format, tt), format)
+
+    def test_pickle(self):
+        import pickle
+        import time as rctime
+        now = rctime.localtime()
+        new = pickle.loads(pickle.dumps(now))
+        assert new == now
+        assert type(new) is type(now)



More information about the Pypy-commit mailing list