Remove python3_only and six from UTScapy (#3888)

* remove python3_only and six from UTScapy

* remove python2 leftovers

* apply suggestions

* apply suggestions

---------

Co-authored-by: Nils Weiss <nils@dissec.to>
This commit is contained in:
Nils Weiss 2023-03-05 00:16:38 +01:00 committed by GitHub
parent 9946ef17f5
commit af2d000250
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 27 additions and 84 deletions

View File

@ -2,16 +2,6 @@
DIR=$(dirname "$0")
if [ -z "$PYTHON" ]
then
ARGS=""
for arg in "$@"
do
case $arg
in
-2) PYTHON=python2;;
-3) PYTHON=python3;;
*) ARGS="$ARGS $arg";;
esac
done
PYTHON=${PYTHON:-python3}
fi
$PYTHON --version > /dev/null 2>&1
@ -20,4 +10,4 @@ then
echo "WARNING: '$PYTHON' not found, using 'python' instead."
PYTHON=python
fi
PYTHONPATH=$DIR exec "$PYTHON" -m scapy $ARGS
PYTHONPATH=$DIR exec "$PYTHON" -m scapy $@

View File

@ -6,14 +6,7 @@ set "_args=%*"
IF "%PYTHON%" == "" set PYTHON=py
WHERE %PYTHON% >nul 2>&1
IF %ERRORLEVEL% NEQ 0 set PYTHON=
IF "%1" == "-2" (
if "%PYTHON%" == "py" (
set "PYTHON=py -2"
) else (
set PYTHON=python
)
set "_args=%_args:~3%"
) ELSE IF "%1" == "-3" (
IF "%1" == "-3" (
if "%PYTHON%" == "py" (
set "PYTHON=py -3"
) else (

View File

@ -6,8 +6,7 @@
"""
Unit testing infrastructure for Scapy
"""
import builtins
import bz2
import copy
import code
@ -26,8 +25,7 @@ import traceback
import warnings
import zlib
from scapy.consts import WINDOWS, DARWIN
import scapy.libs.six as six
from scapy.consts import WINDOWS
from scapy.config import conf
from scapy.compat import base64_bytes, bytes_hex, plain_str
from scapy.themes import DefaultTheme, BlackAndWhite
@ -41,8 +39,6 @@ def _utf8_support():
Check UTF-8 support for the output
"""
try:
if six.PY2:
return False
if WINDOWS:
return (sys.stdout.encoding == "utf-8")
return True
@ -68,20 +64,17 @@ class Bunch:
def retry_test(func):
"""Retries the passed function 3 times before failing"""
success = False
v = None
tb = None
for _ in range(3):
try:
result = func()
return func()
except Exception:
t, v, tb = sys.exc_info()
time.sleep(1)
else:
success = True
break
if not success:
six.reraise(t, v, tb)
assert success
return result
if v and tb:
raise v.with_traceback(tb)
def scapy_path(fname):
@ -177,12 +170,12 @@ I4LDm5WP7s2NaRkhhV7A\nFVSD5zA8V/DJzfTk0QHmCT2wRgwPKjP60EqqlDUaST
/i7kinChIXSAmRgA==\n""")
def get_local_dict(cls):
return {x: y.name for (x, y) in six.iteritems(cls.__dict__)
return {x: y.name for (x, y) in cls.__dict__.items()
if isinstance(y, File)}
get_local_dict = classmethod(get_local_dict)
def get_URL_dict(cls):
return {x: y.URL for (x, y) in six.iteritems(cls.__dict__)
return {x: y.URL for (x, y) in cls.__dict__.items()
if isinstance(y, File)}
get_URL_dict = classmethod(get_URL_dict)
@ -211,7 +204,7 @@ class TestClass:
return getattr(self, item)
def add_keywords(self, kws):
if isinstance(kws, six.string_types):
if isinstance(kws, str):
kws = [kws.lower()]
for kwd in kws:
kwd = kwd.lower()
@ -298,11 +291,6 @@ class UnitTest(TestClass):
self.expand = 1
def prepare(self, theme):
if six.PY2:
self.test = self.test.decode("utf8", "ignore")
self.output = self.output.decode("utf8", "ignore")
self.comments = self.comments.decode("utf8", "ignore")
self.result = self.result.decode("utf8", "ignore")
if self.result == "passed":
self.fresult = theme.success(self.result)
else:
@ -461,18 +449,12 @@ def docs_campaign(test_campaign):
# COMPUTE CAMPAIGN DIGESTS #
if six.PY2:
def crc32(x):
return "%08X" % (0xffffffff & zlib.crc32(x))
def crc32(x):
return "%08X" % (0xffffffff & zlib.crc32(bytearray(x, "utf8")))
def sha1(x):
return hashlib.sha1(x).hexdigest().upper()
else:
def crc32(x):
return "%08X" % (0xffffffff & zlib.crc32(bytearray(x, "utf8")))
def sha1(x):
return hashlib.sha1(x.encode("utf8")).hexdigest().upper()
def sha1(x):
return hashlib.sha1(x.encode("utf8")).hexdigest().upper()
def compute_campaign_digests(test_campaign):
@ -1107,11 +1089,6 @@ def main():
# Disable tests if needed
# Discard Python3 tests when using Python2
if six.PY2:
KW_KO.append("python3_only")
if VERB > 2:
print(" " + arrow + " Python 2 mode")
try:
if NON_ROOT or os.getuid() != 0: # Non root
# Discard root tests
@ -1128,11 +1105,6 @@ def main():
KW_KO.append("disabled")
# Process extras
if six.PY2 and DARWIN:
# On MacOS 12, Python 2.7 find_library is broken
KW_KO.append("libpcap")
if ANNOTATIONS_MODE:
try:
from pyannotate_runtime import collect_types
@ -1153,7 +1125,7 @@ def main():
for m in MODULES:
try:
mod = import_module(m)
six.moves.builtins.__dict__.update(mod.__dict__)
builtins.__dict__.update(mod.__dict__)
except ImportError as e:
raise getopt.GetoptError("cannot import [%s]: %s" % (m, e))
@ -1176,7 +1148,7 @@ def main():
UNIQUE = len(TESTFILES) == 1
# Resolve tags and asterix
for prex in six.iterkeys(copy.copy(PREEXEC_DICT)):
for prex in copy.copy(PREEXEC_DICT).keys():
if "*" in prex:
pycode = PREEXEC_DICT[prex]
del PREEXEC_DICT[prex]
@ -1238,7 +1210,7 @@ def main():
else:
with open(OUTPUTFILE, "wb") as f:
f.write(glob_output.encode("utf8", "ignore")
if 'b' in f.mode or six.PY2 else glob_output)
if 'b' in f.mode else glob_output)
# Print end message
if VERB > 2:

View File

@ -1,5 +1,5 @@
% Regression tests for nativecanfdsocket
~ python3_only not_pypy vcan_socket needs_root linux
~ not_pypy vcan_socket needs_root linux
# More information at http://www.secdev.org/projects/UTscapy/

View File

@ -1,5 +1,5 @@
% Regression tests for compatibility between NativeCANSocket and PythonCANSocket
~ python3_only not_pypy vcan_socket needs_root linux
~ not_pypy vcan_socket needs_root linux
# More information at http://www.secdev.org/projects/UTscapy/

View File

@ -1,5 +1,5 @@
% Regression tests for nativecansocket
~ python3_only not_pypy vcan_socket needs_root linux
~ not_pypy vcan_socket needs_root linux
# More information at http://www.secdev.org/projects/UTscapy/

View File

@ -514,7 +514,7 @@ assert not rxThread.is_alive()
assert succ
+ ISOTPNativeSocket MITM attack tests
~ python3_only vcan_socket needs_root linux
~ vcan_socket needs_root linux
= bridge and sniff with isotp native sockets set up vcan0 and vcan1 for package forwarding vcan1
exit_if_no_isotp_module()

View File

@ -1123,10 +1123,6 @@ assert fcb.i2repr_one(None, RandNum(0, 10)) == '<RandNum>'
True
= EnumField with Enum
~ python3_only
# not available on Python 2...
from enum import Enum
class JUICE(Enum):

View File

@ -1,7 +1,7 @@
% Import tests
+ Import tests
~ python3_only imports
~ imports
= Prepare importing all scapy files

View File

@ -318,7 +318,7 @@ assert _interface_selection(None, IP(dst="192.0.2.42")/UDP()) == "scapy0"
exit_status = os.system("ip link del name dev scapy0")
= Test 802.Q sniffing
~ linux needs_root python3_only veth
~ linux needs_root veth
from threading import Thread, Condition

View File

@ -21,7 +21,6 @@ then
do
case $arg
in
-2) PYTHON=python2;;
-3) PYTHON=python3;;
*) ARGS="$ARGS $arg";;
esac

View File

@ -5,10 +5,7 @@ set PYTHONPATH=%MYDIR%
REM Note: shift will not work with %*
REM ### Get args, Handle Python version ###
set "_args=%*"
IF "%1" == "-2" (
set PYTHON=python
set "_args=%_args:~3%"
) ELSE IF "%1" == "-3" (
IF "%1" == "-3" (
set PYTHON=python3
set "_args=%_args:~3%"
)

View File

@ -116,7 +116,6 @@ assert DHCPOptions[46].name == "NetBIOS_node_type"
assert DHCPRevOptions['static-routes'][0] == 33
= Check that the dhcpd alias is properly defined and documented
~ python3_only
assert dhcpd
import IPython

View File

@ -362,7 +362,6 @@ options = pkt.options._fix()
options
= TCP random options - MD5 (#GH3777)
~ python3_only
random.seed(0x2813)
pkt = TCP(options=RandTCPOptions()._fix())
assert pkt.options[0][0] == "MD5"

View File

@ -107,7 +107,6 @@ for out in expected_output:
= Test extended scan
~ python3_only
def isotp_scan(sock, # type: SuperSocket
scan_range=range(0x7ff + 1), # type: Iterable[int]
@ -140,7 +139,6 @@ with patch.object(sys, "argv", testargs), patch.object(scapy.contrib.isotp, "iso
= Test scan with piso flag
~ python3_only
def isotp_scan(sock, # type: SuperSocket
scan_range=range(0x7ff + 1), # type: Iterable[int]