From 5cd888d60e6542f29d1cb48f86a83c67b3c76b11 2010-04-25 04:08:47 From: Mark E. Smith Date: 2010-04-25 04:08:47 Subject: [PATCH] Fix broken demo reload(). Thanks to a patch by Mark E. Smith (https://launchpad.net/~mark-e-smith) Closes https://bugs.launchpad.net/ipython/+bug/532145 --- diff --git a/IPython/lib/demo.py b/IPython/lib/demo.py index 992cd20..746188a 100644 --- a/IPython/lib/demo.py +++ b/IPython/lib/demo.py @@ -222,7 +222,6 @@ class Demo(object): """ if hasattr(src, "read"): # It seems to be a file or a file-like object - self.fobj = src self.fname = "from a file-like object" if title == '': self.title = "from a file-like object" @@ -230,7 +229,6 @@ class Demo(object): self.title = title else: # Assume it's a string or something that can be converted to one - self.fobj = open(src) self.fname = src if title == '': (filepath, filename) = os.path.split(src) @@ -239,6 +237,7 @@ class Demo(object): self.title = title self.sys_argv = [src] + shlex.split(arg_str) self.auto_all = auto_all + self.src = src # get a few things from ipython. While it's a bit ugly design-wise, # it ensures that things like color scheme and the like are always in @@ -257,6 +256,15 @@ class Demo(object): def reload(self): """Reload source from disk and initialize state.""" # read data and parse into blocks + if hasattr(self, 'fobj') and self.fobj is not None: + self.fobj.close() + if hasattr(self.src, "read"): + # It seems to be a file or a file-like object + self.fobj = self.src + else: + # Assume it's a string or something that can be converted to one + self.fobj = open(self.fname) + self.src = self.fobj.read() src_b = [b.strip() for b in self.re_stop.split(self.src) if b] self._silent = [bool(self.re_silent.findall(b)) for b in src_b]