jobject JySync_Init_JyWeakCallableProxy_From_PyWeakCallableProxy(PyObject* src, jclass subtype) { PyObject* referent = PyWeakref_GET_OBJECT0(src); jobject jReferent = NULL; if (referent) jReferent = JyNI_JythonPyObject_FromPyObject(referent); env(NULL); return (*env)->CallStaticObjectMethod(env, JyNIClass, JyNI_createCallableProxyFromNative, jReferent, (jlong) referent, NULL); //todo: Support native callbacks. }
static int func_set_code(PyFunctionObject *op, PyObject *value) { PyObject *tmp; Py_ssize_t nfree, nclosure; if (restricted()) return -1; /* Not legal to del f.func_code or to set it to anything * other than a code object. */ if (value == NULL || !PyCode_Check(value)) { PyErr_SetString(PyExc_TypeError, "__code__ must be set to a code object"); return -1; } nfree = PyCode_GetNumFree((PyCodeObject *)value); nclosure = (op->func_closure == NULL ? 0 : PyTuple_GET_SIZE(op->func_closure)); if (nclosure != nfree) { PyErr_Format(PyExc_ValueError, "%s() requires a code object with %zd free vars," " not %zd", PyString_AsString(op->func_name), nclosure, nfree); return -1; } env(-1); jobject jFunc = JyNI_JythonPyObject_FromPyObject(op); jobject jCode = JyNI_JythonPyObject_FromPyObject(value); (*env)->CallObjectMethod(env, jFunc, pyFunctionSetCode, jCode); if ((*env)->ExceptionCheck(env)) { jputs("Exception in func_set_code"); (*env)->ExceptionClear(env); return -1; } tmp = op->func_code; Py_INCREF(value); op->func_code = value; updateJyGCHeadLink(op, AS_JY_WITH_GC(op), func_code_gcindex, value, AS_JY_NO_GC(value)); Py_DECREF(tmp); return 0; }
static PyObject * func_get_name(PyFunctionObject *op) { env(NULL); jobject jFunc = JyNI_JythonPyObject_FromPyObject(op); jobject result = (*env)->GetObjectField(env, jFunc, pyFunction__name__); return JyNI_PyObject_FromJythonPyObject(result); // Py_INCREF(op->func_name); // return op->func_name; }
static PyObject* func_repr(PyFunctionObject *op) { env(NULL); jobject jFunc = JyNI_JythonPyObject_FromPyObject(op); jobject result = (*env)->CallObjectMethod(env, jFunc, pyObject__repr__); return JyNI_PyObject_FromJythonPyObject(result); // return PyString_FromFormat("<function %s at %p>", // PyString_AsString(op->func_name), // op); }
jobject JySync_Init_JyWeakReference_From_PyWeakReference(PyObject* src, jclass subtype) { PyObject* referent = PyWeakref_GET_OBJECT0(src); // jputs("Sync PyWeakRef* -> jython"); // jputsLong((jlong) referent); jobject jReferent = NULL; if (referent) jReferent = JyNI_JythonPyObject_FromPyObject(referent); env(NULL); return (*env)->CallStaticObjectMethod(env, JyNIClass, JyNI_createWeakReferenceFromNative, jReferent, (jlong) referent, NULL); //todo: Support native callbacks. }
/* Call when an exception has occurred but there is no way for Python to handle it. Examples: exception in __del__ or during GC. */ void PyErr_WriteUnraisable(PyObject *obj) { env(); (*env)->CallStaticVoidMethod(env, JyNIClass, JyNIPyErr_WriteUnraisable, JyNI_JythonPyObject_FromPyObject(obj)); // PyObject *f, *t, *v, *tb; // PyErr_Fetch(&t, &v, &tb); // f = PySys_GetObject("stderr"); // if (f != NULL) { // PyFile_WriteString("Exception ", f); // if (t) { // PyObject* moduleName; // char* className; // assert(PyExceptionClass_Check(t)); // className = PyExceptionClass_Name(t); // if (className != NULL) { // char *dot = strrchr(className, '.'); // if (dot != NULL) // className = dot+1; // } // // moduleName = PyObject_GetAttrString(t, "__module__"); // if (moduleName == NULL) // PyFile_WriteString("<unknown>", f); // else { // char* modstr = PyString_AsString(moduleName); // if (modstr && // strcmp(modstr, "exceptions") != 0) // { // PyFile_WriteString(modstr, f); // PyFile_WriteString(".", f); // } // } // if (className == NULL) // PyFile_WriteString("<unknown>", f); // else // PyFile_WriteString(className, f); // if (v && v != Py_None) { // PyFile_WriteString(": ", f); // PyFile_WriteObject(v, f, 0); // } // Py_XDECREF(moduleName); // } // PyFile_WriteString(" in ", f); // PyFile_WriteObject(obj, f, 0); // PyFile_WriteString(" ignored\n", f); // PyErr_Clear(); /* Just in case */ // } // Py_XDECREF(t); // Py_XDECREF(v); // Py_XDECREF(tb); }
PyObject* PyCode_Get_co_stacksize(PyObject* code) { env(NULL); jobject jCode = JyNI_JythonPyObject_FromPyObject(code); if ((*env)->IsInstanceOf(env, jCode, pyBytecodeClass)) { jint stacksize = (*env)->GetIntField(env, jCode, pyBytecode_co_stacksize); return PyInt_FromLong(stacksize); } else { return NULL; } }
static int code_compare(PyCodeObject *co, PyCodeObject *cp) { env(NULL); jobject jCode = JyNI_JythonPyObject_FromPyObject(co); jobject cmpCode = JyNI_JythonPyObject_FromPyObject(cp); return (*env)->CallObjectMethod(env, jCode, pyObject__cmp__, cmpCode); // int cmp; // cmp = PyObject_Compare(co->co_name, cp->co_name); // if (cmp) return cmp; // cmp = co->co_argcount - cp->co_argcount; // if (cmp) goto normalize; // cmp = co->co_nlocals - cp->co_nlocals; // if (cmp) goto normalize; // cmp = co->co_flags - cp->co_flags; // if (cmp) goto normalize; // cmp = co->co_firstlineno - cp->co_firstlineno; // if (cmp) goto normalize; // cmp = PyObject_Compare(co->co_code, cp->co_code); // if (cmp) return cmp; // cmp = PyObject_Compare(co->co_consts, cp->co_consts); // if (cmp) return cmp; // cmp = PyObject_Compare(co->co_names, cp->co_names); // if (cmp) return cmp; // cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); // if (cmp) return cmp; // cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); // if (cmp) return cmp; // cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); // return cmp; // // normalize: // if (cmp > 0) // return 1; // else if (cmp < 0) // return -1; // else // return 0; }
PyObject* PyCode_Get_co_consts(PyObject* code) { env(NULL); jobject jCode = JyNI_JythonPyObject_FromPyObject(code); jobject jArray = (*env)->GetObjectField(env, jCode, pyBytecode_co_consts); Py_ssize_t size = 0; if (jArray) size = (*env)->GetArrayLength(env, jArray); PyTupleObject* result = PyTuple_New(size); Py_ssize_t i; for (i = 0; i < size; ++i) PyTuple_SET_ITEM(result, i, JyNI_PyObject_FromJythonPyObject( (*env)->GetObjectArrayElement(env, jArray, i))); return result; }
PyObject* PyCode_Get_co_lnotab(PyObject* code) { env(NULL); jobject jCode = JyNI_JythonPyObject_FromPyObject(code); if ((*env)->IsInstanceOf(env, jCode, pyBytecodeClass)) { jobject jstr = (*env)->CallStaticObjectMethod(env, JyNIClass, JyNI_pyCode_co_lnotab, jCode); cstr_from_jstring(cstr, jstr); return PyString_FromString(cstr); } else { return NULL; } }
/* * JyNI Warning: The returned pointer is only valid until the value * in the dict is changed! (Should usually not happen.) */ char * PyModule_GetFilename(PyObject *m) { if (!PyModule_Check(m)) { PyErr_BadArgument(); return NULL; } //stringIntern env(NULL); //puts("bis hier..."); jobject pyStr = (*env)->CallObjectMethod(env, JyNI_JythonPyObject_FromPyObject(m), pyObject__getattr__, (*env)->CallObjectMethod(env, (*env)->NewStringUTF(env, "__file__"), stringIntern)); //puts("und noch etwas weiter"); if (pyStr == NULL) { puts("module filename missing"); //printf("PyExc_SystemError isException? %i\n", PyExceptionClass_Check2(PyExc_SystemError)); //printf("PyExc_SystemError isType? %i\n", PyType_Check(PyExc_SystemError)); //puts("hier nicht"); //todo: fix this later //PyErr_SetString(PyExc_SystemError, "module filename missing"); //puts("und hier0?"); return NULL; } //puts("und hier?"); jstring er = (*env)->CallObjectMethod(env, pyStr, pyStringAsString); global_cstr_from_jstring(cstr, er); //puts("fast fertig..."); JyNI_AddOrSetJyAttributeWithFlags(AS_JY_WITH_GC(m), JyAttributeModuleFile, cstr, JY_ATTR_OWNS_VALUE_FLAG_MASK); return cstr; /* PyObject *d; PyObject *fileobj; if (!PyModule_Check(m)) { PyErr_BadArgument(); return NULL; } d = ((PyModuleObject *)m)->md_dict; if (d == NULL || (fileobj = PyDict_GetItemString(d, "__file__")) == NULL || !PyString_Check(fileobj)) { PyErr_SetString(PyExc_SystemError, "module filename missing"); return NULL; } return PyString_AsString(fileobj); */ }
static int slice_compare(PySliceObject *v, PySliceObject *w) { env(-2); return (*env)->CallIntMethod(env, JyNIClass, JyNISlice_compare, JyNI_JythonPyObject_FromPyObject((PyObject*) v), JyNI_JythonPyObject_FromPyObject((PyObject*) w)); // int result = 0; // // if (v == w) // return 0; // // if (PyObject_Cmp(v->start, w->start, &result) < 0) // return -2; // if (result != 0) // return result; // if (PyObject_Cmp(v->stop, w->stop, &result) < 0) // return -2; // if (result != 0) // return result; // if (PyObject_Cmp(v->step, w->step, &result) < 0) // return -2; // return result; }
jobject JySync_Init_JyCFunction_From_PyCFunction(PyObject* src, jclass subtype) { // jputs(__FUNCTION__); if (JyNI_HasJyAttribute(AS_JY(src), JyAttributeMethodDef)) { jputs("JyNI-warning: JySync_Init_JyCFunction_From_PyCFunction shouldn't be called with non-native PyCFunction."); } JyObject* srcJy = AS_JY_WITH_GC(src); // Note that JyNI.PyCFunction implements CPeerInterface, so we must set the CPeer-flag: srcJy->flags |= JY_CPEER_FLAG_MASK; Py_INCREF(src); //JyNI_GC_ExploreObject(src); // jboolean dbg = strcmp(((PyCFunctionObject*) src)->m_ml->ml_name, "in_dll") == 0; // jputs(Py_TYPE(src)->tp_name); //if (_PyThreadState_Current) jputsPy(PyObject_GetAttrString(src, "__class__")); PyCFunctionObject* func = (PyCFunctionObject*) src; // jputs(func->m_ml->ml_name); // jputsLong(func->m_self); //putsPy(func->m_self); // jputsLong(func->m_module); env(NULL); jstring name = (*env)->NewStringUTF(env, func->m_ml->ml_name); jstring doc = (*env)->NewStringUTF(env, func->m_ml->ml_doc); jobject jtype = JyNI_JythonPyObject_FromPyObject(Py_TYPE(src)); //JyNI_jprintJ(jtype); //builtinTypes[TME_INDEX_CFunction].jy_class //jobject test = _JyNI_JythonPyTypeObject_FromPyTypeObject(Py_TYPE(src), NULL); jobject result = (*env)->NewObject(env, pyCFunctionClass, pyCFunction_Constructor, (jlong) src, jtype, name, func->m_ml->ml_flags & METH_NOARGS, doc); // if (dbg) { // jputs(__FUNCTION__); // jputs(((PyCFunctionObject*) src)->m_ml->ml_name); // jputsPy(((PyCFunctionObject*) src)->m_self); // jputsLong(((PyCFunctionObject*) src)); // jputsLong(((PyCFunctionObject*) src)->m_ml); // jputsLong(((PyCFunctionObject*) src)->m_self); // jPrintCStackTrace(); // } // jputs("result:"); // JyNI_jprintJ(result); // if ((*env)->IsSameObject(env, result, NULL)) jputs("result is null"); // JyNI_printJInfo(result); // JyNI_jprintHash(result); return result; }
PyObject *PyCodec_Encode(PyObject *object, const char *encoding, const char *errors) { env(NULL); jstring enc = (*env)->NewStringUTF(env, encoding); jstring err = (*env)->NewStringUTF(env, errors); return JyNI_PyObject_FromJythonPyObject( //todo: Type check of object - must be PyString or PyUnicode or a subtype of any of these (*env)->CallStaticObjectMethod(env, pyCodecsClass, pyCodecsEncode, JyNI_JythonPyObject_FromPyObject(object), enc, err)); // PyObject *encoder = NULL; // PyObject *args = NULL, *result = NULL; // PyObject *v; // // encoder = PyCodec_Encoder(encoding); // if (encoder == NULL) // goto onError; // // args = args_tuple(object, errors); // if (args == NULL) // goto onError; // // result = PyEval_CallObject(encoder,args); // if (result == NULL) // goto onError; // // if (!PyTuple_Check(result) || // PyTuple_GET_SIZE(result) != 2) { // PyErr_SetString(PyExc_TypeError, // "encoder must return a tuple (object,integer)"); // goto onError; // } // v = PyTuple_GET_ITEM(result,0); // Py_INCREF(v); // /* We don't check or use the second (integer) entry. */ // // Py_DECREF(args); // Py_DECREF(encoder); // Py_DECREF(result); // return v; // // onError: // Py_XDECREF(result); // Py_XDECREF(args); // Py_XDECREF(encoder); // return NULL; }
static PyObject * func_get_dict(PyFunctionObject *op) { env(NULL); jobject jFunc = JyNI_JythonPyObject_FromPyObject(op); jobject result = (*env)->CallObjectMethod(env, jFunc, pyObjectGetDict); return JyNI_PyObject_FromJythonPyObject(result); // if (restricted()) // return NULL; // if (op->func_dict == NULL) { // op->func_dict = PyDict_New(); // if (op->func_dict == NULL) // return NULL; // } // Py_INCREF(op->func_dict); // return op->func_dict; }
jobject JySync_Init_JyMethodDescr_From_PyMethodDescr(PyObject* src, jclass subtype) { // jboolean jdbg = strcmp(((PyMethodDescrObject*) src)->d_method->ml_name, "__array__") == 0 && // strcmp(((PyMethodDescrObject*) src)->d_type->tp_name, "numpy.ndarray") == 0; // if (jdbg) jputs(__FUNCTION__); // if (jdbg) jputs(((PyMethodDescrObject*) src)->d_method->ml_name); // if (jdbg) jputs(((PyMethodDescrObject*) src)->d_type->tp_name); // if (jdbg) jputsLong(((PyMethodDescrObject*) src)->d_type->tp_as_number->nb_add); env(NULL); jobject mdef = (*env)->NewObject(env, pyCMethodDefClass, pyCMethodDef_Constructor, (jlong) ((PyMethodDescrObject*) src)->d_method, (*env)->NewStringUTF(env, ((PyMethodDescrObject*) src)->d_method->ml_name), ((PyMethodDescrObject*) src)->d_method->ml_flags & METH_NOARGS, (*env)->NewStringUTF(env, ((PyMethodDescrObject*) src)->d_method->ml_doc)); jobject res = (*env)->NewObject(env, pyMethodDescrClass, pyMethodDescr_Constructor, JyNI_JythonPyObject_FromPyObject((PyObject*) ((PyMethodDescrObject*) src)->d_type), mdef); return res; }
// Warning with explicit origin int PyErr_WarnExplicit(PyObject *category, const char *text, const char *filename_str, int lineno, const char *module_str, PyObject *registry) { env(-1); jstring message = (*env)->NewStringUTF(env, text); if (message == NULL) return -1; if (category == NULL) category = PyExc_RuntimeWarning; (*env)->CallStaticVoidMethod(env, pyPyClass, pyPy_explicitWarning, JyNI_JythonExceptionType_FromPyExceptionType(category), message, (*env)->NewStringUTF(env, filename_str), (*env)->NewStringUTF(env, module_str), JyNI_JythonPyObject_FromPyObject(registry)); return 0; /*PyObject *res; PyObject *message = PyString_FromString(text); PyObject *filename = PyString_FromString(filename_str); PyObject *module = NULL; int ret = -1; if (message == NULL || filename == NULL) goto exit; if (module_str != NULL) { module = PyString_FromString(module_str); if (module == NULL) goto exit; } if (category == NULL) category = PyExc_RuntimeWarning; res = warn_explicit(category, message, filename, lineno, module, registry, NULL); if (res == NULL) goto exit; Py_DECREF(res); ret = 0; exit: Py_XDECREF(message); Py_XDECREF(module); Py_XDECREF(filename); return ret;*/ }
static PyObject * module_repr(PyModuleObject *m) { env(NULL); return JyNI_PyObject_FromJythonPyObject((*env)->CallObjectMethod(env, JyNI_JythonPyObject_FromPyObject((PyObject*) m), pyObject___repr__)); // char *name; // char *filename; // // name = PyModule_GetName((PyObject *)m); // if (name == NULL) { // PyErr_Clear(); // name = "?"; // } // filename = PyModule_GetFilename((PyObject *)m); // if (filename == NULL) { // PyErr_Clear(); // return PyString_FromFormat("<module '%s' (built-in)>", name); // } // return PyString_FromFormat("<module '%s' from '%s'>", name, filename); }
/* Register the error handling callback function error under the name name. This function will be called by the codec when it encounters an unencodable characters/undecodable bytes and doesn't know the callback name, when name is specified as the error parameter in the call to the encode/decode function. Return 0 on success, -1 on error */ int PyCodec_RegisterError(const char *name, PyObject *error) { env(-1); (*env)->CallStaticVoidMethod(env, pyCodecsClass, pyCodecsRegisterError, (*env)->NewStringUTF(env, name), JyNI_JythonPyObject_FromPyObject(error)); if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); return -1; } return 0; // PyInterpreterState *interp = PyThreadState_GET()->interp; // if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) // return -1; // if (!PyCallable_Check(error)) { // PyErr_SetString(PyExc_TypeError, "handler must be callable"); // return -1; // } // return PyDict_SetItemString(interp->codec_error_registry, // (char *)name, error); }
/* * JyNI Warning: The returned pointer is only valid until the value * in the dict is changed! (Should usually not happen.) */ char * PyModule_GetName(PyObject *m) { //todo: Maybe attempt to read result from JyAttributeModuleName. if (!PyModule_Check(m)) { PyErr_BadArgument(); return NULL; } //stringIntern env(NULL); jobject pyStr = (*env)->CallObjectMethod(env, JyNI_JythonPyObject_FromPyObject(m), pyObject___getattr__, (*env)->CallObjectMethod(env, (*env)->NewStringUTF(env, "__name__"), string_intern)); if (pyStr == NULL) { PyErr_SetString(PyExc_SystemError, "nameless module"); return NULL; } jstring er = (*env)->CallObjectMethod(env, pyStr, pyObject_asString); global_cstr_from_jstring(cstr, er); JyNI_AddOrSetJyAttributeWithFlags(AS_JY_NO_GC(m), JyAttributeModuleName, cstr, JY_ATTR_OWNS_VALUE_FLAG_MASK); return cstr; /* PyObject *d; PyObject *nameobj; if (!PyModule_Check(m)) { PyErr_BadArgument(); return NULL; } d = ((PyModuleObject *)m)->md_dict; if (d == NULL || (nameobj = PyDict_GetItemString(d, "__name__")) == NULL || !PyString_Check(nameobj)) { PyErr_SetString(PyExc_SystemError, "nameless module"); return NULL; } return PyString_AsString(nameobj);*/ }
static PyObject * slice_repr(PySliceObject *r) { env(NULL); return JyNI_PyObject_FromJythonPyObject( (*env)->CallObjectMethod(env, JyNI_JythonPyObject_FromPyObject((PyObject*) r), pySliceToString)); // PyObject *s, *comma; // // s = PyString_FromString("slice("); // comma = PyString_FromString(", "); // PyString_ConcatAndDel(&s, PyObject_Repr(r->start)); // PyString_Concat(&s, comma); // PyString_ConcatAndDel(&s, PyObject_Repr(r->stop)); // PyString_Concat(&s, comma); // PyString_ConcatAndDel(&s, PyObject_Repr(r->step)); // PyString_ConcatAndDel(&s, PyString_FromString(")")); // Py_DECREF(comma); // return s; }
PyObject* PyCode_Get_co_cellvars(PyObject* code) { env(NULL); jobject jCode = JyNI_JythonPyObject_FromPyObject(code); jobject jArray = (*env)->GetObjectField(env, jCode, pyBaseCode_co_cellvars); jStringArray2pyTuple(jArray, result); // Py_ssize_t size = (*env)->GetArrayLength(env, jArray); // PyTupleObject* result = PyTuple_New(size); // Py_ssize_t i; // char* utf_string; // jobject jstr; // for (i = 0; i < size; ++i) // { // jstr = (*env)->GetObjectArrayElement(env, jArray, i); // utf_string = (*env)->GetStringUTFChars(env, jstr, NULL); // char cstr[strlen(utf_string)+1]; // strcpy(cstr, utf_string); // (*env)->ReleaseStringUTFChars(env, jstr, utf_string); // PyTuple_SET_ITEM(result, i, PyString_FromString(cstr)); // } return result; }
int PyCell_Set(PyObject *op, PyObject *obj) { PyObject* oldobj; if (!PyCell_Check(op)) { PyErr_BadInternalCall(); return -1; } oldobj = PyCell_GET(op); Py_XINCREF(obj); PyCell_SET(op, obj); updateJyGCHeadLink(op, AS_JY_WITH_GC(op), 0, obj, obj ? AS_JY(obj) : NULL); Py_XDECREF(oldobj); JyObject* jy = AS_JY_WITH_GC(op); if (JyObject_IS_INITIALIZED(jy)) { env(-1); (*env)->SetObjectField(env, jy->jy, pyCell_ob_refField, JyNI_JythonPyObject_FromPyObject(obj)); } return 0; }
PyObject * PyModule_GetDict(PyObject *m) { //puts("PyModule_GetDict"); if (!PyModule_Check(m)) { PyErr_BadInternalCall(); return NULL; } env(NULL); //jobject jm = (*env)->CallObjectMethod(env, JyNI_JythonPyObject_FromPyObject(m), pyModuleGetDict); PyObject* er = JyNI_PyObject_FromJythonPyObject( (*env)->CallObjectMethod(env, JyNI_JythonPyObject_FromPyObject(m), pyModule_getDict)); return er; /*PyObject *d; if (!PyModule_Check(m)) { PyErr_BadInternalCall(); return NULL; } d = ((PyModuleObject *)m) -> md_dict; if (d == NULL) ((PyModuleObject *)m) -> md_dict = d = PyDict_New(); return d;*/ }
static PyObject * code_repr(PyCodeObject *co) { env(NULL); jobject jCode = JyNI_JythonPyObject_FromPyObject(co); jobject repr_str = (*env)->CallObjectMethod(env, jCode, pyObject__repr__); return JyNI_PyObject_FromJythonPyObject(repr_str); // char buf[500]; // int lineno = -1; // char *filename = "???"; // char *name = "???"; // // if (co->co_firstlineno != 0) // lineno = co->co_firstlineno; // if (co->co_filename && PyString_Check(co->co_filename)) // filename = PyString_AS_STRING(co->co_filename); // if (co->co_name && PyString_Check(co->co_name)) // name = PyString_AS_STRING(co->co_name); // PyOS_snprintf(buf, sizeof(buf), // "<code object %.100s at %p, file \"%.300s\", line %d>", // name, co, filename, lineno); // return PyString_FromString(buf); }
jobject JySync_Init_JyDictProxy_From_PyDictProxy(PyObject* src, jclass subtype) { env(NULL); return (*env)->NewObject(env, pyDictProxyClass, pyDictProxy_Constructor, JyNI_JythonPyObject_FromPyObject(((proxyobject*) src)->dict)); }
int PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength) { env(-1); jarray res = (*env)->CallObjectMethod(env, JyNI_JythonPyObject_FromPyObject((PyObject*) r), pySliceIndicesEx, (jint) length); jint* arr = (*env)->GetIntArrayElements(env, res, NULL); if (arr == NULL) { //(*env)->ReleaseIntArrayElements(env, res, arr, JNI_ABORT); return -1; } else { //if (start != NULL) *start = arr[0]; //if (stop != NULL) *stop = arr[1]; //if (step != NULL) *step = arr[2]; if (slicelength != NULL) *slicelength = arr[3]; (*env)->ReleaseIntArrayElements(env, res, arr, JNI_ABORT); return 0; } /* this is harder to get right than you might think */ // Py_ssize_t defstart, defstop; // // if (r->step == Py_None) { // *step = 1; // } // else { // if (!_PyEval_SliceIndex(r->step, step)) return -1; // if (*step == 0) { // PyErr_SetString(PyExc_ValueError, // "slice step cannot be zero"); // return -1; // } // } // // defstart = *step < 0 ? length-1 : 0; // defstop = *step < 0 ? -1 : length; // // if (r->start == Py_None) { // *start = defstart; // } // else { // if (!_PyEval_SliceIndex(r->start, start)) return -1; // if (*start < 0) *start += length; // if (*start < 0) *start = (*step < 0) ? -1 : 0; // if (*start >= length) // *start = (*step < 0) ? length - 1 : length; // } // // if (r->stop == Py_None) { // *stop = defstop; // } // else { // if (!_PyEval_SliceIndex(r->stop, stop)) return -1; // if (*stop < 0) *stop += length; // if (*stop < 0) *stop = (*step < 0) ? -1 : 0; // if (*stop >= length) // *stop = (*step < 0) ? length - 1 : length; // } // // if ((*step < 0 && *stop >= *start) // || (*step > 0 && *start >= *stop)) { // *slicelength = 0; // } // else if (*step < 0) { // *slicelength = (*stop-*start+1)/(*step)+1; // } // else { // *slicelength = (*stop-*start-1)/(*step)+1; // } // // return 0; }
PyCodeObject * PyCode_New(int argcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab) { // Types: // PyObject *code something supporting buffer // PyObject *consts Tuple of PyObject // PyObject *names Tuple of PyString // PyObject *varnames Tuple of PyString // PyObject *freevars Tuple of PyString // PyObject *cellvars Tuple of PyString // PyObject *filename PyString // PyObject *name PyString // PyObject *lnotab PyString // PyCodeObject *co; /* Check argument types */ if (argcount < 0 || nlocals < 0 || code == NULL || consts == NULL || !PyTuple_Check(consts) || names == NULL || !PyTuple_Check(names) || varnames == NULL || !PyTuple_Check(varnames) || freevars == NULL || !PyTuple_Check(freevars) || cellvars == NULL || !PyTuple_Check(cellvars) || name == NULL || !PyString_Check(name) || filename == NULL || !PyString_Check(filename) || lnotab == NULL || !PyString_Check(lnotab) || !PyObject_CheckReadBuffer(code)) { PyErr_BadInternalCall(); return NULL; } env(NULL); //Try to get code from a buffer into a jstring... char* code_cstr; Py_ssize_t i; jstring code_jstr; if (PyObject_AsCharBuffer(code, &code_cstr, &i) == 0) { char code_cstr0[i+1]; //code_cstr might not be null-terminated strcpy(code_cstr0, code_cstr); code_jstr = (*env)->NewStringUTF(env, code_cstr0); } else { PyErr_BadInternalCall(); return NULL; } // jobject jConsts = NULL; // if (PyTuple_GET_SIZE(consts)) // { // (*env)->NewObjectArray(env, PyTuple_GET_SIZE(consts), pyObjectClass, NULL); // for (i = 0; i < PyTuple_GET_SIZE(consts); ++i) // (*env)->SetObjectArrayElement(env, jConsts, i, // JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(consts, i))); // } pyTuple2jArray(consts, pyObjectClass, jConsts); // jobject jNames = (*env)->NewObjectArray(env, PyTuple_GET_SIZE(names), pyStringClass, NULL); // for (i = 0; i < PyTuple_GET_SIZE(names); ++i) // (*env)->SetObjectArrayElement(env, jNames, i, // JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(names, i))); pyTuple2jArray(names, pyStringClass, jNames); // jobject jVarnames = (*env)->NewObjectArray(env, PyTuple_GET_SIZE(varnames), pyStringClass, NULL); // for (i = 0; i < PyTuple_GET_SIZE(varnames); ++i) // (*env)->SetObjectArrayElement(env, jVarnames, i, // JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(varnames, i))); pyTuple2jArray(varnames, pyStringClass, jVarnames); // jobject jFreevars = (*env)->NewObjectArray(env, PyTuple_GET_SIZE(freevars), pyStringClass, NULL); // for (i = 0; i < PyTuple_GET_SIZE(freevars); ++i) // (*env)->SetObjectArrayElement(env, jFreevars, i, // JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(freevars, i))); pyTuple2jArray(freevars, pyStringClass, jFreevars); // jobject jCellvars = (*env)->NewObjectArray(env, PyTuple_GET_SIZE(cellvars), pyStringClass, NULL); // for (i = 0; i < PyTuple_GET_SIZE(cellvars); ++i) // (*env)->SetObjectArrayElement(env, jCellvars, i, // JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(cellvars, i))); pyTuple2jArray(cellvars, pyStringClass, jCellvars); jobject jFilename = JyNI_JythonPyObject_FromPyObject(filename); jobject jName = JyNI_JythonPyObject_FromPyObject(name); jobject jLnotab = JyNI_JythonPyObject_FromPyObject(lnotab); jobject result = (*env)->NewObject(env, pyBytecodeClass, pyBytecodeConstructor, argcount, nlocals, stacksize, flags, code_jstr, jConsts, jNames, jVarnames, jFilename, jName, firstlineno, jLnotab, jCellvars, jFreevars); return (PyCodeObject*) JyNI_PyObject_FromJythonPyObject(result); // intern_strings(names); // intern_strings(varnames); // intern_strings(freevars); // intern_strings(cellvars); /* Intern selected string constants */ // for (i = PyTuple_Size(consts); --i >= 0; ) { // PyObject *v = PyTuple_GetItem(consts, i); // if (!PyString_Check(v)) // continue; // if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) // continue; // PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); // } // co = PyObject_NEW(PyCodeObject, &PyCode_Type); // if (co != NULL) { // co->co_argcount = argcount; // co->co_nlocals = nlocals; // co->co_stacksize = stacksize; // co->co_flags = flags; // Py_INCREF(code); // co->co_code = code; // Py_INCREF(consts); // co->co_consts = consts; // Py_INCREF(names); // co->co_names = names; // Py_INCREF(varnames); // co->co_varnames = varnames; // Py_INCREF(freevars); // co->co_freevars = freevars; // Py_INCREF(cellvars); // co->co_cellvars = cellvars; // Py_INCREF(filename); // co->co_filename = filename; // Py_INCREF(name); // co->co_name = name; // co->co_firstlineno = firstlineno; // Py_INCREF(lnotab); // co->co_lnotab = lnotab; // co->co_zombieframe = NULL; // co->co_weakreflist = NULL; // } // return co; }
jobject JySync_Init_JyStaticMethod_From_PyStaticMethod(PyObject* src, jclass subtype) { env(NULL); return (*env)->NewObject(env, pyStaticMethodClass, pyStaticMethod_Constructor, JyNI_JythonPyObject_FromPyObject(((staticmethod*) src)->sm_callable)); }
PyObject * PyErr_NewException(char *name, PyObject *base, PyObject *dict) { char *dot; PyObject *modulename = NULL; PyObject *classname = NULL; PyObject *mydict = NULL; PyObject *bases = NULL; PyObject *result = NULL; dot = strrchr(name, '.'); if (dot == NULL) { PyErr_SetString(PyExc_SystemError, "PyErr_NewException: name must be module.class"); return NULL; } if (base == NULL) base = PyExc_Exception; if (dict == NULL) { dict = mydict = PyDict_New(); if (dict == NULL) goto failure; } if (PyDict_GetItemString(dict, "__module__") == NULL) { modulename = PyString_FromStringAndSize(name, (Py_ssize_t)(dot-name)); if (modulename == NULL) goto failure; if (PyDict_SetItemString(dict, "__module__", modulename) != 0) goto failure; } //JyNI note: if ob_type is NULL, we assume that base is a Type object that was //not initialized because of our use-on-demand philosophy regarding PyType_Ready. if (base->ob_type == NULL)// && PyType_Check(base)) PyType_Ready((PyTypeObject*) base); if (PyTuple_Check(base)) { bases = base; /* INCREF as we create a new ref in the else branch */ Py_INCREF(bases); } else { bases = PyTuple_Pack(1, base); if (bases == NULL) goto failure; } //puts("enter JNI part of exception creation..."); /* Create a real new-style class. */ //TODO clean this up. There should be a method to create objects like is done below. env(NULL); jobject jbases = (*env)->NewObjectArray(env, PyTuple_GET_SIZE(bases), pyObjectClass, NULL); if ((*env)->ExceptionCheck(env)) { jputs("Exception on creating jbases call:"); jobject exc = (*env)->ExceptionOccurred(env); JyNI_jprintJ(exc); (*env)->ExceptionClear(env); } int i; for (i = 0; i < PyTuple_GET_SIZE(bases); ++i) (*env)->SetObjectArrayElement(env, jbases, i, JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(bases, i))); if ((*env)->ExceptionCheck(env)) { jputs("Exception on storing in jbases call:"); jobject exc = (*env)->ExceptionOccurred(env); JyNI_jprintJ(exc); (*env)->ExceptionClear(env); } // jputs("PyErrNewException:"); // if (name) jputs(name); // else jputs("name is NULL"); jobject jres = (*env)->CallStaticObjectMethod(env, pyPyClass, pyPyMakeClass, (*env)->NewStringUTF(env, name), jbases, JyNI_JythonPyObject_FromPyObject(dict)); if ((*env)->ExceptionCheck(env)) { jputs("Exception on makeClass call:"); jobject exc = (*env)->ExceptionOccurred(env); JyNI_jprintJ(exc); (*env)->ExceptionClear(env); } //result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO", dot+1, bases, dict); result = JyNI_PyObject_FromJythonPyObject(jres); // jputs("control name"); // jputs(((PyTypeObject*) result)->tp_name); failure: Py_XDECREF(bases); Py_XDECREF(mydict); Py_XDECREF(classname); Py_XDECREF(modulename); // if (result == NULL) puts("exception result is NULL"); // else puts("exception result is not NULL"); return result; }