Author: Philip Jenvey <pjen...@underboss.org>
Branch: 
Changeset: r65490:2997e5b63e2e
Date: 2013-07-19 16:08 -0700
http://bitbucket.org/pypy/pypy/changeset/2997e5b63e2e/

Log:    convert to formal app level tests so they have a chance of running
        on the py3k branch. they'll run slower but that can be avoided w/
        appdirect mode

diff --git a/pypy/module/test_lib_pypy/support.py 
b/pypy/module/test_lib_pypy/support.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/support.py
@@ -0,0 +1,34 @@
+import py
+
+from pypy.conftest import option
+from pypy.interpreter.error import OperationError
+
+def import_lib_pypy(space, name, skipmsg=None):
+    """Import a top level module ensuring it's sourced from the lib_pypy
+    package.
+
+    Raises a pytest Skip on ImportError if a skip message was specified.
+    """
+    if option.runappdirect:
+        try:
+            mod = __import__('lib_pypy.' + name)
+        except ImportError as e:
+            if skipmsg is not None:
+                py.test.skip('%s (%s))' % (skipmsg, str(e)))
+            raise
+        return getattr(mod, name)
+
+    try:
+        # Assume app-level import finds it from the right place (we
+        # assert so afterwards). It should as long as a builtin module
+        # overshadows it
+        w_mod = space.appexec([], "(): import %s; return %s" % (name, name))
+    except OperationError as e:
+        if skipmsg is not None or not e.match(space, space.w_ImportError):
+            raise
+        py.test.skip('%s (%s))' % (skipmsg, str(e)))
+    w_file = space.getattr(w_mod, space.wrap('__file__'))
+    assert space.is_true(space.contains(w_file, space.wrap('lib_pypy'))), \
+        ("%s didn't import from lib_pypy. Is a usemodules directive "
+         "overshadowing it?" % name)
+    return w_mod
diff --git a/pypy/module/test_lib_pypy/test_grp_extra.py 
b/pypy/module/test_lib_pypy/test_grp_extra.py
--- a/pypy/module/test_lib_pypy/test_grp_extra.py
+++ b/pypy/module/test_lib_pypy/test_grp_extra.py
@@ -1,26 +1,32 @@
-from __future__ import absolute_import
-import py
-try:
-    from lib_pypy import grp
-except ImportError:
-    py.test.skip("No grp module on this platform")
+from pypy.module.test_lib_pypy.support import import_lib_pypy
 
-def test_basic():
-    g = grp.getgrnam("root")
-    assert g.gr_gid == 0
-    assert g.gr_mem == ['root'] or g.gr_mem == []
-    assert g.gr_name == 'root'
-    assert isinstance(g.gr_passwd, str)    # usually just 'x', don't hope :-)
 
-def test_extra():
-    py.test.raises(TypeError, grp.getgrnam, False)
-    py.test.raises(TypeError, grp.getgrnam, None)
+class AppTestGrp:
 
-def test_struct_group():
-    g = grp.struct_group((10, 20, 30, 40))
-    assert len(g) == 4
-    assert list(g) == [10, 20, 30, 40]
-    assert g.gr_name == 10
-    assert g.gr_passwd == 20
-    assert g.gr_gid == 30
-    assert g.gr_mem == 40
+    spaceconfig = dict(usemodules=('_ffi', '_rawffi', 'itertools'))
+
+    def setup_class(cls):
+        cls.w_grp = import_lib_pypy(cls.space, 'grp',
+                                    "No grp module on this platform")
+
+    def test_basic(self):
+        g = self.grp.getgrnam("root")
+        assert g.gr_gid == 0
+        assert g.gr_mem == ['root'] or g.gr_mem == []
+        assert g.gr_name == 'root'
+        assert isinstance(g.gr_passwd, str)    # usually just 'x', don't hope 
:-)
+
+    def test_extra(self):
+        grp = self.grp
+        print(grp.__file__)
+        raises(TypeError, grp.getgrnam, False)
+        raises(TypeError, grp.getgrnam, None)
+
+    def test_struct_group(self):
+        g = self.grp.struct_group((10, 20, 30, 40))
+        assert len(g) == 4
+        assert list(g) == [10, 20, 30, 40]
+        assert g.gr_name == 10
+        assert g.gr_passwd == 20
+        assert g.gr_gid == 30
+        assert g.gr_mem == 40
diff --git a/pypy/module/test_lib_pypy/test_os_wait.py 
b/pypy/module/test_lib_pypy/test_os_wait.py
--- a/pypy/module/test_lib_pypy/test_os_wait.py
+++ b/pypy/module/test_lib_pypy/test_os_wait.py
@@ -1,18 +1,29 @@
 # Generates the resource cache (it might be there already, but maybe not)
 from __future__ import absolute_import
-from lib_pypy.ctypes_config_cache import rebuild
-rebuild.rebuild_one('resource.ctc.py')
-
 import os
 
-if hasattr(os, 'wait3'):
-    from lib_pypy._pypy_wait import wait3
-    def test_os_wait3():
+import py
+
+from lib_pypy.ctypes_config_cache import rebuild
+from pypy.module.test_lib_pypy.support import import_lib_pypy
+
+
+class AppTestOsWait:
+
+    spaceconfig = dict(usemodules=('_ffi', '_rawffi', 'itertools'))
+
+    def setup_class(cls):
+        if not hasattr(os, "fork"):
+            py.test.skip("Need fork() to test wait3/wait4()")
+        rebuild.rebuild_one('resource.ctc.py')
+        cls.w__pypy_wait = import_lib_pypy(
+            cls.space, '_pypy_wait',
+            '_pypy_wait not supported on this platform')
+
+    def test_os_wait3(self):
+        import os
+        wait3 = self._pypy_wait.wait3
         exit_status = 0x33
-
-        if not hasattr(os, "fork"):
-            skip("Need fork() to test wait3()")
-
         child = os.fork()
         if child == 0: # in child
             os._exit(exit_status)
@@ -24,14 +35,10 @@
             assert isinstance(rusage.ru_utime, float)
             assert isinstance(rusage.ru_maxrss, int)
 
-if hasattr(os, 'wait4'):
-    from lib_pypy._pypy_wait import wait4
-    def test_os_wait4():
+    def test_os_wait4(self):
+        import os
+        wait4 = self._pypy_wait.wait4
         exit_status = 0x33
-
-        if not hasattr(os, "fork"):
-            skip("Need fork() to test wait4()")
-
         child = os.fork()
         if child == 0: # in child
             os._exit(exit_status)
diff --git a/pypy/module/test_lib_pypy/test_resource.py 
b/pypy/module/test_lib_pypy/test_resource.py
--- a/pypy/module/test_lib_pypy/test_resource.py
+++ b/pypy/module/test_lib_pypy/test_resource.py
@@ -1,36 +1,41 @@
 from __future__ import absolute_import
-import py
-try:
-    from lib_pypy import resource
-except ImportError:
-    py.test.skip('no resource module available')
 
 from lib_pypy.ctypes_config_cache import rebuild
-rebuild.rebuild_one('resource.ctc.py')
+from pypy.module.test_lib_pypy.support import import_lib_pypy
 
 
-def test_resource():
-    x = resource.getrusage(resource.RUSAGE_SELF)
-    assert len(x) == 16
-    assert x[0] == x[-16] == x.ru_utime
-    assert x[1] == x[-15] == x.ru_stime
-    assert x[2] == x[-14] == x.ru_maxrss
-    assert x[3] == x[-13] == x.ru_ixrss
-    assert x[4] == x[-12] == x.ru_idrss
-    assert x[5] == x[-11] == x.ru_isrss
-    assert x[6] == x[-10] == x.ru_minflt
-    assert x[7] == x[-9] == x.ru_majflt
-    assert x[8] == x[-8] == x.ru_nswap
-    assert x[9] == x[-7] == x.ru_inblock
-    assert x[10] == x[-6] == x.ru_oublock
-    assert x[11] == x[-5] == x.ru_msgsnd
-    assert x[12] == x[-4] == x.ru_msgrcv
-    assert x[13] == x[-3] == x.ru_nsignals
-    assert x[14] == x[-2] == x.ru_nvcsw
-    assert x[15] == x[-1] == x.ru_nivcsw
-    for i in range(16):
-        if i < 2:
-            expected_type = float
-        else:
-            expected_type = (int, long)
-        assert isinstance(x[i], expected_type)
+class AppTestResource:
+
+    spaceconfig = dict(usemodules=('_ffi', '_rawffi', 'itertools'))
+
+    def setup_class(cls):
+        rebuild.rebuild_one('resource.ctc.py')
+        cls.w_resource = import_lib_pypy(cls.space, 'resource',
+                                         'No resource module available')
+
+    def test_resource(self):
+        resource = self.resource
+        x = resource.getrusage(resource.RUSAGE_SELF)
+        assert len(x) == 16
+        assert x[0] == x[-16] == x.ru_utime
+        assert x[1] == x[-15] == x.ru_stime
+        assert x[2] == x[-14] == x.ru_maxrss
+        assert x[3] == x[-13] == x.ru_ixrss
+        assert x[4] == x[-12] == x.ru_idrss
+        assert x[5] == x[-11] == x.ru_isrss
+        assert x[6] == x[-10] == x.ru_minflt
+        assert x[7] == x[-9] == x.ru_majflt
+        assert x[8] == x[-8] == x.ru_nswap
+        assert x[9] == x[-7] == x.ru_inblock
+        assert x[10] == x[-6] == x.ru_oublock
+        assert x[11] == x[-5] == x.ru_msgsnd
+        assert x[12] == x[-4] == x.ru_msgrcv
+        assert x[13] == x[-3] == x.ru_nsignals
+        assert x[14] == x[-2] == x.ru_nvcsw
+        assert x[15] == x[-1] == x.ru_nivcsw
+        for i in range(16):
+            if i < 2:
+                expected_type = float
+            else:
+                expected_type = (int, long)
+            assert isinstance(x[i], expected_type)
diff --git a/pypy/module/test_lib_pypy/test_sha_extra.py 
b/pypy/module/test_lib_pypy/test_sha_extra.py
--- a/pypy/module/test_lib_pypy/test_sha_extra.py
+++ b/pypy/module/test_lib_pypy/test_sha_extra.py
@@ -1,14 +1,21 @@
-# Testing sha module (NIST's Secure Hash Algorithm)
+"""Testing sha module (NIST's Secure Hash Algorithm)
 
-# use the three examples from Federal Information Processing Standards
-# Publication 180-1, Secure Hash Standard,  1995 April 17
-# http://www.itl.nist.gov/div897/pubs/fip180-1.htm
-from __future__ import absolute_import
-from lib_pypy import _sha as pysha
+use the three examples from Federal Information Processing Standards
+Publication 180-1, Secure Hash Standard,  1995 April 17
+http://www.itl.nist.gov/div897/pubs/fip180-1.htm
+"""
+from pypy.module.test_lib_pypy.support import import_lib_pypy
 
-class TestSHA: 
-    def check(self, data, digest):
-        computed = pysha.new(data).hexdigest()
+
+class AppTestSHA:
+
+    spaceconfig = dict(usemodules=('struct',))
+
+    def setup_class(cls):
+        cls.w__sha = import_lib_pypy(cls.space, '_sha')
+
+    def w_check(self, data, digest):
+        computed = self._sha.new(data).hexdigest()
         assert computed == digest
 
     def test_case_1(self):
@@ -23,10 +30,10 @@
         self.check("a" * 1000000,
                    "34aa973cd4c4daa4f61eeb2bdbad27316534016f")
 
-
-def test_attributes():
-    assert pysha.digest_size == 20
-    assert pysha.digestsize == 20
-    assert pysha.blocksize == 1
-    assert pysha.new().digest_size == 20
-    assert pysha.new().digestsize == 20
+    def test_attributes(self):
+        _sha = self._sha
+        assert _sha.digest_size == 20
+        assert _sha.digestsize == 20
+        assert _sha.blocksize == 1
+        assert _sha.new().digest_size == 20
+        assert _sha.new().digestsize == 20
diff --git a/pypy/module/test_lib_pypy/test_structseq.py 
b/pypy/module/test_lib_pypy/test_structseq.py
--- a/pypy/module/test_lib_pypy/test_structseq.py
+++ b/pypy/module/test_lib_pypy/test_structseq.py
@@ -1,80 +1,106 @@
-from __future__ import absolute_import
-import py
-from lib_pypy._structseq import structseqfield, structseqtype
+from pypy.module.test_lib_pypy.support import import_lib_pypy
 
 
-class mydata:
-    __metaclass__ = structseqtype
+class AppTestStructseq:
 
-    st_mode  = structseqfield(0, "protection bits")
-    st_ino   = structseqfield(1)
-    st_dev   = structseqfield(2)
-    st_nlink = structseqfield(3)
-    st_uid   = structseqfield(4)
-    st_gid   = structseqfield(5)
-    st_size  = structseqfield(6)
-    _st_atime_as_int = structseqfield(7)
-    _st_mtime_as_int = structseqfield(8)
-    _st_ctime_as_int = structseqfield(9)
-    # skip to higher numbers for fields not part of the sequence.
-    # the numbers are only used to ordering
-    st_rdev  = structseqfield(50, "device type (if inode device)")
-    st_atime = structseqfield(57, default=lambda self: self._st_atime_as_int)
-    st_mtime = structseqfield(58, default=lambda self: self._st_mtime_as_int)
-    st_ctime = structseqfield(59, default=lambda self: self._st_ctime_as_int)
+    spaceconfig = dict(usemodules=('binascii', 'struct',))
 
+    def setup_class(cls):
+        cls.w__structseq = import_lib_pypy(cls.space, '_structseq')
 
-def test_class():
-    assert mydata.st_mode.__doc__ == "protection bits"
-    assert mydata.n_fields == 14
-    assert mydata.n_sequence_fields == 10
-    assert mydata.n_unnamed_fields == 0
+    def w_get_mydata(self):
+        _structseq = self._structseq
+        ssfield = _structseq.structseqfield
+        class mydata:
+            __metaclass__ = _structseq.structseqtype
 
-def test_mydata():
-    x = mydata(range(100, 111))
-    assert x.n_sequence_fields == type(x).n_sequence_fields == 10
-    assert x.n_fields == type(x).n_fields == 14
-    assert x.st_mode  == 100
-    assert x.st_size  == 106
-    assert x.st_ctime == 109    # copied by the default=lambda...
-    assert x.st_rdev  == 110
-    assert len(x)     == 10
-    assert list(x)    == range(100, 110)
-    assert x + (5,)   == tuple(range(100, 110)) + (5,)
-    assert x[4:12:2]  == (104, 106, 108)
-    assert 104 in x
-    assert 110 not in x
+            st_mode  = ssfield(0, "protection bits")
+            st_ino   = ssfield(1)
+            st_dev   = ssfield(2)
+            st_nlink = ssfield(3)
+            st_uid   = ssfield(4)
+            st_gid   = ssfield(5)
+            st_size  = ssfield(6)
+            _st_atime_as_int = ssfield(7)
+            _st_mtime_as_int = ssfield(8)
+            _st_ctime_as_int = ssfield(9)
+            # skip to higher numbers for fields not part of the sequence.
+            # the numbers are only used to ordering
+            st_rdev  = ssfield(50, "device type (if inode device)")
+            st_atime = ssfield(57,
+                               default=lambda self: self._st_atime_as_int)
+            st_mtime = ssfield(58,
+                               default=lambda self: self._st_mtime_as_int)
+            st_ctime = ssfield(59,
+                               default=lambda self: self._st_ctime_as_int)
+        return mydata
 
-def test_default_None():
-    x = mydata(range(100, 110))
-    assert x.st_rdev is None
+    def test_class(self):
+        mydata = self.get_mydata()
+        assert mydata.st_mode.__doc__ == "protection bits"
+        assert mydata.n_fields == 14
+        assert mydata.n_sequence_fields == 10
+        assert mydata.n_unnamed_fields == 0
 
-def test_constructor():
-    x = mydata(range(100, 111), {'st_mtime': 12.25})
-    assert x[8] == 108
-    assert x.st_mtime == 12.25
+    def test_mydata(self):
+        mydata = self.get_mydata()
+        x = mydata(range(100, 111))
+        assert x.n_sequence_fields == type(x).n_sequence_fields == 10
+        assert x.n_fields == type(x).n_fields == 14
+        assert x.st_mode  == 100
+        assert x.st_size  == 106
+        assert x.st_ctime == 109    # copied by the default=lambda...
+        assert x.st_rdev  == 110
+        assert len(x)     == 10
+        assert list(x)    == range(100, 110)
+        assert x + (5,)   == tuple(range(100, 110)) + (5,)
+        assert x[4:12:2]  == (104, 106, 108)
+        assert 104 in x
+        assert 110 not in x
 
-def test_compare_like_tuple():
-    x = mydata(range(100, 111))
-    y = mydata(range(100, 110) + [555])
-    assert x == tuple(range(100, 110))
-    assert x == y    # blame CPython
-    assert hash(x) == hash(y) == hash(tuple(range(100, 110)))
+    def test_default_None(self):
+        mydata = self.get_mydata()
+        x = mydata(range(100, 110))
+        assert x.st_rdev is None
 
-def test_pickle():
-    import pickle
-    x = mydata(range(100, 111))
-    s = pickle.dumps(x)
-    y = pickle.loads(s)
-    assert x == y
-    assert x.st_rdev == y.st_rdev == 110
+    def test_constructor(self):
+        mydata = self.get_mydata()
+        x = mydata(range(100, 111), {'st_mtime': 12.25})
+        assert x[8] == 108
+        assert x.st_mtime == 12.25
 
-def test_readonly():
-    x = mydata(range(100, 113))
-    py.test.raises((TypeError, AttributeError), "x.st_mode = 1")
-    py.test.raises((TypeError, AttributeError), "x.st_mtime = 1")
-    py.test.raises((TypeError, AttributeError), "x.st_rdev = 1")
+    def test_compare_like_tuple(self):
+        mydata = self.get_mydata()
+        x = mydata(range(100, 111))
+        y = mydata(range(100, 110) + [555])
+        assert x == tuple(range(100, 110))
+        assert x == y    # blame CPython
+        assert hash(x) == hash(y) == hash(tuple(range(100, 110)))
 
-def test_no_extra_assignments():
-    x = mydata(range(100, 113))
-    py.test.raises((TypeError, AttributeError), "x.some_random_attribute = 1")
+    def test_pickle(self):
+        import pickle
+        import sys
+        import types
+        sys.modules['mod'] = mod = types.ModuleType('mod')
+        try:
+            mod.mydata = mydata = self.get_mydata()
+            mydata.__module__ = 'mod'
+            x = mydata(range(100, 111))
+            s = pickle.dumps(x)
+            y = pickle.loads(s)
+            assert x == y
+            assert x.st_rdev == y.st_rdev == 110
+        finally:
+            del sys.modules['mod']
+
+    def test_readonly(self):
+        mydata = self.get_mydata()
+        x = mydata(range(100, 113))
+        raises((TypeError, AttributeError), "x.st_mode = 1")
+        raises((TypeError, AttributeError), "x.st_mtime = 1")
+        raises((TypeError, AttributeError), "x.st_rdev = 1")
+
+    def test_no_extra_assignments(self):
+        mydata = self.get_mydata()
+        x = mydata(range(100, 113))
+        raises((TypeError, AttributeError), "x.some_random_attribute = 1")
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to