Writing SPDX output to a file raises an exception #436

Closed
sschuberth opened this Issue Jan 6, 2017 · 7 comments

Projects

None yet

2 participants

@sschuberth
Collaborator

This is basically a note to myself since I've introduced the bug. Running something like

$ scancode -f spdx-tv NOTICE a.spdx

results in

  File "scancode-toolkit/src/scancode/cli.py", line 717, in save_results
    write_document(doc, output_file)
  File "scancode-toolkit/local/lib/python2.7/site-packages/spdx/writers/tagvalue.py", line 188, in write_document
    out.write('# Document Information\n\n')
TypeError: write() argument 1 must be unicode, not str

@pombredanne As I'm still a Python beginner, do you think this can be fixed without modifying the spdx-tools package to write unicode instead of str?

@pombredanne
Member

@sschuberth I think that the SPDX tools should be unicode all the way. And for now in ScanCode you could work around this by forcing the doc to be unicode in:
https://github.com/nexB/scancode-toolkit/blob/99f0a0de9062c44cc8cdb9cf3a7efd270dbdfd57/src/scancode/cli.py#L714
with something like

doc = unicode(doc, 'utf-8')
@pombredanne
Member

but one sec... this is non sense. Hold on

@pombredanne
Member

Use something like this for now:

        from StringIO import StringIO

        doc_as_string = StringIO()
        
        if format == 'spdx-tv':
            from spdx.writers.tagvalue import write_document
            write_document(doc, doc_as_string)
        else:
            from spdx.writers.rdf import write_document
            write_document(doc, doc_as_string)

        output_file.write(unicode(doc_as_string.getvalue(), 'utf-8'))
@pombredanne
Member

This will fail if the output is not unicode-decodable.... but I think this is OK for now.

@pombredanne
Member

FWIW, I tested locally the proposed solution above and it works fine.

@sschuberth
Collaborator

output_file.write(unicode(doc_as_string.getvalue(), 'utf-8'))

Interestingly, with that I was getting

TypeError: decoding Unicode is not supported

This apparently is because getvalue() already does return unicode, so I've changed the code to just

output_file.write(str_buffer.getvalue())

see PR #441. However, I'm wondering why the code was working for you then. Any idea about that?

@pombredanne
Member

@sschuberth I had made only a quick test ... and I may not have pasted exactly what I tested :)

@sschuberth sschuberth changed the title from Writing SPDX output to a file raises an expection to Writing SPDX output to a file raises an exception Jan 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment