diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2024-02-10 10:21:35 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-10 10:21:35 (GMT) |
| commit | e2c403892400878707a20d4b7e183de505a64ca5 (patch) | |
| tree | 62a1f10b10d31ae042fb60b4d48e81303055b68f /Python | |
| parent | 6e222a55b1d63de994a2ca39afd4bbf4d2fbdd34 (diff) | |
| download | cpython-e2c403892400878707a20d4b7e183de505a64ca5.zip cpython-e2c403892400878707a20d4b7e183de505a64ca5.tar.gz cpython-e2c403892400878707a20d4b7e183de505a64ca5.tar.bz2 | |
gh-76763: Make chr() always raising ValueError for out-of-range values (GH-114882)
Previously it raised OverflowError for very large or very small values.
Diffstat (limited to 'Python')
| -rw-r--r-- | Python/bltinmodule.c | 25 | ||||
| -rw-r--r-- | Python/clinic/bltinmodule.c.h | 21 |
2 files changed, 22 insertions, 24 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 31c1bf0..b007496 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -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); } diff --git a/Python/clinic/bltinmodule.c.h b/Python/clinic/bltinmodule.c.h index 8d40e65..3898f98 100644 --- a/Python/clinic/bltinmodule.c.h +++ b/Python/clinic/bltinmodule.c.h @@ -233,25 +233,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; - - i = PyLong_AsInt(arg); - if (i == -1 && PyErr_Occurred()) { - 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, *, _feature_version=-1)\n" @@ -1212,4 +1193,4 @@ builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=31bded5d08647a57 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=643a8d5f900e0c36 input=a9049054013a1b77]*/ |
