diff --git a/pyroute2/__init__.py b/pyroute2/__init__.py index 84bddd723..431228108 100644 --- a/pyroute2/__init__.py +++ b/pyroute2/__init__.py @@ -11,10 +11,23 @@ from pyroute2.config.version import __version__ except ImportError: __version__ = 'unknown' +import sys -from pyroute2 import loader from pyroute2.cli.console import Console from pyroute2.cli.server import Server + +## +# +# Logging setup +# +# See the history: +# * https://github.com/svinota/pyroute2/issues/246 +# * https://github.com/svinota/pyroute2/issues/255 +# * https://github.com/svinota/pyroute2/issues/270 +# * https://github.com/svinota/pyroute2/issues/573 +# * https://github.com/svinota/pyroute2/issues/601 +# +from pyroute2.config import log from pyroute2.conntrack import Conntrack, ConntrackEntry from pyroute2.devlink import DL from pyroute2.ethtool.ethtool import Ethtool @@ -58,6 +71,17 @@ from pyroute2.plan9.server import Plan9ServerSocket from pyroute2.wiset import WiSet +## +# +# Windows platform specific: socket module monkey patching +# +# To use the library on Windows, run:: +# pip install win-inet-pton +# +if sys.platform.startswith('win'): # noqa: E402 + import win_inet_pton # noqa: F401 + + modules = [ AcpiEventSocket, AsyncIPRoute, @@ -106,8 +130,8 @@ UeventSocket, WireGuard, WiSet, + log, ] -loader.init() __all__ = [] __all__.extend(modules) diff --git a/pyroute2/cli/server.py b/pyroute2/cli/server.py index ea5b8ad0b..787e3b41c 100644 --- a/pyroute2/cli/server.py +++ b/pyroute2/cli/server.py @@ -1,15 +1,10 @@ import json +from http.server import BaseHTTPRequestHandler +from http.server import HTTPServer as HTTPServer from pyroute2.cli.session import Session from pyroute2.ndb.main import NDB -try: - from BaseHTTPServer import BaseHTTPRequestHandler - from BaseHTTPServer import HTTPServer as HTTPServer -except ImportError: - from http.server import BaseHTTPRequestHandler - from http.server import HTTPServer as HTTPServer - class ProxyEncoder(object): def __init__(self, wfile): diff --git a/pyroute2/dhcp/__init__.py b/pyroute2/dhcp/__init__.py index 526460238..fab6bd7cd 100644 --- a/pyroute2/dhcp/__init__.py +++ b/pyroute2/dhcp/__init__.py @@ -100,7 +100,6 @@ class array8(option): import logging import struct -import sys from array import array from typing import ClassVar, NamedTuple, Optional, TypedDict, TypeVar, Union @@ -188,7 +187,7 @@ def encode(self: _optionSelf) -> _optionSelf: fmt = '%is' % len(value) else: fmt = self.policy.format - if sys.version_info[0] == 3 and isinstance(value, str): + if isinstance(value, str): value = value.encode('utf-8') self.buf = struct.pack(fmt, value) else: diff --git a/pyroute2/iproute/bsd.py b/pyroute2/iproute/bsd.py index 8b3189c4b..f01650f9d 100644 --- a/pyroute2/iproute/bsd.py +++ b/pyroute2/iproute/bsd.py @@ -46,6 +46,7 @@ import errno import os +import queue import select import struct import threading @@ -71,11 +72,6 @@ from pyroute2.netlink.rtnl.ndmsg import ndmsg from pyroute2.netlink.rtnl.rtmsg import rtmsg -try: - import queue -except ImportError: - import Queue as queue - class IPRoute(object): def __init__(self, *argv, **kwarg): diff --git a/pyroute2/loader.py b/pyroute2/loader.py deleted file mode 100644 index 57fa5fe58..000000000 --- a/pyroute2/loader.py +++ /dev/null @@ -1,43 +0,0 @@ -import struct -import sys - -## -# -# Logging setup -# -# See the history: -# * https://github.com/svinota/pyroute2/issues/246 -# * https://github.com/svinota/pyroute2/issues/255 -# * https://github.com/svinota/pyroute2/issues/270 -# * https://github.com/svinota/pyroute2/issues/573 -# * https://github.com/svinota/pyroute2/issues/601 -# -from pyroute2.config import log - -## -# -# Windows platform specific: socket module monkey patching -# -# To use the library on Windows, run:: -# pip install win-inet-pton -# -if sys.platform.startswith('win'): # noqa: E402 - import win_inet_pton # noqa: F401 - - -def init(): - try: - # probe, if the bytearray can be used in struct.unpack_from() - struct.unpack_from('I', bytearray((1, 0, 0, 0)), 0) - except Exception: - if sys.version_info[0] < 3: - # monkeypatch for old Python versions - log.warning('patching struct.unpack_from()') - - def wrapped(fmt, buf, offset=0): - return struct._u_f_orig(fmt, str(buf), offset) - - struct._u_f_orig = struct.unpack_from - struct.unpack_from = wrapped - else: - raise diff --git a/pyroute2/ndb/main.py b/pyroute2/ndb/main.py index c738ac0c4..3d26ea84f 100644 --- a/pyroute2/ndb/main.py +++ b/pyroute2/ndb/main.py @@ -273,7 +273,9 @@ import ctypes.util import logging import logging.handlers +import queue import threading +from urllib.parse import urlparse from pyroute2.common import basestring @@ -287,16 +289,6 @@ from .transaction import Transaction from .view import SourcesView, View -try: - from urlparse import urlparse -except ImportError: - from urllib.parse import urlparse - -try: - import queue -except ImportError: - import Queue as queue - log = logging.getLogger(__name__) diff --git a/pyroute2/ndb/schema.py b/pyroute2/ndb/schema.py index 4a6692af2..3e7852467 100644 --- a/pyroute2/ndb/schema.py +++ b/pyroute2/ndb/schema.py @@ -551,7 +551,7 @@ def fetch(self, *argv, **kwarg): @publish def backup(self, spec): - if sys.version_info >= (3, 7) and self.provider == DBProvider.sqlite3: + if self.provider == DBProvider.sqlite3: backup_connection = sqlite3.connect(spec) self.connection.backup(backup_connection) backup_connection.close() diff --git a/pyroute2/netlink/__init__.py b/pyroute2/netlink/__init__.py index f70d8d38e..2266445d1 100644 --- a/pyroute2/netlink/__init__.py +++ b/pyroute2/netlink/__init__.py @@ -503,8 +503,7 @@ class NotInitialized(Exception): ## # That's a hack for the code linter, which works under # Python3, see unicode reference in the code below -if sys.version[0] == '3': - unicode = str +unicode = str NLMSG_MIN_TYPE = 0x10 @@ -2336,11 +2335,10 @@ def encode(self): def decode(self): nla_base_string.decode(self) self.value = self['value'] - if sys.version_info[0] >= 3: - try: - self.value = self.value.decode('utf-8') - except UnicodeDecodeError: - pass # Failed to decode, keep undecoded value + try: + self.value = self.value.decode('utf-8') + except UnicodeDecodeError: + pass # Failed to decode, keep undecoded value class asciiz(string): ''' diff --git a/setup.cfg b/setup.cfg index cd613d24a..f508ff8b9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -31,7 +31,6 @@ classifiers = [options] install_requires = win_inet_pton ; platform_system == "Windows" - importlib-metadata ; python_version < "3.8" packages_dir = =pyroute2 packages = find: diff --git a/setup.minimal.cfg b/setup.minimal.cfg index 23cc1ed7a..12dc2ad10 100644 --- a/setup.minimal.cfg +++ b/setup.minimal.cfg @@ -31,7 +31,6 @@ classifiers = [options] install_requires = win_inet_pton ; platform_system == "Windows" - importlib-metadata ; python_version < "3.8" packages_dir = =pyroute2 packages = diff --git a/tests/README.md b/tests/README.md index bf73b5cde..f888c2118 100644 --- a/tests/README.md +++ b/tests/README.md @@ -18,7 +18,7 @@ Requirements ============ * nox -* python >= 3.8 +* python >= 3.9 * `-r requirements.dev.txt` Run tests diff --git a/tests/test_linux/pr2test/context_manager.py b/tests/test_linux/pr2test/context_manager.py index 5e4784e5e..c3b0fb300 100644 --- a/tests/test_linux/pr2test/context_manager.py +++ b/tests/test_linux/pr2test/context_manager.py @@ -4,7 +4,6 @@ import itertools import logging import os -import sys import uuid from collections import namedtuple from socket import AF_INET, AF_INET6 @@ -313,7 +312,7 @@ def teardown(self): 2. remove the registered interfaces, ignore not existing ''' # save postmortem DB for SQLite3 - if self.db_provider == 'sqlite3' and sys.version_info >= (3, 7): + if self.db_provider == 'sqlite3': self.ndb.backup(f'{self.spec.uid}-post.db') self.ndb.close() self.ipr.close() diff --git a/tests/test_linux/test_dquot/test_dquot.py b/tests/test_linux/test_dquot/test_dquot.py index fd9749b89..f88aec7b1 100644 --- a/tests/test_linux/test_dquot/test_dquot.py +++ b/tests/test_linux/test_dquot/test_dquot.py @@ -1,20 +1,10 @@ import os import subprocess -import sys import pytest -from pr2test.marks import require_root from pyroute2 import DQuotSocket -pytestmark = [ - pytest.mark.skipif( - sys.version_info < (3, 7), - reason='the test module requires Python > 3.6', - ), - require_root(), -] - class DQuotContextManager: def __init__(self): diff --git a/tests/test_linux/test_ndb/test_backup.py b/tests/test_linux/test_ndb/test_backup.py index b2be41380..25595c95c 100644 --- a/tests/test_linux/test_ndb/test_backup.py +++ b/tests/test_linux/test_ndb/test_backup.py @@ -1,14 +1,7 @@ import sqlite3 -import sys import uuid -import pytest - -@pytest.mark.skipif( - sys.version_info < (3, 7), - reason='SQLite3 backup not supported on this Python version', -) def test_file_backup(context): filename = str(uuid.uuid4()) + '-backup.db' context.ndb.backup(filename) diff --git a/tests/test_repo/test_noxfile.py b/tests/test_repo/test_noxfile.py index a25d6d445..604407694 100644 --- a/tests/test_repo/test_noxfile.py +++ b/tests/test_repo/test_noxfile.py @@ -2,7 +2,6 @@ import collections import inspect import os -import sys import nox import pytest @@ -24,9 +23,6 @@ def session(request): ) -@pytest.mark.skipif( - sys.version_info < (3, 8), reason='unsupported Python version' -) @pytest.mark.parametrize('session', nox_sessions, indirect=True) def test_options_call(session): # walk the AST tree @@ -53,9 +49,6 @@ def test_session_parameters(session): assert args == ['session'] -@pytest.mark.skipif( - sys.version_info < (3, 8), reason='unsupported Python version' -) @pytest.mark.parametrize('session', nox_sessions, indirect=True) def test_requirements_files(session): for node in ast.walk(ast.parse(inspect.getsource(session.src_func))): diff --git a/tests/utils.py b/tests/utils.py index ca67a7ff2..77e2ac0f0 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -5,7 +5,6 @@ import re import stat import subprocess -import sys import uuid from socket import AF_INET, AF_INET6 @@ -105,11 +104,6 @@ def require_kernel(major, minor=None): pytest.skip('incompatible kernel version') -def require_python(target): - if sys.version_info[0] != target: - pytest.skip('test requires Python %i' % target) - - def require_8021q(): try: os.stat('/proc/net/vlan/config')