Example #1
0
int
PyCell_Set(PyObject *op, PyObject *obj)
{
	if (!PyCell_Check(op)) {
		PyErr_BadInternalCall();
		return -1;
	} else {
		PyObject* oldobj;
		JyObject* jy;
		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);
		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;
	}
}
Example #2
0
static int
func_set_defaults(PyFunctionObject *op, PyObject *value)
{
	PyObject *tmp;

	if (restricted())
		return -1;
	/* Legal to del f.func_defaults.
	 * Can only set func_defaults to NULL or a tuple. */
	if (value == Py_None)
		value = NULL;
	if (value != NULL && !PyTuple_Check(value)) {
		PyErr_SetString(PyExc_TypeError,
						"__defaults__ must be set to a tuple object");
		return -1;
	}
	env(-1);
	jobject jFunc = JyNI_JythonPyObject_FromPyObject(op);
	jobject jDefaults = JyNI_JythonPyObject_FromPyObject(value);
	(*env)->CallObjectMethod(env, jFunc, pyFunctionSetFuncDefaults, jDefaults);
	if ((*env)->ExceptionCheck(env))
	{
		jputs("Exception in func_set_defaults");
		(*env)->ExceptionClear(env);
		return -1;
	}
	tmp = op->func_defaults;
	Py_XINCREF(value);
	op->func_defaults = value;
	updateJyGCHeadLink(op, AS_JY_WITH_GC(op), func_defaults_gcindex,
						value, AS_JY_WITH_GC(value));
	Py_XDECREF(tmp);
	return 0;
}
Example #3
0
int
PyFunction_SetClosure(PyObject *op, PyObject *closure)
{
	if (!PyFunction_Check(op)) {
		PyErr_BadInternalCall();
		return -1;
	}
	if (closure == Py_None)
		closure = NULL;
	else if (PyTuple_Check(closure)) {
		Py_INCREF(closure);
	}
	else {
		PyErr_Format(PyExc_SystemError,
					 "expected tuple for closure, got '%.100s'",
					 closure->ob_type->tp_name);
		return -1;
	}
	Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
	((PyFunctionObject *) op) -> func_closure = closure;
	updateJyGCHeadLink(op, AS_JY_WITH_GC(op), func_closure_gcindex,
				closure, AS_JY_WITH_GC(closure));

	env(-1);
	jobject jOp = JyNI_JythonPyObject_FromPyObject(op);
	jobject jClosure = JyNI_JythonPyObject_FromPyObject(closure);
	(*env)->SetObjectField(env, jOp, pyFunctionFuncClosure, jClosure);

	return 0;
}
Example #4
0
int
PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
{
	if (!PyFunction_Check(op)) {
		PyErr_BadInternalCall();
		return -1;
	}
	if (defaults == Py_None)
		defaults = NULL;
	else if (defaults && PyTuple_Check(defaults)) {
		Py_INCREF(defaults);
	}
	else {
		PyErr_SetString(PyExc_SystemError, "non-tuple default args");
		return -1;
	}
	Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
	((PyFunctionObject *) op) -> func_defaults = defaults;
	updateJyGCHeadLink(op, AS_JY_WITH_GC(op), func_defaults_gcindex,
			defaults, AS_JY_WITH_GC(defaults));

	env(-1);
	jobject jOp = JyNI_JythonPyObject_FromPyObject(op);
	jobject jDefaults = JyNI_JythonPyObject_FromPyObject(defaults);
	(*env)->CallVoidMethod(env, jOp, pyFunctionSetFuncDefaults, jDefaults);

	return 0;
}
Example #5
0
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;
}
Example #6
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");
}