Ejemplo n.º 1
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;
}
Ejemplo n.º 2
0
/*
 * 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);
    */
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
/*
 * 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);*/
}
Ejemplo n.º 5
0
void JySync_PyType_From_JyType(jobject src, PyObject* dest)
{
//	jputs(__FUNCTION__);
//	JyNI_jprintJ(src);
	PyTypeObject* tp = (PyTypeObject*) dest;
	env();
	//name:
	jobject jtmp = (*env)->CallObjectMethod(env, src, pyType_getName);
	//jobject jtmp = (*env)->GetObjectField(env, src, pyTypeNameField);
	//if (!jtmp) jputs("JySync_PyType_From_JyType: type with NULL-name!");
	//cstr_from_jstring(cname, jname);
	char* utf_string = (*env)->GetStringUTFChars(env, jtmp, NULL);
	char* cname = malloc(strlen(utf_string)+1);
	strcpy(cname, utf_string);
	(*env)->ReleaseStringUTFChars(env, jtmp, utf_string);
//	jboolean dbg = strcmp(cname, "Class") == 0;
//	if (dbg) {
//		jputs(__FUNCTION__);
//		jputs(cname);
//		jputsLong(dest);
//		jPrintCStackTrace();
//	}
	JyNI_AddOrSetJyAttributeWithFlags(AS_JY(dest), JyAttributeTypeName, cname, JY_ATTR_OWNS_VALUE_FLAG_MASK);
	tp->tp_name = cname;

	//SyncFunctions* sync = (SyncFunctions*) JyNI_GetJyAttribute(AS_JY_WITH_GC(dest), JyAttributeSyncFunctions);

	//dict:
	jtmp = (*env)->CallObjectMethod(env, src, pyObject_fastGetDict);
	tp->tp_dict = JyNI_PyObject_FromJythonPyObject(jtmp);

	//base:
	jtmp = (*env)->CallObjectMethod(env, src, pyType_getBase);
	tp->tp_base = (PyTypeObject*) JyNI_PyObject_FromJythonPyObject(jtmp);

	// Note that basicsize must be set before bases, because some bases access basicsize
	// during convertion to native objects.
	//basicsize:
	tp->tp_basicsize = tp->tp_base == Py_None ? sizeof(PyObject) : tp->tp_base->tp_basicsize;

	//bases:
	jtmp = (*env)->CallObjectMethod(env, src, pyType_getBases);
	tp->tp_bases = JyNI_PyObject_FromJythonPyObject(jtmp);

	//We try to get away with just setting this to default for now:
	tp->tp_flags |= Py_TPFLAGS_DEFAULT;
//	jputsLong(tp->tp_flags);// & Py_TPFLAGS_HAVE_CLASS);
	//jputsLong(tp->tp_mro);
	//if (!tp->tp_alloc)
	if (!(tp->tp_flags & Py_TPFLAGS_READY))
	{
		if (tp->tp_base == Py_None)
			tp->tp_base = NULL;
		PyType_Ready(tp);
		//tp->tp_flags = (tp->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
//		JyNI_GC_ExploreObject(tp);
	} //else puts("already ready");
//	jputs("sync mro...");
//	jputsLong(tp);
	//mro:
	if (!tp->tp_mro)
	{
		jtmp = (*env)->CallObjectMethod(env, src, pyType_getMro);
		PyObject* mro = JySync_Init_PyTuple_From_JyTupleForMRO(jtmp);
		PyTuple_SET_ITEM(mro, 0, tp);
		tp->tp_mro = mro;

		// Currently tp_traverse is out-commented.
		// Once we tested and stabilized heap-type exploration
		// the following section must be included here, because
		// we changed mro after initial exploration in PyType_Ready.
		/* tp_traverse visits type members in this order:
		 *	Py_VISIT(type->tp_dict);
		 *	Py_VISIT(type->tp_cache);
		 *	Py_VISIT(type->tp_mro);
		 *	Py_VISIT(type->tp_bases);
		 *	Py_VISIT(type->tp_base);
		 * so mro-index is 2.
		 */
		if (!IS_UNEXPLORED(tp))
			updateJyGCHeadLink(tp, AS_JY_WITH_GC(tp), 2 /* mro-index */,
					tp->tp_mro, AS_JY_WITH_GC(tp->tp_mro));
	}
//	jputs("type-sync done");
}