[Python-checkins] r63956 - in python/branches/tlee-ast-optimize: Include/optimize.h Python/bltinmodule.c Python/compile.c Python/import.c Python/optimize.c Python/pythonrun.c
thomas.lee
python-checkins at python.org
Thu Jun 5 17:34:33 CEST 2008
Author: thomas.lee
Date: Thu Jun 5 17:34:32 2008
New Revision: 63956
Log:
Separate symtable generation and compilation. The AST optimizer will run between the two phases. AST optimizer yet to be updated to make use of symtable information.
Modified:
python/branches/tlee-ast-optimize/Include/optimize.h
python/branches/tlee-ast-optimize/Python/bltinmodule.c
python/branches/tlee-ast-optimize/Python/compile.c
python/branches/tlee-ast-optimize/Python/import.c
python/branches/tlee-ast-optimize/Python/optimize.c
python/branches/tlee-ast-optimize/Python/pythonrun.c
Modified: python/branches/tlee-ast-optimize/Include/optimize.h
==============================================================================
--- python/branches/tlee-ast-optimize/Include/optimize.h (original)
+++ python/branches/tlee-ast-optimize/Include/optimize.h Thu Jun 5 17:34:32 2008
@@ -5,7 +5,8 @@
extern "C" {
#endif
-PyAPI_FUNC(int) PyAST_Optimize(mod_ty* mod_ptr, PyArena* arena);
+PyAPI_FUNC(int) PyAST_Optimize(mod_ty* mod_ptr, struct symtable* st,
+ PyArena* arena);
#ifdef __cplusplus
};
Modified: python/branches/tlee-ast-optimize/Python/bltinmodule.c
==============================================================================
--- python/branches/tlee-ast-optimize/Python/bltinmodule.c (original)
+++ python/branches/tlee-ast-optimize/Python/bltinmodule.c Thu Jun 5 17:34:32 2008
@@ -6,6 +6,7 @@
#include "node.h"
#include "code.h"
#include "eval.h"
+#include "symtable.h"
#include "optimize.h"
#include <ctype.h>
@@ -448,6 +449,53 @@
return res;
}
+static PyObject*
+_ast_compile(PyObject *cmd, const char *filename, int mode, int optimize,
+ PyCompilerFlags *cf)
+{
+ PyArena *arena = NULL;
+ struct symtable *st = NULL;
+ PyFutureFeatures *ff = NULL;
+ PyCodeObject* co = NULL;
+ mod_ty mod;
+ PyCompilerInfo ci;
+
+ arena = PyArena_New();
+ mod = PyAST_obj2mod(cmd, arena, mode);
+ if (mod == NULL)
+ goto cleanup;
+ ff = PyFuture_FromAST(mod, filename);
+ if (ff == NULL)
+ goto cleanup;
+ st = PySymtable_Build(mod, filename, ff);
+ if (st == NULL)
+ goto cleanup;
+ if (optimize) {
+ if (!PyAST_Optimize(&mod, st, arena))
+ goto cleanup;
+ }
+
+ ci.ci_filename = filename;
+ ci.ci_future = ff;
+ ci.ci_symtable = st;
+ ci.ci_flags = cf;
+
+ if (!cf || !(cf->cf_flags & PyCF_NO_OPTIMIZE))
+ if (!PyAST_Optimize(&mod, ci.ci_symtable, arena))
+ goto cleanup;
+
+ co = PyAST_CompileEx(mod, &ci, arena);
+
+cleanup:
+ if (ff != NULL)
+ PyObject_Free(ff);
+ if (st != NULL)
+ PySymtable_Free(st);
+ if (arena != NULL)
+ PyArena_Free(arena);
+ return (PyObject*)co;
+}
+
PyDoc_STRVAR(coerce_doc,
"coerce(x, y) -> (x1, y1)\n\
\n\
@@ -513,25 +561,12 @@
result = cmd;
}
else {
- PyArena *arena;
- mod_ty mod;
-
- arena = PyArena_New();
- mod = PyAST_obj2mod(cmd, arena, mode);
- if (mod == NULL) {
- PyArena_Free(arena);
- return NULL;
- }
- if (!(supplied_flags & PyCF_NO_OPTIMIZE)) {
- if (!PyAST_Optimize(&mod, arena)) {
- PyArena_Free(arena);
- return NULL;
- }
- }
- result = (PyObject*)PyAST_Compile(mod, filename, &cf, arena);
- PyArena_Free(arena);
+ result = _ast_compile(cmd, filename, mode,
+ !(supplied_flags & PyCF_NO_OPTIMIZE), &cf);
}
return result;
+
+ /* XXX: this is awful */
}
#ifdef Py_USING_UNICODE
Modified: python/branches/tlee-ast-optimize/Python/compile.c
==============================================================================
--- python/branches/tlee-ast-optimize/Python/compile.c (original)
+++ python/branches/tlee-ast-optimize/Python/compile.c Thu Jun 5 17:34:32 2008
@@ -272,7 +272,7 @@
c.c_flags = flags;
c.c_nestlevel = 0;
- c.c_st = PySymtable_Build(mod, info->ci_filename, c.c_future);
+ c.c_st = info->ci_symtable;
if (c.c_st == NULL) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_SystemError, "no symtable");
@@ -292,28 +292,30 @@
PyCodeObject *
PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
- PyArena *arena)
+ PyArena *arena)
{
- PyCompilerInfo info;
- PyCodeObject* result;
+ PyCompilerInfo ci;
+ PyCodeObject* co = NULL;
- info.ci_filename = filename;
- info.ci_flags = flags;
- info.ci_future = PyFuture_FromAST(mod, filename);
- if (info.ci_future == NULL)
- return NULL;
- info.ci_symtable = PySymtable_Build(mod, filename, info.ci_future);
- if (info.ci_symtable == NULL) {
- PyObject_Free(info.ci_future);
- return NULL;
- }
-
- result = PyAST_CompileEx(mod, &info, arena);
-
- PyObject_Free(info.ci_future);
- PySymtable_Free(info.ci_symtable);
-
- return result;
+ ci.ci_filename = filename;
+ ci.ci_flags = flags;
+ ci.ci_future = NULL;
+ ci.ci_symtable = NULL;
+
+ ci.ci_future = PyFuture_FromAST(mod, filename);
+ if (ci.ci_future == NULL)
+ goto cleanup;
+ ci.ci_symtable = PySymtable_Build(mod, filename, ci.ci_future);
+ if (ci.ci_symtable == NULL)
+ goto cleanup;
+ co = PyAST_CompileEx(mod, &ci, arena);
+
+cleanup:
+ if (ci.ci_future != NULL)
+ PyObject_Free(ci.ci_future);
+ if (ci.ci_symtable != NULL)
+ PySymtable_Free(ci.ci_symtable);
+ return co;
}
PyCodeObject *
@@ -321,15 +323,33 @@
{
PyCodeObject *co = NULL;
mod_ty mod;
+ PyCompilerInfo ci;
PyArena *arena = PyArena_New();
if (!arena)
return NULL;
+
+ ci.ci_filename = filename;
+ ci.ci_future = NULL;
+ ci.ci_symtable = NULL;
+ ci.ci_flags = NULL;
+
mod = PyAST_FromNode(n, NULL, filename, arena);
if (mod != NULL) {
- if (PyAST_Optimize(&mod, arena)) {
- co = PyAST_Compile(mod, filename, NULL, arena);
- }
- }
+ ci.ci_future = PyFuture_FromAST(mod, filename);
+ if (ci.ci_future == NULL)
+ goto cleanup;
+ ci.ci_symtable = PySymtable_Build(mod, filename, ci.ci_future);
+ if (ci.ci_symtable == NULL)
+ goto cleanup;
+ if (!PyAST_Optimize(&mod, ci.ci_symtable, arena))
+ goto cleanup;
+ co = PyAST_CompileEx(mod, &ci, arena);
+ }
+cleanup:
+ if (ci.ci_symtable != NULL)
+ PySymtable_Free(ci.ci_symtable);
+ if (ci.ci_future != NULL)
+ PyObject_Free(ci.ci_future);
PyArena_Free(arena);
return co;
}
Modified: python/branches/tlee-ast-optimize/Python/import.c
==============================================================================
--- python/branches/tlee-ast-optimize/Python/import.c (original)
+++ python/branches/tlee-ast-optimize/Python/import.c Thu Jun 5 17:34:32 2008
@@ -14,6 +14,8 @@
#include "eval.h"
#include "osdefs.h"
#include "importdl.h"
+#include "optimize.h"
+#include "symtable.h"
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
@@ -818,18 +820,36 @@
{
PyCodeObject *co = NULL;
mod_ty mod;
- PyCompilerFlags flags;
+ PyCompilerInfo ci;
+ PyCompilerFlags cf;
PyArena *arena = PyArena_New();
if (arena == NULL)
return NULL;
- flags.cf_flags = 0;
+ ci.ci_filename = pathname;
+ ci.ci_future = NULL;
+ ci.ci_symtable = NULL;
+ ci.ci_flags = &cf;
+ cf.cf_flags = 0;
- mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, &flags,
- NULL, arena);
+ mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0,
+ &cf, NULL, arena);
if (mod) {
- co = PyAST_Compile(mod, pathname, NULL, arena);
- }
+ ci.ci_future = PyFuture_FromAST(mod, pathname);
+ if (ci.ci_future == NULL)
+ goto cleanup;
+ ci.ci_symtable = PySymtable_Build(mod, pathname, ci.ci_future);
+ if (ci.ci_symtable == NULL)
+ goto cleanup;
+ if (!PyAST_Optimize(&mod, ci.ci_symtable, arena))
+ goto cleanup;
+ co = PyAST_CompileEx(mod, &ci, arena);
+ }
+cleanup:
+ if (ci.ci_symtable != NULL)
+ PySymtable_Free(ci.ci_symtable);
+ if (ci.ci_future != NULL)
+ PyObject_Free(ci.ci_future);
PyArena_Free(arena);
return co;
}
Modified: python/branches/tlee-ast-optimize/Python/optimize.c
==============================================================================
--- python/branches/tlee-ast-optimize/Python/optimize.c (original)
+++ python/branches/tlee-ast-optimize/Python/optimize.c Thu Jun 5 17:34:32 2008
@@ -1211,8 +1211,9 @@
* Optimize an AST.
*/
int
-PyAST_Optimize(mod_ty* mod_ptr, PyArena* arena)
+PyAST_Optimize(mod_ty* mod_ptr, struct symtable* st, PyArena* arena)
{
+ /* TODO: update optimize_* functions to accept an ste */
return optimize_mod(mod_ptr, arena);
}
Modified: python/branches/tlee-ast-optimize/Python/pythonrun.c
==============================================================================
--- python/branches/tlee-ast-optimize/Python/pythonrun.c (original)
+++ python/branches/tlee-ast-optimize/Python/pythonrun.c Thu Jun 5 17:34:32 2008
@@ -1325,12 +1325,34 @@
PyCompilerFlags *flags, PyArena *arena)
{
PyCodeObject *co;
- PyObject *v;
- co = PyAST_Compile(mod, filename, flags, arena);
- if (co == NULL)
- return NULL;
- v = PyEval_EvalCode(co, globals, locals);
- Py_DECREF(co);
+ PyObject *v = NULL;
+ PyCompilerInfo ci;
+
+ ci.ci_filename = filename;
+ ci.ci_future = NULL;
+ ci.ci_symtable = NULL;
+ ci.ci_flags = flags;
+
+ ci.ci_future = PyFuture_FromAST(mod, filename);
+ if (ci.ci_future == NULL)
+ goto cleanup;
+ ci.ci_symtable = PySymtable_Build(mod, filename, ci.ci_future);
+ if (ci.ci_symtable == NULL)
+ goto cleanup;
+ if (!flags || !(flags->cf_flags & PyCF_NO_OPTIMIZE))
+ if (!PyAST_Optimize(&mod, ci.ci_symtable, arena))
+ goto cleanup;
+
+ co = PyAST_CompileEx(mod, &ci, arena);
+cleanup:
+ if (ci.ci_symtable != NULL)
+ PySymtable_Free(ci.ci_symtable);
+ if (ci.ci_future != NULL)
+ PyObject_Free(ci.ci_future);
+ if (co != NULL) {
+ v = PyEval_EvalCode(co, globals, locals);
+ Py_DECREF(co);
+ }
return v;
}
@@ -1372,21 +1394,39 @@
{
PyCodeObject *co;
mod_ty mod;
+ PyCompilerInfo ci;
PyArena *arena = PyArena_New();
if (arena == NULL)
return NULL;
+ ci.ci_filename = filename;
+ ci.ci_future = NULL;
+ ci.ci_symtable = NULL;
+ ci.ci_flags = flags;
+
mod = PyParser_ASTFromString(str, filename, start, flags, arena);
- if (mod == NULL) {
- PyArena_Free(arena);
- return NULL;
- }
+ if (mod == NULL)
+ goto cleanup;
+ ci.ci_future = PyFuture_FromAST(mod, filename);
+ if (ci.ci_future == NULL)
+ goto cleanup;
+ ci.ci_symtable = PySymtable_Build(mod, filename, ci.ci_future);
+ if (ci.ci_symtable == NULL)
+ goto cleanup;
+ if (!flags || !(flags->cf_flags & PyCF_NO_OPTIMIZE))
+ if (!PyAST_Optimize(&mod, ci.ci_symtable, arena))
+ goto cleanup;
if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
PyObject *result = PyAST_mod2obj(mod);
PyArena_Free(arena);
return result;
}
- co = PyAST_Compile(mod, filename, flags, arena);
+ co = PyAST_CompileEx(mod, &ci, arena);
+cleanup:
+ if (ci.ci_future != NULL)
+ PyObject_Free(ci.ci_future);
+ if (ci.ci_symtable != NULL)
+ PySymtable_Free(ci.ci_symtable);
PyArena_Free(arena);
return (PyObject *)co;
}
@@ -1431,9 +1471,6 @@
}
mod = PyAST_FromNode(n, flags, filename, arena);
PyNode_Free(n);
- if (mod != NULL && flags && !(flags->cf_flags & PyCF_NO_OPTIMIZE))
- if (!PyAST_Optimize(&mod, arena))
- return NULL;
return mod;
}
else {
@@ -1459,9 +1496,6 @@
}
mod = PyAST_FromNode(n, flags, filename, arena);
PyNode_Free(n);
- if (mod != NULL && flags && !(flags->cf_flags & PyCF_NO_OPTIMIZE))
- if (!PyAST_Optimize(&mod, arena))
- return NULL;
return mod;
}
else {
More information about the Python-checkins
mailing list