Example #1
0
/*
 * This function returns a NEW reference, i.e. caller must decref it in the end.
 */
PyObject* JySync_Init_PyMethod_From_JyMethod(jobject src, PyTypeObject* nonNativeSubtype)
{
	env(NULL);
	return PyMethod_New(
		JyNI_PyObject_FromJythonPyObject((*env)->GetObjectField(env, src, pyMethod___func__Field)),
		JyNI_PyObject_FromJythonPyObject((*env)->GetObjectField(env, src, pyMethod___self__Field)),
		JyNI_PyObject_FromJythonPyObject((*env)->GetObjectField(env, src, pyMethod_im_classField)));
}
Example #2
0
/*
 * This function returns a NEW reference, i.e. caller must decref it in the end.
 */
PyObject* JySync_Init_PyInstance_From_JyInstance(jobject src, PyTypeObject* nonNativeSubtype)
{
	env(NULL);
	(*env)->CallStaticVoidMethod(env, JyNIClass, JyNI_suspendPyInstanceFinalizer, src);
	PyObject* er = PyInstance_NewRaw(
		JyNI_PyObject_FromJythonPyObject((*env)->GetObjectField(env, src, pyInstance_instclassField)),
		JyNI_PyObject_FromJythonPyObject((*env)->GetObjectField(env, src, pyInstance___dict__Field)));
	return er;
}
Example #3
0
static PyObject *
slice_reduce(PySliceObject* self)
{
	env(NULL);
	return Py_BuildValue("O(OOO)", Py_TYPE(self), //self->start, self->stop, self->step);
			JyNI_PyObject_FromJythonPyObject((*env)->CallObjectMethod(env,
					JyNI_JythonPyObject_FromPyObject((PyObject*) self), pySliceGetStart)),
			JyNI_PyObject_FromJythonPyObject((*env)->CallObjectMethod(env,
					JyNI_JythonPyObject_FromPyObject((PyObject*) self), pySliceGetStop)),
			JyNI_PyObject_FromJythonPyObject((*env)->CallObjectMethod(env,
					JyNI_JythonPyObject_FromPyObject((PyObject*) self), pySliceGetStep)));
}
Example #4
0
/*
 * This function returns a NEW reference, i.e. caller must decref it in the end.
 */
PyObject* JySync_Init_PyClass_From_JyClass(jobject src, PyTypeObject* nonNativeSubtype)
{
	env(NULL);
	jstring nm = (*env)->GetObjectField(env, src, pyClass___name__Field);
	cstr_from_jstring(cnm, nm);
//	jputs("Sync-Class:");
//	jputs(cnm);
	return PyClass_New(
		JyNI_PyObject_FromJythonPyObject((*env)->GetObjectField(env, src, pyClass___bases__Field)),
		JyNI_PyObject_FromJythonPyObject((*env)->GetObjectField(env, src, pyClass___dict__Field)),
		PyString_FromString(cnm));
}
Example #5
0
PyObject *
_PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
{
	PyObject* m = JyNI_PyObject_FromJythonPyObject(_PyImport_LoadDynamicModuleJy(name, pathname, fp));
	Py_INCREF(m);
	return m;
}
Example #6
0
PyObject* PyCode_Get_co_code(PyObject* code)
{
	env(NULL);
	jobject jCode = JyNI_JythonPyObject_FromPyObject(code);
	jobject result = (*env)->CallStaticObjectMethod(env, JyNIClass, JyNI_pyCode_co_code, jCode);
	return JyNI_PyObject_FromJythonPyObject(result);
}
Example #7
0
PyObject *
PySys_GetObject(char *name)
{
	JNIEnv *env;
	if ((*java)->GetEnv(java, (void **)&env, JNI_VERSION_1_2)) {
		return NULL; // JNI version not supported
	}
	jobject nameStr = (*env)->NewStringUTF(env, name);
	PyObject* er = (PyObject*) (*env)->CallStaticLongMethod(env, JyNIClass, JyNIGetJyObjectByName, nameStr);
	if (er != NULL)
		return  er;
	else
	{
		jobject result = (*env)->CallStaticObjectMethod(env, JyNIClass, JyNIGetPyObjectByName, nameStr);
		if (result == NULL)
			return NULL;
		else
		{
			//puts("PySys_GetObject creates new object:");
			//puts(name);
			return JyNI_PyObject_FromJythonPyObject(result);
		}
	}
	/*PyThreadState *tstate = PyThreadState_GET();
	PyObject *sd = tstate->interp->sysdict;
	if (sd == NULL)
		return NULL;
	return PyDict_GetItemString(sd, name);*/
}
Example #8
0
PyObject *
PyModule_New(const char *name)
{
	env(NULL);
	return JyNI_PyObject_FromJythonPyObject(
			(*env)->NewObject(env, pyModuleClass, pyModule_byStringConstructor,
			(*env)->CallObjectMethod(env, (*env)->NewStringUTF(env, name), string_intern)));

    /*PyModuleObject *m;
    PyObject *nameobj;
    m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
    if (m == NULL)
        return NULL;
    nameobj = PyString_FromString(name);
    m->md_dict = PyDict_New();
    if (m->md_dict == NULL || nameobj == NULL)
        goto fail;
    if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
        goto fail;
    if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
        goto fail;
    if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0)
        goto fail;
    Py_DECREF(nameobj);
    PyObject_GC_Track(m);
    return (PyObject *)m;

 fail:
    Py_XDECREF(nameobj);
    Py_DECREF(m);
    return NULL;*/
}
Example #9
0
static PyObject *
slice_get_step(PySliceObject *m, void *closure)
{
	env(NULL);
	return JyNI_PyObject_FromJythonPyObject(
			(*env)->CallObjectMethod(env, JyNI_JythonPyObject_FromPyObject((PyObject*) m), pySliceGetStep));
}
Example #10
0
/* Lookup the error handling callback function registered under the
   name error. As a special case NULL can be passed, in which case
   the error handling callback for strict encoding will be returned. */
PyObject *PyCodec_LookupError(const char *name)
{
	env(NULL);
	jobject er = (*env)->CallStaticObjectMethod(env, pyCodecsClass, pyCodecsLookupError,
		(*env)->NewStringUTF(env, name));
	jstring tpName = (*env)->CallObjectMethod(env,
				(*env)->CallObjectMethod(env, er, pyObjectGetType),
				pyTypeGetName);
	//puts("tp name obtained:");
	jputsLong(__LINE__);
	cstr_from_jstring(cName, tpName);
	jputs(cName);
	return JyNI_PyObject_FromJythonPyObject(er);
//    PyObject *handler = NULL;
//
//    PyInterpreterState *interp = PyThreadState_GET()->interp;
//    if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
//        return NULL;
//
//    if (name==NULL)
//        name = "strict";
//    handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name);
//    if (!handler)
//        PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
//    else
//        Py_INCREF(handler);
//    return handler;
}
Example #11
0
PyObject *
PyFunction_New(PyObject *code, PyObject *globals)
{
	env(NULL);
	jobject jCode = JyNI_JythonPyObject_FromPyObject(code);
	jobject jGlobals = JyNI_JythonPyObject_FromPyObject(globals);
	jobject result = (*env)->NewObject(env, pyFunctionClass, pyFunctionConstructor, jGlobals, NULL, jCode);
	return JyNI_PyObject_FromJythonPyObject(result);
//	PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
//										&PyFunction_Type);
//	static PyObject *__name__ = 0;
//	if (op != NULL) {
//		PyObject *doc;
//		PyObject *consts;
//		PyObject *module;
//		op->func_weakreflist = NULL;
//		Py_INCREF(code);
//		op->func_code = code;
//		Py_INCREF(globals);
//		op->func_globals = globals;
//		op->func_name = ((PyCodeObject *)code)->co_name;
//		Py_INCREF(op->func_name);
//		op->func_defaults = NULL; /* No default arguments */
//		op->func_closure = NULL;
//		consts = ((PyCodeObject *)code)->co_consts;
//		if (PyTuple_Size(consts) >= 1) {
//			doc = PyTuple_GetItem(consts, 0);
//			if (!PyString_Check(doc) && !PyUnicode_Check(doc))
//				doc = Py_None;
//		}
//		else
//			doc = Py_None;
//		Py_INCREF(doc);
//		op->func_doc = doc;
//		op->func_dict = NULL;
//		op->func_module = NULL;
//
//		/* __module__: If module name is in globals, use it.
//		   Otherwise, use None.
//		*/
//		if (!__name__) {
//			__name__ = PyString_InternFromString("__name__");
//			if (!__name__) {
//				Py_DECREF(op);
//				return NULL;
//			}
//		}
//		module = PyDict_GetItem(globals, __name__);
//		if (module) {
//			Py_INCREF(module);
//			op->func_module = module;
//		}
//	}
//	else
//		return NULL;
//	_JyNI_GC_TRACK(op);
//	return (PyObject *)op;
}
Example #12
0
PyObject* JySync_Init_PyCFunction_From_JyBuiltinCallable(jobject src, PyTypeObject* nonNativeSubtype)
{
//	jputs(__FUNCTION__);
	env(NULL);
	if ((*env)->IsInstanceOf(env, src, pyCFunctionClass))
	{
		jputs("JyNI-warning: JySync_Init_PyCFunction_From_JyBuiltinCallable shouldn't be called with PyCFunction.");
	}
	PyMethodDef* mdef = malloc(sizeof(PyMethodDef));
	jobject info = (*env)->GetObjectField(env, src, pyBuiltinCallable_infoField);
	jint max = (*env)->CallIntMethod(env, info, pyBuiltinCallableInfo_getMaxargs);
	mdef->ml_flags = (max ? (METH_KEYWORDS | METH_VARARGS) : METH_NOARGS) | METH_JYTHON;

	jstring jtmp = (*env)->CallObjectMethod(env, info, pyBuiltinCallableInfo_getName);
	global_cstr_from_jstring(cName, jtmp);
	mdef->ml_name = cName;
//	puts(cName);

	jtmp = (*env)->CallObjectMethod(env, src, pyBuiltinCallable_getDoc);
	if (jtmp)
	{
		global_cstr_from_jstring2(cDoc, jtmp);
		mdef->ml_doc = cDoc;
	} else mdef->ml_doc = NULL;

	mdef->ml_meth = (PyCFunctionWithKeywords) jyBuiltinCallWithKeywords;

	jobject jmodule = (*env)->CallObjectMethod(env, src, pyBuiltinCallable_getModule);
	jobject jself = (*env)->CallObjectMethod(env, src, pyBuiltinCallable_getSelf);
	// Account for incompatible behavior:
	// In Jython an unbound method has self = None while in CPython self = NULL
	PyObject* mself = (*env)->IsSameObject(env, jself, JyNone) ? NULL :
			JyNI_PyObject_FromJythonPyObject(jself);
//	putsPy(mself);
	PyCFunctionObject* res = PyCFunction_NewEx(mdef, mself,
			JyNI_PyObject_FromJythonPyObject(jmodule));
	JyNI_AddOrSetJyAttributeWithFlags(AS_JY(res), JyAttributeMethodName,
			cName, JY_ATTR_OWNS_VALUE_FLAG_MASK);
	if (mdef->ml_doc)
		JyNI_AddOrSetJyAttributeWithFlags(AS_JY(res), JyAttributeMethodDoc,
				mdef->ml_doc, JY_ATTR_OWNS_VALUE_FLAG_MASK);
	JyNI_AddOrSetJyAttributeWithFlags(AS_JY(res), JyAttributeMethodDef,
			mdef, JY_ATTR_OWNS_VALUE_FLAG_MASK);
	return (PyObject*) res;
}
Example #13
0
PyObject* JySync_Init_PyProperty_From_JyProperty(jobject src, PyTypeObject* nonNativeSubtype)
{
	env(NULL);
	propertyobject* result = PyObject_GC_New(propertyobject, &PyProperty_Type);
	if (result != NULL) {
		result->prop_get = JyNI_PyObject_FromJythonPyObject(
				(*env)->GetObjectField(env, src, pyProperty_fgetField));
		result->prop_set = JyNI_PyObject_FromJythonPyObject(
				(*env)->GetObjectField(env, src, pyProperty_fsetField));
		result->prop_del = JyNI_PyObject_FromJythonPyObject(
				(*env)->GetObjectField(env, src, pyProperty_fdelField));
		result->prop_doc = JyNI_PyObject_FromJythonPyObject(
				(*env)->GetObjectField(env, src, pyProperty_docField));
		result->getter_doc = (*env)->GetBooleanField(env, src, pyProperty_docFromGetterField);
		_JyNI_GC_TRACK(result);
	}
	return (PyObject *) result;
}
Example #14
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;
}
Example #15
0
static void
slice_dealloc(PySliceObject *r)
{
	JyObject* jy = AS_JY_NO_GC(r);
	if (JyObject_IS_INITIALIZED(jy))
	{
		env();
		Py_DECREF(JyNI_PyObject_FromJythonPyObject((*env)->CallObjectMethod(env, jy->jy, pySliceGetStart)));
		Py_DECREF(JyNI_PyObject_FromJythonPyObject((*env)->CallObjectMethod(env, jy->jy, pySliceGetStop)));
		Py_DECREF(JyNI_PyObject_FromJythonPyObject((*env)->CallObjectMethod(env, jy->jy, pySliceGetStep)));
//		Py_DECREF(r->step);
//		Py_DECREF(r->start);
//		Py_DECREF(r->stop);
//		PyObject_Del(r);
	}
	JyNI_CleanUp_JyObject(jy);
	//PyObject_Del(jy);
	PyObject_RawFree(jy);
}
Example #16
0
//PyObject* JySync_Init_PyFunction_From_JyFunction(jobject src)
void JySync_PyFunction_From_JyFunction(jobject src, PyObject* dest)
{
//Need to sync:
//func_code, func_globals, func_module, func_defaults, func_closure
	env();
	jobject jCode = (*env)->GetObjectField(env, src, pyFunction___code__Field);
	PyObject* pt = JyNI_PyObject_FromJythonPyObject(jCode);
	((PyFunctionObject*) dest)->func_code = pt;
	jobject jGlobals = (*env)->CallObjectMethod(env, src, pyFunction_getFuncGlobals);
	((PyFunctionObject*) dest)->func_globals = JyNI_PyObject_FromJythonPyObject(jGlobals);
	jobject jModule = (*env)->GetObjectField(env, src, pyFunction___module__Field);
	((PyFunctionObject*) dest)->func_module = JyNI_PyObject_FromJythonPyObject(jModule);
	jobject jDefaults = (*env)->CallObjectMethod(env, src, pyFunction_getFuncDefaults);
	((PyFunctionObject*) dest)->func_defaults = JyNI_PyObject_FromJythonPyObject(jDefaults);
	jobject jClosure = (*env)->GetObjectField(env, src, pyFunction___closure__Field);
	((PyFunctionObject*) dest)->func_closure = JyNI_PyObject_FromJythonPyObject(jClosure);
	jstring jName = (*env)->GetObjectField(env, src, pyFunction___name__Field);
	cstr_from_jstring(cName, jName);
	((PyFunctionObject*) dest)->func_name = PyString_FromString(cName);
}
Example #17
0
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);
}
Example #18
0
/*
 * This function returns a NEW reference, i.e. caller must decref it in the end.
 */
PyObject* JySync_Init_PyList_From_JyList(jobject src, PyTypeObject* nonNativeSubtype)
{
	env(NULL);
	//maybe optimize a bit more here:
	//if most objects in the list have not yet been converted,
	//much lookup is done twice here.
	jarray handles = (*env)->CallStaticObjectMethod(env, JyNIClass, JyNI_lookupNativeHandles, src);
	jsize size = (*env)->GetArrayLength(env, handles);
	PyObject* op = PyList_New((Py_ssize_t) size);
	(*env)->CallStaticObjectMethod(env, JyNIClass, JyNI_setNativeHandle, src, (jlong) op);//, JNI_FALSE);
	AS_JY_WITH_GC(op)->flags |= JY_HAS_JHANDLE_FLAG_MASK;
	jsize i;
	PyObject* v;
	jlong* arr = (*env)->GetLongArrayElements(env, handles, NULL);
	for (i = 0; i < size; ++i)
	{
		v = (PyObject*) arr[i];
		if (!v) v = JyNI_PyObject_FromJythonPyObject((*env)->CallObjectMethod(env, src, pySequence_pyget, i));
		else Py_INCREF(v);
//		if (!v) v = _JyNI_PyObject_FromJythonPyObject(
//				(*env)->CallObjectMethod(env, src, pyListPyGet, i), JNI_FALSE, JNI_FALSE, JNI_TRUE);
//JyNI_PyObject_FromJythonPyObject(jobject jythonPyObject, jboolean lookupNative, jboolean checkCPeer, jboolean checkForType)
		PyList_SET_ITEM(op, i, v);
	}
	//jputsLong(__LINE__);
	(*env)->ReleaseLongArrayElements(env, handles, arr, JNI_ABORT);
	JyNI_GC_ExploreObject(op);
	//JyNI_GC_ExploreObject should have initialized JyGCHead to a JyList-object.
	jobject jyList = (*env)->NewLocalRef(env, (jobject) JyObject_GetJyGCHead(op, AS_JY_WITH_GC(op)));
	if (!jyList || (*env)->IsSameObject(env, jyList, NULL)) {
		jputs("This should not happen! JyList gc-head was deleted after explore.");
		jputs(__FUNCTION__);
		if (!jyList) jputs("truely NULL");
		else (*env)->DeleteWeakGlobalRef(env, jyList);
		jputs("Restore it...");
		jyList = (*env)->NewObject(env, JyListClass, JyList_fromBackendHandleConstructor, (jlong) src);
		JyObject_AddOrSetJyGCHead(src, AS_JY_WITH_GC(src), (*env)->NewWeakGlobalRef(env, jyList));
		JyNI_GC_ExploreObject(src);
	}
	//(*env)->CallVoidMethod(env, jyList, JyListInstallToPyList, src);
	(*env)->SetObjectField(env, src, pyList_listField, jyList);
//	jputsLong(__LINE__);
	(*env)->CallObjectMethod(env, jyList, pyObjectGCHead_setPyObject, src);
	(*env)->DeleteLocalRef(env, jyList);

	//Py_INCREF(op); //For now we make the list immortal here. Later GC will take care of it.
	//(should now work without this)
	//jputsLong(__LINE__);
	return op;
}
Example #19
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;
}
Example #20
0
PyObject* JySync_Init_PyTuple_From_JyTupleForMRO(jobject src)
{
	env(NULL);
	jint srcSize = (*env)->CallIntMethod(env, src, collection_size);
	PyObject* er = PyTuple_New(srcSize);
	(*env)->CallStaticObjectMethod(env, JyNIClass, JyNI_setNativeHandle, src, (jlong) er);
	AS_JY_WITH_GC(er)->flags |= JY_HAS_JHANDLE_FLAG_MASK;
	int i;
	for (i = 1; i < srcSize; ++i)
	{
		PyObject* item = JyNI_PyObject_FromJythonPyObject((*env)->CallObjectMethod(env, src, pySequence_pyget, i));
		PyTuple_SET_ITEM(er, i, item);
	}
	return er;
}
Example #21
0
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;
}
Example #22
0
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;
}
Example #23
0
/*
 * This function returns a NEW reference, i.e. caller must decref it in the end.
 */
PyObject* JySync_Init_PyWeakCallableProxy_From_JyWeakCallableProxy(jobject src, PyTypeObject* nonNativeSubtype)
{
	// Todo: Handle case that the native counterpart of the Java-referent is
	//       not weakly referenceable in CPython-terms.
	//       See PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))
	env(NULL);
	jobject jReferent = (*env)->CallObjectMethod(env, src, AbstractReference_get);
	PyObject* referent = JyNI_PyObject_FromJythonPyObject(jReferent);
	/* Note that an extra Py_INCREF is not necessary since the conversion method
	 * returns a new reference.
	 * JyNI-note: It is okay to hold a refcount for the weakly referenced object.
	 * This will be decreffed when the Java-side GlobalRef is released.
	 */
	PyObject* result = PyWeakref_NewProxy(referent, NULL);
	// Todo: Support callback.
	return result;
}
Example #24
0
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);
}
Example #25
0
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;
}
Example #26
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;*/
}
Example #27
0
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);
}
Example #28
0
/*
 * This function returns a NEW reference, i.e. caller must decref it in the end.
 * For every item in the tuple also a new reference is created. However the tuple
 * deallocator will take care of these.
 */
PyObject* JySync_Init_PyTuple_From_JyTuple(jobject src, PyTypeObject* nonNativeSubtype)
{
	//jputs("JySync_Init_PyTuple_From_JyTuple");
	env(NULL);
	//jarray back = (*env)->CallObjectMethod(env, src, pyTupleGetArray);
	jint srcSize = (*env)->CallIntMethod(env, src, collection_size);
	//jputs("### creating tuple...");
	PyObject* er = PyTuple_New(srcSize);
	(*env)->CallStaticObjectMethod(env, JyNIClass, JyNI_setNativeHandle, src, (jlong) er);//, JNI_FALSE);
	AS_JY_WITH_GC(er)->flags |= JY_HAS_JHANDLE_FLAG_MASK;
	//jputs("### created tuple...");
	//Py_XINCREF(er);
	//if (srcSize != PyTuple_GET_SIZE(dest)) //...throw exception since tuple is immutable
	int i;
	for (i = 0; i < srcSize; ++i)
	{
		//jputsLong(i);
		PyObject* item = JyNI_PyObject_FromJythonPyObject((*env)->CallObjectMethod(env, src, pySequence_pyget, i));
		//Py_XINCREF(item);
		//PyTuple_SetItem(er, i, item);
		PyTuple_SET_ITEM(er, i, item);
//		if (!item) {
//			jputs("Add null-item to tuple:");
//			jputsLong(er);
//			jobject jNull = (*env)->CallObjectMethod(env, src, pyTuplePyGet, i);
//			if (!jNull) jputs("j also null");
//			else if ((*env)->IsSameObject(env, jNull, NULL)) jputs("j equal null");
//			else {
//				jputs("j not null:");
//				JyNI_printJInfo(jNull);
//			}
//		}
	}
	//JYNI_GC_TUPLE_EXPLORE(er);
	return er;
}
Example #29
0
PyObject* JySync_Init_PyMethodDescr_From_JyMethodDescr(jobject src, PyTypeObject* nonNativeSubtype)
{
//	jputs(__FUNCTION__);
	env(NULL);
	jint max = (*env)->CallIntMethod(env, src, pyBuiltinCallableInfo_getMaxargs);
	//mdef->ml_flags = (max ? (METH_KEYWORDS | METH_VARARGS) : METH_NOARGS) | METH_JYTHON;

	jstring jtmp = (*env)->CallObjectMethod(env, src, pyBuiltinCallableInfo_getName);
	global_cstr_from_jstring(cName, jtmp);
	//mdef->ml_name = cName;

	jobject dtype = (*env)->GetObjectField(env, src, pyDescr_dtypeField);

	PyMethodDef* mdef = malloc(sizeof(PyMethodDef));
	mdef->ml_flags = (max ? (METH_KEYWORDS | METH_VARARGS) : METH_NOARGS)
			| METH_JYTHON | METH_JYTHON_CDEF;
	mdef->ml_name = cName;

	jtmp = (*env)->CallObjectMethod(env, src, pyMethodDescr_getDoc);
	if (jtmp)
	{
		global_cstr_from_jstring2(cDoc, jtmp);
		mdef->ml_doc = cDoc;
	} else mdef->ml_doc = NULL;
	mdef->ml_meth = (PyCFunctionWithKeywords) jyBuiltinCallWithKeywords;

	PyObject* result = PyDescr_NewMethod(JyNI_PyObject_FromJythonPyObject(dtype), mdef);
	JyNI_AddOrSetJyAttributeWithFlags(AS_JY_WITH_GC(result), JyAttributeMethodName,
			cName, JY_ATTR_OWNS_VALUE_FLAG_MASK);
	if (mdef->ml_doc)
		JyNI_AddOrSetJyAttributeWithFlags(AS_JY_WITH_GC(result), JyAttributeMethodDoc,
				mdef->ml_doc, JY_ATTR_OWNS_VALUE_FLAG_MASK);
	JyNI_AddOrSetJyAttributeWithFlags(AS_JY_WITH_GC(result), JyAttributeMethodDef,
			mdef, JY_ATTR_OWNS_VALUE_FLAG_MASK);
	return result;
}
Example #30
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;
}