Here are some of the useful functions provided by this module:
- ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
- isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
- isroutine() - check object types
+ ismodule(), isclass(), ismethod(), ispackage(), isfunction(),
+ isgeneratorfunction(), isgenerator(), istraceback(), isframe(),
+ iscode(), isbuiltin(), isroutine() - check object types
getmembers() - get members of an object that satisfy a given condition
getfile(), getsourcefile(), getsource() - find an object's source code
"ismethoddescriptor",
"ismethodwrapper",
"ismodule",
+ "ispackage",
"isroutine",
"istraceback",
"markcoroutinefunction",
"""Return true if the object is an instance method."""
return isinstance(object, types.MethodType)
+def ispackage(object):
+ """Return true if the object is a package."""
+ return ismodule(object) and hasattr(object, "__path__")
+
def ismethoddescriptor(object):
"""Return true if the object is a method descriptor.
# 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
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,
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",))
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)