NatLibFi / Skosify

Validate, convert and improve SKOS vocabularies
http://skosify.readthedocs.io/
MIT License
55 stars 8 forks source link

Write encoded output to stdout.buffer, as required by Python 3 #73

Closed osma closed 4 years ago

osma commented 4 years ago

There is a problem writing RDF/XML output to stdout under Python 3:

$ skosify examples/milk.in.ttl 
WARNING: Concept scheme has no label(s). Use --label option to set the concept scheme label.
INFO: Marking loose concept http://example.org/animal as top concept of scheme http://example.org/scheme
INFO: Marking loose concept http://example.org/milk as top concept of scheme http://example.org/scheme
Traceback (most recent call last):
  File "/home/local/oisuomin/git/Skosify/venv/bin/skosify", line 11, in <module>
    load_entry_point('skosify', 'console_scripts', 'skosify')()
  File "/home/local/oisuomin/git/Skosify/skosify/cli.py", line 220, in main
    write_rdf(voc, output, config.to_format)
  File "/home/local/oisuomin/git/Skosify/skosify/rdftools/io.py", line 64, in write_rdf
    rdf.serialize(destination=out, format=fmt)
  File "/home/local/oisuomin/git/Skosify/venv/lib/python3.7/site-packages/rdflib/graph.py", line 947, in serialize
    serializer.serialize(stream, base=base, encoding=encoding, **args)
  File "/home/local/oisuomin/git/Skosify/venv/lib/python3.7/site-packages/rdflib/plugins/serializers/rdfxml.py", line 55, in serialize
    write('<?xml version="1.0" encoding="%s"?>\n' % self.encoding)
  File "/home/local/oisuomin/git/Skosify/venv/lib/python3.7/site-packages/rdflib/plugins/serializers/rdfxml.py", line 52, in <lambda>
    uni.encode(encoding, 'replace'))
TypeError: write() argument must be str, not bytes

The problem is that under Python 3, sys.stdout doesn't accept writing bytes. sys.stdout.buffer should be used instead. This works also under Python 2.7 so it should be safe to switch.

Unfortunately this part of the codebase doesn't have unit tests.

codecov-io commented 4 years ago

Codecov Report

Merging #73 into master will not change coverage. The diff coverage is 0.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #73   +/-   ##
=======================================
  Coverage   58.65%   58.65%           
=======================================
  Files          10       10           
  Lines         924      924           
=======================================
  Hits          542      542           
  Misses        382      382           
Impacted Files Coverage Δ
skosify/rdftools/io.py 59.52% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 2fd9c3d...5e6dfc2. Read the comment docs.