add 'surrogateescape' support to 'dxf_backslash_replace' error handler
This enables the usage of 'surrogateescape' error handler as default error handler,
which will add support for binary data in group code 300 in XRECORD without any
special treatment. TagWriter.write_raw_bytes() is not required.
# Copyright (c) 2016-2020, Manfred Moitzi
# License: MIT License
import re
-from .const import DXFEncodingError
+import codecs
+
+surrogate_escape = codecs.lookup_error('surrogateescape')
+BACKSLASH_UNICODE = re.compile(r'(\\U\+[A-F0-9]{4})')
def dxf_backslash_replace(exc: Exception):
if isinstance(exc, (UnicodeEncodeError, UnicodeTranslateError)):
s = ""
for c in exc.object[exc.start:exc.end]:
- if ord(c) <= 0xff:
- s += "\\x%02x" % ord(c)
- elif ord(c) <= 0xffff:
- s += "\\U+%04x" % ord(c)
+ x = ord(c)
+ if x <= 0xff:
+ s += "\\x%02x" % x
+ elif 0xdc80 <= x <= 0xdcff:
+ # Delegate surrogate handling:
+ return surrogate_escape(exc)
+ elif x <= 0xffff:
+ s += "\\U+%04x" % x
else:
- s += "\\U+%08x" % ord(c)
+ s += "\\U+%08x" % x
return s, exc.end
else:
raise TypeError(f"Can't handle {exc.__class__.__name__}")
-def encode(unicode: str, encoding: str = 'cp1252', ignore_error: bool = False):
- try:
- return bytes(unicode, encoding)
- except UnicodeEncodeError: # can not use the given encoding
- if ignore_error: # encode string with the default unicode encoding
- return bytes(unicode, 'utf-8')
- else:
- raise DXFEncodingError(
- f"Can not encode string '{unicode}' with given "
- f"encoding '{encoding}'")
-
-
-BACKSLASH_UNICODE = re.compile(r'(\\U\+[A-F0-9]{4})')
-
-
def _decode(s: str) -> str:
if s.startswith(r'\U+'):
return chr(int(s[3:], 16))
