Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Define names for all type symbols known in the standard interpreter. |
| 2 | |
| 3 | Types that are part of optional modules (e.g. array) are not listed. |
| 4 | """ |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 5 | import sys |
| 6 | |
Tim Peters | 26991a7 | 2001-09-25 22:02:03 +0000 | [diff] [blame] | 7 | # 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 Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 12 | NoneType = type(None) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13 | TypeType = type |
| 14 | ObjectType = object |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 15 | |
Martin v. Löwis | ff88556 | 2001-08-08 16:02:01 +0000 | [diff] [blame] | 16 | IntType = int |
| 17 | LongType = long |
| 18 | FloatType = float |
Skip Montanaro | 012ed5d | 2002-05-21 23:17:12 +0000 | [diff] [blame] | 19 | BooleanType = bool |
Guido van Rossum | 0f6f812 | 1996-02-13 00:04:31 +0000 | [diff] [blame] | 20 | try: |
Martin v. Löwis | ff88556 | 2001-08-08 16:02:01 +0000 | [diff] [blame] | 21 | ComplexType = complex |
Guido van Rossum | 0f6f812 | 1996-02-13 00:04:31 +0000 | [diff] [blame] | 22 | except NameError: |
Guido van Rossum | 898c915 | 1997-09-04 22:12:34 +0000 | [diff] [blame] | 23 | pass |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 24 | |
Martin v. Löwis | ff88556 | 2001-08-08 16:02:01 +0000 | [diff] [blame] | 25 | StringType = str |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 26 | |
| 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öwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 30 | try: |
| 31 | UnicodeType = unicode |
Martin v. Löwis | 8b6bd42 | 2001-12-02 12:08:06 +0000 | [diff] [blame] | 32 | StringTypes = (StringType, UnicodeType) |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 33 | except NameError: |
Martin v. Löwis | 8b6bd42 | 2001-12-02 12:08:06 +0000 | [diff] [blame] | 34 | StringTypes = (StringType,) |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 36 | BufferType = buffer |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 37 | |
Martin v. Löwis | ff88556 | 2001-08-08 16:02:01 +0000 | [diff] [blame] | 38 | TupleType = tuple |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 39 | ListType = list |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 40 | DictType = DictionaryType = dict |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | adc940e | 1994-09-29 10:04:43 +0000 | [diff] [blame] | 42 | def _f(): pass |
| 43 | FunctionType = type(_f) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 44 | LambdaType = type(lambda: None) # Same as FunctionType |
Guido van Rossum | 898c915 | 1997-09-04 22:12:34 +0000 | [diff] [blame] | 45 | try: |
| 46 | CodeType = type(_f.func_code) |
Martin v. Löwis | 58682b7 | 2001-08-11 15:02:57 +0000 | [diff] [blame] | 47 | except RuntimeError: |
| 48 | # Execution in restricted environment |
Guido van Rossum | 898c915 | 1997-09-04 22:12:34 +0000 | [diff] [blame] | 49 | pass |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 50 | |
Tim Peters | 264c659 | 2004-07-18 00:08:11 +0000 | [diff] [blame] | 51 | def _g(): |
Tim Peters | 3e7b1a0 | 2001-06-25 19:46:25 +0000 | [diff] [blame] | 52 | yield 1 |
Tim Peters | 264c659 | 2004-07-18 00:08:11 +0000 | [diff] [blame] | 53 | GeneratorType = type(_g()) |
Tim Peters | 3e7b1a0 | 2001-06-25 19:46:25 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | adc940e | 1994-09-29 10:04:43 +0000 | [diff] [blame] | 55 | class _C: |
Guido van Rossum | 898c915 | 1997-09-04 22:12:34 +0000 | [diff] [blame] | 56 | def _m(self): pass |
Guido van Rossum | adc940e | 1994-09-29 10:04:43 +0000 | [diff] [blame] | 57 | ClassType = type(_C) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 58 | UnboundMethodType = type(_C._m) # Same as MethodType |
Guido van Rossum | 65810fe | 2006-05-26 19:12:38 +0000 | [diff] [blame^] | 59 | MethodType = type(_C()._m) |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | adc940e | 1994-09-29 10:04:43 +0000 | [diff] [blame] | 61 | BuiltinFunctionType = type(len) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 62 | BuiltinMethodType = type([].append) # Same as BuiltinFunctionType |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 63 | |
| 64 | ModuleType = type(sys) |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 65 | FileType = file |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 66 | XRangeType = xrange |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 67 | |
| 68 | try: |
Guido van Rossum | 898c915 | 1997-09-04 22:12:34 +0000 | [diff] [blame] | 69 | raise TypeError |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 70 | except TypeError: |
Guido van Rossum | 898c915 | 1997-09-04 22:12:34 +0000 | [diff] [blame] | 71 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 72 | tb = sys.exc_info()[2] |
| 73 | TracebackType = type(tb) |
| 74 | FrameType = type(tb.tb_frame) |
Martin v. Löwis | 58682b7 | 2001-08-11 15:02:57 +0000 | [diff] [blame] | 75 | except AttributeError: |
| 76 | # In the restricted environment, exc_info returns (None, None, |
| 77 | # None) Then, tb.tb_frame gives an attribute error |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 78 | pass |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame] | 79 | tb = None; del tb |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 81 | SliceType = slice |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 82 | EllipsisType = type(Ellipsis) |
Guido van Rossum | 8741b2b | 1996-10-11 16:00:06 +0000 | [diff] [blame] | 83 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 84 | DictProxyType = type(TypeType.__dict__) |
Just van Rossum | ba20533 | 2003-02-10 19:38:33 +0000 | [diff] [blame] | 85 | NotImplementedType = type(NotImplemented) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 65810fe | 2006-05-26 19:12:38 +0000 | [diff] [blame^] | 87 | del sys, _f, _g, _C # Not for export |