summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_inspect
diff options
context:
space:
mode:
authorZhikang Yan <2951256653@qq.com>2024-10-27 04:57:43 (GMT)
committerGitHub <noreply@github.com>2024-10-27 04:57:43 (GMT)
commitdad34531298fc0ea91b9000aafdd2ea2fce5e54a (patch)
tree8250e27c253d190a4a6a1e8a1e1b36f9c77d7d3e /Lib/test/test_inspect
parentc51b56038ba344dece607eb5f035dca544187813 (diff)
downloadcpython-dad34531298fc0ea91b9000aafdd2ea2fce5e54a.zip
cpython-dad34531298fc0ea91b9000aafdd2ea2fce5e54a.tar.gz
cpython-dad34531298fc0ea91b9000aafdd2ea2fce5e54a.tar.bz2
gh-125633: Add function `ispackage` to stdlib `inspect` (#125634)
--------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Diffstat (limited to 'Lib/test/test_inspect')
-rw-r--r--Lib/test/test_inspect/test_inspect.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py
index 77fdc6f..a4857da 100644
--- a/Lib/test/test_inspect/test_inspect.py
+++ b/Lib/test/test_inspect/test_inspect.py
@@ -51,7 +51,7 @@ from test.test_inspect import inspect_deferred_annotations
# Functions tested in this suite:
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
-# isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers,
+# isbuiltin, isroutine, isgenerator, ispackage, isgeneratorfunction, getmembers,
# getdoc, getfile, getmodule, getsourcefile, getcomments, getsource,
# getclasstree, getargvalues, formatargvalues, currentframe,
# stack, trace, ismethoddescriptor, isdatadescriptor, ismethodwrapper
@@ -105,7 +105,7 @@ unsorted_keyword_only_parameters = 'throw out the baby with_ the_ bathwater'.spl
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.ispackage,
inspect.isgenerator, inspect.isgeneratorfunction,
inspect.iscoroutine, inspect.iscoroutinefunction,
inspect.isasyncgen, inspect.isasyncgenfunction,
@@ -121,7 +121,10 @@ class IsTestBase(unittest.TestCase):
predicate == inspect.iscoroutinefunction) and \
other == inspect.isfunction:
continue
- self.assertFalse(other(obj), 'not %s(%s)' % (other.__name__, exp))
+ if predicate == inspect.ispackage and other == inspect.ismodule:
+ self.assertTrue(predicate(obj), '%s(%s)' % (predicate.__name__, exp))
+ else:
+ self.assertFalse(other(obj), 'not %s(%s)' % (other.__name__, exp))
def test__all__(self):
support.check__all__(self, inspect, not_exported=("modulesbyfile",), extra=("get_annotations",))
@@ -201,7 +204,17 @@ class TestPredicates(IsTestBase):
self.assertFalse(inspect.ismethodwrapper(int))
self.assertFalse(inspect.ismethodwrapper(type("AnyClass", (), {})))
+ def test_ispackage(self):
+ self.istest(inspect.ispackage, 'asyncio')
+ self.istest(inspect.ispackage, 'importlib')
+ self.assertFalse(inspect.ispackage(inspect))
+ self.assertFalse(inspect.ispackage(mod))
+ self.assertFalse(inspect.ispackage(':)'))
+
+ class FakePackage:
+ __path__ = None
+ self.assertFalse(inspect.ispackage(FakePackage()))
def test_iscoroutine(self):
async_gen_coro = async_generator_function_example(1)