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;
/**
* <pre>
* 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
* 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.
*
*
* This file is licenced under the the zlib/libpng License.
*
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* 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
* distribution.
*
*
* Created: Thurs Apr 02 2015
*
*
* </pre>
*
*
* @author [ndjensen at gmail.com] Nate Jensen
* @version $Id$
*/
public interface ClassEnquirer {
/**
* Checks if the name is likely available in Java. A return
* value of true implies the name corresponds to a Java package or class, but
* does not guarantee that an import will succeed. A return value of false
* implies that an import from Java would fail, but does not guarantee that an
* import will fail.
*
* @param name the name to check, such as java, java.util, java.util.ArrayList
* @return true if it's likely supported by Java, false if it's likely python
*/
public boolean contains(String name);
/**
* Whether or not this ClassEnquirer supports importing Java classes at the
* 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:
*
* <p><blockquote><pre>
* from java.util import ArrayList
* o = ArrayList()
* </p></blockquote></pre>
*
* However, only in some scenarios can the package be imported separately
* without the fully qualified name, such as:
*
* <p><blockquote><pre>
* 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();
/**
* Checks if the name is likely available in Java. A return value of true
* implies the name corresponds to a Java package or class, but does not
* guarantee that an import will succeed. A return value of false implies
* that an import from Java would fail, but does not guarantee that an
* import will fail.
*
* @param name
* the name to check, such as java, java.util,
* java.util.ArrayList
* @return true if it's likely supported by Java, false if it's likely
* python
*/
public boolean contains(String name);
/**
* Whether or not this ClassEnquirer supports importing Java classes at the
* 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:
*
* <p>
* <blockquote>
*
* <pre>
* from java.util import ArrayList
* o = ArrayList()
*
* </p>
* </blockquote></pre>
*
* However, only in some scenarios can the package be imported separately
* without the fully qualified name, such as:
*
* <p>
* <blockquote>
*
* <pre>
* 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$
*/
public class ClassList implements ClassEnquirer {
private static ClassList inst;
// storage for package, members
@ -289,7 +289,7 @@ public class ClassList implements ClassEnquirer {
@Override
public boolean supportsPackageImport() {
return true;
return true;
}

View file

@ -9,7 +9,7 @@ import jep.python.*;
* <pre>
* 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.
*
@ -36,7 +36,7 @@ import jep.python.*;
* </pre>
*
* @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id: $
* @version $Id: $
*/
public class InvocationHandler implements java.lang.reflect.InvocationHandler {

View file

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

View file

@ -4,7 +4,7 @@ package jep;
* <pre>
* 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.
*
@ -31,7 +31,7 @@ package jep;
*
* </pre>
* @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 {

View file

@ -19,7 +19,7 @@ import jep.python.PyModule;
* <pre>
* 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.
*
@ -47,7 +47,7 @@ import jep.python.PyModule;
* </pre>
*
* @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 {
private Jep jep = null;

View file

@ -13,7 +13,7 @@ import javax.script.ScriptException;
* <pre>
* 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.
*
@ -41,7 +41,7 @@ import javax.script.ScriptException;
* </pre>
*
* @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 {

View file

@ -10,7 +10,7 @@ import java.lang.reflect.InvocationHandler;
* <pre>
* 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.
*
@ -38,7 +38,7 @@ import java.lang.reflect.InvocationHandler;
* </pre>
*
* @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id: $
* @version $Id: $
*/
public class Proxy extends java.lang.reflect.Proxy {

View file

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

View file

@ -13,20 +13,38 @@ import jep.python.*;
* Created: Fri Apr 30 12:42:58 2004
*
* @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 {
private Jep jep = null;
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() {
} // Test constructor
}
public Test(boolean testEval) {
this.testEval = testEval;
}
public static enum TestEnum {
One,
Two
}
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
public Jep getJep() {
return this.jep;
@ -160,33 +173,9 @@ public class Test implements Runnable {
return "toString(). Thanks for calling Java(tm).";
}
public int getInt() {
return 2147483647;
}
public byte getByte() {
return 123;
}
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 TestEnum getEnum() {
return TestEnum.One;
}
public Integer getInteger() {
@ -279,6 +268,86 @@ public class Test implements Runnable {
public byte byteField = 43;
public char charField = 'c';
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
@ -296,35 +365,81 @@ public class Test implements Runnable {
// -------------------------------------------------- static methods
public static String getStaticString() {
return "a static string.";
}
public static boolean getStaticBoolean() {
return false;
}
public static int getStaticInt() {
return 123;
public static boolean isStaticBoolean() {
return staticBoolean;
}
public static short getStaticShort() {
return 321;
public static void setStaticBoolean(boolean staticBoolean) {
Test.staticBoolean = staticBoolean;
}
public static long getStaticLong() {
return 9223372036854775807L;
public static byte getStaticByte() {
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() {
return 123123213.123D;
return staticDouble;
}
public static void setStaticDouble(double staticDouble) {
Test.staticDouble = staticDouble;
}
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() {
return new Object();
}
@ -333,33 +448,17 @@ public class Test implements Runnable {
return;
}
public static byte getStaticByte() {
return 23;
}
public static char getStaticChar() {
return 'b';
}
public static Class getStaticClass() {
return Thread.currentThread().getClass();
}
public static void main(String argv[]) throws Throwable {
if(argv.length < 1) {
new Thread(new Test()).start();
new Thread(new Test()).start();
Jep jep = new Jep();
try {
jep.runScript("runtests.py");
}
else {
int count = Integer.parseInt(argv[0]);
for(int i = 0; i < count; i++)
new Thread(new Test()).start();
finally {
jep.close();
}
new Test().run();
System.gc();
}
} // Test

View file

@ -8,7 +8,10 @@ import java.util.concurrent.Executors;
* ndarrays, and closing/disposing interpreters. This demonstrates the issue
* where numpy's array2string or array_str reference is lost.
*
* Created: Autumn 2013
*
* @author David Gillingham
* @version $Id$
*/
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
* 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
* @version $Id$
@ -28,7 +30,7 @@ public class TestNumpyGILFreeze {
*/
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");
*/
createJepAndUseNumpy();

View file

@ -1,13 +1,13 @@
package jep;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* <pre>
* 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 software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* 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
* distribution.
*
* Created: Fri Dec 22 13:21:02 2006
* </pre>
*
*
* @author [mrjohnson0 at sourceforge.net] Mike Johnson
* @version $Id: $
*/
@ -36,159 +37,135 @@ public final class Util {
// these must be the same as util.h
public static final int JBOOLEAN_ID = 0;
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 JSTRING_ID = 4;
public static final int JVOID_ID = 5;
public static final int JDOUBLE_ID = 6;
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;
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 JSTRING_ID = 4;
public static final int JVOID_ID = 5;
public static final int JDOUBLE_ID = 6;
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() {
}
/**
* <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
* stable to do this in java when able.
*
* </pre>
*
* @param obj
* an <code>Object</code> value
*
* @param obj an <code>Object</code> value
* @return an <code>int</code> one of the type _ID constants
*/
public static final int getTypeId(Object obj) {
if (obj == null)
if(obj == null)
return -1;
if (obj instanceof Integer)
if(obj instanceof Integer)
return JINT_ID;
if (obj instanceof Short)
if(obj instanceof Short)
return JSHORT_ID;
if (obj instanceof Double)
if(obj instanceof Double)
return JDOUBLE_ID;
if (obj instanceof Float)
if(obj instanceof Float)
return JFLOAT_ID;
if (obj instanceof Boolean)
if(obj instanceof Boolean)
return JBOOLEAN_ID;
if (obj instanceof Long)
if(obj instanceof Long)
return JLONG_ID;
if (obj instanceof String)
if(obj instanceof String)
return JSTRING_ID;
if (obj instanceof Void)
if(obj instanceof Void)
return JVOID_ID;
if (obj instanceof Character)
if(obj instanceof Character)
return JCHAR_ID;
if (obj instanceof Byte)
if(obj instanceof Byte)
return JBYTE_ID;
if (obj instanceof Class)
if(obj instanceof Class)
return JCLASS_ID;
Class clazz = obj.getClass();
if (clazz.isArray())
if(clazz.isArray())
return JARRAY_ID;
return JOBJECT_ID;
}
/**
* <pre>
* &lt;b&gt;Internal use only&lt;/b&gt;
* Same as
* <code>
* getTypeId(Object)
* </code>
* but for Class. This is
*
* <b>Internal use only</b>
*
* Same as <code>getTypeId(Object)</code> but for Class. This is
* useful for determining the _ID for things like
* method.getReturnType.
*
* </pre>
*
* @param obj
* an <code>Object</code> value
*
* @param obj an <code>Object</code> value
* @return an <code>int</code> one of the type _ID constants
*/
public static final int getTypeId(Class<?> clazz) {
if (clazz == null)
if(clazz == null)
return -1;
if (clazz.isAssignableFrom(Integer.class))
if(clazz.isAssignableFrom(Integer.class))
return JINT_ID;
if (clazz.isAssignableFrom(Short.class))
if(clazz.isAssignableFrom(Short.class))
return JSHORT_ID;
if (clazz.isAssignableFrom(Double.class))
if(clazz.isAssignableFrom(Double.class))
return JDOUBLE_ID;
if (clazz.isAssignableFrom(Float.class))
if(clazz.isAssignableFrom(Float.class))
return JFLOAT_ID;
if (clazz.isAssignableFrom(Boolean.class))
if(clazz.isAssignableFrom(Boolean.class))
return JBOOLEAN_ID;
if (clazz.isAssignableFrom(Long.class))
if(clazz.isAssignableFrom(Long.class))
return JLONG_ID;
if (clazz.isAssignableFrom(String.class))
if(clazz.isAssignableFrom(String.class))
return JSTRING_ID;
if (clazz.isAssignableFrom(Void.class))
if(clazz.isAssignableFrom(Void.class))
return JVOID_ID;
if (clazz.isAssignableFrom(Character.class))
if(clazz.isAssignableFrom(Character.class))
return JCHAR_ID;
if (clazz.isAssignableFrom(Byte.class))
if(clazz.isAssignableFrom(Byte.class))
return JBYTE_ID;
if (clazz.isAssignableFrom(Class.class))
if(clazz.isAssignableFrom(Class.class))
return JCLASS_ID;
if (clazz.isArray())
if(clazz.isArray())
return JARRAY_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
Copyright (c) 2004 - 2008 Mike Johnson.
Copyright (c) 2015 JEP AUTHORS.
This file is licenced under the the zlib/libpng License.

View file

@ -2,7 +2,7 @@
/*
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.
@ -26,12 +26,6 @@
distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/
#include "util.h"
#include "jep.h"
#include "pyembed.h"
@ -43,7 +37,7 @@
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved) {
return TRUE;
return TRUE;
}
#endif
@ -85,7 +79,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_run
const char *filename;
filename = jstring2char(env, str);
pyembed_run(env, tstate, (char *) filename);
pyembed_run(env, (intptr_t) tstate, (char *) filename);
release_utf_char(env, str, filename);
}
@ -124,7 +118,7 @@ JNIEXPORT jint JNICALL Java_jep_Jep_compileString
jint ret;
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);
return ret;
}
@ -140,7 +134,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_eval
const char *str;
str = jstring2char(env, jstr);
pyembed_eval(env, tstate, (char *) str);
pyembed_eval(env, (intptr_t) tstate, (char *) str);
release_utf_char(env, jstr, str);
}
@ -156,7 +150,7 @@ JNIEXPORT jobject JNICALL Java_jep_Jep_getValue
jobject ret;
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);
return ret;
}
@ -173,7 +167,7 @@ JNIEXPORT jobject JNICALL Java_jep_Jep_getValue_1floatarray
jobject ret;
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);
return ret;
}
@ -190,7 +184,7 @@ JNIEXPORT jobject JNICALL Java_jep_Jep_getValue_1bytearray
jobject ret;
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);
return ret;
}
@ -208,7 +202,7 @@ JNIEXPORT jlong JNICALL Java_jep_Jep_createModule
jlong ret;
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);
return ret;
}
@ -221,7 +215,7 @@ JNIEXPORT jlong JNICALL Java_jep_Jep_createModule
*/
JNIEXPORT void JNICALL Java_jep_Jep_setClassLoader
(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
(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;
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);
}
@ -263,7 +257,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2Ljava_lang_Class_2
const char *name;
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);
}
@ -279,7 +273,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2Ljava_lang_String_2
name = jstring2char(env, jname);
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, jval, value);
}
@ -295,7 +289,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2I
const char *name;
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);
}
@ -310,7 +304,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2J
const char *name;
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);
}
@ -325,7 +319,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2D
const char *name;
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);
}
@ -340,7 +334,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2F
const char *name;
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);
}
@ -355,7 +349,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3Z
const char *name;
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);
}
@ -370,7 +364,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3I
const char *name;
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);
}
@ -385,7 +379,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3S
const char *name;
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);
}
@ -400,7 +394,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3B
const char *name;
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);
}
@ -415,7 +409,7 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3J
const char *name;
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);
}
@ -430,57 +424,10 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3D
const char *name;
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);
}
/*
* 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
@ -492,6 +439,6 @@ JNIEXPORT void JNICALL Java_jep_Jep_set__JLjava_lang_String_2_3F
const char *name;
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);
}

View file

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

View file

@ -2,7 +2,7 @@
/*
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.
@ -24,15 +24,8 @@
3. This notice may not be removed or altered from any source
distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/
#ifndef _Included_pyembed
#define _Included_pyembed
@ -52,7 +45,6 @@
#include "Python.h"
#include "util.h"
#include "pyjobject.h"
#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_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

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,7 @@
/*
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.
@ -23,16 +23,9 @@
3. This notice may not be removed or altered from any source
distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/
// shut up the compiler
#ifdef _POSIX_C_SOURCE
# undef _POSIX_C_SOURCE

View file

@ -2,7 +2,7 @@
/*
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.
@ -24,7 +24,7 @@
3. This notice may not be removed or altered from any source
distribution.
*/
*/
#ifdef WIN32
# include "winconfig.h"
@ -62,7 +62,7 @@
#include "pyjarray.h"
#include "util.h"
staticforward PyTypeObject PyJclass_Type;
PyAPI_DATA(PyTypeObject) PyJclass_Type;
static PyObject* pyjclass_add_inner_classes(JNIEnv*, jobject, PyJobject_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)
0,
"PyJclass",

View file

@ -2,7 +2,7 @@
/*
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.
@ -24,7 +24,7 @@
3. This notice may not be removed or altered from any source
distribution.
*/
*/
// shut up the compiler
#ifdef _POSIX_C_SOURCE

View file

@ -2,7 +2,7 @@
/*
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.
@ -29,7 +29,7 @@
Fields really don't have to be represented as python objects,
but it is nice to have garbage collection and to be able
to cast to PyObject.
*/
*/
#ifdef WIN32
# include "winconfig.h"
@ -65,7 +65,7 @@
#include "pyjclass.h"
#include "util.h"
staticforward PyTypeObject PyJfield_Type;
PyAPI_DATA(PyTypeObject) PyJfield_Type;
static void pyjfield_dealloc(PyJfield_Object *self);
@ -258,8 +258,9 @@ static void pyjfield_dealloc(PyJfield_Object *self) {
(*env)->DeleteGlobalRef(env, self->rfield);
}
if(self->pyFieldName)
if(self->pyFieldName) {
Py_DECREF(self->pyFieldName);
}
PyObject_Del(self);
#endif
@ -916,7 +917,7 @@ static PyMethodDef pyjfield_methods[] = {
};
static PyTypeObject PyJfield_Type = {
PyTypeObject PyJfield_Type = {
PyObject_HEAD_INIT(0)
0,
"PyJfield",

View file

@ -2,7 +2,7 @@
/*
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.
@ -24,7 +24,7 @@
3. This notice may not be removed or altered from any source
distribution.
*/
*/
// shut up the compiler

View file

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

View file

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

View file

@ -2,7 +2,7 @@
/*
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.
@ -24,15 +24,8 @@
3. This notice may not be removed or altered from any source
distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/
#ifdef WIN32
# include "winconfig.h"
#endif
@ -67,7 +60,7 @@
#include "util.h"
#include "pyembed.h"
staticforward PyTypeObject PyJmethod_Type;
PyAPI_DATA(PyTypeObject) PyJmethod_Type;
static void pyjmethod_dealloc(PyJmethod_Object *self);
@ -75,7 +68,6 @@ static void pyjmethod_dealloc(PyJmethod_Object *self);
static jmethodID classGetName = 0;
static jmethodID methodGetType = 0;
static jmethodID methodGetParmTypes = 0;
static jmethodID methodGetExceptions = 0;
static jmethodID methodGetModifiers = 0;
// 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->rmethod = (*env)->NewGlobalRef(env, rmethod);
// pym->pyjobject = pyjobject;
pym->parameters = NULL;
pym->lenParameters = 0;
pym->pyMethodName = NULL;
pym->isStatic = -1;
pym->returnTypeId = -1;
// ------------------------------ get method name
rmethodClass = (*env)->GetObjectClass(env, rmethod);
@ -152,7 +143,6 @@ PyJmethod_Object* pyjmethod_new_static(JNIEnv *env,
pym = PyObject_NEW(PyJmethod_Object, &PyJmethod_Type);
pym->rmethod = (*env)->NewGlobalRef(env, rmethod);
// pym->pyjobject = pyjobject;
pym->parameters = NULL;
pym->lenParameters = 0;
pym->pyMethodName = NULL;
@ -204,12 +194,11 @@ int pyjmethod_init(JNIEnv *env, PyJmethod_Object *self) {
jmethodID methodId;
jobject returnType = NULL;
jobjectArray paramArray = NULL;
jobjectArray exceptions = NULL;
jclass modClass = NULL;
jint modifier = -1;
jboolean isStatic = JNI_FALSE;
jclass rmethodClass = NULL;
// 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
(*env)->PushLocalFrame(env, 20);
@ -346,8 +335,9 @@ static void pyjmethod_dealloc(PyJmethod_Object *self) {
if(self->rmethod)
(*env)->DeleteGlobalRef(env, self->rmethod);
if(self->pyMethodName)
if(self->pyMethodName) {
Py_DECREF(self->pyMethodName);
}
}
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.
//
// 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. :-)
PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
PyJobject_Object *object, // added by njensen
PyJobject_Object *instance,
PyObject *args) {
PyObject *result = NULL;
const char *str = NULL;
@ -426,11 +375,11 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(!self->parameters) {
if(!pyjmethod_init(env, self) || PyErr_Occurred())
return NULL;
return pyjmethod_call_internal(self, object, args);
return pyjmethod_call_internal(self, instance, args);
}
// validate we can call this method
if(!object->object && self->isStatic != JNI_TRUE) {
if(!instance->object && self->isStatic != JNI_TRUE) {
PyErr_Format(PyExc_RuntimeError,
"Instantiate this class before "
"calling an object method.");
@ -441,7 +390,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->lenParameters != PyTuple_GET_SIZE(args)) {
PyErr_Format(PyExc_RuntimeError,
"Invalid number of arguments: %i, expected %i.",
PyTuple_GET_SIZE(args),
(int) PyTuple_GET_SIZE(args),
self->lenParameters);
return NULL;
}
@ -450,26 +399,22 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
// ------------------------------ 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);
for(pos = 0; pos < self->lenParameters; pos++) {
PyObject *param = NULL;
int paramTypeId = -1;
jclass pclazz;
jclass paramType =
(jclass) (*env)->GetObjectArrayElement(env,
self->parameters,
pos);
PyObject *param = NULL;
int paramTypeId = -1;
jclass pclazz = NULL;
jclass paramType = (jclass) (*env)->GetObjectArrayElement(env,
self->parameters, pos);
param = PyTuple_GetItem(args, pos); /* borrowed */
if(PyErr_Occurred()) { /* borrowed */
// changed by njensen
goto EXIT_ERROR;
}
pclazz = (*env)->GetObjectClass(env, paramType);
if(process_java_exception(env) || !pclazz) {
// changed by njensen
goto EXIT_ERROR;
}
@ -485,7 +430,6 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
paramTypeId,
pos);
if(PyErr_Occurred()) { /* borrowed */
// changed by njensen
goto EXIT_ERROR;
}
@ -494,6 +438,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
// ------------------------------ call based off return type
switch(self->returnTypeId) {
case JSTRING_ID: {
@ -503,21 +448,21 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
jstr = (jstring) (*env)->CallStaticObjectMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
// not static, a method on class then.
if(!object->object)
if(!instance->object)
jstr = (jstring) (*env)->CallObjectMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
jstr = (jstring) (*env)->CallObjectMethodA(
env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -541,20 +486,20 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
obj = (jobjectArray) (*env)->CallStaticObjectMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
obj = (jobjectArray) (*env)->CallObjectMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
obj = (jobjectArray) (*env)->CallObjectMethodA(
env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -573,18 +518,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
obj = (*env)->CallStaticObjectMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
obj = (*env)->CallObjectMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
obj = (*env)->CallObjectMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -603,18 +548,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
obj = (*env)->CallStaticObjectMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
obj = (*env)->CallObjectMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
obj = (*env)->CallObjectMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -633,18 +578,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
ret = (*env)->CallStaticIntMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
ret = (*env)->CallIntMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallIntMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -663,18 +608,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
ret = (*env)->CallStaticByteMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
ret = (*env)->CallByteMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallByteMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -694,18 +639,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
ret = (*env)->CallStaticCharMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
ret = (*env)->CallCharMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallCharMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -726,18 +671,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
ret = (*env)->CallStaticShortMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
ret = (*env)->CallShortMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallShortMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -756,18 +701,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
ret = (*env)->CallStaticDoubleMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
ret = (*env)->CallDoubleMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallDoubleMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -786,18 +731,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
ret = (*env)->CallStaticFloatMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
ret = (*env)->CallFloatMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallFloatMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -816,18 +761,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
ret = (*env)->CallStaticLongMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
ret = (*env)->CallLongMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallLongMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -846,18 +791,18 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
if(self->isStatic)
ret = (*env)->CallStaticBooleanMethodA(
env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else {
if(!object->object)
if(!instance->object)
ret = (*env)->CallBooleanMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
ret = (*env)->CallBooleanMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
}
@ -875,21 +820,22 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
// i hereby anoint thee a void method
if(self->isStatic)
(*env)->CallStaticVoidMethodA(env,
object->clazz,
instance->clazz,
self->methodId,
jargs);
else
(*env)->CallVoidMethodA(env,
object->object,
instance->object,
self->methodId,
jargs);
Py_BLOCK_THREADS;
process_java_exception(env);
break;
}
PyMem_Free(jargs);
(*env)->PopLocalFrame(env, NULL); // added by njensen
(*env)->PopLocalFrame(env, NULL);
if(PyErr_Occurred())
return NULL;
@ -902,7 +848,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
pyjarray_pin((PyJarray_Object *) param);
}
}
if(result == NULL) {
Py_INCREF(Py_None);
return Py_None;
@ -910,7 +856,6 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
return result;
// added by njensen
EXIT_ERROR:
PyMem_Free(jargs);
(*env)->PopLocalFrame(env, NULL);
@ -923,7 +868,7 @@ static PyMethodDef pyjmethod_methods[] = {
};
static PyTypeObject PyJmethod_Type = {
PyTypeObject PyJmethod_Type = {
PyObject_HEAD_INIT(0)
0,
"jep.PyJmethod",
@ -939,7 +884,7 @@ static PyTypeObject PyJmethod_Type = {
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
(ternaryfunc) pyjmethod_call, /* tp_call */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */

View file

@ -2,7 +2,7 @@
/*
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.
@ -24,16 +24,9 @@
3. This notice may not be removed or altered from any source
distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/
// shut up the compiler
#ifdef _POSIX_C_SOURCE
# undef _POSIX_C_SOURCE
@ -55,9 +48,6 @@ typedef struct {
PyObject_HEAD
jmethodID methodId; /* resolved methodid */
jobject rmethod; /* reflect/Method object */
// commented out by njensen
//PyJobject_Object *pyjobject; /* parent, should point to
// PyJObject_Object */
int returnTypeId; /* type id of return */
PyObject *pyMethodName; /* python name... :-) */
jobjectArray parameters; /* array of jclass parameter types */

View file

@ -2,7 +2,7 @@
/*
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.
@ -24,12 +24,6 @@
3. This notice may not be removed or altered from any source
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 "pyjmethodwrapper.h"
#include "util.h"
#include "pyjobject.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(
PyJobject_Object *pyjobject,
PyJmethod_Object *pyjmethod
) {
PyJmethodWrapper_Object *pym = NULL;
/*
* Makes a new pyjmethodwrapper. This is required to reuse the pyjmethod
* objects across multiple instances, otherwise we'd lose track of which
* pyjobject is doing the calling.
*/
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)
return NULL;
pym = PyObject_NEW(PyJmethodWrapper_Object, &PyJmethodWrapper_Type);
pym = PyObject_NEW(PyJmethodWrapper_Object, &PyJmethodWrapper_Type);
pym->method = pyjmethod;
pym->object = pyjobject;
Py_INCREF(pyjobject);
return pym;
}
static void pyjmethodwrapper_dealloc(PyJmethodWrapper_Object *self) {
#if USE_DEALLOC
if(self->object) {
Py_DECREF(self->object);
}
PyObject_Del(self);
#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,
PyObject *args,
PyObject *keywords) {
PyObject *ret;
PyJobject_Object* obj;
PyJobject_Object* obj;
if(!PyTuple_Check(args)) {
PyErr_Format(PyExc_RuntimeError, "args is not a valid tuple");
@ -114,8 +116,25 @@ static PyObject* pyjmethodwrapper_call(PyJmethodWrapper_Object *self,
}
obj = self->object;
// pyjobject_find_method will actually call the method
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);
return ret;
@ -127,13 +146,13 @@ static PyMethodDef pyjmethodwrapper_methods[] = {
};
static PyTypeObject PyJmethodWrapper_Type = {
PyTypeObject PyJmethodWrapper_Type = {
PyObject_HEAD_INIT(0)
0,
"jep.PyJmethodWrapper",
sizeof(PyJmethodWrapper_Object),
0,
(destructor) pyjmethodwrapper_dealloc, /* tp_dealloc */
(destructor) pyjmethodwrapper_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
@ -143,20 +162,20 @@ static PyTypeObject PyJmethodWrapper_Type = {
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
(ternaryfunc) pyjmethodwrapper_call, /* tp_call */
(ternaryfunc) pyjmethodwrapper_call, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"jmethodwrapper", /* tp_doc */
"jmethodwrapper", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
pyjmethodwrapper_methods, /* tp_methods */
pyjmethodwrapper_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */

View file

@ -2,7 +2,7 @@
/*
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.
@ -24,12 +24,6 @@
3. This notice may not be removed or altered from any source
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 "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 {
PyObject_HEAD
PyJmethod_Object *method;
PyJobject_Object *object;
PyJmethod_Object *method; /* the original pyjmethod tied to a java.lang.reflect.Method */
PyJobject_Object *object; /* the pyjobject that called this method */
} PyJmethodWrapper_Object;
PyJmethodWrapper_Object* pyjmethodwrapper_new(
PyJobject_Object*, PyJmethod_Object*);
PyJmethodWrapper_Object* pyjmethodwrapper_new(PyJobject_Object*,
PyJmethod_Object*);
#endif // ndef pyjmethodwrapper

View file

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

View file

@ -2,7 +2,7 @@
/*
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.
@ -24,16 +24,9 @@
3. This notice may not be removed or altered from any source
distribution.
*/
/*
August 2, 2012
Modified by Raytheon (c) 2012 Raytheon Company. All Rights Reserved.
Modifications marked and described by 'njensen'
*/
// shut up the compiler
#ifdef _POSIX_C_SOURCE
# undef _POSIX_C_SOURCE
@ -59,16 +52,21 @@ typedef struct {
PyObject *methods; /* list of method names */
PyObject *fields; /* list of field names */
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;
PyAPI_DATA(PyTypeObject) PyJobject_Type;
PyObject* pyjobject_new(JNIEnv*, jobject);
PyObject* pyjobject_new_class(JNIEnv*, jclass);
PyObject* pyjobject_find_method(PyJobject_Object*, PyObject*, PyObject*);
int pyjobject_check(PyObject *obj);
//added by njensen
static PyObject* pyjobject_numpy(PyJobject_Object *obj);
// these methods need to be available to pyjlist
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

View file

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

View file

@ -2,7 +2,7 @@
/*
jep - Java Embedded Python
Copyright (c) 2004 - 2011 Mike Johnson.
Copyright (c) JEP AUTHORS.
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
distribution.
*/
*/
// shut up the compiler