Java源码示例:org.python.core.PyCode

示例1
public void interpreterExecCode(File compiledScript) {
  String scriptFile = compiledScript.getAbsolutePath();
  byte[] data = new byte[0];
  try {
    data = FileUtils.readFileToByteArray(compiledScript);
  } catch (IOException e) {
    log(-1, "exec compiled script: %s", e.getMessage());
  }
  PyCode pyCode = BytecodeLoader.makeCode(FilenameUtils.getBaseName(scriptFile), data, scriptFile);
  interpreter.exec(pyCode);
}
 
示例2
/** Parse and compile script file
 *
 *  @param path Path to add to search path, or <code>null</code>
 *  @param name Name of script (file name, URL)
 *  @param stream Stream for the script content
 *  @return {@link Script}
 *  @throws Exception on error
 */
public Script compile(final String path, final String name, final InputStream stream) throws Exception
{
    if (path != null)
        addToPythonPath(path);
    final long start = System.currentTimeMillis();
    final PyCode code = python.compile(new InputStreamReader(stream), name);
    final long end = System.currentTimeMillis();
    logger.log(Level.FINE, "Time to compile {0}: {1} ms", new Object[] { name, (end - start) });
    return new JythonScript(this, name, code);
}
 
示例3
/** Parse and compile script file
 *
 *  @param support {@link JythonScriptSupport} that will execute this script
 *  @param name Name of script (file name, URL)
 *  @param code Compiled code
 */
public JythonScript(final JythonScriptSupport support, final String name, final PyCode code)
{
    this.support = support;
    this.name = name;
    this.code = code;
}
 
示例4
private void testInterpreter(final PythonInterpreter python) throws Exception
{
    // Run on new threads to get fresh thread-locals
    final ExecutorService new_executor = Executors.newCachedThreadPool();

    final String script = "import sys\nresult = sys.path";
    final Callable<String> test_run = () ->
    {
        // System.out.println("Executing on " + Thread.currentThread().getName());
        final PyCode code = python.compile(script);
        python.exec(code);
        return python.get("result").toString();
    };

    final List<Future<String>> futures = new ArrayList<>();
    final long end = System.currentTimeMillis() + 1000L;
    while (System.currentTimeMillis() < end)
    {
        for (int i=0; i<50; ++i)
            futures.add(new_executor.submit(test_run));
        for (Future<String> future : futures)
        {
            final String result = future.get();
            //System.out.println(result);
            assertThat(result, containsString("always"));
            assertThat(result, containsString("special"));
        }
        futures.clear();
    }
    new_executor.shutdown();
}
 
示例5
/** @return Compiled code */
public PyCode getCode()
{
    return code;
}
 
示例6
public static void exec(byte[] compiledCode, PythonInterpreter interpreter) throws Exception{
        PyCode pyCode = BytecodeLoader.makeCode(NAME, compiledCode, null);
        interpreter.exec(pyCode);
}