Skip to content

Commit ec2b47e

Browse files
committed
Remove need for Python 2to3
All Python examples and tests have been written to be both Python 2 and Python 3 compatible, removing the need for 2to3 to run the examples or test-suite. The 2to3 executable is not always available and even when available does not always work, e.g. with pyenv. An alternative would be to use the lib2to3 Python module instead, but this isn't available in some older versions of Python 3. I had this problem on Ubuntu Bionic on Travis: checking Examples/python/callback pyenv: 2to3-3.8: command not found The `2to3-3.8' command exists in these Python versions: 3.8 3.8.1 Reference issues: pypa/virtualenv#1399 https://travis-ci.community/t/2to3-command-not-found-in-venv-in-bionic/4495
1 parent 89bee6a commit ec2b47e

File tree

5 files changed

+11
-118
lines changed

5 files changed

+11
-118
lines changed

CHANGES.current

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
77
Version 4.1.0 (in progress)
88
===========================
99

10+
2020-08-15: wsfulton
11+
[Python] All Python examples and tests are written to be Python 2 and Python 3
12+
compatible, removing the need for 2to3 to run the examples or test-suite.
13+
1014
2020-08-13: wsfulton
1115
[C#] Add support for void *VOID_INT_PTR for member variables.
1216

Examples/Makefile.in

+1-12
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,7 @@ python_static_cpp: $(SRCDIR_SRCS)
381381
# Running a Python example
382382
# -----------------------------------------------------------------
383383

384-
ifeq (,$(PY3))
385-
PYSCRIPT = $(RUNME).py
386-
else
387-
PYSCRIPT = $(RUNME)3.py
388-
endif
389-
390-
PY2TO3 = @PY2TO3@ `@PY2TO3@ -l | grep -v -E "Available|import$$" | awk '{print "-f "$$0}'`
384+
PYSCRIPT = $(RUNME).py
391385

392386
python_run: $(PYSCRIPT)
393387
ifneq (,$(PYCODESTYLE))
@@ -400,10 +394,6 @@ $(RUNME).py: $(SRCDIR)$(RUNME).py
400394
cp $< $@
401395
endif
402396

403-
$(RUNME)3.py: $(SRCDIR)$(RUNME).py
404-
cp $< $@
405-
$(PY2TO3) -w $@ >/dev/null 2>&1
406-
407397
# -----------------------------------------------------------------
408398
# Version display
409399
# -----------------------------------------------------------------
@@ -421,7 +411,6 @@ python_clean:
421411
rm -f core @EXTRA_CLEAN@
422412
rm -f *.@OBJEXT@ *@SO@ *$(PYTHON_SO)
423413
rm -f $(TARGET).py
424-
if test -f $(SRCDIR)$(RUNME).py; then rm -f $(RUNME)3.py $(RUNME)3.py.bak; fi
425414
case "x$(SRCDIR)" in x|x./);; *) rm -f $(RUNME).py;; esac
426415

427416

Examples/python/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ <h2>Compilation Issues</h2>
8989
<h2>Compatibility</h2>
9090

9191
For Python 3, set the environment variable <tt>PY3=1</tt>.
92-
This will ensure the 2to3 program is run prior to running any example.
9392

9493
<p>
9594
Your mileage may vary. If you experience a problem, please let us know by

Examples/test-suite/python/Makefile.in

+6-81
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,10 @@ endif
1010

1111
LANGUAGE = python
1212
PYTHON = $(PYBIN)
13+
SCRIPTSUFFIX = _runme.py
1314
PYCODESTYLE = @PYCODESTYLE@
1415
PYCODESTYLE_FLAGS = --ignore=E252,E30,E402,E501,E731,E741,W291,W391
1516

16-
#*_runme.py for Python 2.x, *_runme3.py for Python 3.x
17-
PY2SCRIPTSUFFIX = _runme.py
18-
PY3SCRIPTSUFFIX = _runme3.py
19-
PY2TO3 = @PY2TO3@ -x import
20-
21-
ifeq (,$(PY3))
22-
SCRIPTSUFFIX = $(PY2SCRIPTSUFFIX)
23-
else
24-
SCRIPTSUFFIX = $(PY3SCRIPTSUFFIX)
25-
endif
26-
2717
srcdir = @srcdir@
2818
top_srcdir = @top_srcdir@
2919
top_builddir = @top_builddir@
@@ -107,7 +97,6 @@ C_TEST_CASES += \
10797
include $(srcdir)/../common.mk
10898

10999
# Overridden variables here
110-
SCRIPTDIR = .
111100
LIBS = -L.
112101
VALGRIND_OPT += --suppressions=pythonswig.supp
113102

@@ -116,35 +105,25 @@ VALGRIND_OPT += --suppressions=pythonswig.supp
116105

117106
# Rules for the different types of tests
118107
%.cpptest:
119-
+$(convert_testcase)
120108
$(setup)
121109
+$(swig_and_compile_cpp)
122110
$(check_pep8)
123111
$(run_testcase)
124112

125113
%.ctest:
126-
+$(convert_testcase)
127114
$(setup)
128115
+$(swig_and_compile_c)
129116
$(check_pep8)
130117
$(run_testcase)
131118

132119
%.multicpptest:
133-
+$(convert_testcase)
134120
$(setup)
135121
+$(swig_and_compile_multi_cpp)
136122
$(check_pep8_multi_cpp)
137123
$(run_testcase)
138124

139125

140-
141-
# Runs the testcase. A testcase is only run if
142-
# a file is found which has _runme.py (or _runme3.py for Python 3) appended after the testcase name.
143-
144-
py_runme = $(SCRIPTPREFIX)$*$(SCRIPTSUFFIX)
145-
py2_runme = $(SCRIPTPREFIX)$*$(PY2SCRIPTSUFFIX)
146-
py3_runme = $(SCRIPTPREFIX)$*$(PY3SCRIPTSUFFIX)
147-
126+
# Python code style checking
148127
ifneq (,$(PYCODESTYLE))
149128
check_pep8 = $(COMPILETOOL) $(PYCODESTYLE) $(PYCODESTYLE_FLAGS) $(SCRIPTPREFIX)$*.py
150129

@@ -154,70 +133,16 @@ check_pep8_multi_cpp = \
154133
done
155134
endif
156135

157-
run_python = env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH PYTHONPATH=.:$(srcdir):$$PYTHONPATH $(RUNTOOL) $(PYTHON) $(py_runme)
158-
136+
# Runs the testcase. A testcase is only run if
137+
# a file is found which has _runme.py appended after the testcase name.
159138
run_testcase = \
160-
if [ -f $(SCRIPTDIR)/$(py_runme) ]; then \
161-
$(run_python);\
162-
fi
163-
164-
# Grab runme file ready for running: copied for out of source tree builds, and/or run 2to3
165-
# Note terminal (double colon) rules creating runme files to fix possible infinite recursion,
166-
# see https://github.com/swig/swig/pull/688
167-
ifeq ($(SCRIPTDIR),$(srcdir))
168-
# in source tree build
169-
ifeq (,$(PY3))
170-
convert_testcase =
171-
else
172-
convert_testcase = \
173-
if [ -f $(srcdir)/$(py2_runme) ]; then \
174-
$(MAKE) $(SCRIPTDIR)/$(py_runme); \
139+
if [ -f $(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ]; then \
140+
env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH PYTHONPATH=.:$(srcdir):$$PYTHONPATH $(RUNTOOL) $(PYTHON) $(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX); \
175141
fi
176142

177-
# For converting python 2 tests into Python 3 tests
178-
$(SCRIPTDIR)/$(SCRIPTPREFIX)%$(SCRIPTSUFFIX):: $(srcdir)/$(SCRIPTPREFIX)%$(PY2SCRIPTSUFFIX)
179-
cp $< $@
180-
$(PY2TO3) -w $@ >/dev/null 2>&1
181-
182-
endif
183-
else
184-
# out of source tree build
185-
ifeq (,$(PY3))
186-
convert_testcase = \
187-
if [ -f $(srcdir)/$(py2_runme) ]; then \
188-
$(MAKE) $(SCRIPTDIR)/$(py_runme); \
189-
fi
190-
191-
$(SCRIPTDIR)/$(SCRIPTPREFIX)%$(SCRIPTSUFFIX):: $(srcdir)/$(SCRIPTPREFIX)%$(PY2SCRIPTSUFFIX)
192-
cp $< $@
193-
194-
else
195-
convert_testcase = \
196-
if [ -f $(srcdir)/$(py2_runme) ]; then \
197-
$(MAKE) $(SCRIPTDIR)/$(py_runme); \
198-
elif [ -f $(srcdir)/$(py3_runme) ]; then \
199-
$(MAKE) $(SCRIPTDIR)/$(py3_runme); \
200-
fi
201-
202-
# For when there is a _runme3.py instead of a _runme.py, ie a Python 3 only run test
203-
$(SCRIPTDIR)/$(SCRIPTPREFIX)%$(SCRIPTSUFFIX):: $(srcdir)/$(SCRIPTPREFIX)%$(PY3SCRIPTSUFFIX)
204-
cp $< $@
205-
206-
# For converting python 2 tests into Python 3 tests
207-
$(SCRIPTDIR)/$(SCRIPTPREFIX)%$(SCRIPTSUFFIX):: $(srcdir)/$(SCRIPTPREFIX)%$(PY2SCRIPTSUFFIX)
208-
cp $< $@
209-
$(PY2TO3) -w $@ >/dev/null 2>&1
210-
211-
endif
212-
213-
endif
214-
215143
# Clean: remove the generated .py file
216-
# We only remove the _runme3.py if it is generated by 2to3 from a _runme.py.
217144
%.clean:
218145
@rm -f $*.py
219-
@if test -f $(srcdir)/$(py2_runme); then rm -f $(SCRIPTDIR)/$(py3_runme) $(SCRIPTDIR)/$(py3_runme).bak; fi
220-
@if test "x$(SCRIPTDIR)" != "x$(srcdir)"; then rm -f $(SCRIPTDIR)/$(py_runme); fi
221146

222147
clean:
223148
$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile SRCDIR='$(SRCDIR)' python_clean

configure.ac

-24
Original file line numberDiff line numberDiff line change
@@ -919,30 +919,6 @@ if test -n "$PYINCLUDE" || test -n "$PY3INCLUDE" ; then
919919
fi
920920
fi
921921

922-
AC_ARG_WITH(2to3, AS_HELP_STRING([--with-2to3=path], [Set location of Python 2to3 tool]), [PY2TO3BIN="$withval"], [PY2TO3BIN="yes"])
923-
if test -n "$PYTHON3"; then
924-
if test "x$PY2TO3BIN" = xyes; then
925-
py3to2=`echo $PYTHON3 | sed -e "s/python/2to3-/"`
926-
AC_CHECK_PROGS(PY2TO3, $py3to2 2to3)
927-
if test -z "$PY2TO3"; then
928-
# Windows distributions don't always have the 2to3 executable
929-
AC_MSG_CHECKING(for 2to3.py)
930-
py2to3script="$PY3PREFIX/Tools/scripts/2to3.py"
931-
if test -f "$py2to3script"; then
932-
AC_MSG_RESULT($py2to3script)
933-
PY2TO3="$PYTHON3 $py2to3script"
934-
else
935-
AC_MSG_RESULT(Not found)
936-
fi
937-
fi
938-
else
939-
PY2TO3="$PY2TO3BIN"
940-
fi
941-
if test -z "$PY2TO3"; then
942-
PYTHON3=
943-
fi
944-
fi
945-
946922
#----------------------------------------------------------------
947923
# Look for Perl5
948924
#----------------------------------------------------------------

0 commit comments

Comments
 (0)