From 0c35e2ff00934c8d50281ffde38fe49807f1ceed 2006-01-18 07:33:38
From: fperez
Date: 2006-01-18 07:33:38
Subject: [PATCH] - fix missing __file__ for scripts run via  %run.

- fix bug with '%run -d' under python 2.4

---

diff --git a/IPython/Debugger.py b/IPython/Debugger.py
index 0ca2037..d04075f 100644
--- a/IPython/Debugger.py
+++ b/IPython/Debugger.py
@@ -15,7 +15,7 @@ details on the PSF (Python Software Foundation) standard license, see:
 
 http://www.python.org/2.2.3/license.html
 
-$Id: Debugger.py 994 2006-01-08 08:29:44Z fperez $"""
+$Id: Debugger.py 1029 2006-01-18 07:33:38Z fperez $"""
 
 #*****************************************************************************
 #
@@ -66,12 +66,22 @@ def _file_lines(fname):
 
 class Pdb(pdb.Pdb):
     """Modified Pdb class, does not load readline."""
+
+    # Ugly hack: we can't call the parent constructor, because it binds
+    # readline and breaks tab-completion.  This means we have to COPY the
+    # constructor here, and that requires tracking various python versions.
+    
     def __init__(self,color_scheme='NoColor'):
         bdb.Bdb.__init__(self)
         cmd.Cmd.__init__(self,completekey=None) # don't load readline
         self.prompt = 'ipdb> ' # The default prompt is '(Pdb)'
         self.aliases = {}
 
+        # These two lines are part of the py2.4 constructor, let's put them
+        # unconditionally here as they won't cause any problems in 2.3.
+        self.mainpyfile = ''
+        self._wait_for_mainpyfile = 0
+
         # Read $HOME/.pdbrc and ./.pdbrc
         try:
             self.rcLines = _file_lines(os.path.join(os.environ['HOME'],
diff --git a/IPython/Magic.py b/IPython/Magic.py
index f5b0fe2..f159c49 100644
--- a/IPython/Magic.py
+++ b/IPython/Magic.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 """Magic functions for InteractiveShell.
 
-$Id: Magic.py 1023 2006-01-16 19:11:21Z vivainio $"""
+$Id: Magic.py 1029 2006-01-18 07:33:38Z fperez $"""
 
 #*****************************************************************************
 #       Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -1396,6 +1396,10 @@ Currently the magic system has the following functions:\n"""
                 name = '__main__'
             prog_ns = {'__name__':name}
 
+        # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
+        # set the __file__ global in the script's namespace
+        prog_ns['__file__'] = filename
+        
         # pickle fix.  See iplib for an explanation.  But we need to make sure
         # that, if we overwrite __main__, we replace it at the end
         if prog_ns['__name__'] == '__main__':
diff --git a/doc/ChangeLog b/doc/ChangeLog
index cce4bb7..b30bfcd 100644
--- a/doc/ChangeLog
+++ b/doc/ChangeLog
@@ -1,3 +1,15 @@
+2006-01-17  Fernando Perez  <Fernando.Perez@colorado.edu>
+
+	* IPython/Magic.py (magic_run): fix __file__ global missing from
+	script's namespace when executed via %run.  After a report by
+	Vivian.
+
+	* IPython/Debugger.py (Pdb.__init__): Fix breakage with '%run -d'
+	when using python 2.4.  The parent constructor changed in 2.4, and
+	we need to track it directly (we can't call it, as it messes up
+	readline and tab-completion inside our pdb would stop working).
+	After a bug report by R. Bernstein <rocky-AT-panix.com>.
+
 2006-01-16  Ville Vainio  <vivainio@gmail.com>
 
 	* Ipython/magic.py:Reverted back to old %edit functionality