[IronPython] Issues with "exec X in Y"

Jonathan Jacobs korpse-ironpython at kaydash.za.net
Sat Jul 23 06:30:49 PDT 2005


Me again,

I ran across a couple of issues using the above syntax. I usually use it 
when making use of "config" files which are just Python source files:
   config = {}
   exec file('config.py') in config
   del config['__builtins__']

1.) Specifying locals or globals broke things. As far as I can tell this 
is because the CodeGen.emitCall call (called from ExecStmt.Emit) was 
being asked to find a method that took two objects of type "object", 
instead of two objects of type "object" and type "IDictionary<object, 
object>", respectively, of which there are none.

2.) The "exec" statement didn't deal with file objects at all.

I'm not sure whether anybody is aware of these, so I'm not sure if 
they've already been taken care of. I've attached a patch to address 
these issues. My patch however doesn't add a "__builtins__" key to the 
mappable types, so the above code would actually error out in IronPython 
with my patch, so someone probably wants to add that.

In order to make my life easier, I also implemented the "name" property 
on file objects.

Hopefully this helps someone.

--
Jonathan
-------------- next part --------------
diff -ru IronPython-0.7.6-orig\IronPython\AST\Stmt.cs IronPython-0.7.6\IronPython\AST\Stmt.cs
--- IronPython-0.7.6-orig\IronPython\AST\Stmt.cs	Thu Jun 09 15:30:26 2005
+++ IronPython-0.7.6\IronPython\AST\Stmt.cs	Sat Jul 23 15:11:05 2005
@@ -764,11 +764,12 @@
 				cg.emitCall(typeof(Ops), "Exec", new Type[] { typeof(object), typeof(PythonModule) });
 			} else if (globals == null) {
 				locals.Emit(cg);
-				cg.emitCall(typeof(Ops), "Exec", new Type[] { typeof(object), typeof(object) });
+                               cg.emitCall(typeof(Ops), "Exec", new Type[] { typeof(object), typeof(System.Collections.Generic.IDictionary<object, object>) });
 			} else {
 				locals.Emit(cg);
 				globals.Emit(cg);
-				cg.emitCall(typeof(Ops), "Exec", new Type[] { typeof(object), typeof(object), typeof(object) });
+                               Type objDict = typeof(System.Collections.Generic.IDictionary<object, object>);
+                               cg.emitCall(typeof(Ops), "Exec", new Type[] { typeof(object), objDict, objDict });
 			}			
 		}
diff -ru IronPython-0.7.6-orig\IronPython\Objects\Ops.cs IronPython-0.7.6\IronPython\Objects\Ops.cs
--- IronPython-0.7.6-orig\IronPython\Objects\Ops.cs	Sun Jun 12 17:01:42 2005
+++ IronPython-0.7.6\IronPython\Objects\Ops.cs	Sat Jul 23 12:25:59 2005
@@ -2201,6 +2208,10 @@
                 Parser p = Parser.fromString((string)code);
                 Stmt s = p.parseStmt();
                 code = SnippetMaker.Generate(s, "<exec>", false);
+            }
+            if (code is PythonFile) {
+                Parser p = Parser.fromFile((code as PythonFile).name);
+                code = SnippetMaker.Generate(p.parseFileInput(), "<exec>", false);
             }
             FrameCode fc = (FrameCode)code;
             Frame frame = new Frame(null, globals, locals);
diff -ru IronPython-0.7.6-orig\IronPython\Objects\PythonFile.cs IronPython-0.7.6\IronPython\Objects\PythonFile.cs
--- IronPython-0.7.6-orig\IronPython\Objects\PythonFile.cs	Mon Jun 13 16:17:38 2005
+++ IronPython-0.7.6\IronPython\Objects\PythonFile.cs	Sat Jul 23 15:20:36 2005
@@ -168,6 +168,12 @@
 
         public bool softspace = false;
 
+        public string name {
+            get {
+                return fstream.Name;
+            }
+        }
+
         public PythonFile(Stream stream, string mode, bool binary) {
             this.stream = stream;
             this.mode = mode;
@@ -297,7 +303,7 @@
         }
 
         public override string ToString() {
-            return string.Format("<file '{0}', {1}>", fstream.Name, mode);
+            return string.Format("<file '{0}', {1}>", name, mode);
         }
     }
 }


More information about the users-ironpython.com mailing list