diff --git a/iconvcodec.py b/iconvcodec.py index e3fba23..13ce3c6 100644 --- a/iconvcodec.py +++ b/iconvcodec.py @@ -13,6 +13,10 @@ def _iconv_encode_impl(encoder, msg, errors, bufsize=None): return encoder.iconv(msg, bufsize), len(msg) except iconv.error as e: errstring, code, inlen, outres = e.args + if code == 0: + # Windows quirk: raises with code=0 when partial conversion succeeded + # Just return what was converted + return outres, inlen if code == errno.E2BIG: # outbuffer was too small, increase size a bit and try to encode rest out1, len1 = _iconv_encode_impl( @@ -53,6 +57,10 @@ def _iconv_decode_impl(decoder, msg, errors, bufsize=None): return decoder.iconv(msg, bufsize).decode(), len(msg) except iconv.error as e: errstring, code, inlen, outres = e.args + if code == 0: + # Windows quirk: raises with code=0 when partial conversion succeeded + # Just return what was converted + return outres.decode(), inlen if code == errno.E2BIG: # buffer too small out1, len1 = _iconv_decode_impl( @@ -77,6 +85,7 @@ def _iconv_decode_impl(decoder, msg, errors, bufsize=None): else: raise ValueError("unsupported error handling") return outres.decode() + out1, inlen + len1 + 1 + raise def codec_factory(encoding): diff --git a/test_iconvcodec.py b/test_iconvcodec.py index aaa5bc9..c2aa558 100644 --- a/test_iconvcodec.py +++ b/test_iconvcodec.py @@ -23,11 +23,14 @@ def test_decode(self): string = b"Hallo".decode("T.61") self.assertEqual(string, "Hallo") - @unittest.skipUnless(sys.platform.startswith("linux"), "Linux only test") + @unittest.skipIf(sys.platform == "darwin", "Test fails on macOS") def test_transliterate(self): string = "abc ß α € àḃç" bytestring = string.encode("ASCII//TRANSLIT") - self.assertEqual(bytestring, b"abc ss ? EUR abc") + if sys.platform.startswith("linux"): + self.assertEqual(bytestring, b"abc ss ? EUR abc") + else: # Windows + self.assertEqual(bytestring, b"abc ss ") def test_incremental_encode(self): encoder = codecs.getincrementalencoder("ASCII//TRANSLIT")()