Ejemplo n.º 1
0
static int
func_set_code(PyFunctionObject *op, PyObject *value)
{
    Py_ssize_t nfree, nclosure;

    /* 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,
                     "%U() requires a code object with %zd free vars,"
                     " not %zd",
                     op->func_name,
                     nclosure, nfree);
        return -1;
    }
    Py_INCREF(value);
    Py_XSETREF(op->func_code, value);
    return 0;
}
Ejemplo n.º 2
0
DEFINEFN
vinfo_t* pfunction_simple_call(PsycoObject* po, PyObject* f,
                               vinfo_t* arg, bool allow_inline)
{
	PyObject* glob;
	PyObject* defl;
	PyCodeObject* co;
	vinfo_t* fglobals;
	vinfo_t* fdefaults;
	vinfo_t* result;
	int saved_inlining;

	/* XXX we capture the code object, so this breaks if someone
	   changes the .func_code attribute of the function later */
	co = (PyCodeObject*) PyFunction_GET_CODE(f);
	if (PyCode_GetNumFree(co) > 0)
		goto fallback;
	
	glob = PyFunction_GET_GLOBALS(f);
	defl = PyFunction_GET_DEFAULTS(f);
	Py_INCREF(glob);
	fglobals = vinfo_new(CompileTime_NewSk(sk_new
					       ((long)glob, SkFlagPyObj)));
	if (defl == NULL)
		fdefaults = psyco_vi_Zero();
	else {
		Py_INCREF(defl);
		fdefaults = vinfo_new(CompileTime_NewSk(sk_new
						     ((long)defl, SkFlagPyObj)));
	}

	saved_inlining = po->pr.is_inlining;
	if (!allow_inline)
		po->pr.is_inlining = true;
	result = psyco_call_pyfunc(po, co, fglobals, fdefaults,
				   arg, po->pr.auto_recursion);
	po->pr.is_inlining = saved_inlining;
	vinfo_decref(fdefaults, po);
	vinfo_decref(fglobals, po);
	return result;

 fallback:
	return psyco_generic_call(po, PyFunction_Type.tp_call,
				  CfReturnRef|CfPyErrIfNull,
				  "lvl", (long) f, arg, 0);
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
Archivo: Builtins.c Proyecto: 87/cython
static PyObject* __Pyx_PyExec3(PyObject* o, PyObject* globals, PyObject* locals) {
    PyObject* result;
    PyObject* s = 0;
    char *code = 0;

    if (!globals || globals == Py_None) {
        globals = PyModule_GetDict($module_cname);
        if (!globals)
            goto bad;
    } else if (!PyDict_Check(globals)) {
        PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s",
                     globals->ob_type->tp_name);
        goto bad;
    }
    if (!locals || locals == Py_None) {
        locals = globals;
    }


    if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
        PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
    }

    if (PyCode_Check(o)) {
        if (PyCode_GetNumFree((PyCodeObject *)o) > 0) {
            PyErr_SetString(PyExc_TypeError,
                "code object passed to exec() may not contain free variables");
            goto bad;
        }
        #if PY_VERSION_HEX < 0x030200B1
        result = PyEval_EvalCode((PyCodeObject *)o, globals, locals);
        #else
        result = PyEval_EvalCode(o, globals, locals);
        #endif
    } else {
        PyCompilerFlags cf;
        cf.cf_flags = 0;
        if (PyUnicode_Check(o)) {
            cf.cf_flags = PyCF_SOURCE_IS_UTF8;
            s = PyUnicode_AsUTF8String(o);
            if (!s) goto bad;
            o = s;
        #if PY_MAJOR_VERSION >= 3
        } else if (!PyBytes_Check(o)) {
        #else
        } else if (!PyString_Check(o)) {
        #endif
            PyErr_SetString(PyExc_TypeError,
                "exec: arg 1 must be string, bytes or code object");
            goto bad;
        }
        #if PY_MAJOR_VERSION >= 3
        code = PyBytes_AS_STRING(o);
        #else
        code = PyString_AS_STRING(o);
        #endif
        if (PyEval_MergeCompilerFlags(&cf)) {
            result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
        } else {
            result = PyRun_String(code, Py_file_input, globals, locals);
        }
        Py_XDECREF(s);
    }

    return result;
bad:
    Py_XDECREF(s);
    return 0;
}