1 package jep.test; 2 3 import jep.Interpreter; 4 import jep.JepConfig; 5 import jep.JepException; 6 import jep.MainInterpreter; 7 import jep.PyConfig; 8 import jep.SubInterpreter; 9 10 /** 11 * A test class for verifying that Jep can correctly configure the global Python 12 * interpreter with specific flags before actually starting the interpreter. 13 * This test exercises PyConfig options to match Python command line arguments 14 * and checks the output against sys.flags. 15 * 16 * TODO: If you wanted to be extra-thorough you could write tests that don't 17 * explicitly trust sys.flags, e.g. verify that when setting 18 * Py_DontWriteBytecodeFlag that a .pyc or .pyo is not actually written out. 19 * 20 * Created: June 2016 21 * 22 * @author Nate Jensen 23 * @since 3.6 24 * @see "https://github.com/ninia/jep/issues/49" 25 */ 26 public class TestPreInitVariables { 27 main(String[] args)28 public static void main(String[] args) throws JepException { 29 PyConfig pyConfig = new PyConfig(); 30 // pyConfig.setIgnoreEnvironmentFlag(1); 31 // TODO fix test so no site flag can be tested 32 // pyConfig.setNoSiteFlag(1); 33 pyConfig.setNoUserSiteDirectory(1); 34 // verbose prints out too much, when it's on, it's clear it's on 35 // pyConfig.setVerboseFlag(1); 36 pyConfig.setOptimizeFlag(1); 37 pyConfig.setDontWriteBytecodeFlag(1); 38 pyConfig.setHashRandomizationFlag(1); 39 MainInterpreter.setInitParams(pyConfig); 40 try (Interpreter interp = new SubInterpreter( 41 new JepConfig().addIncludePaths("."))) { 42 interp.eval("import sys"); 43 // assert 1 == ((Number) 44 // jep.getValue("sys.flags.ignore_environment")) 45 // .intValue(); 46 assert 0 == ((Number) interp.getValue("sys.flags.no_site")).intValue(); 47 assert 1 == ((Number) interp.getValue("sys.flags.no_user_site")) 48 .intValue(); 49 assert 0 == ((Number) interp.getValue("sys.flags.verbose")).intValue(); 50 assert 1 == ((Number) interp.getValue("sys.flags.optimize")) 51 .intValue(); 52 assert 1 == ((Number) interp.getValue("sys.flags.dont_write_bytecode")) 53 .intValue(); 54 assert 1 == ((Number) interp.getValue("sys.flags.hash_randomization")) 55 .intValue(); 56 } catch (Throwable e) { 57 e.printStackTrace(); 58 System.exit(1); 59 } 60 System.exit(0); 61 } 62 63 } 64