lint cleanups; drop 3.6 from CI and metadata. (#32)
diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 47a8fff..0874933 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml
@@ -18,7 +18,7 @@ strategy: fail-fast: false matrix: - python-version: [3.6, 3.7, 3.8, 3.9, '3.10', '3.11.0-beta - 3.11'] + python-version: [3.7, 3.8, 3.9, '3.10', '3.11.0-beta - 3.11'] steps: - uses: actions/checkout@v3 @@ -42,7 +42,7 @@ strategy: fail-fast: false matrix: - python-version: [3.6, 3.7, 3.8, 3.9, '3.10', '3.11.0-beta - 3.11'] + python-version: [3.7, 3.8, 3.9, '3.10', '3.11.0-beta - 3.11'] steps: - uses: actions/checkout@v3
diff --git a/ChangeLog.md b/ChangeLog.md index c02f5bf..d2e71b7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md
@@ -1,8 +1,16 @@ +## 1.6.x + +* Some pylint based refactorings to portpicker and portpicker\_test. +* Drop 3.6 from our CI test matrix and metadata. It probably still works + there, but expect our unittests to include 3.7-ism's in the future. We'll + *attempt* to avoid modern constructs in portpicker.py itself but zero + guarantees. Using an old Python? Use an old portpicker. + ## 1.5.2 * Do not re-pick a known used (not-yet-returned) port when running stand alone without a portserver. - + ## 1.5.1 * When not using a portserver *(you really should)*, try the `bind(0)`
diff --git a/pyproject.toml b/pyproject.toml index 9f27e60..932b7de 100644 --- a/pyproject.toml +++ b/pyproject.toml
@@ -5,7 +5,7 @@ [tool.tox] legacy_tox_ini = """ [tox] -envlist = py{36,37,38,39} +envlist = py{37,38,39,310,311} isolated_build = true skip_missing_interpreters = true # minimum tox version @@ -17,5 +17,5 @@ commands = check-manifest --ignore 'src/tests/**' python -c 'from setuptools import setup; setup()' check -m -s - py.test -v -s {posargs} + py.test -vv -s {posargs} """
diff --git a/setup.cfg b/setup.cfg index d3ad524..1a5071e 100644 --- a/setup.cfg +++ b/setup.cfg
@@ -1,7 +1,7 @@ # https://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files [metadata] name = portpicker -version = 1.5.2 +version = 1.6.0b1 maintainer = Google LLC maintainer_email = greg@krypto.org license = Apache 2.0 @@ -22,11 +22,11 @@ Intended Audience :: Developers Programming Language :: Python Programming Language :: Python :: 3 - Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.10 + Programming Language :: Python :: 3.11 Programming Language :: Python :: Implementation :: CPython Programming Language :: Python :: Implementation :: PyPy platforms = POSIX, Windows
diff --git a/src/portpicker.py b/src/portpicker.py index 4b514d7..1d678cb 100644 --- a/src/portpicker.py +++ b/src/portpicker.py
@@ -35,6 +35,9 @@ test_port = portpicker.pick_unused_port() """ +# pylint: disable=consider-using-f-string +# Some people still use this on old Pythons despite our test matrix and +# supported versions. Be kind for now, until it gets in our way. from __future__ import print_function import logging @@ -43,10 +46,12 @@ import socket import sys +_winapi = None # pylint: disable=invalid-name if sys.platform == 'win32': - import _winapi -else: - _winapi = None + try: + import _winapi + except ImportError: + _winapi = None # The legacy Bind, IsPortFree, etc. names are not exported. __all__ = ('bind', 'is_port_free', 'pick_unused_port', 'return_port',
diff --git a/src/tests/portpicker_test.py b/src/tests/portpicker_test.py index c2925db..033e50d 100644 --- a/src/tests/portpicker_test.py +++ b/src/tests/portpicker_test.py
@@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # # Copyright 2007 Google Inc. All Rights Reserved. # @@ -14,29 +14,20 @@ # See the License for the specific language governing permissions and # limitations under the License. # -"""Unittests for the portpicker module.""" +"""Unittests for portpicker.""" -from __future__ import print_function +# pylint: disable=invalid-name,protected-access,missing-class-docstring,missing-function-docstring + +from contextlib import ExitStack import errno import os -import random import socket import sys import unittest -from contextlib import ExitStack - -if sys.platform == 'win32': - import _winapi -else: - _winapi = None - -try: - # pylint: disable=no-name-in-module - from unittest import mock # Python >= 3.3. -except ImportError: - import mock # https://pypi.python.org/pypi/mock +from unittest import mock import portpicker +_winapi = portpicker._winapi class PickUnusedPortTest(unittest.TestCase): @@ -47,6 +38,7 @@ return self._bind(port, socket.SOCK_DGRAM, socket.IPPROTO_UDP) def setUp(self): + super().setUp() # So we can Bind even if portpicker.bind is stubbed out. self._bind = portpicker.bind portpicker._owned_ports.clear() @@ -93,7 +85,7 @@ self.assertTrue(self.IsUnusedTCPPort(port)) self.assertTrue(self.IsUnusedUDPPort(port)) finally: - os.environ['PORTSERVER_ADDRESS'] = addr + os.environ['PORTSERVER_ADDRESS'] = addr @unittest.skipIf('PORTSERVER_ADDRESS' not in os.environ, 'no port server to test against') @@ -253,12 +245,11 @@ # Only successfully return a port if an OS-assigned port is # requested, or if we're checking that the last OS-assigned port # is unused on the other protocol. - if port == 0 or port == self.last_assigned_port: + if port in (0, self.last_assigned_port): self.last_assigned_port = self._bind(port, socket_type, socket_proto) return self.last_assigned_port - else: - return None + return None with mock.patch.object(portpicker, 'bind', error_for_explicit_ports): # Without server, this can be little flaky, so check that it @@ -295,7 +286,7 @@ # Now test the second part, the fallback from above, which asks the # OS for a port. - def mock_port_free(port): + def mock_port_free(unused_port): return False with mock.patch.object(portpicker, 'is_port_free', mock_port_free):