Skip to content

bpo-32582: chr() doesn't raise OverflowError anymore #5218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ def test_chr(self):
self.assertEqual(chr(0x0010FFFF), "\U0010FFFF")
self.assertRaises(ValueError, chr, -1)
self.assertRaises(ValueError, chr, 0x00110000)
self.assertRaises((OverflowError, ValueError), chr, 2**32)
self.assertRaises(ValueError, chr, -2**100)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2**1000. It is possible that once Python will run platform with 128-bit C long.

self.assertRaises(ValueError, chr, 2**100)

def test_cmp(self):
self.assertTrue(not hasattr(builtins, "cmp"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :func:`chr` builtin function now catchs :exc:`OverflowError` and raises
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"catches" -- this is an implementation detail. And I think it is better to not raise and catch it.

a :exc:`ValueError` exception instead.
15 changes: 11 additions & 4 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,17 +715,24 @@ builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec)
/*[clinic input]
chr as builtin_chr

i: int
i: object
/

Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
[clinic start generated code]*/

static PyObject *
builtin_chr_impl(PyObject *module, int i)
/*[clinic end generated code: output=c733afcd200afcb7 input=3f604ef45a70750d]*/
builtin_chr(PyObject *module, PyObject *i)
/*[clinic end generated code: output=d34f25b8035a9b10 input=f919867f0ba2f496]*/
{
return PyUnicode_FromOrdinal(i);
int ch = _PyLong_AsInt(i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use PyLongAsLongAndOverflow().

if (ch == -1 && PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_OverflowError)) {
return NULL;
}
PyErr_Clear();
}
return PyUnicode_FromOrdinal(ch);
}


Expand Down
20 changes: 1 addition & 19 deletions Python/clinic/bltinmodule.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,6 @@ PyDoc_STRVAR(builtin_chr__doc__,
#define BUILTIN_CHR_METHODDEF \
{"chr", (PyCFunction)builtin_chr, METH_O, builtin_chr__doc__},

static PyObject *
builtin_chr_impl(PyObject *module, int i);

static PyObject *
builtin_chr(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
int i;

if (!PyArg_Parse(arg, "i:chr", &i)) {
goto exit;
}
return_value = builtin_chr_impl(module, i);

exit:
return return_value;
}

PyDoc_STRVAR(builtin_compile__doc__,
"compile($module, /, source, filename, mode, flags=0,\n"
" dont_inherit=False, optimize=-1)\n"
Expand Down Expand Up @@ -710,4 +692,4 @@ builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
exit:
return return_value;
}
/*[clinic end generated code: output=9f17c7a87d740374 input=a9049054013a1b77]*/
/*[clinic end generated code: output=2ff748273b78b0ac input=a9049054013a1b77]*/