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