Skip to content

Commit 5e66392

Browse files
Stop importing distutils, which Python 3.12 removed (#2112)
enable_wx() imported distutils.version.LooseVersion to check the wxPython version, so it failed on Python 3.12+ without setuptools. Use the existing pydev_ipython.version.check_version instead. The build scripts now take build and Extension from setuptools instead of distutils. Fixes #1659
1 parent 30e1d75 commit 5e66392

5 files changed

Lines changed: 46 additions & 7 deletions

File tree

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def finalize_options(self):
6161
try:
6262
build = cmds["build"]
6363
except KeyError:
64-
from distutils.command.build import build
64+
from setuptools.command.build import build
6565
original = build.finalize_options
6666
build.finalize_options = finalize_options
6767

‎src/debugpy/_vendored/pydevd/pydev_ipython/inputhook.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,9 @@ def enable_wx(self, app=None):
147147
app = wx.App(redirect=False, clearSigInt=False)
148148
"""
149149
import wx
150-
from distutils.version import LooseVersion as V
150+
from pydev_ipython.version import check_version
151151

152-
wx_version = V(wx.__version__).version # @UndefinedVariable
153-
154-
if wx_version < [2, 8]:
152+
if not check_version(wx.__version__, "2.8"): # @UndefinedVariable
155153
raise ValueError("requires wxPython >= 2.8, but you have %s" % wx.__version__) # @UndefinedVariable
156154

157155
from pydev_ipython.inputhookwx import inputhook_wx

‎src/debugpy/_vendored/pydevd/setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from setuptools import setup
2525
from setuptools.dist import Distribution
26-
from distutils.extension import Extension
26+
from setuptools import Extension
2727
import os
2828

2929

‎src/debugpy/_vendored/pydevd/setup_pydevd_cython.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def build_extension(dir_name, extension_name, target_pydevd_name, force_cython,
211211
stream.write(c_file_contents)
212212

213213
# Always compile the .c (and not the .pyx) file (which we should keep up-to-date by running build_tools/build.py).
214-
from distutils.extension import Extension
214+
from setuptools import Extension
215215

216216
extra_compile_args = []
217217
extra_link_args = []
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License. See LICENSE in the project root
3+
# for license information.
4+
5+
"""Tests for the wxPython event loop integration in pydevd's pydev_ipython.inputhook."""
6+
7+
import sys
8+
from types import SimpleNamespace
9+
10+
import pytest
11+
12+
from debugpy._vendored import vendored
13+
14+
with vendored("pydevd"):
15+
from pydev_ipython.inputhook import InputHookManager
16+
17+
18+
@pytest.fixture
19+
def fake_wx(monkeypatch):
20+
app = SimpleNamespace()
21+
wx = SimpleNamespace(GetApp=lambda: app)
22+
monkeypatch.setitem(sys.modules, "wx", wx)
23+
monkeypatch.setitem(
24+
sys.modules, "pydev_ipython.inputhookwx", SimpleNamespace(inputhook_wx=object())
25+
)
26+
# Python 3.12 removed distutils, so it cannot be imported unless setuptools provides it.
27+
monkeypatch.setitem(sys.modules, "distutils", None)
28+
return wx, app
29+
30+
31+
def test_enable_wx_without_distutils(fake_wx):
32+
wx, app = fake_wx
33+
wx.__version__ = "4.2.1"
34+
assert InputHookManager().enable_wx() is app
35+
36+
37+
def test_enable_wx_rejects_old_wxpython(fake_wx):
38+
wx, _ = fake_wx
39+
wx.__version__ = "2.6.4"
40+
with pytest.raises(ValueError, match="requires wxPython >= 2.8"):
41+
InputHookManager().enable_wx()

0 commit comments

Comments
 (0)