Skip to content

Commit

Permalink
Support Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiyukiMineo committed Jul 4, 2016
1 parent 4dbbc30 commit 3c32200
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
@@ -1,4 +1,5 @@
include build.sh
include python/bin/python
include CMakeLists.txt
include LICENSE
include README.md
Expand Down
15 changes: 14 additions & 1 deletion build.sh
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
13 changes: 10 additions & 3 deletions 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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down
2 changes: 2 additions & 0 deletions python/bin/python
@@ -0,0 +1,2 @@
#!/bin/sh
exec python2 $@
2 changes: 1 addition & 1 deletion python/example/js_add.py
Expand Up @@ -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))
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -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",
Expand Down

0 comments on commit 3c32200

Please sign in to comment.