summaryrefslogtreecommitdiff
path: root/Lib/test/test_inspect.py
diff options
context:
space:
mode:
authorFacundo Batista <facundobatista@gmail.com>2008-02-18 03:43:43 +0000
committerFacundo Batista <facundobatista@gmail.com>2008-02-18 03:43:43 +0000
commit759bfc6207e7979d4eaeed2a2ae611e1804aef55 (patch)
tree580985dd110d3cdd94b503c881f2f046906fd92c /Lib/test/test_inspect.py
parentb169eaa91704a44e350cf66c5e68981964b6764d (diff)
downloadcpython-git-759bfc6207e7979d4eaeed2a2ae611e1804aef55.tar.gz
Issue #1916. Added isgenerator() and isgeneratorfunction() to
inspect.py. Thanks Javi Mansilla for patch review and corrections.
Diffstat (limited to 'Lib/test/test_inspect.py')
-rw-r--r--Lib/test/test_inspect.py28
1 files changed, 20 insertions, 8 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 0537d7ebd6..33bcfb4f79 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -11,10 +11,10 @@ from test import inspect_fodder2 as mod2
# Functions tested in this suite:
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
-# isbuiltin, isroutine, getmembers, getdoc, getfile, getmodule,
-# getsourcefile, getcomments, getsource, getclasstree, getargspec,
-# getargvalues, formatargspec, formatargvalues, currentframe, stack, trace
-# isdatadescriptor
+# isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers,
+# getdoc, getfile, getmodule, getsourcefile, getcomments, getsource,
+# getclasstree, getargspec, getargvalues, formatargspec, formatargvalues,
+# currentframe, stack, trace, isdatadescriptor
modfile = mod.__file__
if modfile.endswith(('c', 'o')):
@@ -32,23 +32,33 @@ git = mod.StupidGit()
class IsTestBase(unittest.TestCase):
predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
inspect.isframe, inspect.isfunction, inspect.ismethod,
- inspect.ismodule, inspect.istraceback])
+ inspect.ismodule, inspect.istraceback,
+ inspect.isgenerator, inspect.isgeneratorfunction])
def istest(self, predicate, exp):
obj = eval(exp)
self.failUnless(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
for other in self.predicates - set([predicate]):
+ if predicate == inspect.isgeneratorfunction and\
+ other == inspect.isfunction:
+ continue
self.failIf(other(obj), 'not %s(%s)' % (other.__name__, exp))
+def generator_function_example(self):
+ for i in xrange(2):
+ yield i
+
class TestPredicates(IsTestBase):
- def test_thirteen(self):
+ def test_fifteen(self):
count = len(filter(lambda x:x.startswith('is'), dir(inspect)))
- # Doc/lib/libinspect.tex claims there are 13 such functions
- expected = 13
+ # This test is here for remember you to update Doc/library/inspect.rst
+ # which claims there are 15 such functions
+ expected = 15
err_msg = "There are %d (not %d) is* functions" % (count, expected)
self.assertEqual(count, expected, err_msg)
+
def test_excluding_predicates(self):
self.istest(inspect.isbuiltin, 'sys.exit')
self.istest(inspect.isbuiltin, '[].append')
@@ -62,6 +72,8 @@ class TestPredicates(IsTestBase):
self.istest(inspect.istraceback, 'tb')
self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
+ self.istest(inspect.isgenerator, '(x for x in xrange(2))')
+ self.istest(inspect.isgeneratorfunction, 'generator_function_example')
if hasattr(types, 'GetSetDescriptorType'):
self.istest(inspect.isgetsetdescriptor,
'type(tb.tb_frame).f_locals')