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

On windows, check if the file is opened in binary mode #369

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kbandla
Copy link
Owner

@kbandla kbandla commented Jun 1, 2017

Often times, windows users open the files with the wrong mode. Unfortunately, the file mode property is read-only, so we can not fix it on the fly. Raise an exception instead.

@brifordwylie
Copy link
Collaborator

brifordwylie commented Jun 1, 2017

Why are all the builds suddenly failing? Seems to be related to pypa/setuptools#937. I'll poke around on it. Okay.. I'm not sure what's going on with Travis CI.. I'm going to wait a day and see if they figure it out/clear it up.

dpkt/pcap.py Outdated
@@ -197,6 +197,8 @@ class Writer(object):
"""

def __init__(self, fileobj, snaplen=1500, linktype=DLT_EN10MB, nano=False):
if fileobj.mode != 'wb':
Copy link
Collaborator

Choose a reason for hiding this comment

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

add a check for Windows, same as below?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Oops! yes, thanks for catching

dpkt/pcap.py Outdated
@@ -240,6 +242,9 @@ class Reader(object):

def __init__(self, fileobj):
self.name = getattr(fileobj, 'name', '<%s>' % fileobj.__class__.__name__)
if sys.platform.startswith('win'):
if fileobj.mode != 'rb':
raise ValueError('PCAP file (%s) not opened in binary mode (rb)'%self.name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

pep8

... (rb)' % self.name)
         ^ ^ spaces

dpkt/pcap.py Outdated
@@ -240,6 +242,9 @@ class Reader(object):

def __init__(self, fileobj):
self.name = getattr(fileobj, 'name', '<%s>' % fileobj.__class__.__name__)
if sys.platform.startswith('win'):
if fileobj.mode != 'rb':
Copy link
Collaborator

Choose a reason for hiding this comment

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

both mode checks feel a bit too restrictive (but make complete sense at the same time..). I personally would go with smth like if 'b' not in fileobj.mode

Copy link
Owner Author

Choose a reason for hiding this comment

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

Just noticed that i did not account for append modes, your suggestion takes care of that too.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.6%) to 87.878% when pulling fad3d74 on pcap_binary_mode into 6754f44 on master.

@brifordwylie
Copy link
Collaborator

Okay the tests on TravisCI are now properly running again... there are two failed tests...

    def __init__(self, fileobj, snaplen=1500, linktype=DLT_EN10MB, nano=False):
>       if fileobj.mode != 'wb':
E       AttributeError: 'BytesIO' object has no attribute 'mode'

and then a test for the deprecated decorator (I don't totally understand why that is failing).

@kbandla
Copy link
Owner Author

kbandla commented Jun 1, 2017

going to look at this some more later today. I have a bunch of more updates for pcap.py anyway

While testing the PR some more, I noticed that python3 on Linux did
not open in binary mode when we do no pass 'b'. Therefore it seemed
appropriate to expand this check to all OS, and not just windows.
Copy link
Collaborator

@brifordwylie brifordwylie left a comment

Choose a reason for hiding this comment

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

LGTM :)

dpkt/pcap.py Outdated
@@ -240,6 +242,9 @@ class Reader(object):

def __init__(self, fileobj):
self.name = getattr(fileobj, 'name', '<%s>' % fileobj.__class__.__name__)
if sys.platform.startswith('win'):
Copy link
Collaborator

Choose a reason for hiding this comment

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

So do we want to just check 'rb' mode for all platforms?

@brifordwylie
Copy link
Collaborator

Shrug... somehow the builds are failing... will have to circle back to this and see if I can figure out the issues...

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

Successfully merging this pull request may close these issues.

None yet

4 participants