Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(157357)

Side by Side Diff: Objects/descrobject.c

Issue 11610: Improving property to accept abstract methods
Patch Set: Created 8 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* Descriptors -- a new, flexible way to describe attributes */ 1 /* Descriptors -- a new, flexible way to describe attributes */
2 2
3 #include "Python.h" 3 #include "Python.h"
4 #include "structmember.h" /* Why is this not included in Python.h? */ 4 #include "structmember.h" /* Why is this not included in Python.h? */
5 5
6 static void 6 static void
7 descr_dealloc(PyDescrObject *descr) 7 descr_dealloc(PyDescrObject *descr)
8 { 8 {
9 _PyObject_GC_UNTRACK(descr); 9 _PyObject_GC_UNTRACK(descr);
10 Py_XDECREF(descr->d_type); 10 Py_XDECREF(descr->d_type);
(...skipping 1307 matching lines...) Expand 10 before | Expand all | Expand 10 after
1318 else if (PyErr_ExceptionMatches(PyExc_Exception)) { 1318 else if (PyErr_ExceptionMatches(PyExc_Exception)) {
1319 PyErr_Clear(); 1319 PyErr_Clear();
1320 } 1320 }
1321 else { 1321 else {
1322 return -1; 1322 return -1;
1323 } 1323 }
1324 } 1324 }
1325 1325
1326 return 0; 1326 return 0;
1327 } 1327 }
1328
1329 static int
1330 property_check_isabstract(propertyobject *self, PyObject *obj)
1331 {
1332 if (obj == NULL)
1333 return 0;
1334
1335 PyObject* isabstract = PyObject_GetAttrString(obj, "__isabstractmethod__");
Benjamin Peterson 2011/11/29 02:45:54 You should use _PyObject_GetAttrId instead.
dsdale24 2011/11/29 19:28:03 I can't find any reference to to PyObject_GetAttrI
1336 if (isabstract == NULL) {
Benjamin Peterson 2011/11/29 02:45:54 Catchy only AttributeError.
dsdale24 2011/11/29 17:20:36 Sorry, I need more specific information on what ex
Benjamin Peterson 2011/11/29 17:27:46 Only clear the error if PyErr_ExceptionMatches(PyE
1337 PyErr_Clear();
1338 return 0;
1339 }
1340 int res = PyObject_IsTrue(isabstract);
Benjamin Peterson 2011/11/29 02:45:54 You must declare this at the top of the function.
1341 Py_DECREF(isabstract);
1342 return res;
1343 }
1344
1345 static PyObject *
1346 property_get__isabstractmethod__(PyObject *self, void *closure)
1347 {
1348 propertyobject *prop = (propertyobject *)self;
Benjamin Peterson 2011/11/29 02:45:54 Declare the function to take (propertyobject *) as
dsdale24 2011/11/29 17:20:36 Could you explain how to do that?
Benjamin Peterson 2011/11/29 17:27:46 I meant as is done above where the function takes
dsdale24 2011/11/29 19:24:13 I don't understand how to do the cast in the getse
1349
1350 int res = property_check_isabstract(prop, prop->prop_get);
1351 if (res == -1) {
Benjamin Peterson 2011/11/29 02:45:54 The braces on these ifs can be dropped.
1352 return NULL;
1353 }
1354 else if (res) {
1355 Py_RETURN_TRUE;
1356 }
1357
1358 res = property_check_isabstract(prop, prop->prop_set);
1359 if (res == -1) {
1360 return NULL;
1361 }
1362 else if (res) {
1363 Py_RETURN_TRUE;
1364 }
1365
1366 res = property_check_isabstract(prop, prop->prop_del);
1367 if (res == -1) {
1368 return NULL;
1369 }
1370 else if (res) {
1371 Py_RETURN_TRUE;
1372 }
1373 else {
Benjamin Peterson 2011/11/29 02:45:54 This else block isn't needed.
1374 Py_RETURN_FALSE;
1375 }
1376 }
1377
1378 static PyGetSetDef property_getsetlist[] = {
1379 {"__isabstractmethod__",
1380 (getter)property_get__isabstractmethod__, NULL,
1381 NULL,
1382 NULL},
1383 {NULL} /* Sentinel */
1384 };
1328 1385
1329 PyDoc_STRVAR(property_doc, 1386 PyDoc_STRVAR(property_doc,
1330 "property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n" 1387 "property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1331 "\n" 1388 "\n"
1332 "fget is a function to be used for getting an attribute value, and likewise\n" 1389 "fget is a function to be used for getting an attribute value, and likewise\n"
1333 "fset is a function for setting, and fdel a function for del'ing, an\n" 1390 "fset is a function for setting, and fdel a function for del'ing, an\n"
1334 "attribute. Typical use is to define a managed attribute x:\n" 1391 "attribute. Typical use is to define a managed attribute x:\n"
1335 "class C(object):\n" 1392 "class C(object):\n"
1336 " def getx(self): return self._x\n" 1393 " def getx(self): return self._x\n"
1337 " def setx(self, value): self._x = value\n" 1394 " def setx(self, value): self._x = value\n"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1384 Py_TPFLAGS_BASETYPE, /* tp_flags */ 1441 Py_TPFLAGS_BASETYPE, /* tp_flags */
1385 property_doc, /* tp_doc */ 1442 property_doc, /* tp_doc */
1386 property_traverse, /* tp_traverse */ 1443 property_traverse, /* tp_traverse */
1387 0, /* tp_clear */ 1444 0, /* tp_clear */
1388 0, /* tp_richcompare */ 1445 0, /* tp_richcompare */
1389 0, /* tp_weaklistoffset */ 1446 0, /* tp_weaklistoffset */
1390 0, /* tp_iter */ 1447 0, /* tp_iter */
1391 0, /* tp_iternext */ 1448 0, /* tp_iternext */
1392 property_methods, /* tp_methods */ 1449 property_methods, /* tp_methods */
1393 property_members, /* tp_members */ 1450 property_members, /* tp_members */
1394 0, /* tp_getset */ 1451 property_getsetlist, /* tp_getset */
1395 0, /* tp_base */ 1452 0, /* tp_base */
1396 0, /* tp_dict */ 1453 0, /* tp_dict */
1397 property_descr_get, /* tp_descr_get */ 1454 property_descr_get, /* tp_descr_get */
1398 property_descr_set, /* tp_descr_set */ 1455 property_descr_set, /* tp_descr_set */
1399 0, /* tp_dictoffset */ 1456 0, /* tp_dictoffset */
1400 property_init, /* tp_init */ 1457 property_init, /* tp_init */
1401 PyType_GenericAlloc, /* tp_alloc */ 1458 PyType_GenericAlloc, /* tp_alloc */
1402 PyType_GenericNew, /* tp_new */ 1459 PyType_GenericNew, /* tp_new */
1403 PyObject_GC_Del, /* tp_free */ 1460 PyObject_GC_Del, /* tp_free */
1404 }; 1461 };
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+