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

Side by Side Diff: Objects/typeobject.c

Issue 9856: Change object.__format__(s) where s is non-empty to a TypeError
Patch Set: Created 7 years, 8 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
« no previous file with comments | « Lib/test/test_unicode.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Type object implementation */ 1 /* Type object implementation */
2 2
3 #include "Python.h" 3 #include "Python.h"
4 #include "frameobject.h" 4 #include "frameobject.h"
5 #include "structmember.h" 5 #include "structmember.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 8
9 9
10 /* Support type attribute cache */ 10 /* Support type attribute cache */
(...skipping 2901 matching lines...) Expand 10 before | Expand all | Expand 10 after
2912 2912
2913 static int 2913 static int
2914 object_init(PyObject *self, PyObject *args, PyObject *kwds) 2914 object_init(PyObject *self, PyObject *args, PyObject *kwds)
2915 { 2915 {
2916 int err = 0; 2916 int err = 0;
2917 if (excess_args(args, kwds)) { 2917 if (excess_args(args, kwds)) {
2918 PyTypeObject *type = Py_TYPE(self); 2918 PyTypeObject *type = Py_TYPE(self);
2919 if (type->tp_init != object_init && 2919 if (type->tp_init != object_init &&
2920 type->tp_new != object_new) 2920 type->tp_new != object_new)
2921 { 2921 {
2922 err = PyErr_WarnEx(PyExc_DeprecationWarning, 2922 PyErr_SetString(PyExc_TypeError,
2923 "object.__init__() takes no parameters", 2923 "object.__init__() takes no parameters");
2924 1); 2924 err = -1;
2925 } 2925 }
2926 else if (type->tp_init != object_init || 2926 else if (type->tp_init != object_init ||
2927 type->tp_new == object_new) 2927 type->tp_new == object_new)
2928 { 2928 {
2929 PyErr_SetString(PyExc_TypeError, 2929 PyErr_SetString(PyExc_TypeError,
2930 "object.__init__() takes no parameters"); 2930 "object.__init__() takes no parameters");
2931 err = -1; 2931 err = -1;
2932 } 2932 }
2933 } 2933 }
2934 return err; 2934 return err;
2935 } 2935 }
2936 2936
2937 static PyObject * 2937 static PyObject *
2938 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 2938 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2939 { 2939 {
2940 int err = 0; 2940 int err = 0;
2941 if (excess_args(args, kwds)) { 2941 if (excess_args(args, kwds)) {
2942 if (type->tp_new != object_new && 2942 if (type->tp_new != object_new &&
2943 type->tp_init != object_init) 2943 type->tp_init != object_init)
2944 { 2944 {
2945 err = PyErr_WarnEx(PyExc_DeprecationWarning, 2945 PyErr_SetString(PyExc_TypeError,
2946 "object.__new__() takes no parameters", 2946 "object.__new__() takes no parameters");
2947 1); 2947 err = -1;
2948 } 2948 }
2949 else if (type->tp_new != object_new || 2949 else if (type->tp_new != object_new ||
2950 type->tp_init == object_init) 2950 type->tp_init == object_init)
2951 { 2951 {
2952 PyErr_SetString(PyExc_TypeError, 2952 PyErr_SetString(PyExc_TypeError,
2953 "object.__new__() takes no parameters"); 2953 "object.__new__() takes no parameters");
2954 err = -1; 2954 err = -1;
2955 } 2955 }
2956 } 2956 }
2957 if (err < 0) 2957 if (err < 0)
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
3554 PyObject *result = NULL; 3554 PyObject *result = NULL;
3555 3555
3556 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) 3556 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
3557 return NULL; 3557 return NULL;
3558 3558
3559 self_as_str = PyObject_Str(self); 3559 self_as_str = PyObject_Str(self);
3560 if (self_as_str != NULL) { 3560 if (self_as_str != NULL) {
3561 /* Issue 7994: If we're converting to a string, we 3561 /* Issue 7994: If we're converting to a string, we
3562 should reject format specifications */ 3562 should reject format specifications */
3563 if (PyUnicode_GET_LENGTH(format_spec) > 0) { 3563 if (PyUnicode_GET_LENGTH(format_spec) > 0) {
3564 if (PyErr_WarnEx(PyExc_DeprecationWarning, 3564 PyErr_SetString(PyExc_TypeError,
3565 "object.__format__ with a non-empty format "
3566 "string is deprecated", 1) < 0) {
3567 goto done;
3568 }
3569 /* Eventually this will become an error:
3570 PyErr_Format(PyExc_TypeError,
3571 "non-empty format string passed to object.__format__"); 3565 "non-empty format string passed to object.__format__");
3572 goto done; 3566 goto done;
3573 */
3574 } 3567 }
3575 3568
3576 result = PyObject_Format(self_as_str, format_spec); 3569 result = PyObject_Format(self_as_str, format_spec);
3577 } 3570 }
3578 3571
3579 done: 3572 done:
3580 Py_XDECREF(self_as_str); 3573 Py_XDECREF(self_as_str);
3581 3574
3582 return result; 3575 return result;
3583 } 3576 }
(...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after
4192 for (i = 0; i < n; i++) { 4185 for (i = 0; i < n; i++) {
4193 PyObject *b = PyTuple_GET_ITEM(bases, i); 4186 PyObject *b = PyTuple_GET_ITEM(bases, i);
4194 if (PyType_Check(b) && 4187 if (PyType_Check(b) &&
4195 add_subclass((PyTypeObject *)b, type) < 0) 4188 add_subclass((PyTypeObject *)b, type) < 0)
4196 goto error; 4189 goto error;
4197 } 4190 }
4198 4191
4199 /* Warn for a type that implements tp_compare (now known as 4192 /* Warn for a type that implements tp_compare (now known as
4200 tp_reserved) but not tp_richcompare. */ 4193 tp_reserved) but not tp_richcompare. */
4201 if (type->tp_reserved && !type->tp_richcompare) { 4194 if (type->tp_reserved && !type->tp_richcompare) {
4202 int error; 4195 PyErr_Format(PyExc_TypeError,
4203 error = PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
4204 "Type %.100s defines tp_reserved (formerly tp_compare) " 4196 "Type %.100s defines tp_reserved (formerly tp_compare) "
4205 "but not tp_richcompare. Comparisons may not behave as intended.", 4197 "but not tp_richcompare. Comparisons may not behave as intended.",
4206 type->tp_name); 4198 type->tp_name);
4207 if (error == -1) 4199 goto error;
4208 goto error;
4209 } 4200 }
4210 4201
4211 /* All done -- set the ready flag */ 4202 /* All done -- set the ready flag */
4212 assert(type->tp_dict != NULL); 4203 assert(type->tp_dict != NULL);
4213 type->tp_flags = 4204 type->tp_flags =
4214 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; 4205 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
4215 return 0; 4206 return 0;
4216 4207
4217 error: 4208 error:
4218 type->tp_flags &= ~Py_TPFLAGS_READYING; 4209 type->tp_flags &= ~Py_TPFLAGS_READYING;
(...skipping 2365 matching lines...) Expand 10 before | Expand all | Expand 10 after
6584 0, /* tp_base */ 6575 0, /* tp_base */
6585 0, /* tp_dict */ 6576 0, /* tp_dict */
6586 super_descr_get, /* tp_descr_get */ 6577 super_descr_get, /* tp_descr_get */
6587 0, /* tp_descr_set */ 6578 0, /* tp_descr_set */
6588 0, /* tp_dictoffset */ 6579 0, /* tp_dictoffset */
6589 super_init, /* tp_init */ 6580 super_init, /* tp_init */
6590 PyType_GenericAlloc, /* tp_alloc */ 6581 PyType_GenericAlloc, /* tp_alloc */
6591 PyType_GenericNew, /* tp_new */ 6582 PyType_GenericNew, /* tp_new */
6592 PyObject_GC_Del, /* tp_free */ 6583 PyObject_GC_Del, /* tp_free */
6593 }; 6584 };
OLDNEW
« no previous file with comments | « Lib/test/test_unicode.py ('k') | no next file » | no next file with comments »

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