Mercurial > cpython-lru_cache-weakref
annotate Lib/types.py @ 84007:dbfbdf2b5c19 3.3
Fix #17967: For ftp urls CWD to target instead of hopping to each directory
towards target. This fixes a bug where target is accessible, but parent
directories are restricted.
| author | Senthil Kumaran <senthil@uthcode.com> |
|---|---|
| date | Sat, 01 Jun 2013 08:27:06 -0700 |
| parents | 9c445f4695c1 |
| children | 436b606ecfe8 |
| rev | line source |
|---|---|
|
44370
bce70a477f1d
Fix docstring of types.py.
Georg Brandl <georg@python.org>
parents:
44049
diff
changeset
|
1 """ |
|
bce70a477f1d
Fix docstring of types.py.
Georg Brandl <georg@python.org>
parents:
44049
diff
changeset
|
2 Define names for built-in types that aren't directly accessible as a builtin. |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
10429
diff
changeset
|
3 """ |
|
1680
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 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
|
5 |
|
20097
a381ab0f9d28
SF [#463737] Add types.CallableIterType
Tim Peters <tim.peters@gmail.com>
parents:
19894
diff
changeset
|
6 # 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
|
7 # 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
|
8 # iterator. Don't check the type! Use hasattr to check for both |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40910
diff
changeset
|
9 # "__iter__" and "__next__" attributes instead. |
|
20097
a381ab0f9d28
SF [#463737] Add types.CallableIterType
Tim Peters <tim.peters@gmail.com>
parents:
19894
diff
changeset
|
10 |
| 1917 | 11 def _f(): pass |
| 12 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
|
13 LambdaType = type(lambda: None) # Same as FunctionType |
|
41715
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41645
diff
changeset
|
14 CodeType = type(_f.__code__) |
|
76334
c3a0197256ee
Issue #14386: Expose the dict_proxy internal type as types.MappingProxyType
Victor Stinner <victor.stinner@gmail.com>
parents:
46370
diff
changeset
|
15 MappingProxyType = type(type.__dict__) |
|
77339
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
77059
diff
changeset
|
16 SimpleNamespace = type(sys.implementation) |
|
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 |
|
32726
e4cc78884fc4
Oops! Restored the pickle test to test_pyclbr, but changed types.py so
Tim Peters <tim.peters@gmail.com>
parents:
27803
diff
changeset
|
18 def _g(): |
|
18273
9e326c9e6be5
Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents:
11988
diff
changeset
|
19 yield 1 |
|
32726
e4cc78884fc4
Oops! Restored the pickle test to test_pyclbr, but changed types.py so
Tim Peters <tim.peters@gmail.com>
parents:
27803
diff
changeset
|
20 GeneratorType = type(_g()) |
|
18273
9e326c9e6be5
Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents:
11988
diff
changeset
|
21 |
| 1917 | 22 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
|
23 def _m(self): pass |
|
38397
ac45eca8c35c
SF patch 1495675: Remove types.InstanceType and new.instance
Guido van Rossum <guido@python.org>
parents:
32726
diff
changeset
|
24 MethodType = type(_C()._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
|
25 |
| 1917 | 26 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
|
27 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
|
28 |
|
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 ModuleType = type(sys) |
|
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
|
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 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
|
32 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
|
33 except TypeError: |
|
41715
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41645
diff
changeset
|
34 tb = sys.exc_info()[2] |
|
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41645
diff
changeset
|
35 TracebackType = type(tb) |
|
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41645
diff
changeset
|
36 FrameType = type(tb.tb_frame) |
|
6347
2656d1eea792
Use sys.exc_info() where needed.
Guido van Rossum <guido@python.org>
parents:
6235
diff
changeset
|
37 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
|
38 |
|
46370
2e917b0c8f6e
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44370
diff
changeset
|
39 # For Jython, the following two types are identical |
|
2e917b0c8f6e
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44370
diff
changeset
|
40 GetSetDescriptorType = type(FunctionType.__code__) |
|
2e917b0c8f6e
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44370
diff
changeset
|
41 MemberDescriptorType = type(FunctionType.__globals__) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38397
diff
changeset
|
42 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38397
diff
changeset
|
43 del sys, _f, _g, _C, # Not for export |
|
77059
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
44 |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
45 |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
46 # Provide a PEP 3115 compliant mechanism for class creation |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
47 def new_class(name, bases=(), kwds=None, exec_body=None): |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
48 """Create a class object dynamically using the appropriate metaclass.""" |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
49 meta, ns, kwds = prepare_class(name, bases, kwds) |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
50 if exec_body is not None: |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
51 exec_body(ns) |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
52 return meta(name, bases, ns, **kwds) |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
53 |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
54 def prepare_class(name, bases=(), kwds=None): |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
55 """Call the __prepare__ method of the appropriate metaclass. |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
56 |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
57 Returns (metaclass, namespace, kwds) as a 3-tuple |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
58 |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
59 *metaclass* is the appropriate metaclass |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
60 *namespace* is the prepared class namespace |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
61 *kwds* is an updated copy of the passed in kwds argument with any |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
62 'metaclass' entry removed. If no kwds argument is passed in, this will |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
63 be an empty dict. |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
64 """ |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
65 if kwds is None: |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
66 kwds = {} |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
67 else: |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
68 kwds = dict(kwds) # Don't alter the provided mapping |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
69 if 'metaclass' in kwds: |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
70 meta = kwds.pop('metaclass') |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
71 else: |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
72 if bases: |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
73 meta = type(bases[0]) |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
74 else: |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
75 meta = type |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
76 if isinstance(meta, type): |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
77 # when meta is a type, we first determine the most-derived metaclass |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
78 # instead of invoking the initial candidate directly |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
79 meta = _calculate_meta(meta, bases) |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
80 if hasattr(meta, '__prepare__'): |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
81 ns = meta.__prepare__(name, bases, **kwds) |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
82 else: |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
83 ns = {} |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
84 return meta, ns, kwds |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
85 |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
86 def _calculate_meta(meta, bases): |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
87 """Calculate the most derived metaclass.""" |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
88 winner = meta |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
89 for base in bases: |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
90 base_meta = type(base) |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
91 if issubclass(winner, base_meta): |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
92 continue |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
93 if issubclass(base_meta, winner): |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
94 winner = base_meta |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
95 continue |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
96 # else: |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
97 raise TypeError("metaclass conflict: " |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
98 "the metaclass of a derived class " |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
99 "must be a (non-strict) subclass " |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
100 "of the metaclasses of all its bases") |
|
befd56673c80
Close #14588: added a PEP 3115 compliant dynamic type creation mechanism
Nick Coghlan <ncoghlan@gmail.com>
parents:
76334
diff
changeset
|
101 return winner |
