blob: 0d2905d81536c8b5ff2cf70f338f6555577ea276 [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 Peters3e7b1a02001-06-25 19:46:25 +000051def g():
52 yield 1
53GeneratorType = type(g())
54del g
55
Guido van Rossumadc940e1994-09-29 10:04:43 +000056class _C:
Guido van Rossum898c9151997-09-04 22:12:34 +000057 def _m(self): pass
Guido van Rossumadc940e1994-09-29 10:04:43 +000058ClassType = type(_C)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000059UnboundMethodType = type(_C._m) # Same as MethodType
Guido van Rossumadc940e1994-09-29 10:04:43 +000060_x = _C()
61InstanceType = type(_x)
62MethodType = type(_x._m)
Guido van Rossum85d89451994-06-23 11:53:27 +000063
Guido van Rossumadc940e1994-09-29 10:04:43 +000064BuiltinFunctionType = type(len)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000065BuiltinMethodType = type([].append) # Same as BuiltinFunctionType
Guido van Rossum85d89451994-06-23 11:53:27 +000066
67ModuleType = type(sys)
Tim Peters59c9a642001-09-13 05:38:56 +000068FileType = file
Raymond Hettingerc4c453f2002-06-05 23:12:45 +000069XRangeType = xrange
Guido van Rossum85d89451994-06-23 11:53:27 +000070
71try:
Guido van Rossum898c9151997-09-04 22:12:34 +000072 raise TypeError
Guido van Rossum85d89451994-06-23 11:53:27 +000073except TypeError:
Guido van Rossum898c9151997-09-04 22:12:34 +000074 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000075 tb = sys.exc_info()[2]
76 TracebackType = type(tb)
77 FrameType = type(tb.tb_frame)
Martin v. Löwis58682b72001-08-11 15:02:57 +000078 except AttributeError:
79 # In the restricted environment, exc_info returns (None, None,
80 # None) Then, tb.tb_frame gives an attribute error
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000081 pass
Guido van Rossumf15d1591997-09-29 23:22:12 +000082 tb = None; del tb
Guido van Rossum85d89451994-06-23 11:53:27 +000083
Guido van Rossumbea18cc2002-06-14 20:41:17 +000084SliceType = slice
Guido van Rossume449af71996-10-11 16:25:41 +000085EllipsisType = type(Ellipsis)
Guido van Rossum8741b2b1996-10-11 16:00:06 +000086
Tim Peters6d6c1a32001-08-02 04:15:00 +000087DictProxyType = type(TypeType.__dict__)
Just van Rossumba205332003-02-10 19:38:33 +000088NotImplementedType = type(NotImplemented)
Tim Peters6d6c1a32001-08-02 04:15:00 +000089
Tim Peters496563a2002-04-01 00:28:59 +000090del sys, _f, _C, _x # Not for export