mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Rename 'getset' to 'property'.
This commit is contained in:
parent
2872e8a654
commit
8bce4acb17
6 changed files with 45 additions and 45 deletions
|
@ -32,4 +32,4 @@ extern DL_IMPORT(PyObject *) PyDictProxy_New(PyObject *);
|
||||||
extern DL_IMPORT(PyObject *) PyWrapper_New(PyObject *, PyObject *);
|
extern DL_IMPORT(PyObject *) PyWrapper_New(PyObject *, PyObject *);
|
||||||
|
|
||||||
|
|
||||||
extern DL_IMPORT(PyTypeObject) PyGetSet_Type;
|
extern DL_IMPORT(PyTypeObject) PyProperty_Type;
|
||||||
|
|
|
@ -48,12 +48,12 @@ class Rat(object):
|
||||||
def _get_num(self):
|
def _get_num(self):
|
||||||
"""Accessor function for read-only 'num' attribute of Rat."""
|
"""Accessor function for read-only 'num' attribute of Rat."""
|
||||||
return self.__num
|
return self.__num
|
||||||
num = getset(_get_num, None)
|
num = property(_get_num, None)
|
||||||
|
|
||||||
def _get_den(self):
|
def _get_den(self):
|
||||||
"""Accessor function for read-only 'den' attribute of Rat."""
|
"""Accessor function for read-only 'den' attribute of Rat."""
|
||||||
return self.__den
|
return self.__den
|
||||||
den = getset(_get_den, None)
|
den = property(_get_den, None)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
"""Convert a Rat to an string resembling a Rat constructor call."""
|
"""Convert a Rat to an string resembling a Rat constructor call."""
|
||||||
|
|
|
@ -578,8 +578,8 @@ def metaclass():
|
||||||
return "E" + self.__super.meth()
|
return "E" + self.__super.meth()
|
||||||
verify(E().meth() == "EBCA")
|
verify(E().meth() == "EBCA")
|
||||||
|
|
||||||
class autogetset(type):
|
class autoproperty(type):
|
||||||
# Automatically create getset attributes when methods
|
# Automatically create property attributes when methods
|
||||||
# named _get_x and/or _set_x are found
|
# named _get_x and/or _set_x are found
|
||||||
def __new__(metaclass, name, bases, dict):
|
def __new__(metaclass, name, bases, dict):
|
||||||
hits = {}
|
hits = {}
|
||||||
|
@ -595,11 +595,11 @@ def metaclass():
|
||||||
set = val
|
set = val
|
||||||
hits[key] = get, set
|
hits[key] = get, set
|
||||||
for key, (get, set) in hits.iteritems():
|
for key, (get, set) in hits.iteritems():
|
||||||
dict[key] = getset(get, set)
|
dict[key] = property(get, set)
|
||||||
return super(autogetset, metaclass).__new__(metaclass,
|
return super(autoproperty, metaclass).__new__(metaclass,
|
||||||
name, bases, dict)
|
name, bases, dict)
|
||||||
class A:
|
class A:
|
||||||
__metaclass__ = autogetset
|
__metaclass__ = autoproperty
|
||||||
def _get_x(self):
|
def _get_x(self):
|
||||||
return -self.__x
|
return -self.__x
|
||||||
def _set_x(self, x):
|
def _set_x(self, x):
|
||||||
|
@ -610,7 +610,7 @@ def metaclass():
|
||||||
verify(a.x == 12)
|
verify(a.x == 12)
|
||||||
verify(a._A__x == -12)
|
verify(a._A__x == -12)
|
||||||
|
|
||||||
class multimetaclass(autogetset, autosuper):
|
class multimetaclass(autoproperty, autosuper):
|
||||||
# Merge of multiple cooperating metaclasses
|
# Merge of multiple cooperating metaclasses
|
||||||
pass
|
pass
|
||||||
class A:
|
class A:
|
||||||
|
@ -1274,8 +1274,8 @@ def weakrefs():
|
||||||
verify(r() is None)
|
verify(r() is None)
|
||||||
del r
|
del r
|
||||||
|
|
||||||
def getsets():
|
def properties():
|
||||||
if verbose: print "Testing getset..."
|
if verbose: print "Testing property..."
|
||||||
class C(object):
|
class C(object):
|
||||||
def getx(self):
|
def getx(self):
|
||||||
return self.__x
|
return self.__x
|
||||||
|
@ -1283,7 +1283,7 @@ def getsets():
|
||||||
self.__x = value
|
self.__x = value
|
||||||
def delx(self):
|
def delx(self):
|
||||||
del self.__x
|
del self.__x
|
||||||
x = getset(getx, setx, delx)
|
x = property(getx, setx, delx)
|
||||||
a = C()
|
a = C()
|
||||||
verify(not hasattr(a, "x"))
|
verify(not hasattr(a, "x"))
|
||||||
a.x = 42
|
a.x = 42
|
||||||
|
@ -1445,7 +1445,7 @@ def all():
|
||||||
methods()
|
methods()
|
||||||
specials()
|
specials()
|
||||||
weakrefs()
|
weakrefs()
|
||||||
getsets()
|
properties()
|
||||||
supers()
|
supers()
|
||||||
inherits()
|
inherits()
|
||||||
|
|
||||||
|
|
|
@ -315,7 +315,7 @@ test_5 = """
|
||||||
Attributes defined by get/set methods
|
Attributes defined by get/set methods
|
||||||
|
|
||||||
|
|
||||||
>>> class getset(object):
|
>>> class property(object):
|
||||||
...
|
...
|
||||||
... def __init__(self, get, set=None):
|
... def __init__(self, get, set=None):
|
||||||
... self.__get = get
|
... self.__get = get
|
||||||
|
@ -344,7 +344,7 @@ getx() and and setx():
|
||||||
... if x < 0: x = 0
|
... if x < 0: x = 0
|
||||||
... self.__x = x
|
... self.__x = x
|
||||||
...
|
...
|
||||||
... x = getset(getx, setx)
|
... x = property(getx, setx)
|
||||||
|
|
||||||
Here's a small demonstration:
|
Here's a small demonstration:
|
||||||
|
|
||||||
|
@ -357,11 +357,11 @@ Here's a small demonstration:
|
||||||
0
|
0
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
Hmm -- getset is builtin now, so let's try it that way too.
|
Hmm -- property is builtin now, so let's try it that way too.
|
||||||
|
|
||||||
>>> del getset # unmask the builtin
|
>>> del property # unmask the builtin
|
||||||
>>> getset
|
>>> property
|
||||||
<type 'getset'>
|
<type 'property'>
|
||||||
|
|
||||||
>>> class C(object):
|
>>> class C(object):
|
||||||
... def __init__(self):
|
... def __init__(self):
|
||||||
|
@ -371,7 +371,7 @@ Hmm -- getset is builtin now, so let's try it that way too.
|
||||||
... def setx(self, x):
|
... def setx(self, x):
|
||||||
... if x < 0: x = 0
|
... if x < 0: x = 0
|
||||||
... self.__x = x
|
... self.__x = x
|
||||||
... x = getset(getx, setx)
|
... x = property(getx, setx)
|
||||||
|
|
||||||
|
|
||||||
>>> a = C()
|
>>> a = C()
|
||||||
|
|
|
@ -840,10 +840,10 @@ PyWrapper_New(PyObject *d, PyObject *self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* A built-in 'getset' type */
|
/* A built-in 'property' type */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
class getset(object):
|
class property(object):
|
||||||
|
|
||||||
def __init__(self, get=None, set=None):
|
def __init__(self, get=None, set=None):
|
||||||
self.__get = get
|
self.__get = get
|
||||||
|
@ -867,12 +867,12 @@ typedef struct {
|
||||||
PyObject *get;
|
PyObject *get;
|
||||||
PyObject *set;
|
PyObject *set;
|
||||||
PyObject *del;
|
PyObject *del;
|
||||||
} getsetobject;
|
} propertyobject;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
getset_dealloc(PyObject *self)
|
property_dealloc(PyObject *self)
|
||||||
{
|
{
|
||||||
getsetobject *gs = (getsetobject *)self;
|
propertyobject *gs = (propertyobject *)self;
|
||||||
|
|
||||||
Py_XDECREF(gs->get);
|
Py_XDECREF(gs->get);
|
||||||
Py_XDECREF(gs->set);
|
Py_XDECREF(gs->set);
|
||||||
|
@ -881,9 +881,9 @@ getset_dealloc(PyObject *self)
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
getset_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
||||||
{
|
{
|
||||||
getsetobject *gs = (getsetobject *)self;
|
propertyobject *gs = (propertyobject *)self;
|
||||||
|
|
||||||
if (gs->get == NULL) {
|
if (gs->get == NULL) {
|
||||||
PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
|
PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
|
||||||
|
@ -897,9 +897,9 @@ getset_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
getset_descr_set(PyObject *self, PyObject *obj, PyObject *value)
|
property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
|
||||||
{
|
{
|
||||||
getsetobject *gs = (getsetobject *)self;
|
propertyobject *gs = (propertyobject *)self;
|
||||||
PyObject *func, *res;
|
PyObject *func, *res;
|
||||||
|
|
||||||
if (value == NULL)
|
if (value == NULL)
|
||||||
|
@ -924,12 +924,12 @@ getset_descr_set(PyObject *self, PyObject *obj, PyObject *value)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
getset_init(PyObject *self, PyObject *args, PyObject *kwds)
|
property_init(PyObject *self, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
PyObject *get = NULL, *set = NULL, *del = NULL;
|
PyObject *get = NULL, *set = NULL, *del = NULL;
|
||||||
getsetobject *gs = (getsetobject *)self;
|
propertyobject *gs = (propertyobject *)self;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "|OOO:getset", &get, &set, &del))
|
if (!PyArg_ParseTuple(args, "|OOO:property", &get, &set, &del))
|
||||||
return -1;
|
return -1;
|
||||||
if (get == Py_None)
|
if (get == Py_None)
|
||||||
get = NULL;
|
get = NULL;
|
||||||
|
@ -944,23 +944,23 @@ getset_init(PyObject *self, PyObject *args, PyObject *kwds)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char getset_doc[] =
|
static char property_doc[] =
|
||||||
"getset([getfunc[, setfunc[, delfunc]]]) -> getset attribute\n"
|
"property([getfunc[, setfunc[, delfunc]]]) -> property attribute\n"
|
||||||
"Typical use to define a managed attribute x of C instances:\n"
|
"Typical use to define a managed attribute x of C instances:\n"
|
||||||
"class C(object):\n"
|
"class C(object):\n"
|
||||||
" def getx(self): return self.__x\n"
|
" def getx(self): return self.__x\n"
|
||||||
" def setx(self, value): self.__x = value\n"
|
" def setx(self, value): self.__x = value\n"
|
||||||
" def delx(self): del self.__x\n"
|
" def delx(self): del self.__x\n"
|
||||||
" x = getset(getx, setx, delx)";
|
" x = property(getx, setx, delx)";
|
||||||
|
|
||||||
PyTypeObject PyGetSet_Type = {
|
PyTypeObject PyProperty_Type = {
|
||||||
PyObject_HEAD_INIT(&PyType_Type)
|
PyObject_HEAD_INIT(&PyType_Type)
|
||||||
0, /* ob_size */
|
0, /* ob_size */
|
||||||
"getset", /* tp_name */
|
"property", /* tp_name */
|
||||||
sizeof(getsetobject), /* tp_basicsize */
|
sizeof(propertyobject), /* tp_basicsize */
|
||||||
0, /* tp_itemsize */
|
0, /* tp_itemsize */
|
||||||
/* methods */
|
/* methods */
|
||||||
getset_dealloc, /* tp_dealloc */
|
property_dealloc, /* tp_dealloc */
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
|
@ -976,7 +976,7 @@ PyTypeObject PyGetSet_Type = {
|
||||||
0, /* tp_setattro */
|
0, /* tp_setattro */
|
||||||
0, /* tp_as_buffer */
|
0, /* tp_as_buffer */
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
||||||
getset_doc, /* tp_doc */
|
property_doc, /* tp_doc */
|
||||||
0, /* tp_traverse */
|
0, /* tp_traverse */
|
||||||
0, /* tp_clear */
|
0, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
0, /* tp_richcompare */
|
||||||
|
@ -988,10 +988,10 @@ PyTypeObject PyGetSet_Type = {
|
||||||
0, /* tp_getset */
|
0, /* tp_getset */
|
||||||
0, /* tp_base */
|
0, /* tp_base */
|
||||||
0, /* tp_dict */
|
0, /* tp_dict */
|
||||||
getset_descr_get, /* tp_descr_get */
|
property_descr_get, /* tp_descr_get */
|
||||||
getset_descr_set, /* tp_descr_set */
|
property_descr_set, /* tp_descr_set */
|
||||||
0, /* tp_dictoffset */
|
0, /* tp_dictoffset */
|
||||||
getset_init, /* tp_init */
|
property_init, /* tp_init */
|
||||||
PyType_GenericAlloc, /* tp_alloc */
|
PyType_GenericAlloc, /* tp_alloc */
|
||||||
PyType_GenericNew, /* tp_new */
|
PyType_GenericNew, /* tp_new */
|
||||||
_PyObject_Del, /* tp_free */
|
_PyObject_Del, /* tp_free */
|
||||||
|
|
|
@ -1869,8 +1869,8 @@ _PyBuiltin_Init(void)
|
||||||
if (PyDict_SetItemString(dict, "float",
|
if (PyDict_SetItemString(dict, "float",
|
||||||
(PyObject *) &PyFloat_Type) < 0)
|
(PyObject *) &PyFloat_Type) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (PyDict_SetItemString(dict, "getset",
|
if (PyDict_SetItemString(dict, "property",
|
||||||
(PyObject *) &PyGetSet_Type) < 0)
|
(PyObject *) &PyProperty_Type) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (PyDict_SetItemString(dict, "int", (PyObject *) &PyInt_Type) < 0)
|
if (PyDict_SetItemString(dict, "int", (PyObject *) &PyInt_Type) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue