# HG changeset patch # User Paul Boddie # Date 1484071691 -3600 # Node ID c78e7e892ee196d19bd36766dad8f786513e06e4 # Parent 4aed2292f1d4461c5d8f2d443613ed51013c6aba Replaced errno usage with specialised exceptions because error numbers differ between platforms and architectures. diff -r 4aed2292f1d4 -r c78e7e892ee1 lib/native/iconv.py --- a/lib/native/iconv.py Tue Jan 10 18:47:33 2017 +0100 +++ b/lib/native/iconv.py Tue Jan 10 19:08:11 2017 +0100 @@ -24,7 +24,11 @@ this program. If not, see . """ +from posix.iconv import IncompleteSequenceError, InvalidSequenceError + def iconv(cd, state): + IncompleteSequenceError + InvalidSequenceError OSError def iconv_close(cd): diff -r 4aed2292f1d4 -r c78e7e892ee1 lib/posix/iconv.py --- a/lib/posix/iconv.py Tue Jan 10 18:47:33 2017 +0100 +++ b/lib/posix/iconv.py Tue Jan 10 19:08:11 2017 +0100 @@ -22,10 +22,17 @@ from __builtins__.types import check_int, check_string from native import iconv, iconv_close, iconv_open, iconv_reset -# Errors produced by iconv. +class IncompleteSequenceError(OSError): + + "An error indicating an incomplete multibyte sequence." + + pass -EINVAL = 22 -EILSEQ = 84 +class InvalidSequenceError(OSError): + + "An error indicating an incomplete multibyte sequence." + + pass class ConverterError(Exception): @@ -86,14 +93,14 @@ # Incomplete input does not cause an exception. - except OSError, exc: - if exc.value == EINVAL: - self.result.append(exc.arg) - return - elif exc.value == EILSEQ: - raise UnicodeDecodeError(exc.arg) - else: - raise + except IncompleteSequenceError, exc: + self.result.append(exc.arg) + return + + # Invalid input causes a Unicode exception. + + except InvalidSequenceError, exc: + raise UnicodeDecodeError(exc.arg) # Add any returned text to the result. diff -r 4aed2292f1d4 -r c78e7e892ee1 templates/native/iconv.c --- a/templates/native/iconv.c Tue Jan 10 18:47:33 2017 +0100 +++ b/templates/native/iconv.c Tue Jan 10 19:08:11 2017 +0100 @@ -81,7 +81,7 @@ /* Incomplete sequence: raise the string in an OSError instead. */ if (errno == EINVAL) - __raise_os_error(__new_int(errno), __new_str(resultbuf, outbytestotal)); + __raise_incomplete_sequence_error(__new_int(errno), __new_str(resultbuf, outbytestotal)); return __new_str(resultbuf, outbytestotal); } @@ -92,7 +92,7 @@ { resultbuf = __ALLOCATE(inbytesleft + 1, sizeof(char)); memcpy(resultbuf, inbuf, inbytesleft); - __raise_os_error(__new_int(errno), __new_str(resultbuf, inbytesleft)); + __raise_invalid_sequence_error(__new_int(errno), __new_str(resultbuf, inbytesleft)); } /* General failure. */ diff -r 4aed2292f1d4 -r c78e7e892ee1 templates/progops.c --- a/templates/progops.c Tue Jan 10 18:47:33 2017 +0100 +++ b/templates/progops.c Tue Jan 10 19:08:11 2017 +0100 @@ -103,6 +103,24 @@ #endif /* __HAVE___builtins___exception_io_EOFError */ } +void __raise_incomplete_sequence_error(__attr value, __attr arg) +{ +#ifdef __HAVE_posix_iconv_IncompleteSequenceError + __attr args[3] = {{0, 0}, value, arg}; + __attr exc = __new_posix_iconv_IncompleteSequenceError(args); + __Raise(exc); +#endif /* __HAVE_posix_iconv_IncompleteSequenceError */ +} + +void __raise_invalid_sequence_error(__attr value, __attr arg) +{ +#ifdef __HAVE_posix_iconv_InvalidSequenceError + __attr args[3] = {{0, 0}, value, arg}; + __attr exc = __new_posix_iconv_InvalidSequenceError(args); + __Raise(exc); +#endif /* __HAVE_posix_iconv_InvalidSequenceError */ +} + void __raise_io_error(__attr value) { #ifdef __HAVE___builtins___exception_io_IOError diff -r 4aed2292f1d4 -r c78e7e892ee1 templates/progops.h --- a/templates/progops.h Tue Jan 10 18:47:33 2017 +0100 +++ b/templates/progops.h Tue Jan 10 19:08:11 2017 +0100 @@ -40,6 +40,10 @@ void __raise_eof_error(); +void __raise_incomplete_sequence_error(__attr value, __attr arg); + +void __raise_invalid_sequence_error(__attr value, __attr arg); + void __raise_io_error(__attr value); void __raise_memory_error();