From af2d000250a8505ea7f8e160e2a376c600dfb65b Mon Sep 17 00:00:00 2001 From: Nils Weiss Date: Sun, 5 Mar 2023 00:16:38 +0100 Subject: [PATCH] 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 --- run_scapy | 12 +----- run_scapy.bat | 9 +--- scapy/tools/UTscapy.py | 64 ++++++++-------------------- test/contrib/canfdsocket_native.uts | 2 +- test/contrib/cansocket.uts | 2 +- test/contrib/cansocket_native.uts | 2 +- test/contrib/isotp_native_socket.uts | 2 +- test/fields.uts | 4 -- test/imports.uts | 2 +- test/linux.uts | 2 +- test/run_tests | 1 - test/run_tests.bat | 5 +-- test/scapy/layers/dhcp.uts | 1 - test/scapy/layers/inet.uts | 1 - test/tools/isotpscanner.uts | 2 - 15 files changed, 27 insertions(+), 84 deletions(-) diff --git a/run_scapy b/run_scapy index ce21208a1..87b6f50e9 100755 --- a/run_scapy +++ b/run_scapy @@ -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 $@ diff --git a/run_scapy.bat b/run_scapy.bat index 14e71c61c..d801bbdc0 100644 --- a/run_scapy.bat +++ b/run_scapy.bat @@ -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 ( diff --git a/scapy/tools/UTscapy.py b/scapy/tools/UTscapy.py index ce891272b..d326b4906 100644 --- a/scapy/tools/UTscapy.py +++ b/scapy/tools/UTscapy.py @@ -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: diff --git a/test/contrib/canfdsocket_native.uts b/test/contrib/canfdsocket_native.uts index 17889196b..1f0ca94d9 100644 --- a/test/contrib/canfdsocket_native.uts +++ b/test/contrib/canfdsocket_native.uts @@ -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/ diff --git a/test/contrib/cansocket.uts b/test/contrib/cansocket.uts index 002b2021a..52165634c 100644 --- a/test/contrib/cansocket.uts +++ b/test/contrib/cansocket.uts @@ -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/ diff --git a/test/contrib/cansocket_native.uts b/test/contrib/cansocket_native.uts index e5dae7a5e..22d48de11 100644 --- a/test/contrib/cansocket_native.uts +++ b/test/contrib/cansocket_native.uts @@ -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/ diff --git a/test/contrib/isotp_native_socket.uts b/test/contrib/isotp_native_socket.uts index 55a815f6c..e41bf24ee 100644 --- a/test/contrib/isotp_native_socket.uts +++ b/test/contrib/isotp_native_socket.uts @@ -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() diff --git a/test/fields.uts b/test/fields.uts index 540877af9..882b2369e 100644 --- a/test/fields.uts +++ b/test/fields.uts @@ -1123,10 +1123,6 @@ assert fcb.i2repr_one(None, RandNum(0, 10)) == '' True = EnumField with Enum -~ python3_only - -# not available on Python 2... - from enum import Enum class JUICE(Enum): diff --git a/test/imports.uts b/test/imports.uts index 635c6aba7..5984fe572 100644 --- a/test/imports.uts +++ b/test/imports.uts @@ -1,7 +1,7 @@ % Import tests + Import tests -~ python3_only imports +~ imports = Prepare importing all scapy files diff --git a/test/linux.uts b/test/linux.uts index 516eade1a..589f1897a 100644 --- a/test/linux.uts +++ b/test/linux.uts @@ -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 diff --git a/test/run_tests b/test/run_tests index 49a10e9f9..bc6b58490 100755 --- a/test/run_tests +++ b/test/run_tests @@ -21,7 +21,6 @@ then do case $arg in - -2) PYTHON=python2;; -3) PYTHON=python3;; *) ARGS="$ARGS $arg";; esac diff --git a/test/run_tests.bat b/test/run_tests.bat index f3812ea35..b66ddcd70 100644 --- a/test/run_tests.bat +++ b/test/run_tests.bat @@ -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%" ) diff --git a/test/scapy/layers/dhcp.uts b/test/scapy/layers/dhcp.uts index 73323acf9..a6defec10 100644 --- a/test/scapy/layers/dhcp.uts +++ b/test/scapy/layers/dhcp.uts @@ -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 diff --git a/test/scapy/layers/inet.uts b/test/scapy/layers/inet.uts index a8987ec3f..c6684d6c7 100644 --- a/test/scapy/layers/inet.uts +++ b/test/scapy/layers/inet.uts @@ -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" diff --git a/test/tools/isotpscanner.uts b/test/tools/isotpscanner.uts index 39caf3981..a2d788ae8 100644 --- a/test/tools/isotpscanner.uts +++ b/test/tools/isotpscanner.uts @@ -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]