Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ByteIO with StringIO #1222

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/serializers/nquads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions rdflib/plugins/serializers/nt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. "
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/serializers/rdfxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?xml version="1.0" encoding="%s"?>\n' % self.encoding)
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/serializers/trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ def serialize(
self.write("}\n")

self.endDocument()
stream.write("\n".encode("latin-1"))
stream.write("\n")
2 changes: 1 addition & 1 deletion rdflib/plugins/serializers/trix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down