From 3c32200da4edb894bf97ff93686fb881ce7c3cc3 Mon Sep 17 00:00:00 2001 From: Yoshiyuki Mineo Date: Mon, 4 Jul 2016 16:36:35 +0900 Subject: [PATCH] Support Python3 --- MANIFEST.in | 1 + build.sh | 15 ++++++++++++++- python/_v8eval.py | 13 ++++++++++--- python/bin/python | 2 ++ python/example/js_add.py | 2 +- setup.py | 1 + 6 files changed, 29 insertions(+), 5 deletions(-) create mode 100755 python/bin/python diff --git a/MANIFEST.in b/MANIFEST.in index 9d37a1a..24bdd49 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,5 @@ include build.sh +include python/bin/python include CMakeLists.txt include LICENSE include README.md diff --git a/build.sh b/build.sh index a55ea12..95a5e4c 100755 --- a/build.sh +++ b/build.sh @@ -3,7 +3,7 @@ V8EVAL_ROOT=`cd $(dirname $0) && pwd` PLATFORM=`uname` -if [ $PLATFORM = "Linux" ] ; then +if [ $PLATFORM = "Linux" ]; then NUM_CPU_CORES=`cat /proc/cpuinfo | grep cores | grep -o '[0-9]\+' | awk '{total=total+$1}; END{print total}'` export CC=$V8EVAL_ROOT/v8/third_party/llvm-build/Release+Asserts/bin/clang @@ -50,11 +50,24 @@ install_v8() { return 0 fi + PY_VER=`python -c 'import sys; print(sys.version_info[0])'` + if [ $PY_VER = 3 ]; then + OLD_PATH=$PATH + export PATH=$V8EVAL_ROOT/python/bin:$PATH + fi + cd $V8EVAL_ROOT fetch v8 cd v8 git checkout 5.2.163 + if [ $PY_VER = 3 ]; then + sed -i -e 's/python -c/python2 -c/' Makefile + fi CFLAGS="-fPIC -Wno-unknown-warning-option" CXXFLAGS="-fPIC -Wno-unknown-warning-option" make x64.release -j$NUM_CPU_CORES V=1 + + if [ $PY_VER = 3 ]; then + export PATH=$OLD_PATH + fi } install_libuv() { diff --git a/python/_v8eval.py b/python/_v8eval.py index 568d491..81d172c 100644 --- a/python/_v8eval.py +++ b/python/_v8eval.py @@ -1,5 +1,12 @@ # the following is appended to swig-generated file including _PythonV8 -import json +import json, sys + + +PY3 = sys.version_info[0] == 3 +if PY3: + string_type = str +else: + string_type = basestring class V8Error(Exception): @@ -35,7 +42,7 @@ def eval(self, src): V8Error: If some JavaScript exception happens. """ - if not isinstance(src, basestring): + if not isinstance(src, string_type): raise TypeError('source code not string') res = self._v8.eval(src) @@ -64,7 +71,7 @@ def call(self, func, args): V8Error: If some JavaScript exception happens. """ - if not isinstance(func, basestring): + if not isinstance(func, string_type): raise TypeError('function name not string') if not isinstance(args, list): raise TypeError('arguments not list') diff --git a/python/bin/python b/python/bin/python new file mode 100755 index 0000000..3dc7974 --- /dev/null +++ b/python/bin/python @@ -0,0 +1,2 @@ +#!/bin/sh +exec python2 $@ diff --git a/python/example/js_add.py b/python/example/js_add.py index 8d43caf..26974f2 100644 --- a/python/example/js_add.py +++ b/python/example/js_add.py @@ -5,4 +5,4 @@ def add(x, y): v8.eval('var add = (x, y) => x + y;') return v8.call('add', [x, y]) -print add(1, 2) +print(add(1, 2)) diff --git a/setup.py b/setup.py index 9c27a26..7e0713e 100755 --- a/setup.py +++ b/setup.py @@ -84,6 +84,7 @@ package_dir={'': 'python/v8eval'}, classifiers=["License :: OSI Approved :: MIT License", "Programming Language :: Python :: 2.7", + "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: Implementation :: CPython", "Operating System :: POSIX :: Linux",