Skip to content

Commit

Permalink
Fix ability to run compiled scripts in python 3.
Browse files Browse the repository at this point in the history
Fixes #77
  • Loading branch information
bsteffensmeier committed May 19, 2017
1 parent 2e51066 commit 96445a5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/jep/pyembed.c
Expand Up @@ -1351,6 +1351,11 @@ static void pyembed_run_pyc(JepThread *jepThread,
return;
}
(void) PyMarshal_ReadLongFromFile(fp);
#if PY_MAJOR_VERSION >= 3
// Python 3.3 added an extra long containing the size of the source.
// https://github.com/python/cpython/commit/5136ac0ca21a05691978df8d0650f902c8ca3463
(void) PyMarshal_ReadLongFromFile(fp);
#endif
v = (PyObject *) (intptr_t) PyMarshal_ReadLastObjectFromFile(fp);
if (v == NULL || !PyCode_Check(v)) {
Py_XDECREF(v);
Expand Down
34 changes: 34 additions & 0 deletions src/jep/test/TestCompiledScript.java
@@ -0,0 +1,34 @@
package jep.test;

import java.lang.IllegalStateException;
import java.lang.Boolean;

import jep.Jep;
import jep.JepConfig;
import jep.JepException;

/**
* Tests that a compiled script can be loaded by Jep.runscript()
*
* Created: May 2017
*
* @author Ben Steffensmeier
*/
public class TestCompiledScript {

public static void main(String[] args) throws JepException{
JepConfig config = new JepConfig();
config.addIncludePaths(".");
Jep jep = new Jep(config);
jep.eval("import py_compile");
jep.eval("py_compile.compile(file='build/testScript.py', cfile='build/testScript.pyc')");
jep.eval(null);
jep.runScript("build/testScript.pyc");
Object result = jep.getValue("isGood()");
jep.close();
if(!Boolean.TRUE.equals(result)){
throw new IllegalStateException("isGood() returned " + result);
}
}

}
26 changes: 26 additions & 0 deletions tests/test_run_script.py
@@ -0,0 +1,26 @@
import unittest
import os
import os.path

from tests.jep_pipe import jep_pipe
from tests.jep_pipe import build_java_process_cmd


class TestRunScript(unittest.TestCase):

def setUp(self):
with open('build/testScript.py', 'w') as testScript:
testScript.write("def isGood():\n")
testScript.write(" return True\n")


def test_compiledScript(self):
jep_pipe(build_java_process_cmd('jep.test.TestCompiledScript'))


def tearDown(self):
if os.path.exists('build/testScript.py'):
os.remove('build/testScript.py')
if os.path.exists('build/testScript.pyc'):
os.remove('build/testScript.pyc')

0 comments on commit 96445a5

Please sign in to comment.