annotate Lib/types.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents a13a4393f45c
children fa0ac845929b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10429
diff changeset
1 """Define names for all type symbols known in the standard interpreter.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10429
diff changeset
2
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10429
diff changeset
3 Types that are part of optional modules (e.g. array) are not listed.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 10429
diff changeset
4 """
18609
59446d0f8341 Preliminary support for "from __future__ import generators" to enable
Guido van Rossum <guido@python.org>
parents: 18273
diff changeset
5 from __future__ import generators
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 import sys
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
20097
a381ab0f9d28 SF [#463737] Add types.CallableIterType
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
9 # Iterators in Python aren't a matter of type but of protocol. A large
a381ab0f9d28 SF [#463737] Add types.CallableIterType
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
10 # and changing number of builtin types implement *some* flavor of
a381ab0f9d28 SF [#463737] Add types.CallableIterType
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
11 # iterator. Don't check the type! Use hasattr to check for both
a381ab0f9d28 SF [#463737] Add types.CallableIterType
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
12 # "__iter__" and "next" attributes instead.
a381ab0f9d28 SF [#463737] Add types.CallableIterType
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
13
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 NoneType = type(None)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18609
diff changeset
15 TypeType = type
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18609
diff changeset
16 ObjectType = object
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
19071
764923d985a7 Patch #449083: Use builtins to initalize the module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
18 IntType = int
764923d985a7 Patch #449083: Use builtins to initalize the module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
19 LongType = long
764923d985a7 Patch #449083: Use builtins to initalize the module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
20 FloatType = float
3385
d5532a71b454 better way to define ComplexType
Guido van Rossum <guido@python.org>
parents: 3351
diff changeset
21 try:
19071
764923d985a7 Patch #449083: Use builtins to initalize the module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
22 ComplexType = complex
3385
d5532a71b454 better way to define ComplexType
Guido van Rossum <guido@python.org>
parents: 3351
diff changeset
23 except NameError:
6235
24845968323f Added some try-excepts so that it can be imported in restricted mode
Guido van Rossum <guido@python.org>
parents: 4403
diff changeset
24 pass
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
19071
764923d985a7 Patch #449083: Use builtins to initalize the module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
26 StringType = str
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19172
diff changeset
27 try:
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19172
diff changeset
28 UnicodeType = unicode
21129
a13a4393f45c Patch #487455: make types.StringTypes a tuple.
Martin v. Löwis <martin@v.loewis.de>
parents: 20754
diff changeset
29 StringTypes = (StringType, UnicodeType)
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19172
diff changeset
30 except NameError:
21129
a13a4393f45c Patch #487455: make types.StringTypes a tuple.
Martin v. Löwis <martin@v.loewis.de>
parents: 20754
diff changeset
31 StringTypes = (StringType,)
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19172
diff changeset
32
10429
f9ad148c980c Added BufferType, the type returned by the new builtin buffer(). Greg Stein.
Guido van Rossum <guido@python.org>
parents: 9659
diff changeset
33 BufferType = type(buffer(''))
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
19071
764923d985a7 Patch #449083: Use builtins to initalize the module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
35 TupleType = tuple
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18609
diff changeset
36 ListType = list
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20115
diff changeset
37 DictType = DictionaryType = dict
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
1917
014a1fb9c646 Cosmetic changes
Guido van Rossum <guido@python.org>
parents: 1680
diff changeset
39 def _f(): pass
014a1fb9c646 Cosmetic changes
Guido van Rossum <guido@python.org>
parents: 1680
diff changeset
40 FunctionType = type(_f)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6347
diff changeset
41 LambdaType = type(lambda: None) # Same as FunctionType
6235
24845968323f Added some try-excepts so that it can be imported in restricted mode
Guido van Rossum <guido@python.org>
parents: 4403
diff changeset
42 try:
24845968323f Added some try-excepts so that it can be imported in restricted mode
Guido van Rossum <guido@python.org>
parents: 4403
diff changeset
43 CodeType = type(_f.func_code)
19172
cb346baedad6 Only catch the errors that can actually occur, as reported in bug #411881.
Martin v. Löwis <martin@v.loewis.de>
parents: 19071
diff changeset
44 except RuntimeError:
cb346baedad6 Only catch the errors that can actually occur, as reported in bug #411881.
Martin v. Löwis <martin@v.loewis.de>
parents: 19071
diff changeset
45 # Execution in restricted environment
6235
24845968323f Added some try-excepts so that it can be imported in restricted mode
Guido van Rossum <guido@python.org>
parents: 4403
diff changeset
46 pass
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
18273
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 11988
diff changeset
48 def g():
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 11988
diff changeset
49 yield 1
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 11988
diff changeset
50 GeneratorType = type(g())
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 11988
diff changeset
51 del g
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 11988
diff changeset
52
1917
014a1fb9c646 Cosmetic changes
Guido van Rossum <guido@python.org>
parents: 1680
diff changeset
53 class _C:
6235
24845968323f Added some try-excepts so that it can be imported in restricted mode
Guido van Rossum <guido@python.org>
parents: 4403
diff changeset
54 def _m(self): pass
1917
014a1fb9c646 Cosmetic changes
Guido van Rossum <guido@python.org>
parents: 1680
diff changeset
55 ClassType = type(_C)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6347
diff changeset
56 UnboundMethodType = type(_C._m) # Same as MethodType
1917
014a1fb9c646 Cosmetic changes
Guido van Rossum <guido@python.org>
parents: 1680
diff changeset
57 _x = _C()
014a1fb9c646 Cosmetic changes
Guido van Rossum <guido@python.org>
parents: 1680
diff changeset
58 InstanceType = type(_x)
014a1fb9c646 Cosmetic changes
Guido van Rossum <guido@python.org>
parents: 1680
diff changeset
59 MethodType = type(_x._m)
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
1917
014a1fb9c646 Cosmetic changes
Guido van Rossum <guido@python.org>
parents: 1680
diff changeset
61 BuiltinFunctionType = type(len)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6347
diff changeset
62 BuiltinMethodType = type([].append) # Same as BuiltinFunctionType
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 ModuleType = type(sys)
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
65 FileType = file
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 XRangeType = type(xrange(0))
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 try:
6235
24845968323f Added some try-excepts so that it can be imported in restricted mode
Guido van Rossum <guido@python.org>
parents: 4403
diff changeset
69 raise TypeError
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 except TypeError:
6235
24845968323f Added some try-excepts so that it can be imported in restricted mode
Guido van Rossum <guido@python.org>
parents: 4403
diff changeset
71 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6347
diff changeset
72 tb = sys.exc_info()[2]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6347
diff changeset
73 TracebackType = type(tb)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6347
diff changeset
74 FrameType = type(tb.tb_frame)
19172
cb346baedad6 Only catch the errors that can actually occur, as reported in bug #411881.
Martin v. Löwis <martin@v.loewis.de>
parents: 19071
diff changeset
75 except AttributeError:
cb346baedad6 Only catch the errors that can actually occur, as reported in bug #411881.
Martin v. Löwis <martin@v.loewis.de>
parents: 19071
diff changeset
76 # In the restricted environment, exc_info returns (None, None,
cb346baedad6 Only catch the errors that can actually occur, as reported in bug #411881.
Martin v. Löwis <martin@v.loewis.de>
parents: 19071
diff changeset
77 # None) Then, tb.tb_frame gives an attribute error
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6347
diff changeset
78 pass
6347
2656d1eea792 Use sys.exc_info() where needed.
Guido van Rossum <guido@python.org>
parents: 6235
diff changeset
79 tb = None; del tb
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
4402
1d275cd42c9b Added Slice and Ellipses types.
Guido van Rossum <guido@python.org>
parents: 3385
diff changeset
81 SliceType = type(slice(0))
4403
3b3f7c3f6da2 Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents: 4402
diff changeset
82 EllipsisType = type(Ellipsis)
4402
1d275cd42c9b Added Slice and Ellipses types.
Guido van Rossum <guido@python.org>
parents: 3385
diff changeset
83
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18609
diff changeset
84 DictProxyType = type(TypeType.__dict__)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18609
diff changeset
85
20115
8ec95ce78735 Don't export generators future info
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20097
diff changeset
86 del sys, _f, _C, _x, generators # Not for export