ion-python
ion-python copied to clipboard
Using `dump` from `simpleion` requires file to be open in binary mode
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.
suspect this is related to #115