Update cStringIO imports#95
Conversation
modified: apptools/logger/agent/attachments.py modified: apptools/logger/log_point.py modified: apptools/logger/plugin/logger_service.py
Codecov Report
@@ Coverage Diff @@
## master #95 +/- ##
=========================================
Coverage ? 41.68%
=========================================
Files ? 240
Lines ? 8912
Branches ? 1158
=========================================
Hits ? 3715
Misses ? 5045
Partials ? 152
Continue to review full report at Codecov.
|
|
|
||
| import zipfile | ||
| from cStringIO import StringIO | ||
| from six.moves import cStringIO as StringIO |
There was a problem hiding this comment.
Would the simpler from six import StringIO work here?
There was a problem hiding this comment.
Actually, looking at the place where this is used, do we need BytesIO rather than StringIO?
There was a problem hiding this comment.
Looks like we do. On Python 3, working in a directory that contains a tox.ini file, I see this:
>>> z = zipfile.ZipFile(StringIO(), "w")
>>> z.write("tox.ini")
Exception ignored in: <function ZipFile.__del__ at 0x1076fe7b8>
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1789, in __del__
self.close()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1807, in close
self._write_end_record()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1911, in _write_end_record
self.fp.write(endrec)
TypeError: string argument expected, got 'bytes'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1743, in write
with open(filename, "rb") as src, self.open(zinfo, 'w') as dest:
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1470, in open
return self._open_to_write(zinfo, force_zip64=force_zip64)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1583, in _open_to_write
self.fp.write(zinfo.FileHeader(zip64))
TypeError: string argument expected, got 'bytes'
There was a problem hiding this comment.
I think we need to use io.BytesIO (on both Python 2 and Python 3) for the two cases where we're writing a zip file. We could use six.BytesIO, but io.BytesIO is a bit more correct here: six.BytesIO will accept Unicode text on Python 2, but io.BytesIO doesn't.
For the other case, I'd suggest six.StringIO as a shorter way to spell six.moves.cStringIO. In that case, I think we do want six.StringIO rather than io.StringIO, for the leniency of allowing either str or unicode to be written on Python 2.
|
And this also shows that unfortunately this code isn't being exercised by unit tests. Does it look possible to write a regression test? (And one that would catch the accidental misuse of |
when used with zipfile.ZipFile, we need a BytesIO and not a StringIO object. also update the imports to be less verbose modified: apptools/logger/agent/attachments.py modified: apptools/logger/log_point.py modified: apptools/logger/plugin/logger_service.py
modified: apptools/logger/agent/attachments.py new file: apptools/logger/agent/tests/__init__.py new file: apptools/logger/agent/tests/test_attachments.py new file: apptools/logger/plugin/tests/__init__.py new file: apptools/logger/plugin/tests/test_logger_service.py
|
@mdickinson i was going to say that writing tests will take too long but it didn't really. can you take another look at the PR? I think i've addressed all of the comments and i've added the regression tests (to atleast catch trivial bugs). |
|
modified: apptools/logger/agent/tests/test_attachments.py
modified: apptools/logger/agent/attachments.py modified: apptools/logger/agent/tests/test_attachments.py modified: apptools/logger/plugin/logger_service.py
modified: apptools/logger/agent/tests/test_attachments.py
| msg = logger_service.create_email_message( | ||
| fromaddr='', toaddrs='', ccaddrs='', subject='', priority='' | ||
| ) | ||
| self.assertIsNotNone(msg) |
There was a problem hiding this comment.
I'd drop this, unless there's some a priori reason to expect the msg to be None. This check is redundant with the assertIsInstance check below.
| fromaddr='', toaddrs='', ccaddrs='', subject='', priority='', | ||
| include_userdata=True, | ||
| ) | ||
| self.assertIsNotNone(msg) |
| include_userdata=True, | ||
| ) | ||
| self.assertIsNotNone(msg) | ||
| self.assertIsInstance(msg, MIMEMultipart) No newline at end of file |
mdickinson
left a comment
There was a problem hiding this comment.
Done with review; various nitpicks, but looks good otherwise.
modified: apptools/logger/plugin/tests/test_logger_service.py
fixes #94