コード例 #1
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
PyObject *
PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
                  PyObject *locals, int closeit, PyCompilerFlags *flags)
{
    PyObject *ret = NULL;
    mod_ty mod;
    PyArena *arena = NULL;
    PyObject *filename;

    filename = PyUnicode_DecodeFSDefault(filename_str);
    if (filename == NULL)
        goto exit;

    arena = PyArena_New();
    if (arena == NULL)
        goto exit;

    mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
                                     flags, NULL, arena);
    if (closeit)
        fclose(fp);
    if (mod == NULL) {
        goto exit;
    }
    ret = run_mod(mod, filename, globals, locals, flags, arena);

exit:
    Py_XDECREF(filename);
    if (arena != NULL)
        PyArena_Free(arena);
    return ret;
}
コード例 #2
0
ファイル: pyinjectcode.c プロジェクト: albertz/pydbattach
static PyCodeObject *
parse_source_module(const char *pathname, FILE *fp)
{
    PyCodeObject *co = NULL;
    mod_ty mod;
    PyCompilerFlags flags;
    PyArena *arena = PyArena_New();
    if (arena == NULL)
        return NULL;
	
    flags.cf_flags = 0;
	
    mod = PyParser_ASTFromFile(
		fp, pathname,
#if PY_MAJOR_VERSION >= 3
		"utf-8",
#endif
		Py_file_input, 0, 0, &flags,
		NULL, arena);
    if (mod) {
        co = PyAST_Compile(mod, pathname, NULL, arena);
    }
    PyArena_Free(arena);
    return co;
}
コード例 #3
0
ファイル: import_module.c プロジェクト: yuriyao/PS
/*将源文件编译为字节码文件*/
static PyCodeObject*
compile(FILE *fp)
{
	PyCodeObject *co = NULL;
	mod_ty mod;
	PyCompilerFlags flags;
	PyArena *arena = PyArena_New();
	
	if(arena == NULL)
	{
		Ps_Log("Py arena new failed\n", Ps_LOG_WARING);
		return NULL;
	}
	
	flags.cf_flags = 0;
	mod = PyParser_ASTFromFile(fp, "", Py_file_input, 0, 0, &flags, NULL, arena);
	
	if(mod)
	{
		co = PyAST_Compile(mod, "", NULL, arena);
	}
	
	PyArena_Free(arena);
	
	return co;
}
コード例 #4
0
PyObject *
PyRun_StringFlags(const char *str, int start, PyObject *globals,
		  PyObject *locals, PyCompilerFlags *flags)
{
	PyObject *ret = NULL;
	mod_ty mod;
	PyArena *arena = PyArena_New();
	if (arena == NULL)
		return NULL;
	
	mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
	if (mod != NULL)
		ret = run_mod(mod, "<string>", globals, locals, flags, arena);
	PyArena_Free(arena);
	return ret;
}
コード例 #5
0
struct symtable *
Py_SymtableString(const char *str, const char *filename, int start)
{
	struct symtable *st;
	mod_ty mod;
	PyArena *arena = PyArena_New();
	if (arena == NULL)
		return NULL;

	mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
	if (mod == NULL) {
		PyArena_Free(arena);
		return NULL;
	}
	st = PySymtable_Build(mod, filename, 0);
	PyArena_Free(arena);
	return st;
}
コード例 #6
0
ファイル: parsermodule.c プロジェクト: zooba/cpython
/*  parser_compilest(PyObject* self, PyObject* args)
 *
 *  This function creates code objects from the parse tree represented by
 *  the passed-in data object.  An optional file name is passed in as well.
 *
 */
static PyObject*
parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
{
    PyObject*     res = NULL;
    PyArena*      arena = NULL;
    mod_ty        mod;
    PyObject*     filename = NULL;
    int ok;

    static char *keywords[] = {"st", "filename", NULL};

    if (self == NULL || PyModule_Check(self))
        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords,
                                         &PyST_Type, &self,
                                         PyUnicode_FSDecoder, &filename);
    else
        ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1],
                                         PyUnicode_FSDecoder, &filename);
    if (!ok)
        goto error;

    if (filename == NULL) {
        filename = PyUnicode_FromString("<syntax-tree>");
        if (filename == NULL)
            goto error;
    }

    arena = PyArena_New();
    if (!arena)
        goto error;

    mod = PyAST_FromNodeObject(self->st_node, &self->st_flags,
                               filename, arena);
    if (!mod)
        goto error;

    res = (PyObject *)PyAST_CompileObject(mod, filename,
                                          &self->st_flags, -1, arena);
error:
    Py_XDECREF(filename);
    if (arena != NULL)
        PyArena_Free(arena);
    return res;
}
コード例 #7
0
ファイル: typed_ast.c プロジェクト: Michael0x2a/typed_ast
// adapted from Py_CompileStringObject in Python/pythonrun.c
static PyObject *
string_object_to_py_ast(const char *str, PyObject *filename, int start,
                       PyCompilerFlags *flags)
{
    mod_ty mod;
    PyArena *arena = PyArena_New();
    if (arena == NULL)
        return NULL;

    mod = string_object_to_c_ast(str, filename, start, flags, arena);
    if (mod == NULL) {
        PyArena_Free(arena);
        return NULL;
    }

    PyObject *result = Ta27AST_mod2obj(mod);
    PyArena_Free(arena);
    return result;
}
コード例 #8
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
struct symtable *
Py_SymtableStringObject(const char *str, PyObject *filename, int start)
{
    struct symtable *st;
    mod_ty mod;
    PyCompilerFlags flags;
    PyArena *arena;

    arena = PyArena_New();
    if (arena == NULL)
        return NULL;

    flags.cf_flags = 0;
    mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena);
    if (mod == NULL) {
        PyArena_Free(arena);
        return NULL;
    }
    st = PySymtable_BuildObject(mod, filename, 0);
    PyArena_Free(arena);
    return st;
}
コード例 #9
0
PyObject *
PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
		  PyObject *locals, int closeit, PyCompilerFlags *flags)
{
	PyObject *ret;
	mod_ty mod;
	PyArena *arena = PyArena_New();
	if (arena == NULL)
		return NULL;
	
	mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
				   flags, NULL, arena);
	if (closeit)
		fclose(fp);
	if (mod == NULL) {
		PyArena_Free(arena);
		return NULL;
	}
	ret = run_mod(mod, filename, globals, locals, flags, arena);
	PyArena_Free(arena);
	return ret;
}
コード例 #10
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
PyObject *
PyRun_StringFlags(const char *str, int start, PyObject *globals,
                  PyObject *locals, PyCompilerFlags *flags)
{
    PyObject *ret = NULL;
    mod_ty mod;
    PyArena *arena;
    PyObject *filename;

    filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
    if (filename == NULL)
        return NULL;

    arena = PyArena_New();
    if (arena == NULL)
        return NULL;

    mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
    if (mod != NULL)
        ret = run_mod(mod, filename, globals, locals, flags, arena);
    PyArena_Free(arena);
    return ret;
}
コード例 #11
0
ファイル: python_testers.cpp プロジェクト: 9cat/cyphesis
// This function is expanded out from the Python library function
// PyRun_SimpleStringFlags(). We do this so we can tell the difference,
// and fail in the case of a parse error in the code text. The unit tests
// do contain code which should fail, but never any code that fails because
// it won't parse.
int CyPyRun_SimpleString(const char * command, PyObject * exception)
{
    PyCompilerFlags *flags = NULL;
    PyObject * m = PyImport_AddModule("__main__");
    if (m == NULL)
        return -1;
    PyObject * d = PyModule_GetDict(m);
    // v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
    PyArena *arena = PyArena_New();
    if (arena == NULL)
        return -1;

    mod_ty mod = PyParser_ASTFromString(command, "<string>", Py_file_input, flags, arena);
    if (mod == NULL) {
        PyArena_Free(arena);
        PyErr_Print();
        return -2;
    }

    PyObject *ret = run_mod(mod, "<string>", d, d, flags, arena);
    PyArena_Free(arena);

    if (ret == NULL) {
        int errcode = -1;
        if (exception != 0) {
            if (PyErr_ExceptionMatches(exception)) {
                errcode = -3;
            }
        }
        PyErr_Print();
        return errcode;
    }
    Py_DECREF(ret);
    if (Py_FlushLine())
        PyErr_Clear();
    return 0;
}
コード例 #12
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
PyObject *
Py_CompileStringObject(const char *str, PyObject *filename, int start,
                       PyCompilerFlags *flags, int optimize)
{
    PyCodeObject *co;
    mod_ty mod;
    PyArena *arena = PyArena_New();
    if (arena == NULL)
        return NULL;

    mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
    if (mod == NULL) {
        PyArena_Free(arena);
        return NULL;
    }
    if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
        PyObject *result = PyAST_mod2obj(mod);
        PyArena_Free(arena);
        return result;
    }
    co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
    PyArena_Free(arena);
    return (PyObject *)co;
}
コード例 #13
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
/* A PyRun_InteractiveOneObject() auxiliary function that does not print the
 * error on failure. */
static int
PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
                             PyCompilerFlags *flags)
{
    PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
    mod_ty mod;
    PyArena *arena;
    const char *ps1 = "", *ps2 = "", *enc = NULL;
    int errcode = 0;
    _Py_IDENTIFIER(encoding);
    _Py_IDENTIFIER(__main__);

    mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
    if (mod_name == NULL) {
        return -1;
    }

    if (fp == stdin) {
        /* Fetch encoding from sys.stdin if possible. */
        v = _PySys_GetObjectId(&PyId_stdin);
        if (v && v != Py_None) {
            oenc = _PyObject_GetAttrId(v, &PyId_encoding);
            if (oenc)
                enc = PyUnicode_AsUTF8(oenc);
            if (!enc)
                PyErr_Clear();
        }
    }
    v = _PySys_GetObjectId(&PyId_ps1);
    if (v != NULL) {
        v = PyObject_Str(v);
        if (v == NULL)
            PyErr_Clear();
        else if (PyUnicode_Check(v)) {
            ps1 = PyUnicode_AsUTF8(v);
            if (ps1 == NULL) {
                PyErr_Clear();
                ps1 = "";
            }
        }
    }
    w = _PySys_GetObjectId(&PyId_ps2);
    if (w != NULL) {
        w = PyObject_Str(w);
        if (w == NULL)
            PyErr_Clear();
        else if (PyUnicode_Check(w)) {
            ps2 = PyUnicode_AsUTF8(w);
            if (ps2 == NULL) {
                PyErr_Clear();
                ps2 = "";
            }
        }
    }
    arena = PyArena_New();
    if (arena == NULL) {
        Py_XDECREF(v);
        Py_XDECREF(w);
        Py_XDECREF(oenc);
        return -1;
    }
    mod = PyParser_ASTFromFileObject(fp, filename, enc,
                                     Py_single_input, ps1, ps2,
                                     flags, &errcode, arena);
    Py_XDECREF(v);
    Py_XDECREF(w);
    Py_XDECREF(oenc);
    if (mod == NULL) {
        PyArena_Free(arena);
        if (errcode == E_EOF) {
            PyErr_Clear();
            return E_EOF;
        }
        return -1;
    }
    m = PyImport_AddModuleObject(mod_name);
    if (m == NULL) {
        PyArena_Free(arena);
        return -1;
    }
    d = PyModule_GetDict(m);
    v = run_mod(mod, filename, d, d, flags, arena);
    PyArena_Free(arena);
    if (v == NULL) {
        return -1;
    }
    Py_DECREF(v);
    flush_io();
    return 0;
}
コード例 #14
0
int
PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
{
	PyObject *m, *d, *v, *w;
	mod_ty mod;
	PyArena *arena;
	char *ps1 = "", *ps2 = "";
	int errcode = 0;

	v = PySys_GetObject("ps1");
	if (v != NULL) {
		v = PyObject_Str(v);
		if (v == NULL)
			PyErr_Clear();
		else if (PyString_Check(v))
			ps1 = PyString_AsString(v);
	}
	w = PySys_GetObject("ps2");
	if (w != NULL) {
		w = PyObject_Str(w);
		if (w == NULL)
			PyErr_Clear();
		else if (PyString_Check(w))
			ps2 = PyString_AsString(w);
	}
	arena = PyArena_New();
	if (arena == NULL) {
		Py_XDECREF(v);
		Py_XDECREF(w);
		return -1;
	}
	mod = PyParser_ASTFromFile(fp, filename,
				   Py_single_input, ps1, ps2,
				   flags, &errcode, arena);
	Py_XDECREF(v);
	Py_XDECREF(w);
	if (mod == NULL) {
		PyArena_Free(arena);
		if (errcode == E_EOF) {
			PyErr_Clear();
			return E_EOF;
		}
		PyErr_Print();
		return -1;
	}
	m = PyImport_AddModule("__main__");
	if (m == NULL) {
		PyArena_Free(arena);
		return -1;
	}
	d = PyModule_GetDict(m);
	v = run_mod(mod, filename, d, d, flags, arena);
	PyArena_Free(arena);
	if (v == NULL) {
		PyErr_Print();
		return -1;
	}
	Py_DECREF(v);
	if (Py_FlushLine())
		PyErr_Clear();
	return 0;
}