forked from oracle/graalpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperty.py
More file actions
176 lines (141 loc) · 6.05 KB
/
property.py
File metadata and controls
176 lines (141 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# The Universal Permissive License (UPL), Version 1.0
#
# Subject to the condition set forth below, permission is hereby granted to any
# person obtaining a copy of this software, associated documentation and/or
# data (collectively the "Software"), free of charge and under any and all
# copyright rights in the Software, and any and all patent rights owned or
# freely licensable by each licensor hereunder covering either (i) the
# unmodified Software as contributed to or provided by such licensor, or (ii)
# the Larger Works (as defined below), to deal in both
#
# (a) the Software, and
#
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
# one is included with the Software each a "Larger Work" to which the Software
# is contributed by such licensors),
#
# without restriction, including without limitation the rights to copy, create
# derivative works of, display, perform, and distribute the Software and make,
# use, sell, offer for sale, import, export, have made, and have sold the
# Software and the Larger Work(s), and to sublicense the foregoing rights on
# either these or other terms.
#
# This license is subject to the following condition:
#
# The above copyright notice and either this complete permission notice or at a
# minimum a reference to the UPL must be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
def _f(): pass
FunctionType = type(_f)
descriptor = type(FunctionType.__code__)
class property(object):
"""
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
fget is a function to be used for getting an attribute value, and likewise
fset is a function for setting, and fdel a function for del'ing, an
attribute. Typical use is to define a managed attribute x:
class C(object):
def getx(self): return self._x
def setx(self, value): self._x = value
def delx(self): del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")
Decorators make defining new properties or modifying existing ones easy:
class C(object):
@property
def x(self):
"I am the 'x' property."
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
"""
__slots__ = ['_fget', '_fset', '_fdel', '_doc', '_getter_doc', '_name', '_owner']
def __init__(self, fget=None, fset=None, fdel=None, doc=None, name=None):
self._fget = fget
self._fset = fset
self._fdel = fdel
self._doc = doc
self._getter_doc = False
if self._doc is None and fget:
gdoc = getattr(fget, "__doc__")
if gdoc:
if type(self) is property:
self._doc = gdoc
else:
self.__doc__ = gdoc
self._getter_doc = True
self._name = name
self._owner = None
def __get__(self, instance, owner=None):
if self._owner is None:
self._owner = owner
if instance is None:
return self
if self._fget is None:
raise AttributeError("unreadable attribute")
return self._fget(instance)
def __set__(self, instance, value):
if self._fset is None:
raise AttributeError("attribute '{}' of '{}' objects is not writable".format(
self._name, getattr(self._owner, "__name__", str(self._owner))))
return self._fset(instance, value)
def __delete__(self, instance):
if self._fdel is None:
raise AttributeError("can't delete attribute")
return self._fdel(instance)
def setter(self, func):
return self.__copy(fset=func)
def deleter(self, func):
return self.__copy(fdel=func)
def getter(self, func):
return self.__copy(fget=func)
def __repr__(self):
return "'".join([
"<property ",
str(self._name),
" of ",
getattr(self._owner, "__name__", str(self._owner)),
" objects>"
])
def __copy(self, fget=None, fset=None, fdel=None):
_fget = fget if fget is not None else self._fget
_fset = fset if fset is not None else self._fset
_fdel = fdel if fdel is not None else self._fdel
_doc = None if (self._getter_doc and _fget) else self._doc
return type(self)(fget=_fget, fset=_fset, fdel=_fdel, doc=_doc, name=self._name)
def isabstract(self):
return (bool(getattr(self._fget, "__isabstractmethod__", False)) or
bool(getattr(self._fset, "__isabstractmethod__", False)) or
bool(getattr(self._fdel, "__isabstractmethod__", False)))
property.__isabstractmethod__ = descriptor(fget=isabstract, name="__isabstractmethod__", owner=property)
def get_doc(self):
return self._doc
def set_doc(self, value):
self._doc = value
self._getter_doc = False
property.__doc__ = descriptor(fget=get_doc, fset=set_doc, name="__doc__", owner=property)
def get_fget(self):
return self._fget
def set_fxxx(self, value):
raise AttributeError("readonly attribute")
property.fget = descriptor(fget=get_fget, fset=set_fxxx, name="fget", owner=property)
def get_fset(self):
return self._fset
property.fset = descriptor(fget=get_fset, fset=set_fxxx, name="fset", owner=property)
def get_fdel(self):
return self._fdel
property.fdel = descriptor(fget=get_fdel, fset=set_fxxx, name="fdel", owner=property)