Skip to content

Update cStringIO imports#95

Merged
mdickinson merged 7 commits into
masterfrom
fix/py3-bugs
Oct 9, 2019
Merged

Update cStringIO imports#95
mdickinson merged 7 commits into
masterfrom
fix/py3-bugs

Conversation

@rahulporuri
Copy link
Copy Markdown
Contributor

fixes #94

	modified:   apptools/logger/agent/attachments.py
	modified:   apptools/logger/log_point.py
	modified:   apptools/logger/plugin/logger_service.py
@codecov-io
Copy link
Copy Markdown

codecov-io commented Jul 3, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@30cd14d). Click here to learn what that means.
The diff coverage is 87.5%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master      #95   +/-   ##
=========================================
  Coverage          ?   41.68%           
=========================================
  Files             ?      240           
  Lines             ?     8912           
  Branches          ?     1158           
=========================================
  Hits              ?     3715           
  Misses            ?     5045           
  Partials          ?      152
Impacted Files Coverage Δ
apptools/logger/log_point.py 0% <0%> (ø)
apptools/logger/agent/attachments.py 81.35% <100%> (ø)
apptools/logger/plugin/logger_service.py 65.88% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 30cd14d...327ef6a. Read the comment docs.

Comment thread apptools/logger/agent/attachments.py Outdated

import zipfile
from cStringIO import StringIO
from six.moves import cStringIO as StringIO
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the simpler from six import StringIO work here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, looking at the place where this is used, do we need BytesIO rather than StringIO?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'

Copy link
Copy Markdown
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mdickinson
Copy link
Copy Markdown
Member

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 StringIO instead of BytesIO for the zip files?)

poruri sai rahul added 2 commits October 8, 2019 17:07
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
@rahulporuri
Copy link
Copy Markdown
Contributor Author

@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).

@rahulporuri
Copy link
Copy Markdown
Contributor Author

rahulporuri commented Oct 8, 2019

======================================================================

ERROR: test_attaching_single_project (apptools.logger.agent.tests.test_attachments.AttachmentsTestCase)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/home/travis/build/enthought/apptools/apptools/logger/agent/tests/test_attachments.py", line 25, in setUp

    filehandle.write("Dummy data in dummy file for dummies")

TypeError: write() argument 1 must be unicode, not str

======================================================================

ERROR: test_attaching_workspace (apptools.logger.agent.tests.test_attachments.AttachmentsTestCase)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/home/travis/build/enthought/apptools/apptools/logger/agent/tests/test_attachments.py", line 25, in setUp

    filehandle.write("Dummy data in dummy file for dummies")

TypeError: write() argument 1 must be unicode, not str

	modified:   apptools/logger/agent/tests/test_attachments.py
Comment thread apptools/logger/agent/attachments.py Outdated
	modified:   apptools/logger/agent/attachments.py
	modified:   apptools/logger/agent/tests/test_attachments.py
	modified:   apptools/logger/plugin/logger_service.py
Comment thread apptools/logger/agent/tests/test_attachments.py Outdated
Comment thread apptools/logger/agent/tests/test_attachments.py Outdated
Comment thread apptools/logger/agent/tests/test_attachments.py Outdated
	modified:   apptools/logger/agent/tests/test_attachments.py
Comment thread apptools/logger/plugin/logger_service.py Outdated
msg = logger_service.create_email_message(
fromaddr='', toaddrs='', ccaddrs='', subject='', priority=''
)
self.assertIsNotNone(msg)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

include_userdata=True,
)
self.assertIsNotNone(msg)
self.assertIsInstance(msg, MIMEMultipart) No newline at end of file
Copy link
Copy Markdown
Member

@mdickinson mdickinson Oct 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline!

Copy link
Copy Markdown
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with review; various nitpicks, but looks good otherwise.

	modified:   apptools/logger/plugin/tests/test_logger_service.py
@mdickinson mdickinson merged commit a36dc9a into master Oct 9, 2019
@mdickinson mdickinson deleted the fix/py3-bugs branch October 9, 2019 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No module named 'cStringIO' under Python 3

3 participants