Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Python < 3.8 (3.2 to 3.7) obsolete code and comments #4096

Merged
merged 12 commits into from
Jan 24, 2024
7 changes: 0 additions & 7 deletions _distutils_hack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
import os


is_pypy = '__pypy__' in sys.builtin_module_names


def warn_distutils_present():
if 'distutils' not in sys.modules:
return
if is_pypy and sys.version_info < (3, 7):
# PyPy for 3.6 unconditionally imports distutils, so bypass the warning
# https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250
return
import warnings

warnings.warn(
Expand Down
5 changes: 0 additions & 5 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ def pytest_configure(config):
]


if sys.version_info < (3, 6):
collect_ignore.append('docs/conf.py') # uses f-strings
collect_ignore.append('pavement.py')


if sys.version_info < (3, 9) or sys.platform == 'cygwin':
collect_ignore.append('tools/finalize.py')

Expand Down
8 changes: 4 additions & 4 deletions docs/userguide/declarative_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ boilerplate code in some cases.
zip_safe = False
include_package_data = True
packages = find:
python_requires = >=3.7
python_requires = >=3.8
install_requires =
requests
importlib-metadata; python_version<"3.8"
importlib-metadata; python_version<"3.10"

[options.package_data]
* = *.txt, *.rst
Expand Down Expand Up @@ -271,11 +271,11 @@ data_files section 40.6.0 [#

[options]
install_requires =
importlib-metadata; python_version<"3.8"
abravalheri marked this conversation as resolved.
Show resolved Hide resolved
importlib-metadata; python_version<"3.10"

[options.extras_require]
all =
importlib-metadata; python_version < "3.8"
importlib-metadata; python_version<"3.10"

.. [#opt-3] The ``find:`` and ``find_namespace:`` directive can be further configured
in a dedicated subsection ``options.packages.find``. This subsection accepts the
Expand Down
7 changes: 4 additions & 3 deletions docs/userguide/entry_point.rst
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,10 @@ entry points remains the same as for console/GUI scripts, and is discussed in th
.. tip::
The recommended approach for loading and importing entry points is the
:mod:`importlib.metadata` module,
which is a part of the standard library since Python 3.8. For older versions of
Python, its backport :pypi:`importlib_metadata` should be used. While using the
backport, the only change that has to be made is to replace ``importlib.metadata``
which is a part of the standard library since Python 3.8 and is non-provisional
Copy link
Contributor Author

@Avasam Avasam Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue/reason to use importlib_metadata for Python older than 3.10 is python/importlib_metadata#396 ? Is that something we want to mention?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other reasons too - basically for early adoption of any behavior that was added to importlib_metadata but isn't yet in the supported Pythons. Still, that seems like something for cpython/importlib_metadata to advertise.

since Python 3.10. For older versions of Python, its backport
:pypi:`importlib_metadata` should be used. While using the backport, the only
change that has to be made is to replace ``importlib.metadata``
with ``importlib_metadata``, i.e.

.. code-block:: python
Expand Down
4 changes: 2 additions & 2 deletions docs/userguide/pyproject_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The ``project`` table contains metadata fields as described by the
]
description = "My package description"
readme = "README.rst"
requires-python = ">=3.7"
requires-python = ">=3.8"
keywords = ["one", "two"]
license = {text = "BSD-3-Clause"}
classifiers = [
Expand All @@ -56,7 +56,7 @@ The ``project`` table contains metadata fields as described by the
]
dependencies = [
"requests",
'importlib-metadata; python_version<"3.8"',
abravalheri marked this conversation as resolved.
Show resolved Hide resolved
'importlib-metadata; python_version<"3.10"',
]
dynamic = ["version"]

Expand Down
7 changes: 4 additions & 3 deletions docs/userguide/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ The following example demonstrates a minimum configuration
version = "0.0.1"
dependencies = [
"requests",
'importlib-metadata; python_version<"3.8"',
abravalheri marked this conversation as resolved.
Show resolved Hide resolved
'importlib-metadata; python_version<"3.10"',
]

See :doc:`/userguide/pyproject_config` for more information.
Expand All @@ -101,7 +101,8 @@ The following example demonstrates a minimum configuration
[options]
install_requires =
requests
importlib-metadata; python_version < "3.8"
importlib-metadata; python_version<"3.10"


See :doc:`/userguide/declarative_config` for more information.

Expand All @@ -116,7 +117,7 @@ The following example demonstrates a minimum configuration
version='0.0.1',
install_requires=[
'requests',
'importlib-metadata; python_version == "3.8"',
abravalheri marked this conversation as resolved.
Show resolved Hide resolved
'importlib-metadata; python_version<"3.10"',
],
)

Expand Down
1 change: 1 addition & 0 deletions newsfragments/4096.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated documentation referencing obsolete Python 3.7 code. -- by :user:`Avasam`
1 change: 1 addition & 0 deletions newsfragments/4096.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated and removed obsolete Python < 3.8 code and comments. -- by :user:`Avasam`
18 changes: 5 additions & 13 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"""

import sys

if sys.version_info < (3, 8):
raise RuntimeError("Python 3.8 or later is required")

import os
import io
import time
Expand All @@ -43,16 +47,7 @@
import importlib
from pkgutil import get_importer

try:
import _imp
except ImportError:
# Python 3.2 compatibility
import imp as _imp

try:
FileExistsError
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileExistsError added in Python 3.3

except NameError:
FileExistsError = OSError
import _imp

# capture these to bypass sandboxing
from os import utime
Expand Down Expand Up @@ -91,9 +86,6 @@
__import__('pkg_resources.extern.packaging.markers')
__import__('pkg_resources.extern.packaging.utils')

if sys.version_info < (3, 5):
raise RuntimeError("Python 3.5 or later is required")

# declare some globals that will be defined later to
# satisfy the linters.
require = None
Expand Down
17 changes: 1 addition & 16 deletions pkg_resources/tests/test_pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import zipfile
import datetime
import time
import subprocess
import stat
import distutils.dist
Expand All @@ -22,17 +21,6 @@
import pkg_resources


def timestamp(dt):
"""
Return a timestamp for a local, naive datetime instance.
"""
try:
return dt.timestamp()
except AttributeError:
# Python 3.2 and earlier
return time.mktime(dt.timetuple())


class EggRemover(str):
def __call__(self):
if self in sys.path:
Expand Down Expand Up @@ -123,7 +111,7 @@ def test_resource_filename_rewrites_on_change(self):
f = open(filename, 'w')
f.write('hello, world?')
f.close()
ts = timestamp(self.ref_time)
ts = self.ref_time.timestamp()
os.utime(filename, (ts, ts))
filename = zp.get_resource_filename(manager, 'data.dat')
with open(filename) as f:
Expand Down Expand Up @@ -241,9 +229,6 @@ def make_distribution_no_version(tmpdir, basename):
# will detect it and yield it.
dist_dir.join('temp.txt').ensure()

if sys.version_info < (3, 6):
dist_dir = str(dist_dir)

dists = list(pkg_resources.distributions_from_metadata(dist_dir))
assert len(dists) == 1
(dist,) = dists
Expand Down
2 changes: 1 addition & 1 deletion pkg_resources/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)


# from Python 3.6 docs.
# from Python 3.6 docs. Available from itertools on Python 3.10
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
Expand Down
5 changes: 1 addition & 4 deletions setuptools/command/bdist_egg.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,7 @@ def scan_module(egg_dir, base, name, stubs):
return True # Extension module
pkg = base[len(egg_dir) + 1 :].replace(os.sep, '.')
module = pkg + (pkg and '.' or '') + os.path.splitext(name)[0]
if sys.version_info < (3, 7):
skip = 12 # skip magic & date & file size
else:
skip = 16 # skip magic & reserved? & date & file size
skip = 16 # skip magic & reserved? & date & file size
f = open(filename, 'rb')
f.read(skip)
code = marshal.load(f)
Expand Down
11 changes: 1 addition & 10 deletions setuptools/command/build.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import sys
from typing import TYPE_CHECKING, List, Dict
from typing import Dict, List, Protocol
from distutils.command.build import build as _build

from ..warnings import SetuptoolsDeprecationWarning

if sys.version_info >= (3, 8):
from typing import Protocol
elif TYPE_CHECKING:
from typing_extensions import Protocol
else:
from abc import ABC as Protocol


_ORIGINAL_SUBCOMMANDS = {"build_py", "build_clib", "build_ext", "build_scripts"}


Expand Down
9 changes: 1 addition & 8 deletions setuptools/command/dist_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import os
import shutil
import sys
from contextlib import contextmanager
from distutils import log
from distutils.core import Command
Expand Down Expand Up @@ -77,7 +76,7 @@ def _maybe_bkp_dir(self, dir_path: str, requires_bkp: bool):
if requires_bkp:
bkp_name = f"{dir_path}.__bkp__"
_rm(bkp_name, ignore_errors=True)
_copy(dir_path, bkp_name, dirs_exist_ok=True, symlinks=True)
shutil.copytree(dir_path, bkp_name, dirs_exist_ok=True, symlinks=True)
try:
yield
finally:
Expand All @@ -103,9 +102,3 @@ def run(self):
def _rm(dir_name, **opts):
if os.path.isdir(dir_name):
shutil.rmtree(dir_name, **opts)


def _copy(src, dst, **opts):
if sys.version_info < (3, 8):
opts.pop("dirs_exist_ok", None)
shutil.copytree(src, dst, **opts)
3 changes: 2 additions & 1 deletion setuptools/command/easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ def finalize_options(self): # noqa: C901 # is too complex (25) # FIXME
'py_version_nodot': f'{sys.version_info.major}{sys.version_info.minor}',
'sys_prefix': self.config_vars['prefix'],
'sys_exec_prefix': self.config_vars['exec_prefix'],
# Only python 3.2+ has abiflags
# Only POSIX systems have abiflags
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'abiflags': getattr(sys, 'abiflags', ''),
# Only python 3.9+ has platlibdir
Copy link
Contributor Author

@Avasam Avasam Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'platlibdir': getattr(sys, 'platlibdir', 'lib'),
}
)
Expand Down
8 changes: 1 addition & 7 deletions setuptools/command/editable_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
List,
Mapping,
Optional,
Protocol,
Tuple,
TypeVar,
Union,
Expand All @@ -54,13 +55,6 @@
if TYPE_CHECKING:
from wheel.wheelfile import WheelFile # noqa

if sys.version_info >= (3, 8):
from typing import Protocol
elif TYPE_CHECKING:
from typing_extensions import Protocol
else:
from abc import ABC as Protocol

_Path = Union[str, Path]
_P = TypeVar("_P", bound=_Path)
_logger = logging.getLogger(__name__)
Expand Down
9 changes: 0 additions & 9 deletions setuptools/command/sdist.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from distutils import log
import distutils.command.sdist as orig
import os
import sys
import contextlib
from itertools import chain

Expand Down Expand Up @@ -72,14 +71,6 @@ def run(self):
def initialize_options(self):
orig.sdist.initialize_options(self)

self._default_to_gztar()

def _default_to_gztar(self):
# only needed on Python prior to 3.6.
if sys.version_info >= (3, 6, 0, 'beta', 1):
return
self.formats = ['gztar']

def make_distribution(self):
"""
Workaround for #516
Expand Down
18 changes: 1 addition & 17 deletions setuptools/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,6 @@ def patch_all():
# we can't patch distutils.cmd, alas
distutils.core.Command = setuptools.Command

has_issue_12885 = sys.version_info <= (3, 5, 3)

if has_issue_12885:
# fix findall bug in distutils (https://bugs.python.org/issue12885)
distutils.filelist.findall = setuptools.findall

needs_warehouse = (3, 4) < sys.version_info < (3, 4, 6) or (
3,
5,
) < sys.version_info <= (3, 5, 3)

if needs_warehouse:
warehouse = 'https://upload.pypi.org/legacy/'
distutils.config.PyPIRCCommand.DEFAULT_REPOSITORY = warehouse

_patch_distribution_metadata()

# Install Distribution throughout the distutils
Expand Down Expand Up @@ -138,8 +123,7 @@ def patch_for_msvc_specialized_compiler():
Patch functions in distutils to use standalone Microsoft Visual C++
compilers.
"""
# import late to avoid circular imports on Python < 3.5
msvc = import_module('setuptools.msvc')
from . import msvc
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically a revert of b7b9cb2
Unless this still happens in newer versions, in which case the comment should be updated instead.


if platform.system() != 'Windows':
# Compilers only available on Microsoft Windows
Expand Down
7 changes: 3 additions & 4 deletions setuptools/namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ def _get_target(self):

_nspkg_tmpl = (
"import sys, types, os",
"has_mfs = sys.version_info > (3, 5)",
"p = os.path.join(%(root)s, *%(pth)r)",
"importlib = has_mfs and __import__('importlib.util')",
"has_mfs and __import__('importlib.machinery')",
"importlib = __import__('importlib.util')",
"__import__('importlib.machinery')",
(
"m = has_mfs and "
"m = "
"sys.modules.setdefault(%(pkg)r, "
"importlib.util.module_from_spec("
"importlib.machinery.PathFinder.find_spec(%(pkg)r, "
Expand Down