1 /* ====================================================================
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 * ====================================================================
16 package org.apache.jcc;
19 public class PythonVM {
20 static protected PythonVM vm;
23 System.loadLibrary("jcc");
27 * Start the embedded Python interpreter. The specified
28 * program name and args are set into the Python variable sys.argv.
29 * This returns an instance of the Python VM; it may be called
30 * multiple times, and will return the same VM instance each time.
32 * @param programName the name of the Python program, typically
33 * /usr/bin/python. This is informational; the program is not
35 * @param args additional arguments to be put into sys.argv.
36 * @return a singleton instance of PythonVM
38 static public PythonVM start(String programName, String[] args)
43 vm.init(programName, args);
50 * Start the embedded Python interpreter. The specified
51 * program name is set into the Python variable sys.argv[0].
52 * This returns an instance of the Python VM; it may be called
53 * multiple times, and will return the same VM instance each time.
55 * @param programName the name of the Python program, typically
56 * /usr/bin/python. This is informational; the program is not
58 * @return a singleton instance of PythonVM
60 static public PythonVM start(String programName)
62 return start(programName, null);
66 * Obtain the PythonVM instance, or null if the Python VM
67 * has not yet been started.
69 * @return a singleton instance of PythonVM, or null
71 static public PythonVM get()
80 protected native void init(String programName, String[] args);
83 * Instantiate the specified Python class, and return the instance.
85 * @param moduleName the Python module the class is defined in
86 * @param className the Python class to instantiate.
87 * @return a handle on the Python instance.
89 public native Object instantiate(String moduleName, String className)
90 throws PythonException;
93 * Bump the Python thread state counter. Every thread should
94 * do this before calling into Python, to prevent the Python
95 * thread state from being inadvertently collected (and causing loss
96 * of thread-local variables)
98 * @return the Python thread state counter. A return value less
99 * than zero signals an error.
101 public native int acquireThreadState();
104 * Release the Python thread state counter. Every thread that has
105 * called acquireThreadState() should call this before
108 * @return the Python thread state counter. A return value less
109 * than zero signals an error.
111 public native int releaseThreadState();