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

[Deprecated PR] Python 3- Step 2: Auto-code migration #574

Closed
wants to merge 9 commits into from

Conversation

gpotter2
Copy link
Member

@gpotter2 gpotter2 commented Mar 22, 2017

EDIT: That PR was splitted in smaller ones. This will stay open for legacy until all have been merged

Next part of supporting Python 3. This increases the compatibility between python 2 and 3, by using common methods + six.py module. It does not provide a suitable python 3 compatibility yet.

The PR:

  • used Python-modernize as a base to update scapy's code
  • manually fixed some conflicts/bugs
  • Included six.py in modules
  • fix some small bugs (windows)

@codecov-io
Copy link

codecov-io commented Mar 22, 2017

Codecov Report

Merging #574 into master will decrease coverage by 0.15%.
The diff coverage is 63.41%.

@@            Coverage Diff             @@
##           master     #574      +/-   ##
==========================================
- Coverage   77.48%   77.33%   -0.16%     
==========================================
  Files         125      127       +2     
  Lines       30980    31633     +653     
==========================================
+ Hits        24005    24462     +457     
- Misses       6975     7171     +196
Impacted Files Coverage Δ
scapy/layers/netflow.py 100% <100%> (ø) ⬆️
scapy/layers/radius.py 100% <100%> (ø) ⬆️
scapy/layers/tftp.py 36.51% <100%> (+0.42%) ⬆️
scapy/layers/mobileip.py 100% <100%> (ø) ⬆️
scapy/layers/tls/__init__.py 57.14% <100%> (+7.14%) ⬆️
scapy/as_resolvers.py 90.29% <100%> (+0.19%) ⬆️
scapy/layers/vxlan.py 88% <100%> (+0.5%) ⬆️
scapy/arch/common.py 100% <100%> (ø) ⬆️
scapy/contrib/eigrp.py 73.02% <100%> (+0.38%) ⬆️
scapy/layers/tls/all.py 100% <100%> (ø) ⬆️
... and 118 more

Copy link
Member

@p-l- p-l- left a comment

Choose a reason for hiding this comment

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

I have not reviewed in entirely yet, but that's a good start!

@@ -145,7 +147,7 @@ def attach_filter(s, bpf_filter, iface):
nb = int(lines[0])
bpf = ""
for l in lines[1:]:
bpf += struct.pack("HBBI",*map(long,l.split()))
bpf += struct.pack("HBBI",*list(map(int,l.split())))
Copy link
Member

Choose a reason for hiding this comment

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

You don't need list() here, this should be enough: bpf += struct.pack("HBBI",*map(int, l.split()))

@@ -88,7 +90,7 @@ def resolve(self, *ips):
for l in r.splitlines()[1:]:
if "|" not in l:
continue
asn,ip,desc = map(str.strip, l.split("|"))
asn,ip,desc = list(map(str.strip, l.split("|")))
Copy link
Member

Choose a reason for hiding this comment

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

Again, list() is not needed here.

import gtp_v2
return gtp_v2.GTPHeader
return GTPHeader
@classmethod
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this windows EOL?

Copy link
Member Author

Choose a reason for hiding this comment

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

Any ideas how to do it ? I cannot see it using vim/windows and want to avoid having the entire file changed....

Copy link
Member

Choose a reason for hiding this comment

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

dos2unix?

Copy link
Member Author

Choose a reason for hiding this comment

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

Cannot do it without chaning the hole file... Will do this in a next PR :/ (5 files in total are badly encoded)

Copy link
Member Author

Choose a reason for hiding this comment

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

Files that have wrong line endings: gtp.py, ppi.py, ppi_cace.py, ppi_geotag.py, rsvp.py
Editing those files often creates messy eol like this one :(

@rjarry
Copy link
Contributor

rjarry commented Mar 23, 2017

Oh yeah 👍

@gpotter2 what about adding:

from __future__ import unicode_literals

in all files?

This would require using b'byte strings' but this would be cleaner (I think).

Copy link
Contributor

@rjarry rjarry left a comment

Choose a reason for hiding this comment

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

I did not have time to read all of this but it looks good.

Do you think this would be possible to split this commit a70a803 into several ones?

Maybe at least a separate commit for the print() function.

import re,random,socket
import types
from six.moves import map
from six.moves import range
from six.moves import zip
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe merge the 3 import lines like this?

from six.moves import map, range, zip

@@ -27,6 +28,9 @@
from scapy.layers.inet6 import IP6Field
from scapy.config import conf, ConfClass
from scapy.error import log_runtime
from six.moves import map
import six
from six.moves import range
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here:

from six.moves import map, range

import six
from six.moves import map
from six.moves import range
from six.moves import zip
Copy link
Contributor

Choose a reason for hiding this comment

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

from six.moves import map, range, zip

r.sprintf("%-15s,IP.src% {TCP:%TCP.flags%}{ICMP:%ir,ICMP.type%}")))
return self.make_table(lambda s_r: (s_r[0].sprintf("%IP.dst%:{TCP:tcp%ir,TCP.dport%}{UDP:udp%ir,UDP.dport%}{ICMP:ICMP}"),
s_r[0].ttl,
s_r[1].sprintf("%-15s,IP.src% {TCP:%TCP.flags%}{ICMP:%ir,ICMP.type%}")))
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix indentation here.

r.sprintf("%-42s,IPv6.src% {TCP:%TCP.flags%}"+
return self.make_table(lambda s_r: (s_r[0].sprintf("%-42s,IPv6.dst%:{TCP:tcp%TCP.dport%}{UDP:udp%UDP.dport%}{ICMPv6EchoRequest:IER}"), # TODO: ICMPv6 !
s_r[0].hlim,
s_r[1].sprintf("%-42s,IPv6.src% {TCP:%TCP.flags%}"+
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation here also :)

@@ -89,7 +90,7 @@ class NBNSRequest(Packet):
IntField("TTL", 0),
ShortField("RDLENGTH", 6),
BitEnumField("G",0,1,{0:"Unique name",1:"Group name"}),
BitEnumField("OWNER_NODE_TYPE",00,2,{00:"B node",01:"P node",02:"M node",03:"H node"}),
BitEnumField("OWNER_NODE_TYPE",00,2,{00:"B node",0o1:"P node",0o2:"M node",0o3:"H node"}),
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understand this. Are you sure this is octal?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well no. But written like 01, it's interpreted as an octal...

Copy link
Member

Choose a reason for hiding this comment

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

@gpotter2 that's an error, can you use decimal values here?

@@ -131,7 +132,7 @@ class NBNSQueryResponseNegative(Packet):
IntField("TTL",0),
ShortField("RDLENGTH",6),
BitEnumField("G",0,1,{0:"Unique name",1:"Group name"}),
BitEnumField("OWNER_NODE_TYPE",00,2,{00:"B node",01:"P node",02:"M node",03:"H node"}),
BitEnumField("OWNER_NODE_TYPE",00,2,{00:"B node",0o1:"P node",0o2:"M node",0o3:"H node"}),
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

@gpotter2 gpotter2 changed the title [convergence] Python 3- Migrate code [convergence] Python 3- Step 2: Auto-code migration Mar 23, 2017
@gpotter2
Copy link
Member Author

@robin-jarry I know the commit is massive. As I said, it partially result from an auto tool, so I won't be able to split it :/

@gpotter2
Copy link
Member Author

@p-l- @robin-jarry

  • I've added an instruction in the .gitattributes file to auto fix EOL in the future...
  • Please ignore ed571b7. The entire file format was changed, that's why git is messed up

@rjarry
Copy link
Contributor

rjarry commented Mar 23, 2017

@gpotter2, I had not seen @guedou comment on another PR which you closed: #567

I completely agree on the "divide and conquer" strategy. This is always better than large patches. It is easier to check for details in small patches, limiting the risks.

On the whole print() function thing, I think I prefer adding:

from __future__ import print_function

everywhere, even if this is quite cumbersome. It has the benefit of completely preventing the use of the old print statement. Also, defining a scapy_print function would require importing it too, so well...

@guedou, what do you think?

To all, what do you think about also adding:

from __future__ import unicode_literals

@gpotter2
Copy link
Member Author

gpotter2 commented Mar 23, 2017

I got a patch for the failing travis test...
Just waiting for #578 before pushing...

@robin-jarry

To all, what do you think about also adding:
from __future__ import unicode_literals

Why not... I don't really understand how useful it is ?

@rjarry
Copy link
Contributor

rjarry commented Mar 23, 2017

@gpotter2

To all, what do you think about also adding:
from __future__ import unicode_literals

Why not... I don't really understand how useful it is ?

This makes python 2 behave like python 3 regarding string literals.

I found this article inspiring on that matter:

http://python-future.org/unicode_literals.html

@gpotter2
Copy link
Member Author

gpotter2 commented Mar 23, 2017

Thanks @robin-jarry
I'll check this tomorrow.

@guedou
Copy link
Member

guedou commented Mar 24, 2017 via email

@gpotter2 gpotter2 force-pushed the scpy-30-prog branch 2 times, most recently from 99a3365 to 768557f Compare March 25, 2017 00:36
@gpotter2
Copy link
Member Author

gpotter2 commented Mar 25, 2017

It might take some until the compatibility is achieved, and I would like that contributors keep writing code without restrictions.

That's right. We can always add it later, once the compatibility will be really working.

@gpotter2
Copy link
Member Author

gpotter2 commented Mar 25, 2017

To all:

I'll won't add more fixes to this PR. Waiting for it to be reviewed. Every thing will be in parts 3+

I'm stopping there for now, waiting for all this to be approved + modified.

@@ -16,6 +17,7 @@
import scapy.config
from scapy.pton_ntop import inet_pton
from scapy.data import *
from scapy.modules.six.moves import map
Copy link
Member

Choose a reason for hiding this comment

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

I think that it will be nice to rewrite the map calls using list comprehensions. I can do that in another PR if you want.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that would be great. I don't want to make overweight this one...

Copy link
Member

Choose a reason for hiding this comment

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

I am still working on it =/

@p-l-
Copy link
Member

p-l- commented Mar 29, 2017

I agree with @robin-jarry regarding the use of from __future__ import print_function and from __future__ import unicode_literals.

@guedou
Copy link
Member

guedou commented Mar 29, 2017

@p-l- regarding print_function, it don't like that regular Python 2 users will be forced to use print(). I think that it is not necessary, and its might scare away contributors. That's why, my preference goes do scapy_print().

@gpotter2
Copy link
Member Author

gpotter2 commented Apr 17, 2017

Here is a nice and clean rebased state. (removed all merge commits + fixed conflicts).
Also, i would like to add
from __future__ import unicode_literals
in a future PR.

@rjarry
Copy link
Contributor

rjarry commented Apr 17, 2017

@guedou, I don’t see why it would bother anyone to put parentheses arround print arguments. And this is the new official python syntax, I think everyone should get used to it.

https://pythonclock.org :-)

Also, using scapy_print() will require importing something anyway. Since there already is a future import for older python versions, why not use it?

@guedou
Copy link
Member

guedou commented Apr 17, 2017 via email

@gpotter2 gpotter2 force-pushed the scpy-30-prog branch 8 times, most recently from 5bd1e8f to d8c4ee8 Compare April 22, 2017 21:52
@gpotter2
Copy link
Member Author

gpotter2 commented Apr 23, 2017

To all, @p-l- @guedou
There are not much left to add/correct I guess. If you still got any suggestion please tell me.

A last question: what do we do about print(), as every one does not agree yet on what we should do. I personally prefer using print() as for the same reasons than @rjarry... Which means I think we can leave what's in this PR as such.

Apart from that, it's quite painful to maintain this PR up to date with the rest of scapy, so it would be great if it could be merged or included quite soon :) Thanks !

@guedou
Copy link
Member

guedou commented Apr 23, 2017 via email

@gpotter2
Copy link
Member Author

gpotter2 commented Apr 24, 2017

@guedou You are right, but It's hard to split a PR like this one:

  • It's based on an automatic tool, which brings tons of fixes: it would be a giant pain to split this in parts :/ more than reviewing it.
  • I tried to make the commits as explicit as possible, so the reviewing would be easier...
  • Most of the commits are fixes for the automatic detection: it would not make any sense to split them in separate PRs.
  • Most of the changes are really easy to understand: reviewing this PR is long, but not hard.
  • Lastly, i will certainly fix part 3 into smaller parts, but part 2 is really made of basic changes, which (in my opinion) don't need separate PRs.

I know it's a hard work for you guys, and i'm sorry of that...

@b-x
Copy link
Contributor

b-x commented Apr 24, 2017

@gpotter2 python-modernize has a nice -l and -f options, so it's not so hard to split this pull request

@gpotter2
Copy link
Member Author

Please don't make me do every thing again :(

@guedou
Copy link
Member

guedou commented Apr 25, 2017

@gpotter2 I really understand your frustration, but we need to find a way to merge your patches easily. Unfortunately, you would need to submit smaller PRs to do so, as we previously discussed. I can assure you that I will be able to review and merge the smaller ones quickly.

I consider that this one is a proof of concept that needs to be split for easier review and maintenance. We won't have a time to proof read all of the subsequent changes to make sure that everything is fine. This current PR as already some issues: for example from scapy.modules.six.moves import map is useless in AS_resolvers.py. If you fix it, we will need to read the changes again.

Could you submit the following small PRs?

  • new exception style
  • replace long with int
  • print as a function
  • filter() replaced with list comprehension

I am still working on rewriting the map() with list comprehension and I am planning to submit a PRs next week.

@gpotter2
Copy link
Member Author

gpotter2 commented Apr 25, 2017

OK.
@guedou I've splitted the thing in 3 PRs (one more is comming with filter() + Zopieux's fixes)

@gpotter2 gpotter2 changed the title [convergence] Python 3- Step 2: Auto-code migration [Deprecated PR] Python 3- Step 2: Auto-code migration May 3, 2017
@guedou
Copy link
Member

guedou commented Jun 16, 2017

@gpotter2 can we close this PR ?

@gpotter2 gpotter2 closed this Jun 16, 2017
@gpotter2 gpotter2 deleted the scpy-30-prog branch September 23, 2017 14:56
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.

None yet

7 participants