summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_inspect.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_inspect.py')
-rw-r--r--Lib/test/test_inspect.py72
1 files changed, 68 insertions, 4 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 43ef755..20df755 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -18,6 +18,7 @@ import textwrap
import unicodedata
import unittest
import unittest.mock
+import warnings
try:
from concurrent.futures import ThreadPoolExecutor
@@ -62,14 +63,16 @@ class IsTestBase(unittest.TestCase):
predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
inspect.isframe, inspect.isfunction, inspect.ismethod,
inspect.ismodule, inspect.istraceback,
- inspect.isgenerator, inspect.isgeneratorfunction])
+ inspect.isgenerator, inspect.isgeneratorfunction,
+ inspect.iscoroutine, inspect.iscoroutinefunction])
def istest(self, predicate, exp):
obj = eval(exp)
self.assertTrue(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
for other in self.predicates - set([predicate]):
- if predicate == inspect.isgeneratorfunction and\
+ if (predicate == inspect.isgeneratorfunction or \
+ predicate == inspect.iscoroutinefunction) and \
other == inspect.isfunction:
continue
self.assertFalse(other(obj), 'not %s(%s)' % (other.__name__, exp))
@@ -78,13 +81,21 @@ def generator_function_example(self):
for i in range(2):
yield i
+async def coroutine_function_example(self):
+ return 'spam'
+
+@types.coroutine
+def gen_coroutine_function_example(self):
+ yield
+ return 'spam'
+
class TestPredicates(IsTestBase):
- def test_sixteen(self):
+ def test_eightteen(self):
count = len([x for x in dir(inspect) if x.startswith('is')])
# This test is here for remember you to update Doc/library/inspect.rst
# which claims there are 16 such functions
- expected = 16
+ expected = 19
err_msg = "There are %d (not %d) is* functions" % (count, expected)
self.assertEqual(count, expected, err_msg)
@@ -115,11 +126,64 @@ class TestPredicates(IsTestBase):
self.istest(inspect.isdatadescriptor, 'collections.defaultdict.default_factory')
self.istest(inspect.isgenerator, '(x for x in range(2))')
self.istest(inspect.isgeneratorfunction, 'generator_function_example')
+
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ self.istest(inspect.iscoroutine, 'coroutine_function_example(1)')
+ self.istest(inspect.iscoroutinefunction, 'coroutine_function_example')
+
if hasattr(types, 'MemberDescriptorType'):
self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
else:
self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
+ def test_iscoroutine(self):
+ gen_coro = gen_coroutine_function_example(1)
+ coro = coroutine_function_example(1)
+
+ self.assertTrue(
+ inspect.iscoroutinefunction(gen_coroutine_function_example))
+ self.assertTrue(inspect.iscoroutine(gen_coro))
+
+ self.assertTrue(
+ inspect.isgeneratorfunction(gen_coroutine_function_example))
+ self.assertTrue(inspect.isgenerator(gen_coro))
+
+ self.assertTrue(
+ inspect.iscoroutinefunction(coroutine_function_example))
+ self.assertTrue(inspect.iscoroutine(coro))
+
+ self.assertFalse(
+ inspect.isgeneratorfunction(coroutine_function_example))
+ self.assertFalse(inspect.isgenerator(coro))
+
+ coro.close(); gen_coro.close() # silence warnings
+
+ def test_isawaitable(self):
+ def gen(): yield
+ self.assertFalse(inspect.isawaitable(gen()))
+
+ coro = coroutine_function_example(1)
+ gen_coro = gen_coroutine_function_example(1)
+
+ self.assertTrue(
+ inspect.isawaitable(coro))
+ self.assertTrue(
+ inspect.isawaitable(gen_coro))
+
+ class Future:
+ def __await__():
+ pass
+ self.assertTrue(inspect.isawaitable(Future()))
+ self.assertFalse(inspect.isawaitable(Future))
+
+ class NotFuture: pass
+ not_fut = NotFuture()
+ not_fut.__await__ = lambda: None
+ self.assertFalse(inspect.isawaitable(not_fut))
+
+ coro.close(); gen_coro.close() # silence warnings
+
def test_isroutine(self):
self.assertTrue(inspect.isroutine(mod.spam))
self.assertTrue(inspect.isroutine([].count))