[py-svn] r51566 - py/branch/event/py/test2

hpk at codespeak.net hpk at codespeak.net
Sun Feb 17 20:42:28 CET 2008


Author: hpk
Date: Sun Feb 17 20:42:27 2008
New Revision: 51566

Modified:
   py/branch/event/py/test2/collect.py
   py/branch/event/py/test2/item.py
   py/branch/event/py/test2/present.py
Log:
* move away and streamline some methods and attributes 
  from the collection tree base class
* get rid of some presumtions that collection items always have 
  a filesystem path or are connected to Python function based tests. 



Modified: py/branch/event/py/test2/collect.py
==============================================================================
--- py/branch/event/py/test2/collect.py	(original)
+++ py/branch/event/py/test2/collect.py	Sun Feb 17 20:42:27 2008
@@ -43,9 +43,6 @@
         self.name = name 
         self.parent = parent
         self._config = getattr(parent, '_config', py.test2.config)
-        self.fspath = getattr(parent, 'fspath', None) 
-
-    _stickyfailure = None
 
     def __repr__(self): 
         return "<%s %r>" %(self.__class__.__name__, self.name) 
@@ -67,28 +64,10 @@
         #print "cmp", s1, s2
         return cmp(s1, s2) 
   
-    def obj(): 
-        def fget(self):
-            try: 
-                return self._obj   
-            except AttributeError: 
-                self._obj = obj = self._getobj() 
-                return obj 
-        def fset(self, value): 
-            self._obj = value 
-        return property(fget, fset, None, "underlying object") 
-    obj = obj()
-
-    def _getobj(self):
-        return getattr(self.parent.obj, self.name)
-
     def multijoin(self, namelist): 
         """ return a list of colitems for the given namelist. """ 
         return [self.join(name) for name in namelist]
 
-    def _getpathlineno(self): 
-        return self.fspath, py.std.sys.maxint 
-
     def setup(self): 
         pass 
 
@@ -96,7 +75,7 @@
         pass 
 
     def listchain(self): 
-        """ return list of all parent collectors up to ourself. """ 
+        """ return list of all parent collectors up to self. """ 
         l = [self]
         while 1: 
             x = l[-1]
@@ -179,6 +158,7 @@
         """
         return self._config.get_collector_trail(self)
 
+
 class Collector(Base):
     """ 
         Collector instances generate children through 
@@ -197,6 +177,10 @@
     Function = configproperty('Function')
     Generator = configproperty('Generator')
 
+    def __init__(self, name, parent=None):
+        super(Collector, self).__init__(name, parent)
+        self.fspath = getattr(parent, 'fspath', None) 
+
     def run(self):
         """ deprecated: use listdir(). """
         py.std.warnings.warn("deprecated: use listdir()", category=DeprecationWarning)
@@ -263,7 +247,24 @@
             name2items[name] = res 
         return res
 
-class PyCollectorMixin(Collector): 
+class PyobjMixin(object):
+    def obj(): 
+        def fget(self):
+            try: 
+                return self._obj   
+            except AttributeError: 
+                self._obj = obj = self._getobj() 
+                return obj 
+        def fset(self, value): 
+            self._obj = value 
+        return property(fget, fset, None, "underlying python object") 
+    obj = obj()
+
+    def _getobj(self):
+        return getattr(self.parent.obj, self.name)
+
+
+class PyCollectorMixin(PyobjMixin, Collector): 
     def funcnamefilter(self, name): 
         return name.startswith('test') 
     def classnamefilter(self, name): 
@@ -321,6 +322,8 @@
 
 
 class Module(FSCollector, PyCollectorMixin):
+    _stickyfailure = None
+
     def listdir(self):
         if getattr(self.obj, 'disabled', 0):
             return []
@@ -343,22 +346,18 @@
     def __repr__(self): 
         return "<%s %r>" % (self.__class__.__name__, self.name)
 
-    def obj(self): 
+    def _getobj(self):
+        failure = self._stickyfailure
+        if failure is not None: 
+            raise failure[0], failure[1], failure[2]
         try: 
-            return self._obj    
-        except AttributeError:
-            failure = getattr(self, '_stickyfailure', None)
-            if failure is not None: 
-                raise failure[0], failure[1], failure[2]
-            try: 
-                self._obj = obj = self.fspath.pyimport() 
-            except KeyboardInterrupt: 
-                raise
-            except:
-                self._stickyfailure = py.std.sys.exc_info()
-                raise 
-            return obj 
-    obj = property(obj, None, None, "module object")
+            self._obj = obj = self.fspath.pyimport() 
+        except KeyboardInterrupt: 
+            raise
+        except:
+            self._stickyfailure = py.std.sys.exc_info()
+            raise 
+        return self._obj 
 
     def setup(self): 
         if hasattr(self.obj, 'setup_module'): 
@@ -416,15 +415,15 @@
     Function = property(Function)
 
 
-class FunctionMixin(object):
+class FunctionMixin(PyobjMixin):
     """ mixin for the code common to Function and Generator.
     """
-    def _getpathlineno(self):
-        code = py.code.Code(self.obj) 
-        return code.path, code.firstlineno 
-
+    _sortvalue = None
     def _getsortvalue(self):  
-        return self._getpathlineno() 
+        if self._sortvalue is None:
+            code = py.code.Code(self.obj) 
+            self._sortvalue = code.path, code.firstlineno 
+        return self._sortvalue 
 
     def setup(self): 
         """ perform setup for this test function. """

Modified: py/branch/event/py/test2/item.py
==============================================================================
--- py/branch/event/py/test2/item.py	(original)
+++ py/branch/event/py/test2/item.py	Sun Feb 17 20:42:27 2008
@@ -30,6 +30,10 @@
             self.stack.append(col) 
 
 class Item(Base): 
+    def __init__(self, name, parent=None):
+        super(Item, self).__init__(name, parent)
+        self.fspath = getattr(parent, 'fspath', None) 
+        
     def startcapture(self): 
         self._config._startcapture(self, path=self.fspath)
 
@@ -51,11 +55,6 @@
     def __repr__(self): 
         return "<%s %r>" %(self.__class__.__name__, self.name)
 
-    def _getsortvalue(self):  
-        if self._sort_value is None:
-            return self._getpathlineno()
-        return self._sort_value
-
     def run(self):
         """ setup and execute the underlying test function. """
         self._state.prepare(self) 

Modified: py/branch/event/py/test2/present.py
==============================================================================
--- py/branch/event/py/test2/present.py	(original)
+++ py/branch/event/py/test2/present.py	Sun Feb 17 20:42:27 2008
@@ -22,6 +22,7 @@
 
 def getmodpath(pycolitem): 
     """ return dotted module path for the given colitem. """ 
+    # XXX what about non-functions? 
     colitems = pycolitem.listchain()
     while colitems: 
         colitem = colitems.pop(0)
@@ -77,18 +78,9 @@
     def repr_failure_headline(self, item):
         """ This method represents py.test2.collect.Item info (path and module)
         """
-        root = item.fspath 
+        # XXX do something for non-python test items 
         modpath = getmodpath(item)
-        try: 
-            fn, lineno = item._getpathlineno() 
-        except TypeError: 
-            assert isinstance(item.parent, py.test2.collect.Generator) 
-            # a generative test yielded a non-callable 
-            fn, lineno = item.parent._getpathlineno() 
-        if root == fn:
-            self.out.sep("_", "entrypoint: %s" %(modpath))
-        else:
-            self.out.sep("_", "entrypoint: %s %s" %(root.basename, modpath))
+        self.out.sep("_", "entrypoint: %s" %(modpath))
 
     def repr_failure_explanation(self, excinfo, source):
         try:
@@ -129,8 +121,10 @@
                         py.std.pprint.pprint(value, stream=self.out)
 
     def filtertraceback(self, item, traceback):
-        if item and not self.config.option.fulltrace: 
-            path, firstlineno = item._getpathlineno()
+        if isinstance(item, py.test2.collect.Function) \
+           and not self.config.option.fulltrace: 
+            code = py.code.Code(item.obj) 
+            path, firstlineno = code.path, code.firstlineno 
             ntraceback = traceback.cut(path=path, firstlineno=firstlineno)
             if ntraceback == traceback:
                 ntraceback = ntraceback.cut(path=path)


More information about the py-svn mailing list