blob: 39812ac3228d17f26eea573bd9093eddef512c28 [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Define names for all type symbols known in the standard interpreter.
2
3Types that are part of optional modules (e.g. array) are not listed.
4"""
Guido van Rossum85d89451994-06-23 11:53:27 +00005import sys
6
Tim Peters26991a72001-09-25 22:02:03 +00007# Iterators in Python aren't a matter of type but of protocol. A large
8# and changing number of builtin types implement *some* flavor of
9# iterator. Don't check the type! Use hasattr to check for both
10# "__iter__" and "next" attributes instead.
11
Guido van Rossum85d89451994-06-23 11:53:27 +000012NoneType = type(None)
Tim Peters6d6c1a32001-08-02 04:15:00 +000013TypeType = type
14ObjectType = object
Guido van Rossum85d89451994-06-23 11:53:27 +000015
Martin v. Löwisff885562001-08-08 16:02:01 +000016IntType = int
17LongType = long
18FloatType = float
Skip Montanaro012ed5d2002-05-21 23:17:12 +000019BooleanType = bool
Guido van Rossum0f6f8121996-02-13 00:04:31 +000020try:
Martin v. Löwisff885562001-08-08 16:02:01 +000021 ComplexType = complex
Guido van Rossum0f6f8121996-02-13 00:04:31 +000022except NameError:
Guido van Rossum898c9151997-09-04 22:12:34 +000023 pass
Guido van Rossum85d89451994-06-23 11:53:27 +000024
Martin v. Löwisff885562001-08-08 16:02:01 +000025StringType = str
Guido van Rossumbea18cc2002-06-14 20:41:17 +000026
27# StringTypes is already outdated. Instead of writing "type(x) in
28# types.StringTypes", you should use "isinstance(x, basestring)". But
29# we keep around for compatibility with Python 2.2.
Martin v. Löwis339d0f72001-08-17 18:39:25 +000030try:
31 UnicodeType = unicode
Martin v. Löwis8b6bd422001-12-02 12:08:06 +000032 StringTypes = (StringType, UnicodeType)
Martin v. Löwis339d0f72001-08-17 18:39:25 +000033except NameError:
Martin v. Löwis8b6bd422001-12-02 12:08:06 +000034 StringTypes = (StringType,)
Martin v. Löwis339d0f72001-08-17 18:39:25 +000035
Guido van Rossumbea18cc2002-06-14 20:41:17 +000036BufferType = buffer
Guido van Rossum85d89451994-06-23 11:53:27 +000037
Martin v. Löwisff885562001-08-08 16:02:01 +000038TupleType = tuple
Tim Peters6d6c1a32001-08-02 04:15:00 +000039ListType = list
Tim Petersa427a2b2001-10-29 22:25:45 +000040DictType = DictionaryType = dict
Guido van Rossum85d89451994-06-23 11:53:27 +000041
Guido van Rossumadc940e1994-09-29 10:04:43 +000042def _f(): pass
43FunctionType = type(_f)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000044LambdaType = type(lambda: None) # Same as FunctionType
Guido van Rossum898c9151997-09-04 22:12:34 +000045try:
46 CodeType = type(_f.func_code)
Martin v. Löwis58682b72001-08-11 15:02:57 +000047except RuntimeError:
48 # Execution in restricted environment
Guido van Rossum898c9151997-09-04 22:12:34 +000049 pass
Guido van Rossum85d89451994-06-23 11:53:27 +000050
Tim Peters264c6592004-07-18 00:08:11 +000051def _g():
Tim Peters3e7b1a02001-06-25 19:46:25 +000052 yield 1
Tim Peters264c6592004-07-18 00:08:11 +000053GeneratorType = type(_g())
Tim Peters3e7b1a02001-06-25 19:46:25 +000054
Guido van Rossumadc940e1994-09-29 10:04:43 +000055class _C:
Guido van Rossum898c9151997-09-04 22:12:34 +000056 def _m(self): pass
Guido van Rossumadc940e1994-09-29 10:04:43 +000057ClassType = type(_C)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000058UnboundMethodType = type(_C._m) # Same as MethodType
Guido van Rossumadc940e1994-09-29 10:04:43 +000059_x = _C()
60InstanceType = type(_x)
61MethodType = type(_x._m)
Guido van Rossum85d89451994-06-23 11:53:27 +000062
Guido van Rossumadc940e1994-09-29 10:04:43 +000063BuiltinFunctionType = type(len)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000064BuiltinMethodType = type([].append) # Same as BuiltinFunctionType
Guido van Rossum85d89451994-06-23 11:53:27 +000065
66ModuleType = type(sys)
Tim Peters59c9a642001-09-13 05:38:56 +000067FileType = file
Raymond Hettingerc4c453f2002-06-05 23:12:45 +000068XRangeType = xrange
Guido van Rossum85d89451994-06-23 11:53:27 +000069
70try:
Guido van Rossum898c9151997-09-04 22:12:34 +000071 raise TypeError
Guido van Rossum85d89451994-06-23 11:53:27 +000072except TypeError:
Guido van Rossum898c9151997-09-04 22:12:34 +000073 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000074 tb = sys.exc_info()[2]
75 TracebackType = type(tb)
76 FrameType = type(tb.tb_frame)
Martin v. Löwis58682b72001-08-11 15:02:57 +000077 except AttributeError:
78 # In the restricted environment, exc_info returns (None, None,
79 # None) Then, tb.tb_frame gives an attribute error
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000080 pass
Guido van Rossumf15d1591997-09-29 23:22:12 +000081 tb = None; del tb
Guido van Rossum85d89451994-06-23 11:53:27 +000082
Guido van Rossumbea18cc2002-06-14 20:41:17 +000083SliceType = slice
Guido van Rossume449af71996-10-11 16:25:41 +000084EllipsisType = type(Ellipsis)
Guido van Rossum8741b2b1996-10-11 16:00:06 +000085
Tim Peters6d6c1a32001-08-02 04:15:00 +000086DictProxyType = type(TypeType.__dict__)
Just van Rossumba205332003-02-10 19:38:33 +000087NotImplementedType = type(NotImplemented)
Tim Peters6d6c1a32001-08-02 04:15:00 +000088
Tim Peters264c6592004-07-18 00:08:11 +000089del sys, _f, _g, _C, _x # Not for export