Mercurial > cpython-lru_cache-weakref
annotate Lib/types.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 39b06c3fbe2e |
| children |
| 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 |
|
85796
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
102 |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
103 class DynamicClassAttribute: |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
104 """Route attribute access on a class to __getattr__. |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
105 |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
106 This is a descriptor, used to define attributes that act differently when |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
107 accessed through an instance and through a class. Instance access remains |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
108 normal, but access to an attribute through a class will be routed to the |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
109 class's __getattr__ method; this is done by raising AttributeError. |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
110 |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
111 This allows one to have properties active on an instance, and have virtual |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
112 attributes on the class with the same name (see Enum for an example). |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
113 |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
114 """ |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
115 def __init__(self, fget=None, fset=None, fdel=None, doc=None): |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
116 self.fget = fget |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
117 self.fset = fset |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
118 self.fdel = fdel |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
119 # next two lines make DynamicClassAttribute act the same as property |
|
86419
39b06c3fbe2e
Close #19030: inspect.getmembers and inspect.classify_class_attrs
Ethan Furman <ethan@stoneleaf.us>
parents:
85796
diff
changeset
|
120 self.__doc__ = doc or fget.__doc__ |
|
85796
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
121 self.overwrite_doc = doc is None |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
122 # support for abstract methods |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
123 self.__isabstractmethod__ = bool(getattr(fget, '__isabstractmethod__', False)) |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
124 |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
125 def __get__(self, instance, ownerclass=None): |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
126 if instance is None: |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
127 if self.__isabstractmethod__: |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
128 return self |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
129 raise AttributeError() |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
130 elif self.fget is None: |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
131 raise AttributeError("unreadable attribute") |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
132 return self.fget(instance) |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
133 |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
134 def __set__(self, instance, value): |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
135 if self.fset is None: |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
136 raise AttributeError("can't set attribute") |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
137 self.fset(instance, value) |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
138 |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
139 def __delete__(self, instance): |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
140 if self.fdel is None: |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
141 raise AttributeError("can't delete attribute") |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
142 self.fdel(instance) |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
143 |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
144 def getter(self, fget): |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
145 fdoc = fget.__doc__ if self.overwrite_doc else None |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
146 result = type(self)(fget, self.fset, self.fdel, fdoc or self.__doc__) |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
147 result.overwrite_doc = self.overwrite_doc |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
148 return result |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
149 |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
150 def setter(self, fset): |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
151 result = type(self)(self.fget, fset, self.fdel, self.__doc__) |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
152 result.overwrite_doc = self.overwrite_doc |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
153 return result |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
154 |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
155 def deleter(self, fdel): |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
156 result = type(self)(self.fget, self.fset, fdel, self.__doc__) |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
157 result.overwrite_doc = self.overwrite_doc |
|
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
77339
diff
changeset
|
158 return result |
