[Python-checkins] r63913 - in python/branches/tlee-ast-optimize: Include/compile.h Python/compile.c

thomas.lee python-checkins at python.org
Tue Jun 3 11:19:26 CEST 2008


Author: thomas.lee
Date: Tue Jun  3 11:19:25 2008
New Revision: 63913

Log:
Implement PyAST_CompileEx, allowing symtable construction to occur without necessarily performing a compile.

Modified:
   python/branches/tlee-ast-optimize/Include/compile.h
   python/branches/tlee-ast-optimize/Python/compile.c

Modified: python/branches/tlee-ast-optimize/Include/compile.h
==============================================================================
--- python/branches/tlee-ast-optimize/Include/compile.h	(original)
+++ python/branches/tlee-ast-optimize/Include/compile.h	Tue Jun  3 11:19:25 2008
@@ -27,10 +27,18 @@
 #define FUTURE_PRINT_FUNCTION "print_function"
 #define FUTURE_UNICODE_LITERALS "unicode_literals"
 
+typedef struct {
+    const char*       ci_filename;
+    PyFutureFeatures* ci_future;
+    struct symtable*  ci_symtable;
+    PyCompilerFlags*  ci_flags;
+} PyCompilerInfo;
 
 struct _mod; /* Declare the existence of this type */
 PyAPI_FUNC(PyCodeObject *) PyAST_Compile(struct _mod *, const char *,
 					PyCompilerFlags *, PyArena *);
+PyAPI_FUNC(PyCodeObject *) PyAST_CompileEx(struct _mod *,
+                    PyCompilerInfo *, PyArena *);
 PyAPI_FUNC(PyFutureFeatures *) PyFuture_FromAST(struct _mod *, const char *);
 
 #define ERR_LATE_FUTURE \

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	Tue Jun  3 11:19:25 2008
@@ -241,13 +241,13 @@
 }
 
 PyCodeObject *
-PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
-	      PyArena *arena)
+PyAST_CompileEx(mod_ty mod, PyCompilerInfo* info, PyArena* arena)
 {
 	struct compiler c;
 	PyCodeObject *co = NULL;
 	PyCompilerFlags local_flags;
 	int merged;
+    PyCompilerFlags* flags = info->ci_flags;
 
 	if (!__doc__) {
 		__doc__ = PyBytes_InternFromString("__doc__");
@@ -257,9 +257,9 @@
 
 	if (!compiler_init(&c))
 		return NULL;
-	c.c_filename = filename;
+	c.c_filename = info->ci_filename;
 	c.c_arena = arena;
-	c.c_future = PyFuture_FromAST(mod, filename);
+	c.c_future = info->ci_future;
 	if (c.c_future == NULL)
 		goto finally;
 	if (!flags) {
@@ -272,7 +272,7 @@
 	c.c_flags = flags;
 	c.c_nestlevel = 0;
 
-	c.c_st = PySymtable_Build(mod, filename, c.c_future);
+	c.c_st = PySymtable_Build(mod, info->ci_filename, c.c_future);
 	if (c.c_st == NULL) {
 		if (!PyErr_Occurred())
 			PyErr_SetString(PyExc_SystemError, "no symtable");
@@ -291,6 +291,32 @@
 }
 
 PyCodeObject *
+PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
+	      PyArena *arena)
+{
+    PyCompilerInfo info;
+    PyCodeObject* result;
+
+    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;
+}
+
+PyCodeObject *
 PyNode_Compile(struct _node *n, const char *filename)
 {
 	PyCodeObject *co = NULL;
@@ -311,10 +337,6 @@
 static void
 compiler_free(struct compiler *c)
 {
-	if (c->c_st)
-		PySymtable_Free(c->c_st);
-	if (c->c_future)
-		PyObject_Free(c->c_future);
 	Py_DECREF(c->c_stack);
 }
 


More information about the Python-checkins mailing list