Skip to content

Commit add2da3

Browse files
committed
Python3 support
Fixes #1
1 parent 0a6ece7 commit add2da3

File tree

7 files changed

+62
-106
lines changed

7 files changed

+62
-106
lines changed

Makefile

+6-10
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,25 @@ EMFLAGS=\
2727
$$([ $(EMPYOPT) = 1 ] && echo $(EMOPT) || echo $(EMDEBUG))
2828

2929
EMEXPORTS=\
30-
-s EXPORTED_FUNCTIONS="['_Py_Initialize', '_PyRun_SimpleString']"
30+
-s EXPORTED_FUNCTIONS="['_Py_Initialize', '_PyRun_SimpleString']" -s "EXTRA_EXPORTED_RUNTIME_METHODS=['cwrap']"
3131

32-
lp.js: libpython.a
33-
python mapfiles.py python/Lib datafilezip > js/postJs.js
32+
empython.js: python/libpython3.5.a
33+
python2 mapfiles.py python/Lib datafilezip > js/postJs.js
3434
cat js/postJs.js.in >> js/postJs.js
3535
emcc $(EMFLAGS) $(EMEXPORTS) -o $@ $< python/Modules/zlib/libz.a
3636

37-
CONFFLAGS=OPT="$(COPT)" --without-threads --without-pymalloc --disable-shared --without-signal-module --disable-ipv6
37+
CONFFLAGS=OPT="$(COPT)" --without-threads --without-pymalloc --disable-shared --disable-ipv6
3838
prep:
39-
#sudo apt-get install gcc-multilib
4039
./configure
41-
make Parser/pgen python
42-
#cp Makefile ../Makefile.native
43-
#cp Parser/pgen ../pgen.native
40+
make python
4441
cp python ../python.native
4542
make clean
4643
git clean -f -x -d
4744
em:
48-
cd Modules/zlib && emconfigure ./configure && sed -i 's/^AR=.*/\0 rc/' Makefile && emmake make
45+
cd Modules/zlib && emconfigure ./configure --static && emmake make libz.a
4946
(export BASECFLAGS=-m32 LDFLAGS=-m32 && emconfigure ./configure $(CONFFLAGS))
5047
git apply ../hacks.patch
5148
emmake make || true # errors on running python
5249
mv python python.bc # only useful if replacing the emscripten test .bc file
5350
cp ../python.native python && chmod +x python
54-
#cp ../pgen.native Parser/pgen && chmod +x Parser/pgen
5551
emmake make

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ $ cd python
44
$ make -f ../Makefile prep
55
$ make -f ../Makefile em
66
$ cd ..
7-
$ ln -sf python/libpython2.7.a libpython.a
8-
$ make lp.js
7+
$ make empython.js
98
```

hacks.patch

+28-69
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,33 @@
1-
diff --git a/pyconfig.h.orig b/pyconfig.h
2-
--- a/pyconfig.h
3-
+++ b/pyconfig.h
4-
@@ -269,7 +269,7 @@
5-
6-
/* Define if we can use gcc inline assembler to get and set x87 control word
7-
*/
8-
-#define HAVE_GCC_ASM_FOR_X87 1
9-
+#undef HAVE_GCC_ASM_FOR_X87
10-
11-
/* Define if you have the getaddrinfo function. */
12-
#define HAVE_GETADDRINFO 1
13-
diff --git a/setup.py b/setup.py
14-
--- a/setup.py
15-
+++ b/setup.py
16-
@@ -18,6 +18,7 @@ from distutils.command.install_lib import install_lib
17-
from distutils.spawn import find_executable
18-
19-
cross_compiling = "_PYTHON_HOST_PLATFORM" in os.environ
20-
+cross_compiling = True
21-
22-
def get_platform():
23-
# cross build
24-
@@ -442,7 +443,9 @@ class PyBuildExt(build_ext):
25-
add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
26-
if cross_compiling:
27-
self.add_gcc_paths()
28-
- self.add_multiarch_paths()
29-
+ #self.add_multiarch_paths()
30-
+ add_dir_to_list(self.compiler.library_dirs, './Modules/zlib')
31-
+ add_dir_to_list(self.compiler.include_dirs, './Modules/zlib')
32-
33-
# Add paths specified in the environment variables LDFLAGS and
34-
# CPPFLAGS for header and library files.
35-
diff --git a/Modules/Setup b/Modules/Setup
36-
--- a/Modules/Setup
37-
+++ b/Modules/Setup
38-
@@ -171,14 +171,14 @@ GLHACK=-Dclear=__GLclear
39-
#array arraymodule.c # array objects
40-
#cmath cmathmodule.c _math.c # -lm # complex math library functions
41-
#math mathmodule.c _math.c # -lm # math library functions, e.g. sin()
42-
-#_struct _struct.c # binary structure packing/unpacking
43-
+_struct _struct.c # binary structure packing/unpacking
44-
#time timemodule.c # -lm # time operations and variables
45-
-#operator operator.c # operator.add() and similar goodies
46-
+operator operator.c # operator.add() and similar goodies
47-
#_testcapi _testcapimodule.c # Python C API test module
48-
#_random _randommodule.c # Random number generator
49-
-#_collections _collectionsmodule.c # Container types
50-
-#_heapq _heapqmodule.c # Heapq type
51-
-#itertools itertoolsmodule.c # Functions creating iterators for efficient looping
52-
+_collections _collectionsmodule.c # Container types
53-
+_heapq _heapqmodule.c # Heapq type
54-
+itertools itertoolsmodule.c # Functions creating iterators for efficient looping
55-
#strop stropmodule.c # String manipulations
56-
#_functools _functoolsmodule.c # Tools for working with functions and callable objects
57-
#_elementtree -I$(srcdir)/Modules/expat -DHAVE_EXPAT_CONFIG_H -DUSE_PYEXPAT_CAPI _elementtree.c # elementtree accelerator
58-
@@ -434,7 +434,7 @@ itertools itertoolsmodule.c # Functions creating iterators for efficient looping
59-
60-
61-
# Helper module for various ascii-encoders
62-
-#binascii binascii.c
63-
+binascii binascii.c
64-
65-
# Fred Drake's interface to the Python parser
66-
#parser parsermodule.c
67-
@@ -465,6 +465,7 @@ GLHACK=-Dclear=__GLclear
1+
--- pyconfig.h.orig 2020-10-24 22:13:50.745086908 +0100
2+
+++ pyconfig.h 2020-10-24 23:04:46.647877114 +0100
3+
@@ -979,7 +979,7 @@
4+
#define HAVE_SYS_FILE_H 1
5+
6+
/* Define to 1 if you have the <sys/ioctl.h> header file. */
7+
-#define HAVE_SYS_IOCTL_H 1
8+
+//#define HAVE_SYS_IOCTL_H 1
9+
10+
/* Define to 1 if you have the <sys/kern_control.h> header file. */
11+
/* #undef HAVE_SYS_KERN_CONTROL_H */
12+
--- Modules/Setup.orig 2020-10-24 23:04:17.224003619 +0100
13+
+++ Modules/Setup 2020-10-24 23:04:24.079974258 +0100
14+
@@ -358,7 +358,7 @@
15+
# Andrew Kuchling's zlib module.
6816
# This require zlib 1.1.3 (or later).
6917
# See http://www.gzip.org/zlib/
70-
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
71-
+zlib zlibmodule.c -I./Modules/zlib -L./Modules/zlib -lz
18+
-#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
19+
+zlib zlibmodule.c -IModules/zlib -LModules/zlib -lz
7220

7321
# Interface to the Expat XML parser
7422
#
23+
--- setup.py
24+
+++ setup.py
25+
@@ -16,7 +16,7 @@ from distutils.command.install_lib import install_lib
26+
from distutils.command.build_scripts import build_scripts
27+
from distutils.spawn import find_executable
28+
29+
-cross_compiling = "_PYTHON_HOST_PLATFORM" in os.environ
30+
+cross_compiling = True
31+
32+
# Add special CFLAGS reserved for building the interpreter and the stdlib
33+
# modules (Issue #21121).

js/postJs.js.in

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
Module.FS = FS;
22
Module.ENV = ENV;
3-
root['Module'] = Module;
4-
}
3+
Module.onRuntimeInitialized = onRuntimeInitialized;
4+
return Module;
5+
})()
56
};
67

7-
root.empython();
88
// Undo pollution of window
99
delete window.Module;
1010

1111
// Init emscripten stuff
1212
root.Module.run();
1313

14-
root.Initialize = root.Module.cwrap('Py_Initialize', 'number', []);
15-
root.Run = root.Module.cwrap('PyRun_SimpleString', 'number', [
16-
'string' // string to eval
17-
]);
18-
19-
return root;
20-
21-
})();
14+
function onRuntimeInitialized() {
15+
root.Initialize = root.Module.cwrap('Py_Initialize', 'number', []);
16+
root.Run = root.Module.cwrap('PyRun_SimpleString', 'number', [
17+
'string' // string to eval
18+
]);
19+
root.Initialize();
20+
// empython comes out from this callback
21+
initialized_callback(root);
22+
}
23+
};

js/preJs.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
this.empython = (function () {
1+
this.init_empython = function (initialized_callback, print_hook) {
22

33
var root = {
4-
empython: function () {
4+
Module: (function () {
55
var Module = {
66
noInitialRun: true,
77
noExitRuntime: true,
88
preRun: [],
9-
postRun: []
9+
postRun: [],
10+
print: print_hook,
11+
printErr: print_hook,
1012
};

mapfiles.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def mk_contents(data):
2020
) % (base64.b64encode(data), len(data), len(data))
2121

2222
def files_to_datafilecalls(fpaths):
23-
basedir = '/usr/local/lib/python2.7'
23+
basedir = '/usr/local/lib/python3.5'
2424
commands = []
2525
dpaths = set([basedir])
2626
for fpath, targetdir in fpaths:
@@ -38,9 +38,7 @@ def files_to_datafilecalls(fpaths):
3838

3939
dpaths.remove(basedir)
4040
for dpath in sorted(dpaths, key=len, reverse=True):
41-
commands.insert(0, 'FS.createFolder("%s", "%s", true, true)' % (
42-
os.path.dirname(dpath), os.path.basename(dpath)
43-
))
41+
commands.insert(0, 'FS.mkdirTree("%s")' % (dpath,))
4442
commands.insert(0, 'FS.createPath("/", "' + basedir[1:] + '", true, true)')
4543

4644
return commands
@@ -52,7 +50,7 @@ def files_to_datafilezipcall(fpaths):
5250
zipfile.write(fpath, os.path.join(targetdir, os.path.basename(fpath)))
5351
zipfile.close()
5452

55-
target = '/usr/local/lib/python27.zip'
53+
target = '/usr/local/lib/python35.zip'
5654
commands = []
5755
commands.insert(0, 'FS.createPath("/", "' + os.path.dirname(target)[1:] + '", true, true)')
5856
commands.append('FS.createDataFile("%s", "%s", %s, true, true)' % (
@@ -89,17 +87,17 @@ def main(root):
8987
fpaths.append((os.path.join(dirpath, filename), dirpath))
9088

9189
# _sysconfigdata is created by the build process
92-
fpaths.append(('../build/lib.linux-x86_64-2.7/_sysconfigdata.py', '.'))
90+
fpaths.append(('../build/lib.linux-x86_64-3.5/_sysconfigdata.py', '.'))
9391

9492
# Some checks and assertions
9593
assert all([targetdir[0] == '.' for fpath, targetdir in fpaths])
9694
fpaths = [
9795
(fpath, targetdir) for fpath, targetdir in fpaths
9896
if os.path.splitext(fpath)[1] == '.py'
9997
]
100-
# Compile to save space and time in the parser
101-
check_call(['python', '-OO', '-m', 'py_compile'] + [fpath for fpath, _ in fpaths])
102-
fpaths = [(fpath + 'o', targetdir) for fpath, targetdir in fpaths]
98+
## Compile to save space and time in the parser
99+
#check_call(['python3', '-OO', '-m', 'py_compile'] + [fpath for fpath, _ in fpaths])
100+
#fpaths = [(fpath + 'o', targetdir) for fpath, targetdir in fpaths]
103101

104102
if sys.argv[2] == 'datafiles':
105103
commands = files_to_datafilecalls(fpaths)
@@ -109,7 +107,7 @@ def main(root):
109107
assert False
110108

111109
# Start out in a writeable folder.
112-
commands.append('FS.createFolder(".", "sandbox", true, true)')
110+
commands.append('FS.mkdirTree("/sandbox")')
113111
commands.append('FS.currentPath = "/sandbox"')
114112

115113
# http://bugs.python.org/issue22689

python

0 commit comments

Comments
 (0)