Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-76763: Make chr() always raising ValueError for out-of-range values #114882

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

Merged
merged 2 commits into from
Feb 10, 2024
Merged
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
11 changes: 7 additions & 4 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,13 @@ class C3(C2): pass
self.assertTrue(callable(c3))

def test_chr(self):
self.assertEqual(chr(0), '\0')
self.assertEqual(chr(32), ' ')
self.assertEqual(chr(65), 'A')
self.assertEqual(chr(97), 'a')
self.assertEqual(chr(0xff), '\xff')
self.assertRaises(ValueError, chr, 1<<24)
self.assertEqual(chr(sys.maxunicode),
str('\\U0010ffff'.encode("ascii"), 'unicode-escape'))
self.assertRaises(TypeError, chr)
self.assertRaises(TypeError, chr, 65.0)
self.assertEqual(chr(0x0000FFFF), "\U0000FFFF")
self.assertEqual(chr(0x00010000), "\U00010000")
self.assertEqual(chr(0x00010001), "\U00010001")
Expand All @@ -327,7 +326,11 @@ 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, 1<<24)
self.assertRaises(ValueError, chr, 2**32-1)
self.assertRaises(ValueError, chr, -2**32)
self.assertRaises(ValueError, chr, 2**1000)
self.assertRaises(ValueError, chr, -2**1000)

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,3 @@
The :func:`chr` builtin function now always raises :exc:`ValueError` for
values outside the valid range. Previously it raised :exc:`OverflowError` for
very large or small values.
25 changes: 21 additions & 4 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,17 +703,34 @@ 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 overflow;
long v = PyLong_AsLongAndOverflow(i, &overflow);
if (v == -1 && PyErr_Occurred()) {
return NULL;
}
if (overflow) {
v = overflow < 0 ? INT_MIN : INT_MAX;
/* Allow PyUnicode_FromOrdinal() to raise an exception */
}
#if SIZEOF_INT < SIZEOF_LONG
else if (v < INT_MIN) {
v = INT_MIN;
}
else if (v > INT_MAX) {
v = INT_MAX;
}
#endif
return PyUnicode_FromOrdinal(v);
}


Expand Down
21 changes: 1 addition & 20 deletions Python/clinic/bltinmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.