Mercurial > cpython-lru_cache-weakref
diff Python/bltinmodule.c @ 47136:7118fade575b legacy-trunk
add Py3k warnings to oct and hex. backport hex behavior (because it's not different)
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 17 May 2008 19:21:20 +0000 |
| parents | 4c8375574aa9 |
| children | 64f3873438b1 |
line wrap: on
line diff
--- a/Python/bltinmodule.c Sat May 17 19:14:23 2008 +0000 +++ b/Python/bltinmodule.c Sat May 17 19:21:20 2008 +0000 @@ -1181,22 +1181,29 @@ { PyNumberMethods *nb; PyObject *res; - - if ((nb = v->ob_type->tp_as_number) == NULL || - nb->nb_hex == NULL) { - PyErr_SetString(PyExc_TypeError, - "hex() argument can't be converted to hex"); - return NULL; + + nb = Py_TYPE(v)->tp_as_number; + + if (nb != NULL && nb->nb_hex != NULL) { + if (PyErr_WarnPy3k("In 3.x, hex() converts " + "the result of __index__ to hexidecimal.", + 1) < 0) + return NULL; + res = (*nb->nb_hex)(v); + if (res && !PyString_Check(res)) { + PyErr_Format(PyExc_TypeError, + "__hex__ returned non-string (type %.200s)", + res->ob_type->tp_name); + Py_DECREF(res); + return NULL; + } + return res; } - res = (*nb->nb_hex)(v); - if (res && !PyString_Check(res)) { - PyErr_Format(PyExc_TypeError, - "__hex__ returned non-string (type %.200s)", - res->ob_type->tp_name); - Py_DECREF(res); - return NULL; - } - return res; + else if (PyIndex_Check(v)) + return PyNumber_ToBase(v, 16); + PyErr_SetString(PyExc_TypeError, + "hex() argument can't be converted to hex"); + return NULL; } PyDoc_STRVAR(hex_doc, @@ -1456,6 +1463,11 @@ "oct() argument can't be converted to oct"); return NULL; } + if (PyErr_WarnPy3k("In 3.x, oct() converts the result of __index__ to octal; " + "Use future_builtins.oct for this behavior. " + "Also, note the returned format is different.", + 1) < 0) + return NULL; res = (*nb->nb_oct)(v); if (res && !PyString_Check(res)) { PyErr_Format(PyExc_TypeError,
