ion-python icon indicating copy to clipboard operation
ion-python copied to clipboard

Using `dump` from `simpleion` requires file to be open in binary mode

Open therapon opened this issue 5 years ago • 1 comments

The current implementation of dump requires that the file passed to dump is opened in (Python's) binary mode ('b'). The decision on whether the content of the file is Ion Text or Ion Binary is made by the value of dump's argument binary.

For example the following code errors

import amazon.ion.simpleion as ion

def main():
    fd = open("out.ion", "w")
    simple_string = "Hello"
    ion.dump(simple_string, fd, binary=False)


if __name__ == "__main__":
    main()

with

Traceback (most recent call last):
  File "TSsample.py", line 10, in <module>
    main()
  File "TSsample.py", line 6, in main
    ion.dump(simple_string, fd, binary=False)
  File "/.local/lib/python3.6/site-packages/amazon/ion/simpleion.py", line 151, in dump
    writer.send(ION_VERSION_MARKER_EVENT)  # The IVM is emitted automatically in binary; it's optional in text.
  File "/.local/lib/python3.6/site-packages/amazon/ion/writer.py", line 151, in blocking_writer
    output.write(result_event.data)
TypeError: write() argument must be str, not bytes

While the following succeeds

import amazon.ion.simpleion as ion

def main():
    fd = open("out.ion", "wb")
    simple_string = "Hello"
    ion.dump(simple_string, fd, binary=False)


if __name__ == "__main__":
    main()

We should at least update the documentation to help users and provide an implementation that can work with non-binary opened files.

therapon avatar Apr 22 '20 23:04 therapon

suspect this is related to #115

pbcornell avatar Apr 22 '20 23:04 pbcornell