Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Epypj #54

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ jacoco/
wheelhouse/
vc*.pdb
*.class
headers/
/project/epypj_java/nbproject/private/
/project/epype_java/nbproject/private/
test-output/
34 changes: 34 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,37 @@ liability incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

**END OF TERMS AND CONDITIONS**


==============
ASM License
==============

ASM: a very small and fast Java bytecode manipulation framework
Copyright (c) 2000-2011 INRIA, France Telecom
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.

1 change: 1 addition & 0 deletions ivy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
</dependency>
<dependency org="org.apache.derby" name="derby" rev="10.15.2.0" conf="deps->default"/>
<dependency org="org.hsqldb" name="hsqldb" rev="2.5.0" conf="deps->default"/>
<dependency org="org.testng" name="testng" rev="7.3.0" conf="deps->default"/>
</dependencies>
</ivy-module>
7 changes: 7 additions & 0 deletions jpype/_jclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,17 @@ def _jclassDoc(cls):

return "\n".join(out)

# Future hook for Extension types


def _JExtension(name, bases, members):
raise TypeError("Java classes cannot be extended in Python")


# Install module hooks
_jpype.JClass = JClass
_jpype.JInterface = JInterface
_jpype._jclassDoc = _jclassDoc
_jpype._jclassPre = _jclassPre
_jpype._jclassPost = _jclassPost
_jpype._JExtension = _JExtension
2 changes: 0 additions & 2 deletions jpype/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ def _JFileConvert(jcls, obj):
return jcls(obj.__fspath__())

# To be added in 1.1.x


@_jcustomizer.JConversion("java.lang.Iterable", instanceof=Sequence, excludes=str)
@_jcustomizer.JConversion("java.util.Collection", instanceof=Sequence, excludes=str)
def _JSequenceConvert(jcls, obj):
Expand Down
111 changes: 111 additions & 0 deletions native/common/ejp_module.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*****************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

See NOTICE file for details.
**************************************************************************** */
#include <jni.h>
#include "jpype.h"
#include "pyjp.h"
#include "jp_boxedtype.h"
#include "epypj.h"
#include <vector>

#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_jpype_python_internal_PyModuleDef
* Method: _getModuleDef
* Signature: (Ljava/lang/Object;)J
*/
JNIEXPORT jlong JNICALL Java_org_jpype_python_internal_PyModuleDef__1find
(JNIEnv *env, jclass cls, jobject pymodule)
{
EJP_TRACE_JAVA_IN("moduleDef::find");
JPPyObject param1 = EJP_ToPython(frame, pymodule);
struct PyModuleDef* def = PyModule_GetDef(param1.get());
return (jlong) def; // This is a reference, so no need to worry
EJP_TRACE_JAVA_OUT(0);
}

/*
* Class: org_jpype_python_internal_PyModuleDef
* Method: _getName
* Signature: (J)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_jpype_python_internal_PyModuleDef__1getName
(JNIEnv *env, jclass cls, jlong jdef)
{
EJP_TRACE_JAVA_IN("moduleDef::getName");
struct PyModuleDef* def = (struct PyModuleDef*) jdef;
if (def->m_name == NULL)
return NULL;
return frame.NewStringUTF(def->m_name);
EJP_TRACE_JAVA_OUT(NULL);
}

/*
* Class: org_jpype_python_internal_PyModuleDef
* Method: _getMethods
* Signature: (J)[[Ljava/lang/Object;
*/
JNIEXPORT jobjectArray JNICALL Java_org_jpype_python_internal_PyModuleDef__1getMethods
(JNIEnv *env, jclass cls, jlong jdef)
{
EJP_TRACE_JAVA_IN("moduleDef::getName");
struct PyModuleDef* def = (struct PyModuleDef*) jdef;

// Count the number of methods
int methodCount = 0;
PyMethodDef *methodDef = def->m_methods;
while( methodDef->ml_name!=NULL)
{
methodCount++;
methodDef++;
}

jobjectArray out = frame.NewObjectArray(3,
context->_java_lang_Object->getJavaClass(), NULL);
jobjectArray names = frame.NewObjectArray(methodCount,
context->_java_lang_Object->getJavaClass(), NULL);
jlongArray ptr = frame.NewLongArray(methodCount);
jlongArray flags = frame.NewLongArray(methodCount);
frame.SetObjectArrayElement(out, 0, names);
frame.SetObjectArrayElement(out, 1, ptr);
frame.SetObjectArrayElement(out, 2, flags);
jboolean copy;
jlong* p1 = frame.GetLongArrayElements(ptr, &copy);
jlong* p2 = frame.GetLongArrayElements(flags, &copy);

methodCount = 0;
methodDef = def->m_methods;
while( methodDef->ml_name!=NULL)
{
jstring name = frame.fromStringUTF8(methodDef->ml_name);
frame.SetObjectArrayElement(names, methodCount, name);
p1[methodCount] = (jlong) (methodDef->ml_meth);
p2[2] = methodDef->ml_flags;
methodCount++;
methodDef++;
frame.DeleteLocalRef(name);
}
frame.ReleaseLongArrayElements(ptr, p1, JNI_COMMIT);
frame.ReleaseLongArrayElements(flags, p2, JNI_COMMIT);

return out;
EJP_TRACE_JAVA_OUT(NULL);
}

#ifdef __cplusplus
}
#endif
5 changes: 5 additions & 0 deletions native/common/include/jp_exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ class JPypeException : std::runtime_error
return m_Type;
}

jthrowable getThrowable()
{
return m_Throwable.get();
}

private:
JPContext* m_Context{};
int m_Type;
Expand Down
1 change: 0 additions & 1 deletion native/common/include/jp_javaframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ class JPJavaFrame
void newWrapper(JPClass* cls);
void registerRef(jobject obj, PyObject* hostRef);
void registerRef(jobject obj, void* ref, JCleanupHook cleanup);

void clearInterrupt(bool throws);

} ;
Expand Down
6 changes: 6 additions & 0 deletions native/common/include/jp_modifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ inline bool isBeanMutator(jlong modifier)
{
return (modifier & 0x40000000) == 0x40000000;
}

inline bool isPython(jlong modifier)
{
return (modifier & 0x02000000) == 0x02000000;
}

}

#ifdef __cplusplus
Expand Down
42 changes: 42 additions & 0 deletions native/common/include/jp_pyobjecttype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2020 nelson85.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* File: jp_pyobjecttype.h
* Author: nelson85
*
* Created on June 30, 2020, 6:45 AM
*/
#ifndef _JPPYOBJECTCLASS_H_
#define _JPPYOBJECTCLASS_H_

class JPPyObjectType : public JPClass
{
public:
JPPyObjectType(JPJavaFrame& frame,
jclass clss,
const string& name,
JPClass* super,
JPClassList& interfaces,
jint modifiers);
virtual ~JPPyObjectType();
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;

protected:
jfieldID m_SelfID;
} ;

#endif // _JPPYOBJECTCLASS_H_
2 changes: 1 addition & 1 deletion native/common/jp_classhints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class JPAttributeConversion : public JPPythonConversion
JPMatch::Type matches(JPClass *cls, JPMatch &match) override
{
JP_TRACE_IN("JPAttributeConversion::matches");
JPPyObject attr = JPPyObject::accept(PyObject_GetAttrString(match.object, attribute_.c_str()));
JPPyObject attr = JPPyObject::acceptClear(PyObject_GetAttrString(match.object, attribute_.c_str()));
if (attr.isNull())
return JPMatch::_none;
match.conversion = this;
Expand Down
4 changes: 0 additions & 4 deletions native/common/jp_classloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,11 @@ JPClassLoader::JPClassLoader(JPJavaFrame& frame)
JP_RAISE(PyExc_RuntimeError, "Can't find jar path");
path = path.substr(0, i + 1);
jobject url1 = toURL(frame, path + "org.jpype.jar");
// jobject url2 = toURL(frame, path + "lib/asm-8.0.1.jar");

// urlArray = new URL[]{url};
jclass urlClass = frame.GetObjectClass(url1);
jobjectArray urlArray = frame.NewObjectArray(1, urlClass, nullptr);
frame.SetObjectArrayElement(urlArray, 0, url1);
// frame.SetObjectArrayElement(urlArray, 1, url2);

// cl = new URLClassLoader(urlArray);
jclass urlLoaderClass = frame.FindClass("java/net/URLClassLoader");
jmethodID newURLClassLoader = frame.GetMethodID(urlLoaderClass, "<init>", "([Ljava/net/URL;Ljava/lang/ClassLoader;)V");
jvalue v[3];
Expand Down
10 changes: 6 additions & 4 deletions native/common/jp_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "jp_proxy.h"
#include "jp_platform.h"
#include "jp_gc.h"
#include "epypj.h"

JPResource::~JPResource() = default;

Expand Down Expand Up @@ -152,9 +153,7 @@ void JPContext::startJVM(const string& vmPath, const StringVector& args,
void JPContext::attachJVM(JNIEnv* env)
{
env->GetJavaVM(&m_JavaVM);
#ifndef ANDROID
m_Embedded = true;
#endif
initializeResources(env, false);
}

Expand Down Expand Up @@ -228,6 +227,9 @@ void JPContext::initializeResources(JNIEnv* env, bool interrupt)

m_JavaContext = JPObjectRef(frame, frame.CallStaticObjectMethodA(contextClass, startMethod, val));

// Set up Java wrappers for Python
EJP_Init(frame);

// Post launch
JP_TRACE("Connect resources");
// Hook up the type manager
Expand Down Expand Up @@ -309,8 +311,8 @@ void JPContext::shutdownJVM(bool destroyJVM, bool freeJVM)
JP_TRACE_IN("JPContext::shutdown");
if (m_JavaVM == nullptr)
JP_RAISE(PyExc_RuntimeError, "Attempt to shutdown without a live JVM");
// if (m_Embedded)
// JP_RAISE(PyExc_RuntimeError, "Cannot shutdown from embedded Python");
if (m_Embedded)
JP_RAISE(PyExc_RuntimeError, "Cannot shutdown from embedded Python");

// Wait for all non-demon threads to terminate
if (destroyJVM)
Expand Down
Loading
Loading