Skip to content

Commit

Permalink
fix: wrap repr of bytes in double quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
SrirachaHorse committed Dec 12, 2023
1 parent 789913a commit 852efcc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 5 additions & 3 deletions tests/formats/dataclass/serializers/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ def test_write_object_with_enum(self):
iterator = self.serializer.write_object(Namespace.SOAP11, 0, set())
self.assertEqual("Namespace.SOAP11", "".join(iterator))

def test_write_bytes_with_single_quote(self):
iterator = self.serializer.write_object(b"\xfaj'", 0, set())
self.assertEqual("b\"\\xfaj'\"", "".join(iterator))
def test_write_bytes(self):
iterator = self.serializer.write_object(b'a', 0, set())
self.assertEqual('b"a"', "".join(iterator))

iterator = self.serializer.write_object(b"\xfa\"'", 0, set())
self.assertEqual('b"\\xfa\"\\\'"', "".join(iterator))

def test_build_imports_with_nested_types(self):
expected = "from tests.fixtures.models import Parent\n"
Expand Down
4 changes: 3 additions & 1 deletion xsdata/utils/objects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
import re
from typing import Any
from xml.etree.ElementTree import QName
from xml.sax.saxutils import quoteattr
Expand Down Expand Up @@ -30,6 +31,7 @@ def literal_value(value: Any) -> str:
return f'QName("{value.text}")'

if isinstance(value, bytes):
return repr(value)
# Use the contents of the bytes verbatim, but ensure that it is wrapped in double quotes
return re.sub(r"b'(.*)'$", r'b"\g<1>"', repr(value))

return repr(value).replace("'", '"')

0 comments on commit 852efcc

Please sign in to comment.