Python 2/3 Compatibility - #500
Conversation
5aa2591 to
82e6ddf
Compare
Codecov Report
@@ Coverage Diff @@
## master #500 +/- ##
=======================================
+ Coverage 64% 64% +<1%
=======================================
Files 198 198
Lines 12071 12079 +8
=======================================
+ Hits 7766 7772 +6
- Misses 4305 4307 +2
Continue to review full report at Codecov.
|
| @@ -1,24 +1,25 @@ | |||
| #!/usr/bin/env python2.7 | |||
There was a problem hiding this comment.
Now all scripts should start with "#!/usr/bin/env python", so that they can run regardless of the installed Python version..?
There was a problem hiding this comment.
(sorry not all scripts, but executable scripts)
|
|
||
| install: | ||
| - sudo apt-get install -y python | ||
| - sudo apt-get install -y python python3 python3-pip |
There was a problem hiding this comment.
While it is not an issue on our test setup, python might be an alias of python3 on other distros. I would be better to specify python2 explicitly.
There was a problem hiding this comment.
Done. python2 is not a package name, but I changed python to python2 in other places.
| install: | ||
| - sudo apt-get install -y python | ||
| - sudo apt-get install -y python python3 python3-pip | ||
| - pip install grpcio scapy codecov |
| - sudo apt-get install -y python | ||
| - sudo apt-get install -y python python3 python3-pip | ||
| - pip install grpcio scapy codecov | ||
| - pip3 install grpcio coverage |
There was a problem hiding this comment.
scapy for pip3 is broken :(
And the tests don't need scapy.
There was a problem hiding this comment.
Oh... then perhaps scapy-python3 is the package we need to install (https://github.com/phaethon/scapy)...
Also bessctl/bessctl -- daemon start -- run testing/run_module_tests in .travis.yml now needs "python2" explicitly, as it does use scapy?
There was a problem hiding this comment.
Also we would need to replace str(scapy packet) with bytes(scapy packets) in BESS scripts... The scapy3k webpage says:
N.B.! As a difference from scapy for python2, use bytes() instead of str() when converting packet to bytes. Also, most arguments expect bytes value instead of str value except the ones, which are naturally suited for human input (e.g. domain name).
| @@ -1,24 +1,25 @@ | |||
| #!/usr/bin/env python2.7 | |||
There was a problem hiding this comment.
(sorry not all scripts, but executable scripts)
| line_buf = [] | ||
|
|
||
| for arg in sys.argv[1:]: | ||
| arg = arg.decode() |
There was a problem hiding this comment.
Why do we convert it into a unicode..? (please add a comment)
There was a problem hiding this comment.
StringIO takes unicodes. I changed it to BytesIO for clarity (and thus avoiding the conversion).
There was a problem hiding this comment.
Ah, it seems that in Python 2 arguments are merely str (not unicode), while in Python 3 they are Unicode str, so we have to convert them into unicode first.
There was a problem hiding this comment.
py3k str is Unicode, yes. (The type name unicode no longer exists, it's all just str.) py2k str and bytes are the same underlying type. The py2k io.StringIO does require unicode, so probably this should use io.BytesIO on py2k and io.StringIO on py3k. Or you can use six.StringIO here, if you're importing six (I forget if you are).
There was a problem hiding this comment.
Yes, I guess it is easier to just make sure the input are Unicode (already did) instead of handling StringIO and BytesIO separately. I am attempting to avoid extra packages like six and future to keep the dependency minimal 😄
There was a problem hiding this comment.
Sure, but you'll have to not do .decode() if it's py3k (either that, or trap AttributeError as there is no decode for str objects).
There was a problem hiding this comment.
Oh, clever! u' '.join(...) works in both, converting py2k string-as-bytes to unicode, leaving py3k strings alone.
| FieldDescriptor.TYPE_BOOL: bool, | ||
| FieldDescriptor.TYPE_STRING: unicode, | ||
| FieldDescriptor.TYPE_STRING: str, | ||
| FieldDescriptor.TYPE_BYTES: str, |
There was a problem hiding this comment.
I am not quite sure about this, but shouldn't it be bytes...?
chris3torek
left a comment
There was a problem hiding this comment.
I've merely scanned this for anything that stood out to me.
| line_buf = [] | ||
|
|
||
| for arg in sys.argv[1:]: | ||
| arg = arg.decode() |
There was a problem hiding this comment.
The elements of sys.argv should already be strings (not bytes) in py3k. Are you sure this is needed?
| @@ -111,7 +112,7 @@ def check_c_lib(lib): | |||
| def required(header_file, lib_name, compiler): | |||
| if not check_header(header_file, compiler): | |||
| print >> sys.stderr, 'Error - #include <%s> failed. ' \ | |||
There was a problem hiding this comment.
Seems to be missing its conversion...?
There was a problem hiding this comment.
Ah, this seems to be a stale version.
| @@ -73,9 +74,9 @@ def do_dist_clean(): | |||
|
|
|||
| def print_usage(): | |||
| print >> sys.stderr, \ | |||
There was a problem hiding this comment.
This also still has the >> sys.stderr (and is not a function call)...
| FieldDescriptor.TYPE_SFIXED64: long, | ||
| FieldDescriptor.TYPE_SFIXED64: int, | ||
| FieldDescriptor.TYPE_BOOL: bool, | ||
| FieldDescriptor.TYPE_STRING: unicode, |
There was a problem hiding this comment.
I don't quite understand how this is to work in both py2k and py3k but I also don't quite understand how it works now in py2k in the first place, so I'm just confessing ignorance here. 😄 (In particular the values have to already have been converted somewhere by this point.)
5353b8d to
5e7fc4a
Compare

This should fix #488.
print()withfrom __future__ import print_function;mapandfilter;items()instead ofiteritems()(Yeah, it would become less efficient in Python 2);iopackage toread()from files, so that the resulting string is Unicode-encoded;raw_inputfor Python 2 andinputfor Python 3;tokenizerbetween Python 2/3 (->is anOPtoken in Python 3);2to3overprotocgenerated scripts, since those scripts still use implicit relative import;b'\x01').