blob: db63c96e5e0f6a5db55b2cf47d8bf3f6ccadda1f [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 Rossum65810fe2006-05-26 19:12:38 +000059MethodType = type(_C()._m)
Guido van Rossum85d89451994-06-23 11:53:27 +000060
Guido van Rossumadc940e1994-09-29 10:04:43 +000061BuiltinFunctionType = type(len)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000062BuiltinMethodType = type([].append) # Same as BuiltinFunctionType
Guido van Rossum85d89451994-06-23 11:53:27 +000063
64ModuleType = type(sys)
Tim Peters59c9a642001-09-13 05:38:56 +000065FileType = file
Raymond Hettingerc4c453f2002-06-05 23:12:45 +000066XRangeType = xrange
Guido van Rossum85d89451994-06-23 11:53:27 +000067
68try:
Guido van Rossum898c9151997-09-04 22:12:34 +000069 raise TypeError
Guido van Rossum85d89451994-06-23 11:53:27 +000070except TypeError:
Guido van Rossum898c9151997-09-04 22:12:34 +000071 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000072 tb = sys.exc_info()[2]
73 TracebackType = type(tb)
74 FrameType = type(tb.tb_frame)
Martin v. Löwis58682b72001-08-11 15:02:57 +000075 except AttributeError:
76 # In the restricted environment, exc_info returns (None, None,
77 # None) Then, tb.tb_frame gives an attribute error
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000078 pass
Guido van Rossumf15d1591997-09-29 23:22:12 +000079 tb = None; del tb
Guido van Rossum85d89451994-06-23 11:53:27 +000080
Guido van Rossumbea18cc2002-06-14 20:41:17 +000081SliceType = slice
Guido van Rossume449af71996-10-11 16:25:41 +000082EllipsisType = type(Ellipsis)
Guido van Rossum8741b2b1996-10-11 16:00:06 +000083
Tim Peters6d6c1a32001-08-02 04:15:00 +000084DictProxyType = type(TypeType.__dict__)
Just van Rossumba205332003-02-10 19:38:33 +000085NotImplementedType = type(NotImplemented)
Tim Peters6d6c1a32001-08-02 04:15:00 +000086
Guido van Rossum65810fe2006-05-26 19:12:38 +000087del sys, _f, _g, _C # Not for export