diff --git a/rdflib/graph.py b/rdflib/graph.py index 8f5b05442..0eb5d7596 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -32,7 +32,7 @@ import tempfile import pathlib -from io import BytesIO +from io import StringIO from urllib.parse import urlparse from urllib.request import url2pathname @@ -1111,13 +1111,9 @@ def serialize( serializer = plugin.get(format, Serializer)(self) stream: IO[bytes] if destination is None: - stream = BytesIO() - if encoding is None: - serializer.serialize(stream, base=base, encoding="utf-8", **args) - return stream.getvalue().decode("utf-8") - else: - serializer.serialize(stream, base=base, encoding=encoding, **args) - return stream.getvalue() + stream = StringIO() + serializer.serialize(stream, base=base, encoding="utf-8", **args) + return stream.getvalue() if hasattr(destination, "write"): stream = cast(IO[bytes], destination) serializer.serialize(stream, base=base, encoding=encoding, **args) @@ -1145,7 +1141,7 @@ def serialize( def print(self, format="turtle", encoding="utf-8", out=None): print( - self.serialize(None, format=format, encoding=encoding).decode(encoding), + self.serialize(None, format=format, encoding=encoding), file=out, flush=True, ) diff --git a/rdflib/plugins/serializers/nquads.py b/rdflib/plugins/serializers/nquads.py index 42cddf4e9..03d6f63b7 100644 --- a/rdflib/plugins/serializers/nquads.py +++ b/rdflib/plugins/serializers/nquads.py @@ -38,9 +38,9 @@ def serialize( for context in self.store.contexts(): for triple in context: stream.write( - _nq_row(triple, context.identifier).encode(encoding, "replace") + _nq_row(triple, context.identifier) ) - stream.write("\n".encode("latin-1")) + stream.write("\n") def _nq_row(triple, context): diff --git a/rdflib/plugins/serializers/nt.py b/rdflib/plugins/serializers/nt.py index 188db78fc..3667dfef6 100644 --- a/rdflib/plugins/serializers/nt.py +++ b/rdflib/plugins/serializers/nt.py @@ -32,6 +32,7 @@ def serialize( ): if base is not None: warnings.warn("NTSerializer does not support base.") + if encoding != "utf-8": warnings.warn( "NTSerializer always uses UTF-8 encoding. " diff --git a/rdflib/plugins/serializers/rdfxml.py b/rdflib/plugins/serializers/rdfxml.py index 790c23939..d010bb258 100644 --- a/rdflib/plugins/serializers/rdfxml.py +++ b/rdflib/plugins/serializers/rdfxml.py @@ -56,7 +56,7 @@ def serialize( self.__stream = stream self.__serialized: Dict[Identifier, int] = {} encoding = self.encoding - self.write = write = lambda uni: stream.write(uni.encode(encoding, "replace")) + self.write = write = lambda uni: stream.write(uni) # startDocument write('\n' % self.encoding) diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py index 3aa9ca689..1c65faa9d 100644 --- a/rdflib/plugins/serializers/trig.py +++ b/rdflib/plugins/serializers/trig.py @@ -110,4 +110,4 @@ def serialize( self.write("}\n") self.endDocument() - stream.write("\n".encode("latin-1")) + stream.write("\n") diff --git a/rdflib/plugins/serializers/trix.py b/rdflib/plugins/serializers/trix.py index 63d58c44d..eb6ebef29 100644 --- a/rdflib/plugins/serializers/trix.py +++ b/rdflib/plugins/serializers/trix.py @@ -52,7 +52,7 @@ def serialize( raise Exception("Unknown graph type: " + type(self.store)) self.writer.pop() - stream.write("\n".encode("latin-1")) + stream.write("\n") def _writeGraph(self, graph): self.writer.push(TRIXNS["graph"]) diff --git a/rdflib/plugins/serializers/turtle.py b/rdflib/plugins/serializers/turtle.py index a62c05c42..e1569f954 100644 --- a/rdflib/plugins/serializers/turtle.py +++ b/rdflib/plugins/serializers/turtle.py @@ -168,7 +168,7 @@ def indent(self, modifier=0): def write(self, text): """Write text in given encoding.""" - self.stream.write(text.encode(self.encoding, "replace")) + self.stream.write(text) SUBJECT = 0 @@ -250,7 +250,7 @@ def serialize(self, stream, base=None, encoding=None, spacious=None, **args): self.write("\n") self.endDocument() - stream.write("\n".encode("latin-1")) + stream.write("\n") self.base = None