diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a1613ceeddeb321a5bcba927f504c0e8e291e661..5e101fada831c63b4d0aabb1278779ef61883986 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -113,7 +113,9 @@ Scapy's performances. ### Python 2 and 3 compatibility The project aims to provide code that works both on Python 2 and Python 3. Therefore, some rules need to be apply to achieve compatibility: -- byte-string must be defined as b"\x00\x01\x02" +- byte-string must be defined as `b"\x00\x01\x02"` +- exceptions must comply with the new Python 3 format: `except SomeError as e:` +- lambdas must be written using a single argument when using tuples: use `lambda x_y: x_y[0] + f(x_y[1])` instead of `lambda (x, y): x + f(y)`. ### Code review diff --git a/scapy/arch/linux.py b/scapy/arch/linux.py index b6f94b6c9f441c43d1f9b0c699a94b87e8550045..39117f9e2eba765f876e91282a2eca14bc1c9cd5 100644 --- a/scapy/arch/linux.py +++ b/scapy/arch/linux.py @@ -359,7 +359,7 @@ else: def _flush_fd(fd): - if not isinstance(fd, int): + if hasattr(fd, 'fileno'): fd = fd.fileno() while True: r,w,e = select([fd],[],[],0) diff --git a/scapy/asn1/ber.py b/scapy/asn1/ber.py index f1dc2901fe4108abb8f9e282725b4f64ca894537..0fd9b4f449d20e642d2955b97466dff83298a890 100644 --- a/scapy/asn1/ber.py +++ b/scapy/asn1/ber.py @@ -256,7 +256,7 @@ class BERcodec_Object: @classmethod def enc(cls, s): - if isinstance(s, str): + if isinstance(s, basestring): return BERcodec_STRING.enc(s) else: return BERcodec_INTEGER.enc(int(s)) diff --git a/scapy/contrib/http2.py b/scapy/contrib/http2.py index a6a7356d740119d3263c9f69f3c9c66081782f06..4ff53c1b265dd6510b69c9427f234523d4c41baf 100644 --- a/scapy/contrib/http2.py +++ b/scapy/contrib/http2.py @@ -192,7 +192,7 @@ class AbstractUVarIntField(fields.Field): @return None @raise AssertionError """ - assert(isinstance(default, type(None)) or (isinstance(default, (int, long)) and default >= 0)) + assert(default is None or (isinstance(default, (int, long)) and default >= 0)) assert(0 < size <= 8) super(AbstractUVarIntField, self).__init__(name, default) self.size = size diff --git a/scapy/dadict.py b/scapy/dadict.py index aa2e834c286df9bd8577b5646fa97fee3a827237..67cb8515e7d782100aa6aba3fbaabace2c95b2b2 100644 --- a/scapy/dadict.py +++ b/scapy/dadict.py @@ -35,7 +35,7 @@ class DADict: def __setitem__(self, attr, val): return setattr(self, self.fixname(attr), val) def __iter__(self): - return iter(map(lambda x_y1:x_y1[1],filter(lambda x_y:x_y[0] and x_y[0][0]!="_", self.__dict__.items()))) + return iter(map(lambda x_y1: x_y1[1],filter(lambda x_y: x_y[0] and x_y[0][0]!="_", self.__dict__.items()))) def _show(self): for k in self.__dict__.keys(): if k and k[0] != "_": diff --git a/scapy/layers/dhcp6.py b/scapy/layers/dhcp6.py index 62199d25db828823e95f8c69a9430a3e90101aea..45eb8607aad6444544ccbe58ef48fd7115f7846b 100644 --- a/scapy/layers/dhcp6.py +++ b/scapy/layers/dhcp6.py @@ -1408,7 +1408,7 @@ dhcp6d( dns="2001:500::1035", domain="localdomain, local", duid=None) return False # provided server DUID must match ours duid = p[DHCP6OptServerId].duid - if (not isinstance(duid, type(self.duid))): + if not isinstance(duid, type(self.duid)): return False if str(duid) != str(self.duid): return False @@ -1475,7 +1475,7 @@ dhcp6d( dns="2001:500::1035", domain="localdomain, local", duid=None) elif p.msgtype == 11: # Information-Request if DHCP6OptServerId in p: duid = p[DHCP6OptServerId].duid - if (not isinstance(duid, type(self.duid))): + if not isinstance(duid, type(self.duid)): return False if str(duid) != str(self.duid): return False diff --git a/scapy/layers/isakmp.py b/scapy/layers/isakmp.py index a1ec87f24579092eb4aca6e07b07f13cd5760f60..f612f280e9058522b8b2415ea512143ef5bb9675 100644 --- a/scapy/layers/isakmp.py +++ b/scapy/layers/isakmp.py @@ -103,7 +103,7 @@ del(val) class ISAKMPTransformSetField(StrLenField): islist=1 def type2num(self, type_val_tuple): - (typ,val) = type_val_tuple + typ, val = type_val_tuple type_val,enc_dict,tlv = ISAKMPTransformTypes.get(typ, (typ,{},0)) val = enc_dict.get(val, val) s = ""