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

Delta Between Two Patch Sets: Objects/funcobject.c

Issue 11610: Improving property to accept abstract methods
Left Patch Set: Created 8 years, 4 months ago
Right Patch Set: Created 8 years 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Objects/descrobject.c ('k') | Objects/object.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 1
2 /* Function object implementation */ 2 /* Function object implementation */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 #include "code.h" 5 #include "code.h"
6 #include "structmember.h" 6 #include "structmember.h"
7 7
8 PyObject * 8 PyObject *
9 PyFunction_New(PyObject *code, PyObject *globals) 9 PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname )
10 { 10 {
11 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, 11 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
12 &PyFunction_Type); 12 &PyFunction_Type);
13 static PyObject *__name__ = 0; 13 static PyObject *__name__ = 0;
14 if (op != NULL) { 14 if (op != NULL) {
15 PyObject *doc; 15 PyObject *doc;
16 PyObject *consts; 16 PyObject *consts;
17 PyObject *module; 17 PyObject *module;
18 op->func_weakreflist = NULL; 18 op->func_weakreflist = NULL;
19 Py_INCREF(code); 19 Py_INCREF(code);
(...skipping 27 matching lines...) Expand all
47 if (!__name__) { 47 if (!__name__) {
48 Py_DECREF(op); 48 Py_DECREF(op);
49 return NULL; 49 return NULL;
50 } 50 }
51 } 51 }
52 module = PyDict_GetItem(globals, __name__); 52 module = PyDict_GetItem(globals, __name__);
53 if (module) { 53 if (module) {
54 Py_INCREF(module); 54 Py_INCREF(module);
55 op->func_module = module; 55 op->func_module = module;
56 } 56 }
57 if (qualname)
58 op->func_qualname = qualname;
59 else
60 op->func_qualname = op->func_name;
61 Py_INCREF(op->func_qualname);
57 } 62 }
58 else 63 else
59 return NULL; 64 return NULL;
60 _PyObject_GC_TRACK(op); 65 _PyObject_GC_TRACK(op);
61 return (PyObject *)op; 66 return (PyObject *)op;
67 }
68
69 PyObject *
70 PyFunction_New(PyObject *code, PyObject *globals)
71 {
72 return PyFunction_NewWithQualName(code, globals, NULL);
62 } 73 }
63 74
64 PyObject * 75 PyObject *
65 PyFunction_GetCode(PyObject *op) 76 PyFunction_GetCode(PyObject *op)
66 { 77 {
67 if (!PyFunction_Check(op)) { 78 if (!PyFunction_Check(op)) {
68 PyErr_BadInternalCall(); 79 PyErr_BadInternalCall();
69 return NULL; 80 return NULL;
70 } 81 }
71 return ((PyFunctionObject *) op) -> func_code; 82 return ((PyFunctionObject *) op) -> func_code;
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 /* Not legal to del f.func_name or to set it to anything 333 /* Not legal to del f.func_name or to set it to anything
323 * other than a string object. */ 334 * other than a string object. */
324 if (value == NULL || !PyUnicode_Check(value)) { 335 if (value == NULL || !PyUnicode_Check(value)) {
325 PyErr_SetString(PyExc_TypeError, 336 PyErr_SetString(PyExc_TypeError,
326 "__name__ must be set to a string object"); 337 "__name__ must be set to a string object");
327 return -1; 338 return -1;
328 } 339 }
329 tmp = op->func_name; 340 tmp = op->func_name;
330 Py_INCREF(value); 341 Py_INCREF(value);
331 op->func_name = value; 342 op->func_name = value;
343 Py_DECREF(tmp);
344 return 0;
345 }
346
347 static PyObject *
348 func_get_qualname(PyFunctionObject *op)
349 {
350 Py_INCREF(op->func_qualname);
351 return op->func_qualname;
352 }
353
354 static int
355 func_set_qualname(PyFunctionObject *op, PyObject *value)
356 {
357 PyObject *tmp;
358
359 /* Not legal to del f.__qualname__ or to set it to anything
360 * other than a string object. */
361 if (value == NULL || !PyUnicode_Check(value)) {
362 PyErr_SetString(PyExc_TypeError,
363 "__qualname__ must be set to a string object");
364 return -1;
365 }
366 tmp = op->func_qualname;
367 Py_INCREF(value);
368 op->func_qualname = value;
332 Py_DECREF(tmp); 369 Py_DECREF(tmp);
333 return 0; 370 return 0;
334 } 371 }
335 372
336 static PyObject * 373 static PyObject *
337 func_get_defaults(PyFunctionObject *op) 374 func_get_defaults(PyFunctionObject *op)
338 { 375 {
339 if (op->func_defaults == NULL) { 376 if (op->func_defaults == NULL) {
340 Py_INCREF(Py_None); 377 Py_INCREF(Py_None);
341 return Py_None; 378 return Py_None;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 static PyGetSetDef func_getsetlist[] = { 471 static PyGetSetDef func_getsetlist[] = {
435 {"__code__", (getter)func_get_code, (setter)func_set_code}, 472 {"__code__", (getter)func_get_code, (setter)func_set_code},
436 {"__defaults__", (getter)func_get_defaults, 473 {"__defaults__", (getter)func_get_defaults,
437 (setter)func_set_defaults}, 474 (setter)func_set_defaults},
438 {"__kwdefaults__", (getter)func_get_kwdefaults, 475 {"__kwdefaults__", (getter)func_get_kwdefaults,
439 (setter)func_set_kwdefaults}, 476 (setter)func_set_kwdefaults},
440 {"__annotations__", (getter)func_get_annotations, 477 {"__annotations__", (getter)func_get_annotations,
441 (setter)func_set_annotations}, 478 (setter)func_set_annotations},
442 {"__dict__", (getter)func_get_dict, (setter)func_set_dict}, 479 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
443 {"__name__", (getter)func_get_name, (setter)func_set_name}, 480 {"__name__", (getter)func_get_name, (setter)func_set_name},
481 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
444 {NULL} /* Sentinel */ 482 {NULL} /* Sentinel */
445 }; 483 };
446 484
447 PyDoc_STRVAR(func_doc, 485 PyDoc_STRVAR(func_doc,
448 "function(code, globals[, name[, argdefs[, closure]]])\n\ 486 "function(code, globals[, name[, argdefs[, closure]]])\n\
449 \n\ 487 \n\
450 Create a function object from a code object and a dictionary.\n\ 488 Create a function object from a code object and a dictionary.\n\
451 The optional name string overrides the name from the code object.\n\ 489 The optional name string overrides the name from the code object.\n\
452 The optional argdefs tuple specifies the default argument values.\n\ 490 The optional argdefs tuple specifies the default argument values.\n\
453 The optional closure tuple supplies the bindings for free variables."); 491 The optional closure tuple supplies the bindings for free variables.");
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 Py_DECREF(op->func_code); 592 Py_DECREF(op->func_code);
555 Py_DECREF(op->func_globals); 593 Py_DECREF(op->func_globals);
556 Py_XDECREF(op->func_module); 594 Py_XDECREF(op->func_module);
557 Py_DECREF(op->func_name); 595 Py_DECREF(op->func_name);
558 Py_XDECREF(op->func_defaults); 596 Py_XDECREF(op->func_defaults);
559 Py_XDECREF(op->func_kwdefaults); 597 Py_XDECREF(op->func_kwdefaults);
560 Py_XDECREF(op->func_doc); 598 Py_XDECREF(op->func_doc);
561 Py_XDECREF(op->func_dict); 599 Py_XDECREF(op->func_dict);
562 Py_XDECREF(op->func_closure); 600 Py_XDECREF(op->func_closure);
563 Py_XDECREF(op->func_annotations); 601 Py_XDECREF(op->func_annotations);
602 Py_XDECREF(op->func_qualname);
564 PyObject_GC_Del(op); 603 PyObject_GC_Del(op);
565 } 604 }
566 605
567 static PyObject* 606 static PyObject*
568 func_repr(PyFunctionObject *op) 607 func_repr(PyFunctionObject *op)
569 { 608 {
570 return PyUnicode_FromFormat("<function %U at %p>", 609 return PyUnicode_FromFormat("<function %U at %p>",
571 op->func_name, op); 610 op->func_qualname, op);
572 } 611 }
573 612
574 static int 613 static int
575 func_traverse(PyFunctionObject *f, visitproc visit, void *arg) 614 func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
576 { 615 {
577 Py_VISIT(f->func_code); 616 Py_VISIT(f->func_code);
578 Py_VISIT(f->func_globals); 617 Py_VISIT(f->func_globals);
579 Py_VISIT(f->func_module); 618 Py_VISIT(f->func_module);
580 Py_VISIT(f->func_defaults); 619 Py_VISIT(f->func_defaults);
581 Py_VISIT(f->func_kwdefaults); 620 Py_VISIT(f->func_kwdefaults);
582 Py_VISIT(f->func_doc); 621 Py_VISIT(f->func_doc);
583 Py_VISIT(f->func_name); 622 Py_VISIT(f->func_name);
584 Py_VISIT(f->func_dict); 623 Py_VISIT(f->func_dict);
585 Py_VISIT(f->func_closure); 624 Py_VISIT(f->func_closure);
586 Py_VISIT(f->func_annotations); 625 Py_VISIT(f->func_annotations);
626 Py_VISIT(f->func_qualname);
587 return 0; 627 return 0;
588 } 628 }
589 629
590 static PyObject * 630 static PyObject *
591 function_call(PyObject *func, PyObject *arg, PyObject *kw) 631 function_call(PyObject *func, PyObject *arg, PyObject *kw)
592 { 632 {
593 PyObject *result; 633 PyObject *result;
594 PyObject *argdefs; 634 PyObject *argdefs;
595 PyObject *kwtuple = NULL; 635 PyObject *kwtuple = NULL;
596 PyObject **d, **k; 636 PyObject **d, **k;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
767 Py_INCREF(callable); 807 Py_INCREF(callable);
768 cm->cm_callable = callable; 808 cm->cm_callable = callable;
769 return 0; 809 return 0;
770 } 810 }
771 811
772 static PyMemberDef cm_memberlist[] = { 812 static PyMemberDef cm_memberlist[] = {
773 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY}, 813 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
774 {NULL} /* Sentinel */ 814 {NULL} /* Sentinel */
775 }; 815 };
776 816
777 static PyObject * 817 static PyObject *
Benjamin Peterson 2011/11/29 02:45:54 Same comments as before.
dsdale24 2011/11/29 17:20:36 What comments would those be?
Benjamin Peterson 2011/11/29 17:27:46 On property_check_isabstract
778 cm_get__isabstractmethod__(classmethod *cm, void *closure) 818 cm_get___isabstractmethod__(classmethod *cm, void *closure)
779 { 819 {
780 if (cm->cm_callable == NULL) 820 int res = _PyObject_IsAbstract(cm->cm_callable);
781 Py_RETURN_FALSE; 821 if (res == -1) {
782 PyObject* is_abstract = PyObject_GetAttrString(cm->cm_callable, 822 return NULL;
783 "__isabstractmethod__"); 823 }
784 if (is_abstract == NULL) { 824 else if (res) {
785 PyErr_Clear(); 825 Py_RETURN_TRUE;
786 Py_RETURN_FALSE; 826 }
787 } 827 Py_RETURN_FALSE;
788 return is_abstract;
789 } 828 }
790 829
791 static PyGetSetDef cm_getsetlist[] = { 830 static PyGetSetDef cm_getsetlist[] = {
792 {"__isabstractmethod__", 831 {"__isabstractmethod__",
793 (getter)cm_get__isabstractmethod__, NULL, 832 (getter)cm_get___isabstractmethod__, NULL,
794 NULL, 833 NULL,
795 NULL}, 834 NULL},
796 {NULL} /* Sentinel */ 835 {NULL} /* Sentinel */
797 }; 836 };
798 837
799 PyDoc_STRVAR(classmethod_doc, 838 PyDoc_STRVAR(classmethod_doc,
800 "classmethod(function) -> method\n\ 839 "classmethod(function) -> method\n\
801 \n\ 840 \n\
802 Convert a function to be a class method.\n\ 841 Convert a function to be a class method.\n\
803 \n\ 842 \n\
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
944 Py_INCREF(callable); 983 Py_INCREF(callable);
945 sm->sm_callable = callable; 984 sm->sm_callable = callable;
946 return 0; 985 return 0;
947 } 986 }
948 987
949 static PyMemberDef sm_memberlist[] = { 988 static PyMemberDef sm_memberlist[] = {
950 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY}, 989 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
951 {NULL} /* Sentinel */ 990 {NULL} /* Sentinel */
952 }; 991 };
953 992
954 static PyObject * 993 static PyObject *
Benjamin Peterson 2011/11/29 02:45:54 And again.
955 sm_get__isabstractmethod__(staticmethod *sm, void *closure) 994 sm_get___isabstractmethod__(staticmethod *sm, void *closure)
956 { 995 {
957 if (sm->sm_callable == NULL) 996 int res = _PyObject_IsAbstract(sm->sm_callable);
958 Py_RETURN_FALSE; 997 if (res == -1) {
959 PyObject* is_abstract = PyObject_GetAttrString(sm->sm_callable, 998 return NULL;
960 "__isabstractmethod__"); 999 }
961 if (is_abstract == NULL) { 1000 else if (res) {
962 PyErr_Clear(); 1001 Py_RETURN_TRUE;
963 Py_RETURN_FALSE; 1002 }
964 } 1003 Py_RETURN_FALSE;
965 return is_abstract;
966 } 1004 }
967 1005
968 static PyGetSetDef sm_getsetlist[] = { 1006 static PyGetSetDef sm_getsetlist[] = {
969 {"__isabstractmethod__", 1007 {"__isabstractmethod__",
970 (getter)sm_get__isabstractmethod__, NULL, 1008 (getter)sm_get___isabstractmethod__, NULL,
971 NULL, 1009 NULL,
972 NULL}, 1010 NULL},
973 {NULL} /* Sentinel */ 1011 {NULL} /* Sentinel */
974 }; 1012 };
975 1013
976 PyDoc_STRVAR(staticmethod_doc, 1014 PyDoc_STRVAR(staticmethod_doc,
977 "staticmethod(function) -> method\n\ 1015 "staticmethod(function) -> method\n\
978 \n\ 1016 \n\
979 Convert a function to be a static method.\n\ 1017 Convert a function to be a static method.\n\
980 \n\ 1018 \n\
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 PyStaticMethod_New(PyObject *callable) 1075 PyStaticMethod_New(PyObject *callable)
1038 { 1076 {
1039 staticmethod *sm = (staticmethod *) 1077 staticmethod *sm = (staticmethod *)
1040 PyType_GenericAlloc(&PyStaticMethod_Type, 0); 1078 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1041 if (sm != NULL) { 1079 if (sm != NULL) {
1042 Py_INCREF(callable); 1080 Py_INCREF(callable);
1043 sm->sm_callable = callable; 1081 sm->sm_callable = callable;
1044 } 1082 }
1045 return (PyObject *)sm; 1083 return (PyObject *)sm;
1046 } 1084 }
LEFTRIGHT

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