[py-svn] r34962 - in py/dist/py/apigen/source: . testing

fijal at codespeak.net fijal at codespeak.net
Sat Nov 25 15:50:35 CET 2006


Author: fijal
Date: Sat Nov 25 15:50:32 2006
New Revision: 34962

Modified:
   py/dist/py/apigen/source/browser.py
   py/dist/py/apigen/source/testing/test_browser.py
Log:
Added possibility of ifing functions and classes.


Modified: py/dist/py/apigen/source/browser.py
==============================================================================
--- py/dist/py/apigen/source/browser.py	(original)
+++ py/dist/py/apigen/source/browser.py	Sat Nov 25 15:50:32 2006
@@ -12,6 +12,8 @@
 
 from py.__.path.common import PathBase
 
+blockers = [ast.Function, ast.Class]
+
 class BaseElem(object):
     pass # purely for testing isinstance
 
@@ -85,14 +87,35 @@
     def get_children(self):
         return self.methods.values()
 
+def dir_nodes(st):
+    """ List all the subnodes, which are not blockers
+    """
+    res = []
+    for i in st.getChildNodes():
+        res.append(i)
+        if not i.__class__ in blockers:
+            res += dir_nodes(i)
+    return res
+
+def update_mod_dict(imp_mod, mod_dict):
+    # make sure that things that are in mod_dict, and not in imp_mod,
+    # are not shown
+    for key, value in mod_dict.items():
+        if not hasattr(imp_mod, key):
+            del mod_dict[key]
+
 def parse_path(path):
     if not isinstance(path, PathBase):
         path = py.path.local(path)
     buf = path.open().read()
     st = parse(buf)
     # first go - we get all functions and classes defined on top-level
-    function_ast = [i for i in st.node.nodes if isinstance(i, ast.Function)]
-    classes_ast = [i for i in st.node.nodes if isinstance(i, ast.Class)]
+    nodes = dir_nodes(st)
+    function_ast = [i for i in nodes if isinstance(i, ast.Function)]
+    classes_ast = [i for i in nodes if isinstance(i, ast.Class)]
     mod_dict = dict([(i.name, function_from_ast(i)) for i in function_ast]
        + [(i.name, class_from_ast(i)) for i in classes_ast])
+    # we check all the elements, if they're really there
+    mod = path.pyimport()
+    update_mod_dict(mod, mod_dict)
     return Module(path, mod_dict)

Modified: py/dist/py/apigen/source/testing/test_browser.py
==============================================================================
--- py/dist/py/apigen/source/testing/test_browser.py	(original)
+++ py/dist/py/apigen/source/testing/test_browser.py	Sat Nov 25 15:50:32 2006
@@ -38,3 +38,17 @@
     assert isinstance(mod.Z.zzz, Method)
     assert mod.Z.zzz.firstlineno == 13
     assert mod.Z.zzz.endlineno == 17
+
+def test_if_browser():
+    tmp = py.test.ensuretemp("sourcebrowser")
+    tmp.ensure("b.py").write(py.code.Source("""
+        if 1:
+            def f():
+                pass
+        if 0:
+            def g():
+                pass
+    """))
+    mod = parse_path(tmp.join("b.py"))
+    assert isinstance(mod.f, Function)
+    py.test.raises(AttributeError, 'mod.g')



More information about the pytest-commit mailing list