From 4f6d09de198b1f9d512e6cf61e0541c73064983d Mon Sep 17 00:00:00 2001 From: jihtsan <15151999611@163.com> Date: Sun, 25 Oct 2026 15:57:28 +0800 Subject: [PATCH 1/2] Require Python 3.10 and remove Python 2 compatibility for #436 --- CHANGES | 5 ++++ Makefile | 13 ++------- README.rst | 4 ++- nodeenv.py | 67 +++++++++---------------------------------- requirements-dev.txt | 1 - setup.py | 14 ++++----- tests/nodeenv_test.py | 11 ++++--- 7 files changed, 34 insertions(+), 81 deletions(-) diff --git a/CHANGES b/CHANGES index 59b4974..56e84d4 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,11 @@ Nodeenv changelog Version [unreleased] -------------------- +- Require Python 3.10 or later, matching the minimum version tested in CI. + Update package classifiers and wheel tags, and remove the unused Python 2 + compatibility paths. Old Node.js source builds still require Python 2.7 + as their build interpreter + `#436 `_ - `--source` builds of node.js 13 and later no longer need a `python2` command; 8.8 to 12.x still need one but point `python` at it on their own, so the `python` to python2 link in `PATH` is only set up for older diff --git a/Makefile b/Makefile index 91dfb71..6e25805 100644 --- a/Makefile +++ b/Makefile @@ -77,22 +77,13 @@ test3: clean clean-test-env setup-test-env # https://github.com/ekalinin/nodeenv/issues/43 test4: clean clean-test-env @echo " =" - @echo " = test4: system nodejs's for python3.9" + @echo " = test4: system nodejs's for python3.10" @echo " =" - @virtualenv --python=python3.9 ${TEST_ENV} && \ + @virtualenv --python=python3.10 ${TEST_ENV} && \ . ${TEST_ENV}/bin/activate && \ ${SETUP} && \ nodeenv -p --node=system -test5: clean clean-test-env - @echo " =" - @echo " = test5: prebuilt nodejs's env for python2" - @echo " =" - @virtualenv --python=python2.7 ${TEST_ENV} && \ - . ${TEST_ENV}/bin/activate && \ - ${SETUP} && \ - nodeenv -p --prebuilt - test7: clean clean-test-env setup-test-env @echo " =" @echo " = test7: freeze for global installation" diff --git a/README.rst b/README.rst index 9a7c3de..6b3897f 100644 --- a/README.rst +++ b/README.rst @@ -107,7 +107,7 @@ Dependency For nodeenv ^^^^^^^^^^^ -* Python 3 (CI tests CPython 3.10 through 3.14) +* Python 3.10 or later (CI tests CPython 3.10 through 3.14) * make * tail @@ -115,6 +115,8 @@ For node.js ^^^^^^^^^^^ * libssl-dev +* Python 2.7 when building Node.js versions older than 13 from source; this + is separate from the Python 3.10+ interpreter that runs nodeenv. Usage ----- diff --git a/nodeenv.py b/nodeenv.py index b33829a..7177ae1 100644 --- a/nodeenv.py +++ b/nodeenv.py @@ -25,10 +25,7 @@ import subprocess import tarfile import time -if sys.version_info < (3, 3): - from pipes import quote as _quote -else: - from shlex import quote as _quote +from shlex import quote as _quote import platform import zipfile import shutil @@ -36,23 +33,10 @@ import sysconfig import glob -try: # pragma: no cover (py2 only) - from ConfigParser import SafeConfigParser as ConfigParser # pyright: ignore[reportMissingImports] # noqa: E501 - # noinspection PyCompatibility - import urllib2 # pyright: ignore[reportMissingImports] - from urlparse import urlsplit, urlunsplit # pyright: ignore[reportMissingImports] # noqa: E501 - from urllib import unquote # pyright: ignore[reportAttributeAccessIssue] - iteritems = operator.methodcaller('iteritems') - import httplib # pyright: ignore[reportMissingImports] - IncompleteRead = httplib.IncompleteRead -except ImportError: # pragma: no cover (py3 only) - from configparser import ConfigParser - # noinspection PyUnresolvedReferences - import urllib.request as urllib2 - from urllib.parse import unquote, urlsplit, urlunsplit - iteritems = operator.methodcaller('items') - import http - IncompleteRead = http.client.IncompleteRead +from configparser import ConfigParser +from http.client import IncompleteRead +import urllib.request as urllib2 +from urllib.parse import unquote, urlsplit, urlunsplit nodeenv_version = '1.11.0' @@ -60,7 +44,6 @@ abspath = os.path.abspath src_base_url = None -is_PY3 = sys.version_info[0] >= 3 is_WIN = platform.system() == 'Windows' is_CYGWIN = platform.system().startswith(('CYGWIN', 'MSYS')) @@ -79,27 +62,6 @@ # Utils -# https://github.com/jhermann/waif/blob/master/python/to_uft8.py -def to_utf8(text): - """Convert given text to UTF-8 encoding (as far as possible).""" - if not text or is_PY3: - return text - - try: # unicode or pure ascii - return text.encode("utf8") - except UnicodeDecodeError: - try: # successful UTF-8 decode means it's pretty sure UTF-8 - text.decode("utf8") - return text - except UnicodeDecodeError: - try: # get desperate; and yes, this has a western hemisphere bias - return text.decode("cp1252").encode("utf8") - except UnicodeDecodeError: - pass - - return text # return unchanged, hope for the best - - class Config(object): """ Configuration namespace. @@ -140,7 +102,7 @@ def _load(cls, configfiles, verbose=False): if not ini_file.has_section(section): continue - for attr, val in iteritems(vars(cls)): + for attr, val in vars(cls).items(): if attr.startswith('_') or not \ ini_file.has_option(section, attr): continue @@ -169,12 +131,12 @@ def _dump(cls): """ print(" [nodeenv]") print(" " + "\n ".join( - "%s = %s" % (k, v) for k, v in sorted(iteritems(vars(cls))) + "%s = %s" % (k, v) for k, v in sorted(vars(cls).items()) if not k.startswith('_'))) Config._default = dict( - (attr, val) for attr, val in iteritems(vars(Config)) + (attr, val) for attr, val in vars(Config).items() if not attr.startswith('_') ) @@ -428,7 +390,7 @@ def create_logger(): def emit(self, record): msg = self.format(record) fs = "%s" if getattr(record, "continued", False) else "%s\n" - self.stream.write(fs % to_utf8(msg)) + self.stream.write(fs % msg) self.flush() logging.StreamHandler.emit = emit @@ -690,8 +652,7 @@ def writefile(dest, content, overwrite=True, append=False): """ Create file and write content in it """ - content = to_utf8(content) - if is_PY3 and not isinstance(content, bytes): + if not isinstance(content, bytes): content = bytes(content, 'utf-8') if not os.path.exists(dest): logger.debug(' * Writing %s ... ', dest, extra=dict(continued=True)) @@ -1052,7 +1013,7 @@ def copy_node_from_prebuilt(env_dir, src_dir, node_version): else: dest = env_dir - src_folder_tpl = src_dir + to_utf8('/node-v%s*' % node_version) + src_folder_tpl = src_dir + '/node-v%s*' % node_version src_folder, = glob.glob(src_folder_tpl) copytree(src_folder, dest, True) @@ -1164,7 +1125,7 @@ def report_node_download_error(errors, args): def install_node_wrapped(env_dir, src_dir, args): env_dir = abspath(env_dir) - node_src_dir = join(src_dir, to_utf8('node-v%s' % args.node)) + node_src_dir = join(src_dir, 'node-v%s' % args.node) src_type = "prebuilt" if args.prebuilt else "source" logger.info(' * Install %s node (%s) ' % (src_type, args.node), @@ -1454,7 +1415,7 @@ def create_environment(env_dir, args): logger.info(' * Environment already exists: %s', env_dir) if not args.force: sys.exit(2) - src_dir = to_utf8(abspath(join(env_dir, 'src'))) + src_dir = abspath(join(env_dir, 'src')) mkdir(src_dir) if args.node == "system": @@ -1602,7 +1563,7 @@ def get_env_dir(args): sys.exit(2) else: res = args.env_dir - return to_utf8(res) + return res # noinspection PyProtectedMember diff --git a/requirements-dev.txt b/requirements-dev.txt index aa38656..ac4466c 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,7 +2,6 @@ coverage flake8 -mock; python_version < '3.3' pytest setuptools tox diff --git a/setup.py b/setup.py index 0ad5e07..c7bb38e 100644 --- a/setup.py +++ b/setup.py @@ -38,9 +38,7 @@ def read_file(file_name): author_email='e.v.kalinin@gmail.com', install_requires=[], extras_require={'certifi': ['certifi']}, - python_requires=( - ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*" - ), + python_requires='>=3.10', description="Node.js virtual environment builder", long_description=ldesc, py_modules=['nodeenv'], @@ -54,16 +52,14 @@ def read_file(file_name): 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', - '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 :: 3.12', + 'Programming Language :: Python :: 3.13', + 'Programming Language :: Python :: 3.14', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: Software Development :: Libraries :: Python Modules' ], - options={'bdist_wheel': {'universal': True}}, ) diff --git a/tests/nodeenv_test.py b/tests/nodeenv_test.py index 0badd52..aa6aaf6 100644 --- a/tests/nodeenv_test.py +++ b/tests/nodeenv_test.py @@ -2431,16 +2431,15 @@ def test_without_python_virtualenv(self): result = nodeenv.get_env_dir(args) assert result == '/path/to/node/env' - def test_returns_utf8_encoded_string(self): - """Test that get_env_dir returns UTF-8 encoded string""" + def test_preserves_unicode_path(self): + """Environment paths remain text, including non-ASCII characters.""" args = mock.Mock() args.python_virtualenv = False - args.env_dir = '/path/to/env' + args.env_dir = '/path/to/öäü 环境' result = nodeenv.get_env_dir(args) - # The to_utf8 function is applied, - # but in Python 3 it returns the same string - assert result == '/path/to/env' + assert result == args.env_dir + assert isinstance(result, str) class TestInstallNpm: From 1d958d97d1f108891325e444107d56bf92fc5544 Mon Sep 17 00:00:00 2001 From: jihtsan <15151999611@163.com> Date: Mon, 26 Oct 2026 03:32:31 +0800 Subject: [PATCH 2/2] Address Python support review feedback for #436 --- Makefile | 4 ++-- README.rst | 5 +++-- setup.py | 1 - tests/nodeenv_test.py | 14 ++------------ tests/test_activate_shells.py | 5 +---- tests/test_install_activate.py | 5 +---- 6 files changed, 9 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 6e25805..5e5740b 100644 --- a/Makefile +++ b/Makefile @@ -77,9 +77,9 @@ test3: clean clean-test-env setup-test-env # https://github.com/ekalinin/nodeenv/issues/43 test4: clean clean-test-env @echo " =" - @echo " = test4: system nodejs's for python3.10" + @echo " = test4: system nodejs's for python3" @echo " =" - @virtualenv --python=python3.10 ${TEST_ENV} && \ + @virtualenv --python=python3 ${TEST_ENV} && \ . ${TEST_ENV}/bin/activate && \ ${SETUP} && \ nodeenv -p --node=system diff --git a/README.rst b/README.rst index 6b3897f..ce89ecd 100644 --- a/README.rst +++ b/README.rst @@ -115,8 +115,9 @@ For node.js ^^^^^^^^^^^ * libssl-dev -* Python 2.7 when building Node.js versions older than 13 from source; this - is separate from the Python 3.10+ interpreter that runs nodeenv. +* Python 2.7 as a ``python2`` command when building Node.js versions older + than 13 from source; this is separate from the Python 3.10+ interpreter + that runs nodeenv. Usage ----- diff --git a/setup.py b/setup.py index c7bb38e..8b5cc0f 100644 --- a/setup.py +++ b/setup.py @@ -59,7 +59,6 @@ def read_file(file_name): 'Programming Language :: Python :: 3.13', 'Programming Language :: Python :: 3.14', 'Programming Language :: Python :: Implementation :: CPython', - 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: Software Development :: Libraries :: Python Modules' ], ) diff --git a/tests/nodeenv_test.py b/tests/nodeenv_test.py index aa6aaf6..f06ead0 100644 --- a/tests/nodeenv_test.py +++ b/tests/nodeenv_test.py @@ -1,11 +1,4 @@ -from __future__ import absolute_import -from __future__ import unicode_literals - -import sys -if sys.version_info < (3, 3): - from pipes import quote as _quote -else: - from shlex import quote as _quote +from shlex import quote as _quote import contextlib import http.server import io @@ -22,10 +15,7 @@ import threading import zipfile -try: - from unittest import mock -except ImportError: - import mock +from unittest import mock import pytest import nodeenv diff --git a/tests/test_activate_shells.py b/tests/test_activate_shells.py index 9e83447..b6a60ef 100644 --- a/tests/test_activate_shells.py +++ b/tests/test_activate_shells.py @@ -10,10 +10,7 @@ import subprocess import sys -try: - from unittest import mock -except ImportError: - import mock # pyright: ignore[reportMissingModuleSource] +from unittest import mock import pytest import nodeenv diff --git a/tests/test_install_activate.py b/tests/test_install_activate.py index 8595941..e83aae5 100644 --- a/tests/test_install_activate.py +++ b/tests/test_install_activate.py @@ -2,10 +2,7 @@ import os import subprocess -try: - from unittest import mock -except ImportError: - import mock # pyright: ignore[reportMissingModuleSource] +from unittest import mock import pytest import nodeenv