Skip to content

Commit

Permalink
PyObject implements AutoCloseable, improve javadoc, format Java files
Browse files Browse the repository at this point in the history
  • Loading branch information
ndjensen committed Jun 13, 2018
1 parent 912c30b commit 4d92a1a
Show file tree
Hide file tree
Showing 14 changed files with 413 additions and 242 deletions.
29 changes: 20 additions & 9 deletions src/main/java/jep/ClassList.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -77,7 +86,7 @@ private void loadClassPath() {
Queue<String> queue = new LinkedList<>();
Set<String> seen = new HashSet<>();

while(tok.hasMoreTokens()) {
while (tok.hasMoreTokens()) {
String el = tok.nextToken();
queue.add(el);
seen.add(el);
Expand All @@ -101,14 +110,16 @@ private void loadClassPath() {
// add entries from manifest to check later
Manifest manifest = jfile.getManifest();
if (manifest != null) {
String classpath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
String classpath = manifest.getMainAttributes()
.getValue(Attributes.Name.CLASS_PATH);

if(classpath != null) {
if (classpath != null) {
String[] relativePaths = classpath.split(" ");

for(String relativePath : relativePaths) {
String path = file.getParent() + File.separator + relativePath;
if(!seen.contains(path)) {
for (String relativePath : relativePaths) {
String path = file.getParent() + File.separator
+ relativePath;
if (!seen.contains(path)) {
queue.add(path);
seen.add(path);
}
Expand Down Expand Up @@ -203,9 +214,9 @@ private void loadClassList() throws JepException {
String rsc = "jep/classlist_";
if (version.startsWith("10.")) {
rsc += "10";
}else if (version.startsWith("9.")) {
} else if (version.startsWith("9.")) {
rsc += "9";
}else if (version.startsWith("1.8")) {
} else if (version.startsWith("1.8")) {
rsc += "8";
} else {
rsc += "7";
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/jep/Jep.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import java.util.List;
import java.util.Map;

import jep.python.MemoryManager;
import jep.python.PyModule;
import jep.python.PyObject;
import jep.python.MemoryManager;

/**
* <p>
Expand All @@ -51,7 +51,6 @@
* prevent memory leaks.
* </p>
*
* @author Mike Johnson
*/
public final class Jep implements AutoCloseable {

Expand Down Expand Up @@ -355,8 +354,8 @@ public void runScript(String script, ClassLoader cl) throws JepException {
* Invokes a Python function.
*
* @param name
* a Python function name in globals dict or the name of a
* global object and method using dot notation.
* a Python function name in globals dict or the name of a global
* object and method using dot notation.
* @param args
* args to pass to the function in order
* @return an <code>Object</code> value
Expand All @@ -376,8 +375,8 @@ public Object invoke(String name, Object... args) throws JepException {
* Invokes a Python function.
*
* @param name
* a Python function name in globals dict or the name of a
* global object and method using dot notation.
* a Python function name in globals dict or the name of a global
* object and method using dot notation.
* @param kwargs
* a Map of keyword args
* @return an <Object> value
Expand Down Expand Up @@ -549,8 +548,8 @@ private native int compileString(long tstate, String str)
* </pre>
*
* @param str
* the name of the Python variable to get from the
* interpreter's global scope
* the name of the Python variable to get from the interpreter's
* global scope
* @return an <code>Object</code> value
* @exception JepException
* if an error occurs
Expand All @@ -560,13 +559,13 @@ public Object getValue(String str) throws JepException {

return getValue(this.tstate, str, Object.class);
}

/**
* Like {@link #getValue(String)} but allows specifying the return type.
* If Jep cannot convert the variable to the specified type then a
* JepException is thrown. This can be used to safely ensure that the
* return value is an expected type. The following table describes what
* conversions are currently possible.
* Like {@link #getValue(String)} but allows specifying the return type. If
* Jep cannot convert the variable to the specified type then a JepException
* is thrown. This can be used to safely ensure that the return value is an
* expected type. The following table describes what conversions are
* currently possible.
*
* <table border="1">
* <tr>
Expand Down Expand Up @@ -625,22 +624,23 @@ public Object getValue(String str) throws JepException {
* </table>
*
* @param str
* the name of the Python variable to get from the
* interpreter's global scope
* the name of the Python variable to get from the interpreter's
* global scope
* @param clazz
* the Java class of the return type.
* @return a Java version of the variable
* @exception JepException
* if an error occurs
* @since 3.8
*/
*/
public <T> T getValue(String str, Class<T> clazz) throws JepException {
isValidThread();

return clazz.cast(getValue(this.tstate, str, clazz));
}

private native Object getValue(long tstate, String str, Class<?> clazz) throws JepException;
private native Object getValue(long tstate, String str, Class<?> clazz)
throws JepException;

/**
* Retrieves a Python string object as a Java byte[].
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/jep/JepException.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ public JepException(String s, Throwable t) {
* Construct with the address of a python exception type. This is for
* internal use only.
*
* @param s error message
* @param pythonType the address of the type of the python exception that triggered this exception.
* @param s
* error message
* @param pythonType
* the address of the type of the python exception that triggered
* this exception
*/
protected JepException(String s, long pythonType) {
super(s);
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/jep/LibraryLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@
package jep;

import java.io.File;

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Utility class for finding the jep native library for JNI. {@link Jep} will
* Utility class for finding the Jep native library for JNI. {@link Jep} will
* try to load the library using a simple {@link System#loadLibrary(String)}
* however if that fails then this class will be used to try to find the
* location of the library.
*
* The jep library is typically distributed inside the jep python module, so
* this class attempts to find a jep module with the python library inside of
* The Jep library is typically distributed inside the jep Python module, so
* this class attempts to find a jep module with the native library inside of
* it. In order to find the module this class will attempt to mimic the process
* python uses to build the path and it will search through the same
* directories. Since this is jsut a mirror of what python is doing, if there
* are changes to python it may require changes here. Python also has alot of
* Python uses to build the path and it will search through the same
* directories. Since this is just a mirror of what Python is doing, if there
* are changes to Python it may require changes here. Python also has a lot of
* specialization for specific operating systems. Rather than try to handle each
* case the same, this class will check in any location that might be valid on
* any operating system and assumes that inappropriate locations will simply not
Expand All @@ -59,7 +59,7 @@ final class LibraryLocator {
private final boolean noUserSite;

private final String pythonHome;

private LibraryLocator(PyConfig pyConfig) {
if (pyConfig != null) {
ignoreEnv = pyConfig.ignoreEnvironmentFlag != 0
Expand All @@ -74,7 +74,7 @@ private LibraryLocator(PyConfig pyConfig) {
noUserSite = false;
pythonHome = null;
}

String libraryName = System.mapLibraryName("jep");
if (libraryName.endsWith(".dylib")) {
/*
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/jep/MainInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@
*/
public final class MainInterpreter implements AutoCloseable {


private static MainInterpreter instance = null;

private static PyConfig pyConfig = null;

private static String[] sharedModulesArgv = null;

private Thread thread;

private BlockingQueue<String> importQueue = new SynchronousQueue<>();
Expand Down Expand Up @@ -131,7 +130,7 @@ protected void initialize() throws Error {
pyConfig.hashRandomizationFlag, pyConfig.pythonHome);
}

thread = new Thread("JepMainInterpreter"){
thread = new Thread("JepMainInterpreter") {

@Override
public void run() {
Expand Down Expand Up @@ -210,7 +209,7 @@ public void sharedImport(String module) throws JepException {
*/
@Override
public void close() {
if(thread != null){
if (thread != null) {
thread.interrupt();
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/jep/Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ protected Proxy(InvocationHandler h) {
super(h);
}

private static Object newDirectProxyInstance(long tstate, long ltarget, Jep jep,
ClassLoader loader, Class<?> targetInterface) {
return newProxyInstance(tstate, ltarget, jep, loader, new String[] { targetInterface.getName() }, true);
private static Object newDirectProxyInstance(long tstate, long ltarget,
Jep jep, ClassLoader loader, Class<?> targetInterface) {
return newProxyInstance(tstate, ltarget, jep, loader,
new String[] { targetInterface.getName() }, true);
}

/**
Expand Down Expand Up @@ -82,14 +83,17 @@ private static Object newDirectProxyInstance(long tstate, long ltarget, Jep jep,
*/
public static Object newProxyInstance(long tstate, long ltarget, Jep jep,
ClassLoader loader, String[] interfaces) {
return newProxyInstance(tstate, ltarget, jep, loader, interfaces, false);
return newProxyInstance(tstate, ltarget, jep, loader, interfaces,
false);
}

private static Object newProxyInstance(long tstate, long ltarget, Jep jep,
ClassLoader loader, String[] interfaces, boolean functionalInterface) {
ClassLoader loader, String[] interfaces,
boolean functionalInterface) {
InvocationHandler ih = null;
try {
ih = new jep.python.InvocationHandler(tstate, ltarget, jep, functionalInterface);
ih = new jep.python.InvocationHandler(tstate, ltarget, jep,
functionalInterface);
} catch (JepException e) {
throw new IllegalArgumentException(e);
}
Expand Down
35 changes: 20 additions & 15 deletions src/main/java/jep/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,37 @@
*/
public final class Util {

/*
* TODO this class should eventually not be needed, convert its
* functionality to native code
*/

// these must be the same as util.h
public static final int JBOOLEAN_ID = 0;
protected static final int JBOOLEAN_ID = 0;

public static final int JINT_ID = 1;
protected static final int JINT_ID = 1;

public static final int JLONG_ID = 2;
protected static final int JLONG_ID = 2;

public static final int JOBJECT_ID = 3;
protected static final int JOBJECT_ID = 3;

public static final int JSTRING_ID = 4;
protected static final int JSTRING_ID = 4;

public static final int JVOID_ID = 5;
protected static final int JVOID_ID = 5;

public static final int JDOUBLE_ID = 6;
protected static final int JDOUBLE_ID = 6;

public static final int JSHORT_ID = 7;
protected static final int JSHORT_ID = 7;

public static final int JFLOAT_ID = 8;
protected static final int JFLOAT_ID = 8;

public static final int JARRAY_ID = 9;
protected static final int JARRAY_ID = 9;

public static final int JCHAR_ID = 10;
protected static final int JCHAR_ID = 10;

public static final int JBYTE_ID = 11;
protected static final int JBYTE_ID = 11;

public static final int JCLASS_ID = 12;
protected static final int JCLASS_ID = 12;

private Util() {
}
Expand All @@ -75,7 +80,7 @@ private Util() {
* an <code>Object</code> value
* @return an <code>int</code> one of the type _ID constants
*/
public static final int getTypeId(Object obj) {
protected static final int getTypeId(Object obj) {
if (obj == null) {
return -1;
}
Expand Down Expand Up @@ -147,7 +152,7 @@ public static final int getTypeId(Object obj) {
* an <code>Object</code> value
* @return an <code>int</code> one of the type _ID constants
*/
public static final int getTypeId(Class<?> clazz) {
protected static final int getTypeId(Class<?> clazz) {
if (clazz == null) {
return -1;
}
Expand Down
Loading

0 comments on commit 4d92a1a

Please sign in to comment.