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

Byte string read and write does not work for Python3 #63

Closed
jmcgeheeiv opened this issue Jan 13, 2016 · 4 comments
Closed

Byte string read and write does not work for Python3 #63

jmcgeheeiv opened this issue Jan 13, 2016 · 4 comments
Labels

Comments

@jmcgeheeiv
Copy link
Contributor

This is tested by test pyfakefs.fake_filesystem_test.FakeFileOpenTest.testByteContents(), which is currently skipped for Python 3 and above.

@allenluce
Copy link

I'd think this is OK to leave as it is. A write to a non-binary filehandle in Python 3 results in an error:

Python 3.5.0 (default, Sep 23 2015, 04:42:00)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> byte_fractions = b'\xe2\x85\x93 \xe2\x85\x94 \xe2\x85\x95 \xe2\x85\x96'
>>> with open("/tmp/foo", 'w') as f:
...   f.write(byte_fractions)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: write() argument must be str, not bytes
>>>

@jmcgeheeiv
Copy link
Contributor Author

Thank you for lending your expertise with binary files.

Indeed the test as it is now does not work with binary data in Python3. I was thinking that the test should be updated to write the file in mode 'wb' and read it in mode 'rb'.

When you do this, pyfakefs raises an exception, complaining that the data cannot be encoded as ascii. This is a bug in pyfakefs.

So I think that the solution it to change the modes in the test to 'wb' and 'rb', and then fixing pyfakes so that the test passes.

@dougluce
Copy link
Contributor

OK, I see what you mean now. The changes I made in #60 should take care of the pyfakefs problem. I've just added the test case you suggested and another one to test Py3-specific behavior with writing in binary and reading in non-binary.

@jmcgeheeiv
Copy link
Contributor Author

Tests pass. Good thing I left it to the expert!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants