Skip to content

Commit

Permalink
Add more advanced round-trip test target that runs decompiled output …
Browse files Browse the repository at this point in the history
…back

through the originating Python compiler (Linux only for now)
  • Loading branch information
zrax committed Aug 31, 2016
1 parent 4b55e0b commit def5d90
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ if(POLICY CMP0037)
# Don't complain about adding a target named "test"
cmake_policy(SET CMP0037 OLD)
endif()

add_custom_target(test "${CMAKE_CURRENT_SOURCE_DIR}/pycdc_test.sh"
"${CMAKE_CURRENT_SOURCE_DIR}/tests"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
add_dependencies(test pycdc)

add_custom_target(rt_test "${CMAKE_CURRENT_SOURCE_DIR}/pycdc_rt_test.sh"
"${CMAKE_CURRENT_BINARY_DIR}/tests"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
add_dependencies(rt_test test)
116 changes: 116 additions & 0 deletions pycdc_rt_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/bash

testdir="$1"
srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
startdir="$(pwd)"

if [[ -z "$testdir" ]]; then
echo "Missing required parameter: testdir" >&2
exit 1
fi

py_versions=('1.5.2' '2.2.3' '2.5.6' '2.6.9' '2.7.12' '3.1.5' '3.4.5' '3.5.2')
py_url="http://www.python.org/ftp/python"

# Make subcommand errors fatal
set -e

fetch_python() {
local url="$1"
local tarball="${url##*/}"
local version="$2"

if [[ ! -d "Python-${version}" ]]; then
echo "Downloading Python ${version}"
curl -LO# "$url"
tar xzf "$tarball"
if [[ -f "${srcdir}/tests/python-builds/Python-${version}.patch" ]]; then
cd "Python-${version}"
patch -p1 < "${srcdir}/tests/python-builds/Python-${version}.patch"
cd ..
fi
fi
}

cd "$startdir"
mkdir -p python-builds
cd python-builds

# Special case for Python 1.5
fetch_python "${py_url}/src/py152.tgz" "1.5.2"

for pv in ${py_versions[@]:1}; do
fetch_python "${py_url}/${pv}/Python-${pv}.tgz" "$pv"
done

for pv in ${py_versions[@]}; do
if [[ ! -x "Python-${pv}/python" ]]; then
echo -n "Building Python ${pv}... "
cd "Python-${pv}"
rm -f "../Python-${pv}.conf.log"
rm -f "../Python-${pv}.build.log"
if ! ./configure > "../Python-${pv}.conf.log" 2>&1 ; then
echo "Configure failed!"
echo "See python-builds/Python-${pv}.conf.log for details"
exit 1
fi
if ! make > "../Python-${pv}.build.log" 2>&1 ; then
echo "Build failed!"
echo "See python-builds/Python-${pv}.build.log for details"
exit 1
fi
cd ..
echo "Success!"
fi
done

# Run any .pyc.src files in $testdir back through its respective python compiler
cd "$startdir"

fails=0
files=()
errors=()

set +e

for srcf in "$testdir"/*.pyc.src; do
# There's probably a better way...
srcver=$(grep '# File: .* (Python [0-9]\.[0-9]\+)$' "$srcf" | sed -e 's/.* (Python //' -e 's/)//')

_found=0
for version in ${py_versions[@]}; do
if [[ "${version%.*}" == "$srcver" ]]; then
_found=1
base="tests/$(basename "$srcf")"
python-builds/Python-${version}/python \
-c "import py_compile; py_compile.compile('$srcf')" 2>"$base.rterr"
if (( $? )) || [[ -s "$base.rterr" ]]; then
let fails+=1
files+=("$srcf")
errors+=("$(cat "$base.rterr")")
echo -ne "\033[31m.\033[m"
else
echo -ne "\033[32m.\033[m"
fi
fi
done

if (( _found == 0 )); then
let fails+=1
files+=("$srcf")
errors+=("No python compiler found for $srcf ($srcver)")
echo -ne "\033[31m.\033[m"
fi
done

if (( $fails == 0 ))
then
echo -e "\n\nAll tests passed!"
else
echo -e "\n\n$fails tests failed:"
for ((i=0; i<${#files[@]}; i++))
do
echo -e "\t\033[31m${files[i]}\033[m"
echo -e "${errors[i]}\n"
done
fi
7 changes: 7 additions & 0 deletions pycdc_test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/bin/bash

testdir="$1"

if [[ -z "$testdir" ]]
then
echo "Missing required parameter: testdir" >&2
exit 1
fi

mkdir -p tests

fails=0
Expand Down
40 changes: 40 additions & 0 deletions tests/python-builds/Python-1.5.2.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index b0eb332..e1aa559 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -587,7 +587,7 @@ file_readinto(f, args)
*/

static PyObject *
-getline(f, n)
+_py_getline(f, n)
PyFileObject *f;
int n;
{
@@ -709,7 +709,7 @@ PyFile_GetLine(f, n)
}
if (((PyFileObject*)f)->f_fp == NULL)
return err_closed();
- return getline((PyFileObject *)f, n);
+ return _py_getline((PyFileObject *)f, n);
}

/* Python method */
@@ -729,7 +729,7 @@ file_readline(f, args)
return PyString_FromString("");
if (n < 0)
n = 0;
- return getline(f, n);
+ return _py_getline(f, n);
}

static PyObject *
@@ -823,7 +823,7 @@ file_readlines(f, args)
goto error;
if (sizehint > 0) {
/* Need to complete the last line */
- PyObject *rest = getline(f, 0);
+ PyObject *rest = _py_getline(f, 0);
if (rest == NULL) {
Py_DECREF(line);
goto error;
13 changes: 13 additions & 0 deletions tests/python-builds/Python-2.5.6.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/Makefile.pre.in b/Makefile.pre.in
index 25a82af..9700faa 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -458,7 +458,7 @@ Modules/getbuildinfo.o: $(PARSER_OBJS) \
$(SIGNAL_OBJS) \
$(MODOBJS) \
$(srcdir)/Modules/getbuildinfo.c
- $(CC) -c $(PY_CFLAGS) -DSVNVERSION=\"`LC_ALL=C $(SVNVERSION)`\" -o $@ $(srcdir)/Modules/getbuildinfo.c
+ $(CC) -c $(PY_CFLAGS) -DSVNVERSION="\"`LC_ALL=C $(SVNVERSION)`\"" -o $@ $(srcdir)/Modules/getbuildinfo.c

Modules/getpath.o: $(srcdir)/Modules/getpath.c Makefile
$(CC) -c $(PY_CFLAGS) -DPYTHONPATH='"$(PYTHONPATH)"' \

0 comments on commit def5d90

Please sign in to comment.