Skip to content

Commit

Permalink
Added PyThreadState_GetDict implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stewori committed May 26, 2016
1 parent ed06aa7 commit 9fa540f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 34 deletions.
7 changes: 6 additions & 1 deletion JyNI-C/include/JyTState.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@
//PyObject *curexc_type;
//PyObject *curexc_value;
//PyObject *curexc_traceback;

//PyObject *exc_type; (not used by JyNI)
//PyObject *exc_value; (not used by JyNI)
//PyObject *exc_traceback; (not used by JyNI)
//PyObject *dict;
#define TS_TRUNCATED_SIZE (sizeof(struct _ts*) + sizeof(PyInterpreterState*) \
+ sizeof(struct _frame*) + 3*sizeof(int) + 2*sizeof(Py_tracefunc) \
+ 5*sizeof(PyObject*))
+ 9*sizeof(PyObject*))
//#define TS_TRUNCATED_SIZE sizeof(PyThreadState)

inline void JyErr_InsertCurExc();
Expand Down
13 changes: 9 additions & 4 deletions JyNI-C/src/JyNI.c
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,11 @@ inline jobject JyNI_InitJythonPyException(ExceptionMapEntry* eme, PyObject* src,
*/
inline jobject JyNI_InitJythonPyObject(TypeMapEntry* tme, PyObject* src, JyObject* srcJy)
{
// jputs(__FUNCTION__);
// if (tme) jputs("tme");//tme->py_type->tp_name);
// else jputs("no tme");
// jputs(src->ob_type->tp_name);
// jputsPy(src->ob_type->tp_bases); //bool_arrtype_new
jobject dest = NULL;
env(NULL);
if (Py_TYPE(src) != tme->py_type) {
Expand Down Expand Up @@ -1846,7 +1851,6 @@ inline jobject JyNI_JythonPyObject_FromPyObject(PyObject* op)
* Heap-types are treated like ordinary objects.
* Note: Don't confuse the following line with checking op->ob_type rather than op itself.
*/
// jboolean dbg = strcmp("numpy.ufunc", Py_TYPE(op)->tp_name) == 0;
env(NULL);
if (PyType_Check(op) && !PyType_HasFeature((PyTypeObject*) op, Py_TPFLAGS_HEAPTYPE))
{
Expand Down Expand Up @@ -1920,14 +1924,15 @@ inline jobject JyNI_JythonPyObject_FromPyObject(PyObject* op)
//if (bl) jputs("converting bool2");
//jputsLong(__LINE__);
TypeMapEntry* tme;
if (jy->jy != NULL)
if (jy->jy)
{
//if (bl) jputsLong(__LINE__);
//if (bl)
//printf("%d_______%s\n", __LINE__, __FUNCTION__);
tme = (TypeMapEntry*) jy->jy;
//if (dbg && tme) {jputs("ufunc tme:"); jputsLong(__LINE__); jputsLong(op);}
} else {
//if (bl) jputsLong(__LINE__);
//if (bl)
//jputsLong(__LINE__);
//printf("%d_______%s\n", __LINE__, __FUNCTION__);
tme = JyNI_JythonTypeEntry_FromPyType(Py_TYPE(op));
//if (dbg && tme) {jputs("ufunc tme:"); jputsLong(__LINE__); jputsLong(op);}
Expand Down
13 changes: 11 additions & 2 deletions JyNI-C/src/JyTState.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ jlong JyTState_initNativeThreadState(JNIEnv *env, jclass class, jobject jyTState
{
//TODO jyTState is currently not used here. Maybe clean it away later...
// jputs("init native ThreadState...");
PyThreadState* tstate = (PyThreadState*) PyObject_RawMalloc(TS_TRUNCATED_SIZE);
//PyThreadState* tstate = (PyThreadState*) PyObject_RawMalloc(TS_TRUNCATED_SIZE);
PyThreadState* tstate = (PyThreadState*) malloc(TS_TRUNCATED_SIZE);
tstate->next = NULL;
tstate->interp = NULL;
tstate->frame = NULL;
Expand All @@ -101,6 +102,10 @@ jlong JyTState_initNativeThreadState(JNIEnv *env, jclass class, jobject jyTState
tstate->curexc_type = NULL;
tstate->curexc_value = NULL;
tstate->curexc_traceback = NULL;
tstate->exc_type = NULL; // (not used)
tstate->exc_value = NULL; // (not used)
tstate->exc_traceback = NULL; // (not used)
tstate->dict = NULL;
TS_SET_JY(tstate, (*env)->NewWeakGlobalRef(env, threadState));
// jputs("done");
// jputsLong(tstate);
Expand All @@ -120,10 +125,14 @@ void JyTState_clearNativeThreadState(JNIEnv *env, jclass class, jlong threadStat
{
(*env)->DeleteWeakGlobalRef(env, TS_GET_JY((PyThreadState*) threadState));
if (!--((PyThreadState*) threadState)->JyNI_gilstate_counter)
{
PyThreadState_Clear((PyThreadState*) threadState);
_delNativeThreadState((PyThreadState*) threadState);
}
}

void _delNativeThreadState(PyThreadState* threadState)
{
PyObject_RawFree(threadState);
//PyObject_RawFree(threadState);
free(threadState);
}
50 changes: 25 additions & 25 deletions JyNI-C/src/Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ PyThreadState_Clear(PyThreadState *tstate)
// "PyThreadState_Clear: warning: thread still has a frame\n");
//
// Py_CLEAR(tstate->frame);
//
// Py_CLEAR(tstate->dict);

Py_CLEAR(tstate->dict);
// Py_CLEAR(tstate->async_exc);

Py_CLEAR(tstate->curexc_type);
Expand All @@ -286,7 +286,7 @@ PyThreadState_Clear(PyThreadState *tstate)
}


///* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
static void
tstate_delete_common(PyThreadState *tstate)
{
Expand Down Expand Up @@ -392,28 +392,28 @@ PyThreadState_Swap(PyThreadState *newts)
return oldts;
}

///* An extension mechanism to store arbitrary additional per-thread state.
// PyThreadState_GetDict() returns a dictionary that can be used to hold such
// state; the caller should pick a unique key and store its state there. If
// PyThreadState_GetDict() returns NULL, an exception has *not* been raised
// and the caller should assume no per-thread state is available. */
//
//PyObject *
//PyThreadState_GetDict(void)
//{
// if (_PyThreadState_Current == NULL)
// return NULL;
//
// if (_PyThreadState_Current->dict == NULL) {
// PyObject *d;
// _PyThreadState_Current->dict = d = PyDict_New();
// if (d == NULL)
// PyErr_Clear();
// }
// return _PyThreadState_Current->dict;
//}
//
//
/* An extension mechanism to store arbitrary additional per-thread state.
PyThreadState_GetDict() returns a dictionary that can be used to hold such
state; the caller should pick a unique key and store its state there. If
PyThreadState_GetDict() returns NULL, an exception has *not* been raised
and the caller should assume no per-thread state is available. */

PyObject *
PyThreadState_GetDict(void)
{
if (_PyThreadState_Current == NULL)
return NULL;

if (_PyThreadState_Current->dict == NULL) {
PyObject *d;
_PyThreadState_Current->dict = d = PyDict_New();
if (d == NULL)
PyErr_Clear();
}
return _PyThreadState_Current->dict;
}


///* Asynchronously raise an exception in a thread.
// Requested by Just van Rossum and Alex Martelli.
// To prevent naive misuse, you must write your own extension
Expand Down
5 changes: 4 additions & 1 deletion JyNI-Java/src/JyNI/JyTState.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public class JyTState {
protected static int nativeRecursionLimit;

//No IdentityHashMap needed, since ThreadState does not overwrite hashCode().
protected static WeakHashMap<ThreadState, JyTState> tStateLookup = new WeakHashMap<ThreadState, JyTState>();
//Note that WeakHashMap has weak keys and "strong" values, so this mapping implements a
//keep-alive relationship with each ThreadState keeping alive the corresponding JyTState.
protected static WeakHashMap<ThreadState, JyTState> tStateLookup =
new WeakHashMap<ThreadState, JyTState>();

protected WeakReference<ThreadState> tState;
protected int nativeCallDepth;
Expand Down
1 change: 0 additions & 1 deletion JyNI-Lib/ctypes/_endian.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
######################################################################
import sys
from ctypes import *
from JyNI import JyNI

_array_type = type(Array)

Expand Down

0 comments on commit 9fa540f

Please sign in to comment.