mirror of
https://github.com/python/cpython.git
synced 2025-07-24 19:54:21 +00:00

inspect.py, and pydoc.py. Specifically, this allows for querying the type of an object against these built-in C types and more importantly, for getting their docstrings printed in the interactive interpreter's help() function. This patch includes a new built-in module called _types which provides definitions of getset and member descriptors for use by the types.py module. These types are exposed as types.GetSetDescriptorType and types.MemberDescriptorType. Query functions are provided as inspect.isgetsetdescriptor() and inspect.ismemberdescriptor(). The implementations of these are robust enough to work with Python implementations other than CPython, which may not have these fundamental types. The patch also includes documentation and test suite updates. I commit these changes now under these guiding principles: 1. Silence is assent. The release manager has not said "no", and of the few people that cared enough to respond to the thread, the worst vote was "0". 2. It's easier to ask for forgiveness than permission. 3. It's so dang easy to revert stuff in svn, that you could view this as a forcing function. :) Windows build patches will follow.
101 lines
2.5 KiB
Python
101 lines
2.5 KiB
Python
"""Define names for all type symbols known in the standard interpreter.
|
|
|
|
Types that are part of optional modules (e.g. array) are not listed.
|
|
"""
|
|
import sys
|
|
|
|
# Iterators in Python aren't a matter of type but of protocol. A large
|
|
# and changing number of builtin types implement *some* flavor of
|
|
# iterator. Don't check the type! Use hasattr to check for both
|
|
# "__iter__" and "next" attributes instead.
|
|
|
|
NoneType = type(None)
|
|
TypeType = type
|
|
ObjectType = object
|
|
|
|
IntType = int
|
|
LongType = long
|
|
FloatType = float
|
|
BooleanType = bool
|
|
try:
|
|
ComplexType = complex
|
|
except NameError:
|
|
pass
|
|
|
|
StringType = str
|
|
|
|
# StringTypes is already outdated. Instead of writing "type(x) in
|
|
# types.StringTypes", you should use "isinstance(x, basestring)". But
|
|
# we keep around for compatibility with Python 2.2.
|
|
try:
|
|
UnicodeType = unicode
|
|
StringTypes = (StringType, UnicodeType)
|
|
except NameError:
|
|
StringTypes = (StringType,)
|
|
|
|
BufferType = buffer
|
|
|
|
TupleType = tuple
|
|
ListType = list
|
|
DictType = DictionaryType = dict
|
|
|
|
def _f(): pass
|
|
FunctionType = type(_f)
|
|
LambdaType = type(lambda: None) # Same as FunctionType
|
|
try:
|
|
CodeType = type(_f.func_code)
|
|
except RuntimeError:
|
|
# Execution in restricted environment
|
|
pass
|
|
|
|
def _g():
|
|
yield 1
|
|
GeneratorType = type(_g())
|
|
|
|
class _C:
|
|
def _m(self): pass
|
|
ClassType = type(_C)
|
|
UnboundMethodType = type(_C._m) # Same as MethodType
|
|
_x = _C()
|
|
InstanceType = type(_x)
|
|
MethodType = type(_x._m)
|
|
|
|
BuiltinFunctionType = type(len)
|
|
BuiltinMethodType = type([].append) # Same as BuiltinFunctionType
|
|
|
|
ModuleType = type(sys)
|
|
FileType = file
|
|
XRangeType = xrange
|
|
|
|
try:
|
|
raise TypeError
|
|
except TypeError:
|
|
try:
|
|
tb = sys.exc_info()[2]
|
|
TracebackType = type(tb)
|
|
FrameType = type(tb.tb_frame)
|
|
except AttributeError:
|
|
# In the restricted environment, exc_info returns (None, None,
|
|
# None) Then, tb.tb_frame gives an attribute error
|
|
pass
|
|
tb = None; del tb
|
|
|
|
SliceType = slice
|
|
EllipsisType = type(Ellipsis)
|
|
|
|
DictProxyType = type(TypeType.__dict__)
|
|
NotImplementedType = type(NotImplemented)
|
|
|
|
# Extension types defined in a C helper module. XXX There may be no
|
|
# equivalent in implementations other than CPython, so it seems better to
|
|
# leave them undefined then to set them to e.g. None.
|
|
try:
|
|
import _types
|
|
except ImportError:
|
|
pass
|
|
else:
|
|
GetSetDescriptorType = type(_types.Helper.getter)
|
|
MemberDescriptorType = type(_types.Helper.member)
|
|
del _types
|
|
|
|
del sys, _f, _g, _C, _x # Not for export
|