A type of out-of-range exception for %c
Currently %c formatting raises OverflowError if an argument is out of range.
'%c' % 1114112 Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: %c arg not in range(0x110000) '{:c}'.format(1114112) Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: %c arg not in range(0x110000)
The same for PyUnicode_Format(), PyUnicode_FromFromat() and PyUnicode_FromFromatV() in C API. But chr() raises ValueError.
chr(1114112) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: chr() arg not in range(0x110000)
And ValueError looks more suitable, because it is not really an overflow (limitation of the C language, or a C type). Can we change an exception type for %c formatting from OverflowError to ValueError?
On Sun, 23 Jun 2013 21:59:37 +0300 Serhiy Storchaka <storchaka@gmail.com> wrote:
Currently %c formatting raises OverflowError if an argument is out of range.
'%c' % 1114112 Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: %c arg not in range(0x110000) '{:c}'.format(1114112) Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: %c arg not in range(0x110000)
The same for PyUnicode_Format(), PyUnicode_FromFromat() and PyUnicode_FromFromatV() in C API.
But chr() raises ValueError.
chr(1114112) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: chr() arg not in range(0x110000)
And ValueError looks more suitable, because it is not really an overflow (limitation of the C language, or a C type). Can we change an exception type for %c formatting from OverflowError to ValueError?
Well, it overflows the limited range of the "unicode character" type (which is virtual in both Python and C, but still conceptually exists). The most annoying thing here is that OverflowError doesn't subclass ValueError. Regards Antoine.
On 23 cze 2013, at 21:07, Antoine Pitrou <solipsis@pitrou.net> wrote:
The most annoying thing here is that OverflowError doesn't subclass ValueError.
My intuition would rather make OverflowError subclass RuntimeError. Am I wrong? That means I support Serhiy's point that %c would be less surprising raising a ValueError. Backwards compatibility is an unfortunate blocker here, I suppose. -- Best regards, Łukasz Langa WWW: http://lukasz.langa.pl/ Twitter: @llanga IRC: ambv on #python-dev
participants (3)
-
Antoine Pitrou
-
Serhiy Storchaka
-
Łukasz Langa