Skip to content

Commit

Permalink
Strict check for Python version on source installs.
Browse files Browse the repository at this point in the history
Include helpful error message and ability to opt out.

Fixes #278.
  • Loading branch information
dhermes committed Nov 29, 2021
1 parent 210dfea commit b29ec7a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
7 changes: 7 additions & 0 deletions DEVELOPMENT.rst
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,13 @@ This project uses environment variables for building the
current build is intended for a wheel. On Windows, this will involve
renaming ``bezier.dll`` to a unique name (to avoid name collision) and
updating ``_speedup*.pyd`` to refer to the new name.
- ``BEZIER_IGNORE_VERSION_CHECK ``: Will instruct ``pip`` and ``setup.py`` to
ignore a check on the current version of Python. By default, Python installs
of ``bezier`` will explicitly check for supported versions and this opts
out of that check (e.g. if a new version of Python was just released).
This will only be relevant when installing from source, but a new version of
Python will also mean the existing wheels on PyPI won't support that new
version.
- ``BEZIER_DLL_HASH``: If set, this will indicate to ``setup.py`` that the
built ``bezier.dll`` should be renamed to ``bezier-${BEZIER_DLL_HASH}.dll``
in situations such as tests where this filename should be deterministic.
Expand Down
7 changes: 7 additions & 0 deletions DEVELOPMENT.rst.template
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,13 @@ This project uses environment variables for building the
current build is intended for a wheel. On Windows, this will involve
renaming ``bezier.dll`` to a unique name (to avoid name collision) and
updating ``_speedup*.pyd`` to refer to the new name.
- ``BEZIER_IGNORE_VERSION_CHECK ``: Will instruct ``pip`` and ``setup.py`` to
ignore a check on the current version of Python. By default, Python installs
of ``bezier`` will explicitly check for supported versions and this opts
out of that check (e.g. if a new version of Python was just released).
This will only be relevant when installing from source, but a new version of
Python will also mean the existing wheels on PyPI won't support that new
version.
- ``BEZIER_DLL_HASH``: If set, this will indicate to ``setup.py`` that the
built ``bezier.dll`` should be renamed to ``bezier-${{BEZIER_DLL_HASH}}.dll``
in situations such as tests where this filename should be deterministic.
Expand Down
45 changes: 42 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@
""".format(
NO_EXTENSION_ENV
)
IGNORE_VERSION_CHECK_ENV = "BEZIER_IGNORE_VERSION_CHECK"
INVALID_VERSION_MESSAGE = """\
The current Python version ({major}.{minor}) is not supported.
The supported versions are: {versions}
Using `bezier` on an unsupported version of Python is not known to work. You
may be seeing this message as part of a source distribution (`sdist`) install
because no wheels exist on PyPI for your current Python environment. The
Python environment is uniquely identified by Python version, operating
system (ABI) and architecture (platform). You are likely seeing this message
because a new version of Python has been released. To disable this check, set
the `BEZIER_IGNORE_VERSION_CHECK` environment variable.
"""
READTHEDOCS_ENV = "READTHEDOCS"
ON_READTHEDOCS_MESSAGE = """\
The {} environment variable has been detected, the binary extension module
Expand Down Expand Up @@ -89,7 +103,7 @@ def is_installed(requirement):

def numpy_include_dir():
if not is_installed("numpy >= 1.9.0"):
print(NUMPY_MESSAGE, file=sys.stderr)
print(NUMPY_MESSAGE, file=sys.stderr, end="")
sys.exit(1)

import numpy as np
Expand Down Expand Up @@ -117,11 +131,11 @@ def _sha256_short_hash(filename):

def extension_modules():
if os.environ.get(READTHEDOCS_ENV) == "True":
print(ON_READTHEDOCS_MESSAGE, file=sys.stderr)
print(ON_READTHEDOCS_MESSAGE, file=sys.stderr, end="")
return []

if NO_EXTENSION_ENV in os.environ:
print(NO_SPEEDUPS_MESSAGE, file=sys.stderr)
print(NO_SPEEDUPS_MESSAGE, file=sys.stderr, end="")
return []

install_prefix = os.environ.get(INSTALL_PREFIX_ENV)
Expand Down Expand Up @@ -269,6 +283,30 @@ def setup():
)


def _check_python_version():
"""Check that this is being installed in a valid version of Python.
If the current version of Python is unsupported, this will exit with an
error.
The ``BEZIER_IGNORE_VERSION_CHECK`` environment variable can be set to
opt out of this check.
"""
if IGNORE_VERSION_CHECK_ENV in os.environ:
return

major = sys.version_info.major
minor = sys.version_info.minor
if (major, minor) in ((3, 8), (3, 9), (3, 10)):
return

message = INVALID_VERSION_MESSAGE.format(
major=major, minor=minor, versions="3.8, 3.9 and 3.10"
)
print(message, file=sys.stderr, end="")
sys.exit(1)


def _patch_setuptools():
"""Patch ``setuptools`` to address known issues.
Expand All @@ -286,6 +324,7 @@ def _patch_setuptools():


def main():
_check_python_version()
_patch_setuptools()
setup()

Expand Down

0 comments on commit b29ec7a

Please sign in to comment.