예제 #1
0
파일: sliceobject.c 프로젝트: chauhraj/JyNI
PyObject *
PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
{
	if (step == NULL) step = Py_None;
	Py_INCREF(step);
	if (start == NULL) start = Py_None;
	Py_INCREF(start);
	if (stop == NULL) stop = Py_None;
	Py_INCREF(stop);
	env(NULL);
	PySliceObject *obj = (PySliceObject*) _JyObject_New(&PySlice_Type, &builtinTypes[37]);
	JyObject* jy = AS_JY_NO_GC(obj);
	jy->jy = (*env)->NewObject(env, pySliceClass, pySliceFromStartStopStepConstructor,
			JyNI_JythonPyObject_FromPyObject(start),
			JyNI_JythonPyObject_FromPyObject(stop),
			JyNI_JythonPyObject_FromPyObject(step));
	jy->flags |= JY_INITIALIZED_FLAG_MASK;
//	PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);
//
//	if (obj == NULL)
//		return NULL;
//
//	if (step == NULL) step = Py_None;
//	Py_INCREF(step);
//	if (start == NULL) start = Py_None;
//	Py_INCREF(start);
//	if (stop == NULL) stop = Py_None;
//	Py_INCREF(stop);
//
//	obj->step = step;
//	obj->start = start;
//	obj->stop = stop;
//
	return (PyObject *) obj;
}
예제 #2
0
파일: JySync.c 프로젝트: pombredanne/JyNI
/*
 * This function returns a NEW reference, i.e. caller must decref it in the end.
 */
PyObject* JySync_Init_PyString_From_JyString(jobject src, PyTypeObject* nonNativeSubtype)
{
	env(NULL);
	jstring jstr = (*env)->CallObjectMethod(env, src, pyObject_asString);
	//cstr_from_jstring(cstr, jstr);

	//cstr_from_jstring(cstrName, jstr)
	char* utf_string = (*env)->GetStringUTFChars(env, jstr, NULL);
	size_t len = strlen(utf_string);
	char cstr[len+1];
	strcpy(cstr, utf_string);
	(*env)->ReleaseStringUTFChars(env, jstr, utf_string);
	if (len == 1 && !nonNativeSubtype) {
		PyObject* result = PyString_FromString(cstr);
		if (isPreAllocatedJythonString(src, cstr[0]))
			/* The JY_CACHE_ETERNAL-flag tells JyNI permanently that accessing
			 * AS_JY_NO_GC(blah)->jy is safe, i.e. the reference cannot be
			 * garbage-collected on Java-side. Usually methods in JySync should
			 * not set flags or perform JyObject initialization. However setting
			 * this flag is only feasible in JySync.c as the decision is very
			 * type-specific. So we set it here to tell the caller.
			 */
			AS_JY_NO_GC(result)->flags |= JY_CACHE_ETERNAL_FLAG_MASK;
		return result;
	} else {
		PyObject* result = PyString_FromString(cstr);
		if (nonNativeSubtype) ((PyStringObject *) result)->ob_sstate = SSTATE_NOT_INTERNED;
		return result;
	}
}
예제 #3
0
파일: JySync.c 프로젝트: pombredanne/JyNI
jobject JySync_Init_JyString_From_PyString(PyObject* src, jclass subtype)
{
	//todo: check interned-regulations on jython-side
	env(NULL);
	jstring jstr = (*env)->NewStringUTF(env, PyString_AS_STRING(src));
	if (JyNI_HasJyAttribute(AS_JY_NO_GC(src), JyAttributeStringInterned))
		jstr = (*env)->CallObjectMethod(env, jstr, string_intern);
	return (*env)->CallStaticObjectMethod(env, pyPyClass, pyPy_newString, jstr);
}
예제 #4
0
파일: frameobject.c 프로젝트: Stewori/JyNI
static void
frame_dealloc(PyFrameObject *f)
{
	JyObject* jy;
	JyNIDebugOp(JY_NATIVE_FINALIZE, f, -1);
	jy = AS_JY_NO_GC(f);
	JyNI_CleanUp_JyObject(jy);
	PyObject_RawFree(jy);
//	PyObject **p, **valuestack;
//	PyCodeObject *co;
//
//	PyObject_GC_UnTrack(f);
//	Py_TRASHCAN_SAFE_BEGIN(f)
//	/* Kill all local variables */
//	valuestack = f->f_valuestack;
//	for (p = f->f_localsplus; p < valuestack; p++)
//		Py_CLEAR(*p);
//
//	/* Free stack */
//	if (f->f_stacktop != NULL) {
//		for (p = valuestack; p < f->f_stacktop; p++)
//			Py_XDECREF(*p);
//	}
//
//	Py_XDECREF(f->f_back);
//	Py_DECREF(f->f_builtins);
//	Py_DECREF(f->f_globals);
//	Py_CLEAR(f->f_locals);
//	Py_CLEAR(f->f_trace);
//	Py_CLEAR(f->f_exc_type);
//	Py_CLEAR(f->f_exc_value);
//	Py_CLEAR(f->f_exc_traceback);
//
//	co = f->f_code;
//	if (co->co_zombieframe == NULL)
//		co->co_zombieframe = f;
//	else if (numfree < PyFrame_MAXFREELIST) {
//		++numfree;
//		f->f_back = free_list;
//		free_list = f;
//	}
//	else
//		PyObject_GC_Del(f);
//
//	Py_DECREF(co);
//	Py_TRASHCAN_SAFE_END(f)
}
예제 #5
0
파일: JySync.c 프로젝트: pombredanne/JyNI
/*
 * This function returns a NEW reference, i.e. caller must decref it in the end.
 */
PyObject* JySync_Init_PyInt_From_JyInt(jobject src, PyTypeObject* nonNativeSubtype)
{
	env(NULL);
	//return PyInt_FromLong((long) (*env)->CallLongMethod(env, src, pyIntAsLong));
	jint value = (*env)->CallIntMethod(env, src, pyInt_getValue);
	PyObject* result = PyInt_FromLong((long) value);
	if (!nonNativeSubtype && isPreAllocatedJythonInt(src, value))
		/* The JY_CACHE_ETERNAL-flag tells JyNI permanently that accessing
		 * AS_JY_NO_GC(blah)->jy is safe, i.e. the reference cannot be
		 * garbage-collected on Java-side. Usually methods in JySync should
		 * not set flags or perform JyObject initialization. However setting
		 * this flag is only feasible in JySync.c as the decision is very
		 * type-specific. So we set it here to tell the caller.
		 */
		AS_JY_NO_GC(result)->flags |= JY_CACHE_ETERNAL_FLAG_MASK;
	return result;
}
예제 #6
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);
	jobject pyStr = (*env)->CallObjectMethod(env, JyNI_JythonPyObject_FromPyObject(m), pyObject___getattr__,
			(*env)->CallObjectMethod(env, (*env)->NewStringUTF(env, "__file__"), string_intern));
	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));
		//todo: fix this later
		//PyErr_SetString(PyExc_SystemError, "module filename missing");
		return NULL;
	}
	jstring er = (*env)->CallObjectMethod(env, pyStr, pyObject_asString);
	global_cstr_from_jstring(cstr, er);
	JyNI_AddOrSetJyAttributeWithFlags(AS_JY_NO_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);
    */
}
예제 #7
0
파일: funcobject.c 프로젝트: clonly/JyNI
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;
}
예제 #8
0
파일: sliceobject.c 프로젝트: chauhraj/JyNI
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);
}
예제 #9
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);*/
}