Omaha #4259 more cleanup of jep

Change-Id: I84a44ea8219132033f60ed3534fe0f00d9b5bbca

Former-commit-id: 022055f57c46cafe125cd407c2e384c46a5a8ead
This commit is contained in:
Nate Jensen 2015-04-14 11:06:51 -05:00
parent 1ea2da949f
commit a6491bd9d8
33 changed files with 901 additions and 931 deletions

View file

@ -1,6 +1,5 @@
package jep; package jep;
/** /**
* <pre> * <pre>
* ClassEnquirer.java - Interface to enquire if a name is available * ClassEnquirer.java - Interface to enquire if a name is available
@ -8,11 +7,21 @@ package jep;
* determine if an attempt to import a module/class should be directed * determine if an attempt to import a module/class should be directed
* to the Python importer or the Java importer. * to the Python importer or the Java importer.
* *
* * TODO: Implement an OSGi class enquirer. Implement a pseudo-secure
* class enquirer. jep.findClass(name) ignores the import hook and wouldn't
* go through the secure enquirer, so it would still require a restricted
* ClassLoader to be secure. However, they could potentially be used in
* tandem for faster performance or dynamic behavior.
*
* TODO Potentially add a method to get a list of sub-packages. However,
* for the most part that won't work for any implementations except ClassList
* so perhaps it shouldn't be on the interface.
*
*
* Copyright (c) 2015 JEP AUTHORS. * Copyright (c) 2015 JEP AUTHORS.
* *
* This file is licenced under the the zlib/libpng License. * This file is licenced under the the zlib/libpng License.
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any * warranty. In no event will the authors be held liable for any
* damages arising from the use of this software. * damages arising from the use of this software.
@ -31,54 +40,68 @@ package jep;
* *
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
* *
* Created: Thurs Apr 02 2015 * Created: Thurs Apr 02 2015
* *
* </pre> * </pre>
* *
* @author [ndjensen at gmail.com] Nate Jensen * @author [ndjensen at gmail.com] Nate Jensen
* @version $Id$ * @version $Id$
*/ */
public interface ClassEnquirer { public interface ClassEnquirer {
/** /**
* Checks if the name is likely available in Java. A return * Checks if the name is likely available in Java. A return value of true
* value of true implies the name corresponds to a Java package or class, but * implies the name corresponds to a Java package or class, but does not
* does not guarantee that an import will succeed. A return value of false * guarantee that an import will succeed. A return value of false implies
* implies that an import from Java would fail, but does not guarantee that an * that an import from Java would fail, but does not guarantee that an
* import will fail. * import will fail.
* *
* @param name the name to check, such as java, java.util, java.util.ArrayList * @param name
* @return true if it's likely supported by Java, false if it's likely python * the name to check, such as java, java.util,
*/ * java.util.ArrayList
public boolean contains(String name); * @return true if it's likely supported by Java, false if it's likely
* python
/** */
* Whether or not this ClassEnquirer supports importing Java classes at the public boolean contains(String name);
* package level in addition to the class level. For example, with the
* right ClassLoader JEP should always be able to successfully import Java /**
* classes with syntax such as: * Whether or not this ClassEnquirer supports importing Java classes at the
* * package level in addition to the class level. For example, with the right
* <p><blockquote><pre> * ClassLoader JEP should always be able to successfully import Java classes
* from java.util import ArrayList * with syntax such as:
* o = ArrayList() *
* </p></blockquote></pre> * <p>
* * <blockquote>
* However, only in some scenarios can the package be imported separately *
* without the fully qualified name, such as: * <pre>
* * from java.util import ArrayList
* <p><blockquote><pre> * o = ArrayList()
* import java.util as ju *
* o = ju.ArrayList() * </p>
* </p></blockquote></pre> * </blockquote></pre>
* *
* This also roughly corresponds to whether or not dir(javaPackage) will * However, only in some scenarios can the package be imported separately
* return a list of available classes or only the classes that have been * without the fully qualified name, such as:
* explicitly imported. *
* * <p>
* @return true if this ClassEnquirer supports import of packages in addition * <blockquote>
* to import of classes, false if it only supports importing classes. *
*/ * <pre>
public boolean supportsPackageImport(); * import java.util as ju
* o = ju.ArrayList()
*
* </p>
* </blockquote></pre>
*
* This also roughly corresponds to whether or not dir(javaPackage) will
* return a list of available classes or only the classes that have been
* explicitly imported.
*
* @return true if this ClassEnquirer supports import of packages in
* addition to import of classes, false if it only supports
* importing classes.
*/
public boolean supportsPackageImport();
} }

View file

@ -52,7 +52,7 @@ import java.util.jar.JarFile;
* @version $Id$ * @version $Id$
*/ */
public class ClassList implements ClassEnquirer { public class ClassList implements ClassEnquirer {
private static ClassList inst; private static ClassList inst;
// storage for package, members // storage for package, members
@ -289,7 +289,7 @@ public class ClassList implements ClassEnquirer {
@Override @Override
public boolean supportsPackageImport() { public boolean supportsPackageImport() {
return true; return true;
} }

View file

@ -9,7 +9,7 @@ import jep.python.*;
* <pre> * <pre>
* InvocationHandler.java - Handle Proxy method calls. * InvocationHandler.java - Handle Proxy method calls.
* *
* Copyright (c) 2004 - 2007 Mike Johnson. * Copyright (c) 2015 JEP AUTHORS.
* *
* This file is licenced under the the zlib/libpng License. * This file is licenced under the the zlib/libpng License.
* *
@ -36,7 +36,7 @@ import jep.python.*;
* </pre> * </pre>
* *
* @author [mrjohnson0 at sourceforge.net] Mike Johnson * @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id: $ * @version $Id: $
*/ */
public class InvocationHandler implements java.lang.reflect.InvocationHandler { public class InvocationHandler implements java.lang.reflect.InvocationHandler {

View file

@ -231,7 +231,7 @@ public final class Jep {
} }
eval("import jep"); eval("import jep");
if(ce == null) { if(ce == null) {
ce = ClassList.getInstance(); ce = ClassList.getInstance();
} }
set("classlist", ce); set("classlist", ce);

View file

@ -4,7 +4,7 @@ package jep;
* <pre> * <pre>
* JepException.java - it happens. * JepException.java - it happens.
* *
* Copyright (c) 2004, 2005 Mike Johnson. * Copyright (c) 2015 JEP AUTHORS.
* *
* This file is licenced under the the zlib/libpng License. * This file is licenced under the the zlib/libpng License.
* *
@ -31,7 +31,7 @@ package jep;
* *
* </pre> * </pre>
* @author [mrjohnson0 at sourceforge.net] Mike Johnson * @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id: JepException.java 342 2006-09-17 23:09:38Z mrjohnson0 $ * @version $Id$
*/ */
public class JepException extends Exception { public class JepException extends Exception {

View file

@ -19,7 +19,7 @@ import jep.python.PyModule;
* <pre> * <pre>
* JepScriptEngine.java - implements javax.script.ScriptEngine * JepScriptEngine.java - implements javax.script.ScriptEngine
* *
* Copyright (c) 2004, 2005 Mike Johnson. * Copyright (c) 2015 JEP AUTHORS.
* *
* This file is licenced under the the zlib/libpng License. * This file is licenced under the the zlib/libpng License.
* *
@ -47,7 +47,7 @@ import jep.python.PyModule;
* </pre> * </pre>
* *
* @author [mrjohnson0 at sourceforge.net] Mike Johnson * @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id: JepScriptEngine.java 366 2006-09-23 21:30:07Z albertstrasheim $ * @version $Id$
*/ */
public class JepScriptEngine implements ScriptEngine { public class JepScriptEngine implements ScriptEngine {
private Jep jep = null; private Jep jep = null;

View file

@ -13,7 +13,7 @@ import javax.script.ScriptException;
* <pre> * <pre>
* JepScriptEngineFactory.java - Embeds CPython in Java. * JepScriptEngineFactory.java - Embeds CPython in Java.
* *
* Copyright (c) 2004, 2005 Mike Johnson. * Copyright (c) 2015 JEP AUTHORS.
* *
* This file is licenced under the the zlib/libpng License. * This file is licenced under the the zlib/libpng License.
* *
@ -41,7 +41,7 @@ import javax.script.ScriptException;
* </pre> * </pre>
* *
* @author [mrjohnson0 at sourceforge.net] Mike Johnson * @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id: JepScriptEngineFactory.java 368 2006-09-23 22:02:37Z albertstrasheim $ * @version $Id$
*/ */
public class JepScriptEngineFactory implements ScriptEngineFactory { public class JepScriptEngineFactory implements ScriptEngineFactory {

View file

@ -10,7 +10,7 @@ import java.lang.reflect.InvocationHandler;
* <pre> * <pre>
* Proxy.java - Extend java.lang.reflect.Proxy for callbacks. * Proxy.java - Extend java.lang.reflect.Proxy for callbacks.
* *
* Copyright (c) 2004 - 2007 Mike Johnson. * Copyright (c) 2015 JEP AUTHORS.
* *
* This file is licenced under the the zlib/libpng License. * This file is licenced under the the zlib/libpng License.
* *
@ -38,7 +38,7 @@ import java.lang.reflect.InvocationHandler;
* </pre> * </pre>
* *
* @author [mrjohnson0 at sourceforge.net] Mike Johnson * @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id: $ * @version $Id: $
*/ */
public class Proxy extends java.lang.reflect.Proxy { public class Proxy extends java.lang.reflect.Proxy {

View file

@ -31,7 +31,7 @@ package jep;
* </pre> * </pre>
* *
* @author [mrjohnson0 at sourceforge.net] Mike Johnson * @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id$ * @version $Id$
*/ */
public class Run { public class Run {
private static boolean interactive = false; private static boolean interactive = false;

View file

@ -13,20 +13,38 @@ import jep.python.*;
* Created: Fri Apr 30 12:42:58 2004 * Created: Fri Apr 30 12:42:58 2004
* *
* @author [mrjohnson0 at sourceforge.net] Mike Johnson * @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id: Test.java 445 2007-11-20 06:57:06Z mrjohnson0 $ * @version $Id$
*/ */
public class Test implements Runnable { public class Test implements Runnable {
private Jep jep = null; private Jep jep = null;
private boolean testEval = false; private boolean testEval = false;
public static ClassLoader restrictedClassLoader = new ClassLoader() {
@Override
public Class<?> loadClass(final String name) throws ClassNotFoundException {
if (name.startsWith("java.io.")) {
throw new ClassNotFoundException("restricted class: " + name);
}
return super.loadClass(name);
}
};
public Test() { public Test() {
} // Test constructor }
public Test(boolean testEval) { public Test(boolean testEval) {
this.testEval = testEval; this.testEval = testEval;
} }
public static enum TestEnum {
One,
Two
}
public void run() { public void run() {
@ -146,11 +164,6 @@ public class Test implements Runnable {
} }
} }
protected void finalize() {
System.out.println("test instance finalized, you should see this " +
"if the reference counting worked...");
}
// get the jep used for this class // get the jep used for this class
public Jep getJep() { public Jep getJep() {
return this.jep; return this.jep;
@ -160,33 +173,9 @@ public class Test implements Runnable {
return "toString(). Thanks for calling Java(tm)."; return "toString(). Thanks for calling Java(tm).";
} }
public int getInt() {
return 2147483647;
}
public byte getByte() { public TestEnum getEnum() {
return 123; return TestEnum.One;
}
public char getChar() {
return 'c';
}
public short getShort() {
return 321;
}
public long getLong() {
return 9223372036854775807L;
}
public double getDouble() {
return 1.7976931348623157E308D;
}
public float getFloat() {
return 3.4028235E38F;
} }
public Integer getInteger() { public Integer getInteger() {
@ -279,6 +268,86 @@ public class Test implements Runnable {
public byte byteField = 43; public byte byteField = 43;
public char charField = 'c'; public char charField = 'c';
public Class classField = this.getClass(); public Class classField = this.getClass();
public boolean isBooleanField() {
return booleanField;
}
public void setBooleanField(boolean booleanField) {
this.booleanField = booleanField;
}
public byte getByteField() {
return byteField;
}
public void setByteField(byte byteField) {
this.byteField = byteField;
}
public char getCharField() {
return charField;
}
public void setCharField(char charField) {
this.charField = charField;
}
public Class getClassField() {
return classField;
}
public void setClassField(Class classField) {
this.classField = classField;
}
public double getDoubleField() {
return doubleField;
}
public void setDoubleField(double doubleField) {
this.doubleField = doubleField;
}
public float getFloatField() {
return floatField;
}
public void setFloatField(float floatField) {
this.floatField = floatField;
}
public int getIntField() {
return intField;
}
public void setIntField(int intField) {
this.intField = intField;
}
public long getLongField() {
return longField;
}
public void setLongField(long longField) {
this.longField = longField;
}
public short getShortField() {
return shortField;
}
public void setShortField(short shortField) {
this.shortField = shortField;
}
public String getStringField() {
return stringField;
}
public void setStringField(String stringField) {
this.stringField = stringField;
}
// -------------------------------------------------- static fields // -------------------------------------------------- static fields
@ -296,35 +365,81 @@ public class Test implements Runnable {
// -------------------------------------------------- static methods // -------------------------------------------------- static methods
public static String getStaticString() { public static boolean isStaticBoolean() {
return "a static string."; return staticBoolean;
}
public static boolean getStaticBoolean() {
return false;
}
public static int getStaticInt() {
return 123;
} }
public static short getStaticShort() { public static void setStaticBoolean(boolean staticBoolean) {
return 321; Test.staticBoolean = staticBoolean;
} }
public static long getStaticLong() { public static byte getStaticByte() {
return 9223372036854775807L; return staticByte;
} }
public static void setStaticByte(byte staticByte) {
Test.staticByte = staticByte;
}
public static char getStaticChar() {
return staticChar;
}
public static void setStaticChar(char staticChar) {
Test.staticChar = staticChar;
}
public static double getStaticDouble() { public static double getStaticDouble() {
return 123123213.123D; return staticDouble;
}
public static void setStaticDouble(double staticDouble) {
Test.staticDouble = staticDouble;
} }
public static float getStaticFloat() { public static float getStaticFloat() {
return 12312.123F; return staticFloat;
} }
public static void setStaticFloat(float staticFloat) {
Test.staticFloat = staticFloat;
}
public static int getStaticInt() {
return staticInt;
}
public static void setStaticInt(int staticInt) {
Test.staticInt = staticInt;
}
public static long getStaticLong() {
return staticLong;
}
public static void setStaticLong(long staticLong) {
Test.staticLong = staticLong;
}
public static short getStaticShort() {
return staticShort;
}
public static void setStaticShort(short staticShort) {
Test.staticShort = staticShort;
}
public static String getStaticString() {
return staticString;
}
public static void setStaticString(String staticString) {
Test.staticString = staticString;
}
// -------------------------------------------------- other static methods
public static Object getStaticObject() { public static Object getStaticObject() {
return new Object(); return new Object();
} }
@ -333,33 +448,17 @@ public class Test implements Runnable {
return; return;
} }
public static byte getStaticByte() {
return 23;
}
public static char getStaticChar() {
return 'b';
}
public static Class getStaticClass() { public static Class getStaticClass() {
return Thread.currentThread().getClass(); return Thread.currentThread().getClass();
} }
public static void main(String argv[]) throws Throwable { public static void main(String argv[]) throws Throwable {
Jep jep = new Jep();
if(argv.length < 1) { try {
new Thread(new Test()).start(); jep.runScript("runtests.py");
new Thread(new Test()).start();
} }
else { finally {
int count = Integer.parseInt(argv[0]); jep.close();
for(int i = 0; i < count; i++)
new Thread(new Test()).start();
} }
new Test().run();
System.gc();
} }
} // Test } // Test

View file

@ -8,7 +8,10 @@ import java.util.concurrent.Executors;
* ndarrays, and closing/disposing interpreters. This demonstrates the issue * ndarrays, and closing/disposing interpreters. This demonstrates the issue
* where numpy's array2string or array_str reference is lost. * where numpy's array2string or array_str reference is lost.
* *
* Created: Autumn 2013
*
* @author David Gillingham * @author David Gillingham
* @version $Id$
*/ */
public class TestNumpyArrayToString { public class TestNumpyArrayToString {

View file

@ -7,7 +7,9 @@ package jep;
* Python is confused about the thread state and GIL state due to the top level * Python is confused about the thread state and GIL state due to the top level
* interpreter being initialized on the same thread as the sub-interpreter(s). * interpreter being initialized on the same thread as the sub-interpreter(s).
* *
* Created: Mon Apr 13 2015 * Please see the TODO below for how to trigger the freeze.
*
* Created: Thu Apr 09 2015
* *
* @author [ndjensen at gmail.com] Nate Jensen * @author [ndjensen at gmail.com] Nate Jensen
* @version $Id$ * @version $Id$
@ -28,7 +30,7 @@ public class TestNumpyGILFreeze {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
/* /*
* TODO: To show the freeze, you need to alter Jep.pyInitialize() to * TODO: To show the freeze, you need to alter Jep.TopInterpreter to
* NOT use a separate thread for System.loadLibrary("jep"); * NOT use a separate thread for System.loadLibrary("jep");
*/ */
createJepAndUseNumpy(); createJepAndUseNumpy();

View file

@ -1,13 +1,13 @@
package jep; package jep;
import java.io.PrintWriter;
import java.io.StringWriter;
/** /**
* <pre> * <pre>
* Util.java - utility functions * Util.java - utility functions
* Copyright (c) 2004 - 2007 Mike Johnson. *
* Copyright (c) 2015 JEP AUTHORS.
*
* This file is licenced under the the zlib/libpng License. * This file is licenced under the the zlib/libpng License.
*
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any * warranty. In no event will the authors be held liable for any
* damages arising from the use of this software. * damages arising from the use of this software.
@ -26,9 +26,10 @@ import java.io.StringWriter;
* *
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*
* Created: Fri Dec 22 13:21:02 2006 * Created: Fri Dec 22 13:21:02 2006
* </pre> * </pre>
* *
* @author [mrjohnson0 at sourceforge.net] Mike Johnson * @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id: $ * @version $Id: $
*/ */
@ -36,159 +37,135 @@ public final class Util {
// these must be the same as util.h // these must be the same as util.h
public static final int JBOOLEAN_ID = 0; public static final int JBOOLEAN_ID = 0;
public static final int JINT_ID = 1;
public static final int JINT_ID = 1; public static final int JLONG_ID = 2;
public static final int JOBJECT_ID = 3;
public static final int JLONG_ID = 2; public static final int JSTRING_ID = 4;
public static final int JVOID_ID = 5;
public static final int JOBJECT_ID = 3; public static final int JDOUBLE_ID = 6;
public static final int JSHORT_ID = 7;
public static final int JSTRING_ID = 4; public static final int JFLOAT_ID = 8;
public static final int JARRAY_ID = 9;
public static final int JVOID_ID = 5; public static final int JCHAR_ID = 10;
public static final int JBYTE_ID = 11;
public static final int JDOUBLE_ID = 6; public static final int JCLASS_ID = 12;
public static final int JSHORT_ID = 7;
public static final int JFLOAT_ID = 8;
public static final int JARRAY_ID = 9;
public static final int JCHAR_ID = 10;
public static final int JBYTE_ID = 11;
public static final int JCLASS_ID = 12;
private Util() { private Util() {
} }
/** /**
* <pre> * <pre>
* &lt;b&gt;Internal use only&lt;/b&gt; *
* <b>Internal use only</b>
*
* Does the same thing as util.c::get_jtype, but it's easier more * Does the same thing as util.c::get_jtype, but it's easier more
* stable to do this in java when able. * stable to do this in java when able.
*
* </pre> * </pre>
* *
* @param obj * @param obj an <code>Object</code> value
* an <code>Object</code> value
* @return an <code>int</code> one of the type _ID constants * @return an <code>int</code> one of the type _ID constants
*/ */
public static final int getTypeId(Object obj) { public static final int getTypeId(Object obj) {
if (obj == null) if(obj == null)
return -1; return -1;
if (obj instanceof Integer) if(obj instanceof Integer)
return JINT_ID; return JINT_ID;
if (obj instanceof Short) if(obj instanceof Short)
return JSHORT_ID; return JSHORT_ID;
if (obj instanceof Double) if(obj instanceof Double)
return JDOUBLE_ID; return JDOUBLE_ID;
if (obj instanceof Float) if(obj instanceof Float)
return JFLOAT_ID; return JFLOAT_ID;
if (obj instanceof Boolean) if(obj instanceof Boolean)
return JBOOLEAN_ID; return JBOOLEAN_ID;
if (obj instanceof Long) if(obj instanceof Long)
return JLONG_ID; return JLONG_ID;
if (obj instanceof String) if(obj instanceof String)
return JSTRING_ID; return JSTRING_ID;
if (obj instanceof Void) if(obj instanceof Void)
return JVOID_ID; return JVOID_ID;
if (obj instanceof Character) if(obj instanceof Character)
return JCHAR_ID; return JCHAR_ID;
if (obj instanceof Byte) if(obj instanceof Byte)
return JBYTE_ID; return JBYTE_ID;
if (obj instanceof Class) if(obj instanceof Class)
return JCLASS_ID; return JCLASS_ID;
Class clazz = obj.getClass(); Class clazz = obj.getClass();
if (clazz.isArray()) if(clazz.isArray())
return JARRAY_ID; return JARRAY_ID;
return JOBJECT_ID; return JOBJECT_ID;
} }
/** /**
* <pre> * <pre>
* &lt;b&gt;Internal use only&lt;/b&gt; *
* Same as * <b>Internal use only</b>
* <code> *
* getTypeId(Object) * Same as <code>getTypeId(Object)</code> but for Class. This is
* </code>
* but for Class. This is
* useful for determining the _ID for things like * useful for determining the _ID for things like
* method.getReturnType. * method.getReturnType.
*
* </pre> * </pre>
* *
* @param obj * @param obj an <code>Object</code> value
* an <code>Object</code> value
* @return an <code>int</code> one of the type _ID constants * @return an <code>int</code> one of the type _ID constants
*/ */
public static final int getTypeId(Class<?> clazz) { public static final int getTypeId(Class<?> clazz) {
if (clazz == null) if(clazz == null)
return -1; return -1;
if (clazz.isAssignableFrom(Integer.class)) if(clazz.isAssignableFrom(Integer.class))
return JINT_ID; return JINT_ID;
if (clazz.isAssignableFrom(Short.class)) if(clazz.isAssignableFrom(Short.class))
return JSHORT_ID; return JSHORT_ID;
if (clazz.isAssignableFrom(Double.class)) if(clazz.isAssignableFrom(Double.class))
return JDOUBLE_ID; return JDOUBLE_ID;
if (clazz.isAssignableFrom(Float.class)) if(clazz.isAssignableFrom(Float.class))
return JFLOAT_ID; return JFLOAT_ID;
if (clazz.isAssignableFrom(Boolean.class)) if(clazz.isAssignableFrom(Boolean.class))
return JBOOLEAN_ID; return JBOOLEAN_ID;
if (clazz.isAssignableFrom(Long.class)) if(clazz.isAssignableFrom(Long.class))
return JLONG_ID; return JLONG_ID;
if (clazz.isAssignableFrom(String.class)) if(clazz.isAssignableFrom(String.class))
return JSTRING_ID; return JSTRING_ID;
if (clazz.isAssignableFrom(Void.class)) if(clazz.isAssignableFrom(Void.class))
return JVOID_ID; return JVOID_ID;
if (clazz.isAssignableFrom(Character.class)) if(clazz.isAssignableFrom(Character.class))
return JCHAR_ID; return JCHAR_ID;
if (clazz.isAssignableFrom(Byte.class)) if(clazz.isAssignableFrom(Byte.class))
return JBYTE_ID; return JBYTE_ID;
if (clazz.isAssignableFrom(Class.class)) if(clazz.isAssignableFrom(Class.class))
return JCLASS_ID; return JCLASS_ID;
if (clazz.isArray()) if(clazz.isArray())
return JARRAY_ID; return JARRAY_ID;
return JOBJECT_ID; return JOBJECT_ID;
} }
/**
* Returns a String version of the throwable with the stacktrace
*
* @param throwable
* the throwable
* @return the throwable's stacktrace
*/
public static String throwableAsString(Throwable throwable) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
throwable.printStackTrace(printWriter);
return stringWriter.toString();
}
} }

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -26,12 +26,6 @@
distribution. distribution.
*/ */
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/
#include "util.h" #include "util.h"
#include "jep.h" #include "jep.h"
#include "pyembed.h" #include "pyembed.h"
@ -43,7 +37,7 @@
BOOL APIENTRY DllMain(HANDLE hModule, BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call, DWORD ul_reason_for_call,
LPVOID lpReserved) { LPVOID lpReserved) {
return TRUE; return TRUE;
} }
#endif #endif
@ -85,7 +79,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_run
const char *filename; const char *filename;
filename = jstring2char(env, str); filename = jstring2char(env, str);
pyembed_run(env, tstate, (char *) filename); pyembed_run(env, (intptr_t) tstate, (char *) filename);
release_utf_char(env, str, filename); release_utf_char(env, str, filename);
} }
@ -124,7 +118,7 @@ JNIEXPORT jint JNICALL Java_jep_Jep_compileString
jint ret; jint ret;
str = jstring2char(env, jstr); str = jstring2char(env, jstr);
ret = (jint) pyembed_compile_string(env, tstate, (char *) str); ret = (jint) pyembed_compile_string(env, (intptr_t) tstate, (char *) str);
release_utf_char(env, jstr, str); release_utf_char(env, jstr, str);
return ret; return ret;
} }
@ -140,7 +134,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_eval
const char *str; const char *str;
str = jstring2char(env, jstr); str = jstring2char(env, jstr);
pyembed_eval(env, tstate, (char *) str); pyembed_eval(env, (intptr_t) tstate, (char *) str);
release_utf_char(env, jstr, str); release_utf_char(env, jstr, str);
} }
@ -156,7 +150,7 @@ JNIEXPORT jobject JNICALL Java_jep_Jep_getValue
jobject ret; jobject ret;
str = jstring2char(env, jstr); str = jstring2char(env, jstr);
ret = pyembed_getvalue(env, tstate, (char *) str); ret = pyembed_getvalue(env, (intptr_t) tstate, (char *) str);
release_utf_char(env, jstr, str); release_utf_char(env, jstr, str);
return ret; return ret;
} }
@ -173,7 +167,7 @@ JNIEXPORT jobject JNICALL Java_jep_Jep_getValue_1floatarray
jobject ret; jobject ret;
str = jstring2char(env, jstr); str = jstring2char(env, jstr);
ret = pyembed_getvalue_array(env, tstate, (char *) str, JFLOAT_ID); ret = pyembed_getvalue_array(env, (intptr_t) tstate, (char *) str, JFLOAT_ID);
release_utf_char(env, jstr, str); release_utf_char(env, jstr, str);
return ret; return ret;
} }
@ -190,7 +184,7 @@ JNIEXPORT jobject JNICALL Java_jep_Jep_getValue_1bytearray
jobject ret; jobject ret;
str = jstring2char(env, jstr); str = jstring2char(env, jstr);
ret = pyembed_getvalue_array(env, tstate, (char *) str, JBYTE_ID); ret = pyembed_getvalue_array(env, (intptr_t) tstate, (char *) str, JBYTE_ID);
release_utf_char(env, jstr, str); release_utf_char(env, jstr, str);
return ret; return ret;
} }
@ -208,7 +202,7 @@ JNIEXPORT jlong JNICALL Java_jep_Jep_createModule
jlong ret; jlong ret;
str = jstring2char(env, jstr); str = jstring2char(env, jstr);
ret = pyembed_create_module(env, tstate, (char *) str); ret = pyembed_create_module(env, (intptr_t) tstate, (char *) str);
release_utf_char(env, jstr, str); release_utf_char(env, jstr, str);
return ret; return ret;
} }
@ -221,7 +215,7 @@ JNIEXPORT jlong JNICALL Java_jep_Jep_createModule
*/ */
JNIEXPORT void JNICALL Java_jep_Jep_setClassLoader JNIEXPORT void JNICALL Java_jep_Jep_setClassLoader
(JNIEnv *env, jobject obj, jlong tstate, jobject cl) { (JNIEnv *env, jobject obj, jlong tstate, jobject cl) {
pyembed_setloader(env, tstate, cl); pyembed_setloader(env, (intptr_t) tstate, cl);
} }
@ -232,7 +226,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_setClassLoader
*/ */
JNIEXPORT void JNICALL Java_jep_Jep_close JNIEXPORT void JNICALL Java_jep_Jep_close
(JNIEnv *env, jobject obj, jlong tstate) { (JNIEnv *env, jobject obj, jlong tstate) {
pyembed_thread_close(tstate); pyembed_thread_close((intptr_t) tstate);
} }
@ -248,7 +242,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2Ljava_lang_Object_2
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_object(env, tstate, 0, name, jval); pyembed_setparameter_object(env, (intptr_t) tstate, 0, name, jval);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -263,7 +257,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2Ljava_lang_Class_2
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_class(env, tstate, 0, name, jval); pyembed_setparameter_class(env, (intptr_t) tstate, 0, name, jval);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -279,7 +273,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2Ljava_lang_String_2
name = jstring2char(env, jname); name = jstring2char(env, jname);
value = jstring2char(env, jval); value = jstring2char(env, jval);
pyembed_setparameter_string(env, tstate, 0, name, value); pyembed_setparameter_string(env, (intptr_t) tstate, 0, name, value);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
release_utf_char(env, jval, value); release_utf_char(env, jval, value);
} }
@ -295,7 +289,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2I
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_int(env, tstate, 0, name, (int) jval); pyembed_setparameter_int(env, (intptr_t) tstate, 0, name, (int) jval);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -310,7 +304,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2J
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_long(env, tstate, 0, name, (jeplong) jval); pyembed_setparameter_long(env, (intptr_t) tstate, 0, name, (jeplong) jval);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -325,7 +319,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2D
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_double(env, tstate, 0, name, (double) jval); pyembed_setparameter_double(env, (intptr_t) tstate, 0, name, (double) jval);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -340,7 +334,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2F
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_float(env, tstate, 0, name, (float) jval); pyembed_setparameter_float(env, (intptr_t) tstate, 0, name, (float) jval);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -355,7 +349,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3Z
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_array(env, tstate, 0, name, (jobjectArray) jarr); pyembed_setparameter_array(env, (intptr_t) tstate, 0, name, (jobjectArray) jarr);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -370,7 +364,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3I
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_array(env, tstate, 0, name, (jobjectArray) jarr); pyembed_setparameter_array(env, (intptr_t) tstate, 0, name, (jobjectArray) jarr);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -385,7 +379,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3S
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_array(env, tstate, 0, name, (jobjectArray) jarr); pyembed_setparameter_array(env, (intptr_t) tstate, 0, name, (jobjectArray) jarr);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -400,7 +394,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3B
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_array(env, tstate, 0, name, (jobjectArray) jarr); pyembed_setparameter_array(env, (intptr_t) tstate, 0, name, (jobjectArray) jarr);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -415,7 +409,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3J
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_array(env, tstate, 0, name, (jobjectArray) jarr); pyembed_setparameter_array(env, (intptr_t) tstate, 0, name, (jobjectArray) jarr);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
@ -430,57 +424,10 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3D
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_array(env, tstate, 0, name, (jobjectArray) jarr); pyembed_setparameter_array(env, (intptr_t) tstate, 0, name, (jobjectArray) jarr);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }
/*
* Class: jep_Jep
* Method: setNumeric
* Signature: (JLjava/lang/String;[FII)V
*/
JNIEXPORT void JNICALL Java_jep_Jep_setNumeric__JLjava_lang_String_2_3FII
(JNIEnv *env, jobject obj, jlong tstate, jstring jname, jfloatArray jarr, jint nx, jint ny) {
// function added by njensen
const char *name;
name = jstring2char(env, jname);
pyembed_setnumeric_array(env, tstate, 0, name, (jobjectArray) jarr, nx, ny);
release_utf_char(env, jname, name);
}
/*
* Class: jep_Jep
* Method: setNumeric
* Signature: (JLjava/lang/String;[III)V
*/
JNIEXPORT void JNICALL Java_jep_Jep_setNumeric__JLjava_lang_String_2_3III
(JNIEnv *env, jobject obj, jlong tstate, jstring jname, jintArray jarr, jint nx, jint ny) {
// function added by njensen
const char *name;
name = jstring2char(env, jname);
pyembed_setnumeric_array(env, tstate, 0, name, (jobjectArray) jarr, nx, ny);
release_utf_char(env, jname, name);
}
/*
* Class: jep_Jep
* Method: setNumeric
* Signature: (JLjava/lang/String;[BII)V
*/
JNIEXPORT void JNICALL Java_jep_Jep_setNumeric__JLjava_lang_String_2_3BII
(JNIEnv *env, jobject obj, jlong tstate, jstring jname, jbyteArray jarr, jint nx, jint ny) {
// function added by chammack
const char *name;
name = jstring2char(env, jname);
pyembed_setnumeric_array(env, tstate, 0, name, (jobjectArray) jarr, nx, ny);
release_utf_char(env, jname, name);
}
/* /*
* Class: jep_Jep * Class: jep_Jep
@ -492,6 +439,6 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3F
const char *name; const char *name;
name = jstring2char(env, jname); name = jstring2char(env, jname);
pyembed_setparameter_array(env, tstate, 0, name, (jobjectArray) jarr); pyembed_setparameter_array(env, (intptr_t) tstate, 0, name, (jobjectArray) jarr);
release_utf_char(env, jname, name); release_utf_char(env, jname, name);
} }

View file

@ -36,7 +36,7 @@
The really interesting stuff is not here. :-) This file simply makes calls The really interesting stuff is not here. :-) This file simply makes calls
to the type definitions for pyjobject, etc. to the type definitions for pyjobject, etc.
***************************************************************************** *****************************************************************************
*/ */
#ifdef WIN32 #ifdef WIN32
# include "winconfig.h" # include "winconfig.h"
@ -306,14 +306,14 @@ void pyembed_thread_close(intptr_t _jepThread) {
Py_DECREF(jepThread->globals); Py_DECREF(jepThread->globals);
} }
if(jepThread->modjep) { if(jepThread->modjep) {
PyObject *moddict = PyModule_GetDict(jepThread->modjep); PyObject *moddict = PyModule_GetDict(jepThread->modjep);
/* /*
* we need to clear out the jep module's dictionary, otherwise it * we need to clear out the jep module's dictionary, otherwise it
* can leak references to some of its attributes * can leak references to some of its attributes
*/ */
if(moddict) { if(moddict) {
PyDict_Clear(moddict); PyDict_Clear(moddict);
} }
Py_DECREF(jepThread->modjep); Py_DECREF(jepThread->modjep);
} }
if(jepThread->classloader) { if(jepThread->classloader) {
@ -399,7 +399,7 @@ static PyObject* pyembed_jproxy(PyObject *self, PyObject *args) {
Py_ssize_t inum, i; Py_ssize_t inum, i;
jobject proxy; jobject proxy;
if(!PyArg_ParseTuple(args, "OO!:jproxy", if(!PyArg_ParseTuple(args, "OO!:jproxy",
&pytarget, &pytarget,
&PyList_Type, &PyList_Type,
&interfaces)) &interfaces))
@ -482,7 +482,7 @@ static PyObject* pyembed_set_print_stack(PyObject *self, PyObject *args) {
JepThread *jepThread; JepThread *jepThread;
char *print = 0; char *print = 0;
if(!PyArg_ParseTuple(args, "b:setPrintStack", &print)) if(!PyArg_ParseTuple(args, "b:setPrintStack", &print))
return NULL; return NULL;
jepThread = pyembed_get_jepthread(); jepThread = pyembed_get_jepthread();
@ -1120,6 +1120,7 @@ jobject pyembed_box_py(JNIEnv *env, PyObject *result) {
} }
#endif #endif
// TODO find a better solution than this
// convert everything else to string // convert everything else to string
{ {
jobject ret; jobject ret;
@ -1350,7 +1351,7 @@ void pyembed_run(JNIEnv *env,
ext = file + strlen(file) - 4; ext = file + strlen(file) - 4;
if (maybe_pyc_file(script, file, ext, 0)) { if (maybe_pyc_file(script, file, ext, 0)) {
/* Try to run a pyc file. First, re-open in binary */ /* Try to run a pyc file. First, re-open in binary */
fclose(script); fclose(script);
if((script = fopen(file, "rb")) == NULL) { if((script = fopen(file, "rb")) == NULL) {
THROW_JEP(env, "pyembed_run: Can't reopen .pyc file"); THROW_JEP(env, "pyembed_run: Can't reopen .pyc file");
goto EXIT; goto EXIT;
@ -1389,73 +1390,69 @@ EXIT:
// gratuitously copyied from pythonrun.c::run_pyc_file // gratuitously copyied from pythonrun.c::run_pyc_file
static void pyembed_run_pyc(JepThread *jepThread, static void pyembed_run_pyc(JepThread *jepThread,
FILE *fp) { FILE *fp) {
PyCodeObject *co; PyCodeObject *co;
PyObject *v; PyObject *v;
long magic; long magic;
long PyImport_GetMagicNumber(void); long PyImport_GetMagicNumber(void);
magic = PyMarshal_ReadLongFromFile(fp); magic = PyMarshal_ReadLongFromFile(fp);
if(magic != PyImport_GetMagicNumber()) { if (magic != PyImport_GetMagicNumber()) {
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError, "Bad magic number in .pyc file");
"Bad magic number in .pyc file"); return;
return; }
} (void) PyMarshal_ReadLongFromFile(fp);
(void) PyMarshal_ReadLongFromFile(fp); v = (PyObject *) (intptr_t) PyMarshal_ReadLastObjectFromFile(fp);
v = (PyObject *) (intptr_t) PyMarshal_ReadLastObjectFromFile(fp); if (v == NULL || !PyCode_Check(v)) {
if(v == NULL || !PyCode_Check(v)) { Py_XDECREF(v);
Py_XDECREF(v); PyErr_SetString(PyExc_RuntimeError, "Bad code object in .pyc file");
PyErr_SetString(PyExc_RuntimeError, return;
"Bad code object in .pyc file"); }
return; co = (PyCodeObject *) v;
} v = PyEval_EvalCode(co, jepThread->globals, jepThread->globals);
co = (PyCodeObject *) v; Py_DECREF(co);
v = PyEval_EvalCode(co, jepThread->globals, jepThread->globals);
Py_DECREF(co);
Py_XDECREF(v); Py_XDECREF(v);
} }
/* Check whether a file maybe a pyc file: Look at the extension, /* Check whether a file maybe a pyc file: Look at the extension,
the file type, and, if we may close it, at the first few bytes. */ the file type, and, if we may close it, at the first few bytes. */
// gratuitously copyied from pythonrun.c::run_pyc_file // gratuitously copyied from pythonrun.c::run_pyc_file
static int maybe_pyc_file(FILE *fp, static int maybe_pyc_file(FILE *fp, const char* filename, const char* ext,
const char* filename, int closeit) {
const char* ext, if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
int closeit) { return 1;
if(strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
return 1;
/* Only look into the file if we are allowed to close it, since /* Only look into the file if we are allowed to close it, since
it then should also be seekable. */ it then should also be seekable. */
if(closeit) { if (closeit) {
/* Read only two bytes of the magic. If the file was opened in /* Read only two bytes of the magic. If the file was opened in
text mode, the bytes 3 and 4 of the magic (\r\n) might not text mode, the bytes 3 and 4 of the magic (\r\n) might not
be read as they are on disk. */ be read as they are on disk. */
unsigned int halfmagic = (unsigned int) PyImport_GetMagicNumber() & 0xFFFF; unsigned int halfmagic = (unsigned int) PyImport_GetMagicNumber()
unsigned char buf[2]; & 0xFFFF;
/* Mess: In case of -x, the stream is NOT at its start now, unsigned char buf[2];
and ungetc() was used to push back the first newline, /* Mess: In case of -x, the stream is NOT at its start now,
which makes the current stream position formally undefined, and ungetc() was used to push back the first newline,
and a x-platform nightmare. which makes the current stream position formally undefined,
Unfortunately, we have no direct way to know whether -x and a x-platform nightmare.
was specified. So we use a terrible hack: if the current Unfortunately, we have no direct way to know whether -x
stream position is not 0, we assume -x was specified, and was specified. So we use a terrible hack: if the current
give up. Bug 132850 on SourceForge spells out the stream position is not 0, we assume -x was specified, and
hopelessness of trying anything else (fseek and ftell give up. Bug 132850 on SourceForge spells out the
don't work predictably x-platform for text-mode files). hopelessness of trying anything else (fseek and ftell
*/ don't work predictably x-platform for text-mode files).
int ispyc = 0; */
if(ftell(fp) == 0) { int ispyc = 0;
if(fread(buf, 1, 2, fp) == 2 && if (ftell(fp) == 0) {
(buf[1]<<8 | buf[0]) == halfmagic) if (fread(buf, 1, 2, fp) == 2
ispyc = 1; && (buf[1] << 8 | buf[0]) == halfmagic)
rewind(fp); ispyc = 1;
} rewind(fp);
}
return ispyc; return ispyc;
} }
return 0; return 0;
} }

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,15 +24,8 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/ */
#ifndef _Included_pyembed #ifndef _Included_pyembed
#define _Included_pyembed #define _Included_pyembed
@ -52,7 +45,6 @@
#include "Python.h" #include "Python.h"
#include "util.h" #include "util.h"
#include "pyjobject.h"
#define DICT_KEY "jep" #define DICT_KEY "jep"
@ -103,9 +95,4 @@ void pyembed_setparameter_long(JNIEnv*, intptr_t, intptr_t, const char*, jeplong
void pyembed_setparameter_double(JNIEnv*, intptr_t, intptr_t, const char*, double); void pyembed_setparameter_double(JNIEnv*, intptr_t, intptr_t, const char*, double);
void pyembed_setparameter_float(JNIEnv*, intptr_t, intptr_t, const char*, float); void pyembed_setparameter_float(JNIEnv*, intptr_t, intptr_t, const char*, float);
// added by njensen
void pyembed_setnumeric_array(JNIEnv*, intptr_t, intptr_t, const char*, jobjectArray, int, int);
static void init(void);
static int ran = 0;
#endif #endif

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -23,16 +23,9 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/ */
// shut up the compiler // shut up the compiler
#ifdef _POSIX_C_SOURCE #ifdef _POSIX_C_SOURCE
# undef _POSIX_C_SOURCE # undef _POSIX_C_SOURCE

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2011 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,7 +24,7 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/ */
#ifdef WIN32 #ifdef WIN32
# include "winconfig.h" # include "winconfig.h"
@ -62,7 +62,7 @@
#include "pyjarray.h" #include "pyjarray.h"
#include "util.h" #include "util.h"
staticforward PyTypeObject PyJclass_Type; PyAPI_DATA(PyTypeObject) PyJclass_Type;
static PyObject* pyjclass_add_inner_classes(JNIEnv*, jobject, PyJobject_Object*); static PyObject* pyjclass_add_inner_classes(JNIEnv*, jobject, PyJobject_Object*);
static void pyjclass_dealloc(PyJclass_Object*); static void pyjclass_dealloc(PyJclass_Object*);
@ -518,7 +518,7 @@ static PyMethodDef pyjclass_methods[] = {
}; };
static PyTypeObject PyJclass_Type = { PyTypeObject PyJclass_Type = {
PyObject_HEAD_INIT(0) PyObject_HEAD_INIT(0)
0, 0,
"PyJclass", "PyJclass",

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2011 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,7 +24,7 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/ */
// shut up the compiler // shut up the compiler
#ifdef _POSIX_C_SOURCE #ifdef _POSIX_C_SOURCE

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -29,7 +29,7 @@
Fields really don't have to be represented as python objects, Fields really don't have to be represented as python objects,
but it is nice to have garbage collection and to be able but it is nice to have garbage collection and to be able
to cast to PyObject. to cast to PyObject.
*/ */
#ifdef WIN32 #ifdef WIN32
# include "winconfig.h" # include "winconfig.h"
@ -65,7 +65,7 @@
#include "pyjclass.h" #include "pyjclass.h"
#include "util.h" #include "util.h"
staticforward PyTypeObject PyJfield_Type; PyAPI_DATA(PyTypeObject) PyJfield_Type;
static void pyjfield_dealloc(PyJfield_Object *self); static void pyjfield_dealloc(PyJfield_Object *self);
@ -258,8 +258,9 @@ static void pyjfield_dealloc(PyJfield_Object *self) {
(*env)->DeleteGlobalRef(env, self->rfield); (*env)->DeleteGlobalRef(env, self->rfield);
} }
if(self->pyFieldName) if(self->pyFieldName) {
Py_DECREF(self->pyFieldName); Py_DECREF(self->pyFieldName);
}
PyObject_Del(self); PyObject_Del(self);
#endif #endif
@ -916,7 +917,7 @@ static PyMethodDef pyjfield_methods[] = {
}; };
static PyTypeObject PyJfield_Type = { PyTypeObject PyJfield_Type = {
PyObject_HEAD_INIT(0) PyObject_HEAD_INIT(0)
0, 0,
"PyJfield", "PyJfield",

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,7 +24,7 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/ */
// shut up the compiler // shut up the compiler

View file

@ -2,8 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2011 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
@author Nate Jensen
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -60,7 +59,7 @@
#include "pyjobject.h" #include "pyjobject.h"
#include "pyembed.h" #include "pyembed.h"
staticforward PyTypeObject PyJlist_Type; PyAPI_DATA(PyTypeObject) PyJlist_Type;
static Py_ssize_t pyjlist_len(PyObject*); static Py_ssize_t pyjlist_len(PyObject*);
static PyObject* pyjlist_add(PyObject*, PyObject*); static PyObject* pyjlist_add(PyObject*, PyObject*);
@ -529,20 +528,18 @@ static PySequenceMethods pyjlist_seq_methods = {
/* /*
* I set tp_base to PyJobject_Type so it has inheritance, but for the life * Inherits from PyJobject_Type
* of me I couldn't get it to inherit the methods, so I set the relevant
* methods directly.
*/ */
static PyTypeObject PyJlist_Type = { PyTypeObject PyJlist_Type = {
PyObject_HEAD_INIT(0) PyObject_HEAD_INIT(0)
0, 0,
"jep.PyJlist", "jep.PyJlist",
sizeof(PyJlist_Object), sizeof(PyJlist_Object),
0, 0,
(destructor) pyjobject_dealloc, /* tp_dealloc */ 0, /* tp_dealloc */
0, /* tp_print */ 0, /* tp_print */
(getattrfunc) pyjobject_getattr, /* tp_getattr */ 0, /* tp_getattr */
(setattrfunc) pyjobject_setattr, /* tp_setattr */ 0, /* tp_setattr */
0, /* tp_compare */ 0, /* tp_compare */
0, /* tp_repr */ 0, /* tp_repr */
0, /* tp_as_number */ 0, /* tp_as_number */
@ -550,7 +547,7 @@ static PyTypeObject PyJlist_Type = {
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
0, /* tp_hash */ 0, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
(reprfunc) pyjobject_str, /* tp_str */ 0, /* tp_str */
0, /* tp_getattro */ 0, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */

View file

@ -2,8 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2011 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
@author Nate Jensen.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,15 +24,8 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/ */
#ifdef WIN32 #ifdef WIN32
# include "winconfig.h" # include "winconfig.h"
#endif #endif
@ -67,7 +60,7 @@
#include "util.h" #include "util.h"
#include "pyembed.h" #include "pyembed.h"
staticforward PyTypeObject PyJmethod_Type; PyAPI_DATA(PyTypeObject) PyJmethod_Type;
static void pyjmethod_dealloc(PyJmethod_Object *self); static void pyjmethod_dealloc(PyJmethod_Object *self);
@ -75,7 +68,6 @@ static void pyjmethod_dealloc(PyJmethod_Object *self);
static jmethodID classGetName = 0; static jmethodID classGetName = 0;
static jmethodID methodGetType = 0; static jmethodID methodGetType = 0;
static jmethodID methodGetParmTypes = 0; static jmethodID methodGetParmTypes = 0;
static jmethodID methodGetExceptions = 0;
static jmethodID methodGetModifiers = 0; static jmethodID methodGetModifiers = 0;
// called internally to make new PyJmethod_Object instances. // called internally to make new PyJmethod_Object instances.
@ -93,13 +85,12 @@ PyJmethod_Object* pyjmethod_new(JNIEnv *env,
pym = PyObject_NEW(PyJmethod_Object, &PyJmethod_Type); pym = PyObject_NEW(PyJmethod_Object, &PyJmethod_Type);
pym->rmethod = (*env)->NewGlobalRef(env, rmethod); pym->rmethod = (*env)->NewGlobalRef(env, rmethod);
// pym->pyjobject = pyjobject;
pym->parameters = NULL; pym->parameters = NULL;
pym->lenParameters = 0; pym->lenParameters = 0;
pym->pyMethodName = NULL; pym->pyMethodName = NULL;
pym->isStatic = -1; pym->isStatic = -1;
pym->returnTypeId = -1; pym->returnTypeId = -1;
// ------------------------------ get method name // ------------------------------ get method name
rmethodClass = (*env)->GetObjectClass(env, rmethod); rmethodClass = (*env)->GetObjectClass(env, rmethod);
@ -152,7 +143,6 @@ PyJmethod_Object* pyjmethod_new_static(JNIEnv *env,
pym = PyObject_NEW(PyJmethod_Object, &PyJmethod_Type); pym = PyObject_NEW(PyJmethod_Object, &PyJmethod_Type);
pym->rmethod = (*env)->NewGlobalRef(env, rmethod); pym->rmethod = (*env)->NewGlobalRef(env, rmethod);
// pym->pyjobject = pyjobject;
pym->parameters = NULL; pym->parameters = NULL;
pym->lenParameters = 0; pym->lenParameters = 0;
pym->pyMethodName = NULL; pym->pyMethodName = NULL;
@ -204,12 +194,11 @@ int pyjmethod_init(JNIEnv *env, PyJmethod_Object *self) {
jmethodID methodId; jmethodID methodId;
jobject returnType = NULL; jobject returnType = NULL;
jobjectArray paramArray = NULL; jobjectArray paramArray = NULL;
jobjectArray exceptions = NULL;
jclass modClass = NULL; jclass modClass = NULL;
jint modifier = -1; jint modifier = -1;
jboolean isStatic = JNI_FALSE; jboolean isStatic = JNI_FALSE;
jclass rmethodClass = NULL; jclass rmethodClass = NULL;
// use a local frame so we don't have to worry too much about local refs. // use a local frame so we don't have to worry too much about local refs.
// make sure if this method errors out, that this is poped off again // make sure if this method errors out, that this is poped off again
(*env)->PushLocalFrame(env, 20); (*env)->PushLocalFrame(env, 20);
@ -346,8 +335,9 @@ static void pyjmethod_dealloc(PyJmethod_Object *self) {
if(self->rmethod) if(self->rmethod)
(*env)->DeleteGlobalRef(env, self->rmethod); (*env)->DeleteGlobalRef(env, self->rmethod);
if(self->pyMethodName) if(self->pyMethodName) {
Py_DECREF(self->pyMethodName); Py_DECREF(self->pyMethodName);
}
} }
PyObject_Del(self); PyObject_Del(self);
@ -362,47 +352,6 @@ int pyjmethod_check(PyObject *obj) {
} }
// called by python for __call__.
// we pass off processing to pyjobject's _find_method.
// sortof a dirty hack to get method overloading...
static PyObject* pyjmethod_call(PyJmethod_Object *self,
PyObject *args,
PyObject *keywords) {
PyObject *ret;
JNIEnv* env = NULL;
JepThread* jepthread = NULL;
if(!PyTuple_Check(args)) {
PyErr_Format(PyExc_RuntimeError, "args is not a valid tuple");
return NULL;
}
if(keywords != NULL) {
PyErr_Format(PyExc_RuntimeError, "Keywords are not supported.");
return NULL;
}
/*
* njensen changed all this for method speed optimizations.
* pyjmethods are tied to the java.lang.Methods which are tied
* to java.lang.Classes which are the same regardless of instance.
* So I removed pyjmethods having knowledge of the object instance, and
* instead they pass it through the calls, and we can reuse the method object
* for every object instance.
*
* To ensure the right object is called with the method at the right time,
* I had to wrap a pyjmethod in a new pyjmethodwrapper object that includes
* the original pyjmethod that we use with JNI, and the object it's called against
* which was determind by pyjobject's getattr when the dot operator is used.
*
*/
// shouldn't be possible to get here, should be going through wrapper.
return NULL;
}
// pyjmethod_call_internal. where the magic happens. // pyjmethod_call_internal. where the magic happens.
// //
// okay, some of the magic -- we already the methodId, so we don't have // okay, some of the magic -- we already the methodId, so we don't have
@ -411,7 +360,7 @@ static PyObject* pyjmethod_call(PyJmethod_Object *self,
// //
// easy. :-) // easy. :-)
PyObject* pyjmethod_call_internal(PyJmethod_Object *self, PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
PyJobject_Object *object, // added by njensen PyJobject_Object *instance,
PyObject *args) { PyObject *args) {
PyObject *result = NULL; PyObject *result = NULL;
const char *str = NULL; const char *str = NULL;
@ -426,11 +375,11 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(!self->parameters) { if(!self->parameters) {
if(!pyjmethod_init(env, self) || PyErr_Occurred()) if(!pyjmethod_init(env, self) || PyErr_Occurred())
return NULL; return NULL;
return pyjmethod_call_internal(self, object, args); return pyjmethod_call_internal(self, instance, args);
} }
// validate we can call this method // validate we can call this method
if(!object->object && self->isStatic != JNI_TRUE) { if(!instance->object && self->isStatic != JNI_TRUE) {
PyErr_Format(PyExc_RuntimeError, PyErr_Format(PyExc_RuntimeError,
"Instantiate this class before " "Instantiate this class before "
"calling an object method."); "calling an object method.");
@ -441,7 +390,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->lenParameters != PyTuple_GET_SIZE(args)) { if(self->lenParameters != PyTuple_GET_SIZE(args)) {
PyErr_Format(PyExc_RuntimeError, PyErr_Format(PyExc_RuntimeError,
"Invalid number of arguments: %i, expected %i.", "Invalid number of arguments: %i, expected %i.",
PyTuple_GET_SIZE(args), (int) PyTuple_GET_SIZE(args),
self->lenParameters); self->lenParameters);
return NULL; return NULL;
} }
@ -450,26 +399,22 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
// ------------------------------ build jargs off python values // ------------------------------ build jargs off python values
// added by njensen, hopefully 40 local references are enough per method call // hopefully 40 local references are enough per method call
(*env)->PushLocalFrame(env, 40); (*env)->PushLocalFrame(env, 40);
for(pos = 0; pos < self->lenParameters; pos++) { for(pos = 0; pos < self->lenParameters; pos++) {
PyObject *param = NULL; PyObject *param = NULL;
int paramTypeId = -1; int paramTypeId = -1;
jclass pclazz; jclass pclazz = NULL;
jclass paramType = jclass paramType = (jclass) (*env)->GetObjectArrayElement(env,
(jclass) (*env)->GetObjectArrayElement(env, self->parameters, pos);
self->parameters,
pos);
param = PyTuple_GetItem(args, pos); /* borrowed */ param = PyTuple_GetItem(args, pos); /* borrowed */
if(PyErr_Occurred()) { /* borrowed */ if(PyErr_Occurred()) { /* borrowed */
// changed by njensen
goto EXIT_ERROR; goto EXIT_ERROR;
} }
pclazz = (*env)->GetObjectClass(env, paramType); pclazz = (*env)->GetObjectClass(env, paramType);
if(process_java_exception(env) || !pclazz) { if(process_java_exception(env) || !pclazz) {
// changed by njensen
goto EXIT_ERROR; goto EXIT_ERROR;
} }
@ -485,7 +430,6 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
paramTypeId, paramTypeId,
pos); pos);
if(PyErr_Occurred()) { /* borrowed */ if(PyErr_Occurred()) { /* borrowed */
// changed by njensen
goto EXIT_ERROR; goto EXIT_ERROR;
} }
@ -494,6 +438,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
// ------------------------------ call based off return type // ------------------------------ call based off return type
switch(self->returnTypeId) { switch(self->returnTypeId) {
case JSTRING_ID: { case JSTRING_ID: {
@ -503,21 +448,21 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
jstr = (jstring) (*env)->CallStaticObjectMethodA( jstr = (jstring) (*env)->CallStaticObjectMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
// not static, a method on class then. // not static, a method on class then.
if(!object->object) if(!instance->object)
jstr = (jstring) (*env)->CallObjectMethodA( jstr = (jstring) (*env)->CallObjectMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
jstr = (jstring) (*env)->CallObjectMethodA( jstr = (jstring) (*env)->CallObjectMethodA(
env, env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -541,20 +486,20 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
obj = (jobjectArray) (*env)->CallStaticObjectMethodA( obj = (jobjectArray) (*env)->CallStaticObjectMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
obj = (jobjectArray) (*env)->CallObjectMethodA( obj = (jobjectArray) (*env)->CallObjectMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
obj = (jobjectArray) (*env)->CallObjectMethodA( obj = (jobjectArray) (*env)->CallObjectMethodA(
env, env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -573,18 +518,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
obj = (*env)->CallStaticObjectMethodA( obj = (*env)->CallStaticObjectMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
obj = (*env)->CallObjectMethodA(env, obj = (*env)->CallObjectMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
obj = (*env)->CallObjectMethodA(env, obj = (*env)->CallObjectMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -603,18 +548,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
obj = (*env)->CallStaticObjectMethodA( obj = (*env)->CallStaticObjectMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
obj = (*env)->CallObjectMethodA(env, obj = (*env)->CallObjectMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
obj = (*env)->CallObjectMethodA(env, obj = (*env)->CallObjectMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -633,18 +578,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
ret = (*env)->CallStaticIntMethodA( ret = (*env)->CallStaticIntMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
ret = (*env)->CallIntMethodA(env, ret = (*env)->CallIntMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
ret = (*env)->CallIntMethodA(env, ret = (*env)->CallIntMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -663,18 +608,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
ret = (*env)->CallStaticByteMethodA( ret = (*env)->CallStaticByteMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
ret = (*env)->CallByteMethodA(env, ret = (*env)->CallByteMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
ret = (*env)->CallByteMethodA(env, ret = (*env)->CallByteMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -694,18 +639,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
ret = (*env)->CallStaticCharMethodA( ret = (*env)->CallStaticCharMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
ret = (*env)->CallCharMethodA(env, ret = (*env)->CallCharMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
ret = (*env)->CallCharMethodA(env, ret = (*env)->CallCharMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -726,18 +671,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
ret = (*env)->CallStaticShortMethodA( ret = (*env)->CallStaticShortMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
ret = (*env)->CallShortMethodA(env, ret = (*env)->CallShortMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
ret = (*env)->CallShortMethodA(env, ret = (*env)->CallShortMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -756,18 +701,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
ret = (*env)->CallStaticDoubleMethodA( ret = (*env)->CallStaticDoubleMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
ret = (*env)->CallDoubleMethodA(env, ret = (*env)->CallDoubleMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
ret = (*env)->CallDoubleMethodA(env, ret = (*env)->CallDoubleMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -786,18 +731,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
ret = (*env)->CallStaticFloatMethodA( ret = (*env)->CallStaticFloatMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
ret = (*env)->CallFloatMethodA(env, ret = (*env)->CallFloatMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
ret = (*env)->CallFloatMethodA(env, ret = (*env)->CallFloatMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -816,18 +761,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
ret = (*env)->CallStaticLongMethodA( ret = (*env)->CallStaticLongMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
ret = (*env)->CallLongMethodA(env, ret = (*env)->CallLongMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
ret = (*env)->CallLongMethodA(env, ret = (*env)->CallLongMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -846,18 +791,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic) if(self->isStatic)
ret = (*env)->CallStaticBooleanMethodA( ret = (*env)->CallStaticBooleanMethodA(
env, env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else { else {
if(!object->object) if(!instance->object)
ret = (*env)->CallBooleanMethodA(env, ret = (*env)->CallBooleanMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
ret = (*env)->CallBooleanMethodA(env, ret = (*env)->CallBooleanMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
} }
@ -875,21 +820,22 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
// i hereby anoint thee a void method // i hereby anoint thee a void method
if(self->isStatic) if(self->isStatic)
(*env)->CallStaticVoidMethodA(env, (*env)->CallStaticVoidMethodA(env,
object->clazz, instance->clazz,
self->methodId, self->methodId,
jargs); jargs);
else else
(*env)->CallVoidMethodA(env, (*env)->CallVoidMethodA(env,
object->object, instance->object,
self->methodId, self->methodId,
jargs); jargs);
Py_BLOCK_THREADS; Py_BLOCK_THREADS;
process_java_exception(env); process_java_exception(env);
break;
} }
PyMem_Free(jargs); PyMem_Free(jargs);
(*env)->PopLocalFrame(env, NULL); // added by njensen (*env)->PopLocalFrame(env, NULL);
if(PyErr_Occurred()) if(PyErr_Occurred())
return NULL; return NULL;
@ -902,7 +848,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
pyjarray_pin((PyJarray_Object *) param); pyjarray_pin((PyJarray_Object *) param);
} }
} }
if(result == NULL) { if(result == NULL) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
@ -910,7 +856,6 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
return result; return result;
// added by njensen
EXIT_ERROR: EXIT_ERROR:
PyMem_Free(jargs); PyMem_Free(jargs);
(*env)->PopLocalFrame(env, NULL); (*env)->PopLocalFrame(env, NULL);
@ -923,7 +868,7 @@ static PyMethodDef pyjmethod_methods[] = {
}; };
static PyTypeObject PyJmethod_Type = { PyTypeObject PyJmethod_Type = {
PyObject_HEAD_INIT(0) PyObject_HEAD_INIT(0)
0, 0,
"jep.PyJmethod", "jep.PyJmethod",
@ -939,7 +884,7 @@ static PyTypeObject PyJmethod_Type = {
0, /* tp_as_sequence */ 0, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
0, /* tp_hash */ 0, /* tp_hash */
(ternaryfunc) pyjmethod_call, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
0, /* tp_getattro */ 0, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,16 +24,9 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/ */
// shut up the compiler // shut up the compiler
#ifdef _POSIX_C_SOURCE #ifdef _POSIX_C_SOURCE
# undef _POSIX_C_SOURCE # undef _POSIX_C_SOURCE
@ -55,9 +48,6 @@ typedef struct {
PyObject_HEAD PyObject_HEAD
jmethodID methodId; /* resolved methodid */ jmethodID methodId; /* resolved methodid */
jobject rmethod; /* reflect/Method object */ jobject rmethod; /* reflect/Method object */
// commented out by njensen
//PyJobject_Object *pyjobject; /* parent, should point to
// PyJObject_Object */
int returnTypeId; /* type id of return */ int returnTypeId; /* type id of return */
PyObject *pyMethodName; /* python name... :-) */ PyObject *pyMethodName; /* python name... :-) */
jobjectArray parameters; /* array of jclass parameter types */ jobjectArray parameters; /* array of jclass parameter types */

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,12 +24,6 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
New file created with modifications to pyjmethod.c
*/ */
@ -62,46 +56,54 @@
#include "Python.h" #include "Python.h"
#include "pyjmethodwrapper.h" #include "pyjmethodwrapper.h"
#include "util.h" #include "pyjobject.h"
#include "pyembed.h" #include "pyembed.h"
staticforward PyTypeObject PyJmethodWrapper_Type; PyAPI_DATA(PyTypeObject) PyJmethodWrapper_Type;
static void pyjmethod_dealloc(PyJmethod_Object *self); static void pyjmethodwrapper_dealloc(PyJmethodWrapper_Object *self);
/*
PyJmethodWrapper_Object* pyjmethodwrapper_new( * Makes a new pyjmethodwrapper. This is required to reuse the pyjmethod
PyJobject_Object *pyjobject, * objects across multiple instances, otherwise we'd lose track of which
PyJmethod_Object *pyjmethod * pyjobject is doing the calling.
) { */
PyJmethodWrapper_Object *pym = NULL; PyJmethodWrapper_Object* pyjmethodwrapper_new(PyJobject_Object *pyjobject, // the instance doing the calling
PyJmethod_Object *pyjmethod) { // the method to be called
PyJmethodWrapper_Object *pym = NULL;
if(PyType_Ready(&PyJmethodWrapper_Type) < 0) if(PyType_Ready(&PyJmethodWrapper_Type) < 0)
return NULL; return NULL;
pym = PyObject_NEW(PyJmethodWrapper_Object, &PyJmethodWrapper_Type); pym = PyObject_NEW(PyJmethodWrapper_Object, &PyJmethodWrapper_Type);
pym->method = pyjmethod; pym->method = pyjmethod;
pym->object = pyjobject; pym->object = pyjobject;
Py_INCREF(pyjobject); Py_INCREF(pyjobject);
return pym; return pym;
} }
static void pyjmethodwrapper_dealloc(PyJmethodWrapper_Object *self) { static void pyjmethodwrapper_dealloc(PyJmethodWrapper_Object *self) {
#if USE_DEALLOC #if USE_DEALLOC
if(self->object) {
Py_DECREF(self->object);
}
PyObject_Del(self); PyObject_Del(self);
#endif #endif
} }
// called by python for __call__. /*
// we pass off processing to pyjmethod. * Called by python for pyjobject.__call__. Since we reuse pyjmethod objects,
* a unique instance of pyjmethodwrapper is created for every method on a
* pyjobject to ensure the correct instance is used. We then pass off the
* actual invocation to the pyjmethod.
*/
static PyObject* pyjmethodwrapper_call(PyJmethodWrapper_Object *self, static PyObject* pyjmethodwrapper_call(PyJmethodWrapper_Object *self,
PyObject *args, PyObject *args,
PyObject *keywords) { PyObject *keywords) {
PyObject *ret; PyObject *ret;
PyJobject_Object* obj; PyJobject_Object* obj;
if(!PyTuple_Check(args)) { if(!PyTuple_Check(args)) {
PyErr_Format(PyExc_RuntimeError, "args is not a valid tuple"); PyErr_Format(PyExc_RuntimeError, "args is not a valid tuple");
@ -114,8 +116,25 @@ static PyObject* pyjmethodwrapper_call(PyJmethodWrapper_Object *self,
} }
obj = self->object; obj = self->object;
// pyjobject_find_method will actually call the method
ret = pyjobject_find_method(obj, self->method->pyMethodName, args); ret = pyjobject_find_method(obj, self->method->pyMethodName, args);
Py_XDECREF(obj);
/*
* This seems odd to Py_DECREF(self), but we had to Py_INCREF it
* over in pyjobject_getattr so that the garbage collector doesn't
* collect it before we even got to __call__ it.
*
* And now we want to make sure it gets cleaned up and can't tell if
* it will be reused. If it is going to be reused, such as stored in
* a variable and repeatedly called, we're still safe. For example:
*
* m = obj.doIt
* while condition:
* m()
*
* As long as m is in scope, there will still be a reference so it won't
* get garbage collected until m goes out of scope.
*/
Py_DECREF(self); Py_DECREF(self);
return ret; return ret;
@ -127,13 +146,13 @@ static PyMethodDef pyjmethodwrapper_methods[] = {
}; };
static PyTypeObject PyJmethodWrapper_Type = { PyTypeObject PyJmethodWrapper_Type = {
PyObject_HEAD_INIT(0) PyObject_HEAD_INIT(0)
0, 0,
"jep.PyJmethodWrapper", "jep.PyJmethodWrapper",
sizeof(PyJmethodWrapper_Object), sizeof(PyJmethodWrapper_Object),
0, 0,
(destructor) pyjmethodwrapper_dealloc, /* tp_dealloc */ (destructor) pyjmethodwrapper_dealloc, /* tp_dealloc */
0, /* tp_print */ 0, /* tp_print */
0, /* tp_getattr */ 0, /* tp_getattr */
0, /* tp_setattr */ 0, /* tp_setattr */
@ -143,20 +162,20 @@ static PyTypeObject PyJmethodWrapper_Type = {
0, /* tp_as_sequence */ 0, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
0, /* tp_hash */ 0, /* tp_hash */
(ternaryfunc) pyjmethodwrapper_call, /* tp_call */ (ternaryfunc) pyjmethodwrapper_call, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
0, /* tp_getattro */ 0, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */ Py_TPFLAGS_DEFAULT, /* tp_flags */
"jmethodwrapper", /* tp_doc */ "jmethodwrapper", /* tp_doc */
0, /* tp_traverse */ 0, /* tp_traverse */
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
0, /* tp_iter */ 0, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
pyjmethodwrapper_methods, /* tp_methods */ pyjmethodwrapper_methods, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
0, /* tp_getset */ 0, /* tp_getset */
0, /* tp_base */ 0, /* tp_base */

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP_AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,12 +24,6 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
New file created with modifications to pyjmethod.h
*/ */
@ -48,14 +42,23 @@
#include "pyjobject.h" #include "pyjobject.h"
#include "pyjmethod.h" #include "pyjmethod.h"
/*
* PyJmethodWrapper_Object enables the ability to reuse a pyjmethod for
* multiple instances of pyjobjects of the same underlying Java type.
*
* Pyjmethods are tied to java.lang.Methods, which are tied
* to java.lang.Classes, which are shared across all instances of a particular
* Class. To ensure the right object is called with the method, the pyjmethod
* wrapper includes both the pyjobject instance doing the calling and the
* pyjmethod to be called.
*/
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
PyJmethod_Object *method; PyJmethod_Object *method; /* the original pyjmethod tied to a java.lang.reflect.Method */
PyJobject_Object *object; PyJobject_Object *object; /* the pyjobject that called this method */
} PyJmethodWrapper_Object; } PyJmethodWrapper_Object;
PyJmethodWrapper_Object* pyjmethodwrapper_new( PyJmethodWrapper_Object* pyjmethodwrapper_new(PyJobject_Object*,
PyJobject_Object*, PyJmethod_Object*); PyJmethod_Object*);
#endif // ndef pyjmethodwrapper #endif // ndef pyjmethodwrapper

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2011 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,7 +24,7 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/ */
#ifdef WIN32 #ifdef WIN32
# include "winconfig.h" # include "winconfig.h"
@ -235,17 +235,18 @@ static int pyjobject_init(JNIEnv *env, PyJobject_Object *pyjob) {
* dictionary at the same time. * dictionary at the same time.
*/ */
lock = (*env)->FindClass(env, "java/lang/String"); lock = (*env)->FindClass(env, "java/lang/String");
if((*env)->MonitorEnter(env, lock) != JNI_OK) { if((*env)->MonitorEnter(env, lock) != JNI_OK) {
PyErr_Format(PyExc_RuntimeError, "Couldn't get synchronization lock on class method creation."); PyErr_Format(PyExc_RuntimeError,
} "Couldn't get synchronization lock on class method creation.");
}
if(classnamePyJMethodsDict == NULL) { if(classnamePyJMethodsDict == NULL) {
classnamePyJMethodsDict = PyDict_New(); classnamePyJMethodsDict = PyDict_New();
} }
cachedMethodList = PyDict_GetItem(classnamePyJMethodsDict, pyClassName); cachedMethodList = PyDict_GetItem(classnamePyJMethodsDict, pyClassName);
if(cachedMethodList == NULL) { if(cachedMethodList == NULL) {
PyObject *pyjMethodList = NULL; PyObject *pyjMethodList = NULL;
pyjMethodList = PyList_New(0); pyjMethodList = PyList_New(0);
// - GetMethodID fails when you pass the clazz object, it expects // - GetMethodID fails when you pass the clazz object, it expects
// a java.lang.Class jobject. // a java.lang.Class jobject.
@ -256,23 +257,20 @@ static int pyjobject_init(JNIEnv *env, PyJobject_Object *pyjob) {
// so what i did here was find the methodid using langClass, // so what i did here was find the methodid using langClass,
// but then i call the method using clazz. methodIds for java // but then i call the method using clazz. methodIds for java
// classes are shared.... // classes are shared....
methodArray = (jobjectArray) (*env)->CallObjectMethod(env, methodArray = (jobjectArray) (*env)->CallObjectMethod(env, pyjob->clazz,
pyjob->clazz, classGetMethods);
classGetMethods);
if(process_java_exception(env) || !methodArray) if(process_java_exception(env) || !methodArray)
goto EXIT_ERROR; goto EXIT_ERROR;
// for each method, create a new pyjmethod object // for each method, create a new pyjmethod object
// and add to the internal methods list. // and add to the internal methods list.
len = (*env)->GetArrayLength(env, methodArray); len = (*env)->GetArrayLength(env, methodArray);
for(i = 0; i < len; i++) { for (i = 0; i < len; i++) {
PyJmethod_Object *pymethod = NULL; PyJmethod_Object *pymethod = NULL;
jobject rmethod = NULL; jobject rmethod = NULL;
rmethod = (*env)->GetObjectArrayElement(env, rmethod = (*env)->GetObjectArrayElement(env, methodArray, i);
methodArray,
i);
// make new PyJmethod_Object, linked to pyjob // make new PyJmethod_Object, linked to pyjob
if(pyjob->object) if(pyjob->object)
@ -284,8 +282,8 @@ static int pyjobject_init(JNIEnv *env, PyJobject_Object *pyjob) {
continue; continue;
if(pymethod->pyMethodName && PyString_Check(pymethod->pyMethodName)) { if(pymethod->pyMethodName && PyString_Check(pymethod->pyMethodName)) {
if(PyList_Append(pyjMethodList, (PyObject*) pymethod) != 0) if(PyList_Append(pyjMethodList, (PyObject*) pymethod) != 0)
printf("WARNING: couldn't add method"); printf("WARNING: couldn't add method");
} }
Py_DECREF(pymethod); Py_DECREF(pymethod);
@ -296,20 +294,23 @@ static int pyjobject_init(JNIEnv *env, PyJobject_Object *pyjob) {
(*env)->DeleteLocalRef(env, methodArray); (*env)->DeleteLocalRef(env, methodArray);
} // end of setting up cache for this Java Class } // end of setting up cache for this Java Class
if((*env)->MonitorExit(env, lock) != JNI_OK) { if((*env)->MonitorExit(env, lock) != JNI_OK) {
PyErr_Format(PyExc_RuntimeError, "Couldn't release synchronization lock on class method creation."); PyErr_Format(PyExc_RuntimeError,
"Couldn't release synchronization lock on class method creation.");
} }
// end of synchronization // end of synchronization
len = PyList_Size(cachedMethodList); len = PyList_Size(cachedMethodList);
for(i = 0; i < len; i++) { for (i = 0; i < len; i++) {
PyJmethod_Object* pymethod = (PyJmethod_Object*) PyList_GetItem(cachedMethodList, i); PyJmethod_Object* pymethod = (PyJmethod_Object*) PyList_GetItem(
if(PyObject_SetAttr((PyObject *) pyjob, pymethod->pyMethodName, (PyObject*) pymethod) != 0) { cachedMethodList, i);
PyErr_Format(PyExc_RuntimeError, "Couldn't add method as attribute."); if(PyObject_SetAttr((PyObject *) pyjob, pymethod->pyMethodName,
} (PyObject*) pymethod) != 0) {
else { PyErr_Format(PyExc_RuntimeError,
pyjobject_addmethod(pyjob, pymethod->pyMethodName); "Couldn't add method as attribute.");
} } else {
} // end of cached method optimizations pyjobject_addmethod(pyjob, pymethod->pyMethodName);
}
} // end of cached method optimizations
// ------------------------------ process fields // ------------------------------ process fields
@ -773,10 +774,10 @@ PyObject* pyjobject_getattr(PyJobject_Object *obj,
// method optimizations // method optimizations
if(pyjmethod_check(ret)) if(pyjmethod_check(ret))
{ {
PyJmethodWrapper_Object *wrapper = pyjmethodwrapper_new(obj, (PyJmethod_Object*) ret); PyJmethodWrapper_Object *wrapper = pyjmethodwrapper_new(obj, (PyJmethod_Object*) ret);
Py_DECREF(ret); Py_DECREF(ret);
Py_INCREF(wrapper); Py_INCREF(wrapper);
ret = (PyObject *) wrapper; ret = (PyObject *) wrapper;
} }
if(PyErr_Occurred() || ret == Py_None) { if(PyErr_Occurred() || ret == Py_None) {
@ -882,7 +883,7 @@ static PyMethodDef pyjobject_methods[] = {
}; };
static PyTypeObject PyJobject_Type = { PyTypeObject PyJobject_Type = {
PyObject_HEAD_INIT(0) PyObject_HEAD_INIT(0)
0, /* ob_size */ 0, /* ob_size */
"PyJobject", /* tp_name */ "PyJobject", /* tp_name */
@ -908,7 +909,7 @@ static PyTypeObject PyJobject_Type = {
"jobject", /* tp_doc */ "jobject", /* tp_doc */
0, /* tp_traverse */ 0, /* tp_traverse */
0, /* tp_clear */ 0, /* tp_clear */
pyjobject_richcompare, /* tp_richcompare */ (richcmpfunc) pyjobject_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
0, /* tp_iter */ 0, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2008 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,16 +24,9 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/ */
// shut up the compiler // shut up the compiler
#ifdef _POSIX_C_SOURCE #ifdef _POSIX_C_SOURCE
# undef _POSIX_C_SOURCE # undef _POSIX_C_SOURCE
@ -59,16 +52,21 @@ typedef struct {
PyObject *methods; /* list of method names */ PyObject *methods; /* list of method names */
PyObject *fields; /* list of field names */ PyObject *fields; /* list of field names */
int finishAttr; /* true if object attributes are finished */ int finishAttr; /* true if object attributes are finished */
PyObject *jclassname; // added by njensen PyObject *javaClassName; /* string of the fully-qualified name of
the object's Java clazz */
} PyJobject_Object; } PyJobject_Object;
PyAPI_DATA(PyTypeObject) PyJobject_Type;
PyObject* pyjobject_new(JNIEnv*, jobject); PyObject* pyjobject_new(JNIEnv*, jobject);
PyObject* pyjobject_new_class(JNIEnv*, jclass); PyObject* pyjobject_new_class(JNIEnv*, jclass);
PyObject* pyjobject_find_method(PyJobject_Object*, PyObject*, PyObject*); PyObject* pyjobject_find_method(PyJobject_Object*, PyObject*, PyObject*);
int pyjobject_check(PyObject *obj); int pyjobject_check(PyObject *obj);
//added by njensen // these methods need to be available to pyjlist
static PyObject* pyjobject_numpy(PyJobject_Object *obj); int pyjobject_setattr(PyJobject_Object*, char*, PyObject*);
PyObject* pyjobject_getattr(PyJobject_Object*, char*);
void pyjobject_dealloc(PyJobject_Object*);
PyObject* pyjobject_str(PyJobject_Object*);
#endif // ndef pyjobject #endif // ndef pyjobject

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2011 Mike Johnson. Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,7 +24,7 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/ */
#ifdef WIN32 #ifdef WIN32
# include "winconfig.h" # include "winconfig.h"
@ -1302,7 +1302,7 @@ int pyarg_matches_jtype(JNIEnv *env,
break; break;
case JBOOLEAN_ID: case JBOOLEAN_ID:
if(PyInt_Check(param)) if(PyBool_Check(param))
return 1; return 1;
break; break;
} }
@ -1891,9 +1891,9 @@ jarray convert_pyndarray_jprimitivearray(JNIEnv* env,
} else if(paType == NPY_FLOAT64) { } else if(paType == NPY_FLOAT64) {
desiredType = JDOUBLE_ARRAY_TYPE; desiredType = JDOUBLE_ARRAY_TYPE;
} else { } else {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"Unable to determine corresponding Java type for ndarray"); "Unable to determine corresponding Java type for ndarray");
return NULL; return NULL;
} }
} }
@ -1925,11 +1925,11 @@ jarray convert_pyndarray_jprimitivearray(JNIEnv* env,
&& (paType == NPY_FLOAT64)) { && (paType == NPY_FLOAT64)) {
arr = (*env)->NewDoubleArray(env, sz); arr = (*env)->NewDoubleArray(env, sz);
} else { } else {
if(copy) if(copy)
Py_DECREF(copy); Py_DECREF(copy);
PyErr_Format(PyExc_RuntimeError, PyErr_Format(PyExc_RuntimeError,
"Error matching ndarray.dtype to Java primitive type"); "Error matching ndarray.dtype to Java primitive type");
return NULL; return NULL;
} }
/* /*
@ -1937,8 +1937,8 @@ jarray convert_pyndarray_jprimitivearray(JNIEnv* env,
* couldn't allocate the array * couldn't allocate the array
*/ */
if(process_java_exception(env) || !arr) { if(process_java_exception(env) || !arr) {
if(copy) if(copy)
Py_DECREF(copy); Py_DECREF(copy);
return NULL; return NULL;
} }
@ -1960,11 +1960,11 @@ jarray convert_pyndarray_jprimitivearray(JNIEnv* env,
} }
if(copy) if(copy)
Py_DECREF(copy); Py_DECREF(copy);
if(process_java_exception(env)) { if(process_java_exception(env)) {
PyErr_Format(PyExc_RuntimeError, "Error setting Java primitive array region"); PyErr_Format(PyExc_RuntimeError, "Error setting Java primitive array region");
return NULL; return NULL;
} }
return arr; return arr;
@ -2012,8 +2012,8 @@ jobject convert_pyndarray_jndarray(JNIEnv *env, PyObject *pyobj) {
jdimObj = (*env)->NewIntArray(env, ndims); jdimObj = (*env)->NewIntArray(env, ndims);
if(process_java_exception(env) || !jdimObj) { if(process_java_exception(env) || !jdimObj) {
free(jdims); free(jdims);
return NULL; return NULL;
} }
(*env)->SetIntArrayRegion(env, jdimObj, 0, ndims, jdims); (*env)->SetIntArrayRegion(env, jdimObj, 0, ndims, jdims);
@ -2105,7 +2105,7 @@ PyObject* convert_jprimitivearray_pyndarray(JNIEnv *env,
return pyjob; return pyjob;
} }
/** /*
* Converts a jep.NDArray to a numpy ndarray. * Converts a jep.NDArray to a numpy ndarray.
* *
* @param env the JNI environment * @param env the JNI environment

View file

@ -2,7 +2,7 @@
/* /*
jep - Java Embedded Python jep - Java Embedded Python
Copyright (c) 2004 - 2011 Mike Johnson. Copyright (c) JEP AUTHORS.
This file is licenced under the the zlib/libpng License. This file is licenced under the the zlib/libpng License.
@ -24,7 +24,7 @@
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*/ */
// shut up the compiler // shut up the compiler