diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 5826539..1c7a66a 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -27,7 +27,6 @@ from IPython.core.oinspect import find_file, find_source_lines from IPython.testing.skipdoctest import skip_doctest from IPython.utils import py3compat from IPython.utils.contexts import preserve_keys -from IPython.utils.io import file_read from IPython.utils.path import get_py_filename, unquote_filename from IPython.utils.warn import warn @@ -531,7 +530,8 @@ class CodeMagics(Magics): # XXX TODO: should this be generalized for all string vars? # For now, this is special-cased to blocks created by cpaste if args.strip() == 'pasted_block': - self.shell.user_ns['pasted_block'] = file_read(filename) + with open(filename, 'r') as f: + self.shell.user_ns['pasted_block'] = f.read() if 'x' in opts: # -x prevents actual execution print @@ -541,8 +541,9 @@ class CodeMagics(Magics): if not is_temp: self.shell.user_ns['__file__'] = filename if 'r' in opts: # Untranslated IPython code - self.shell.run_cell(file_read(filename), - store_history=False) + with open(filename, 'r') as f: + source = f.read() + self.shell.run_cell(source, store_history=False) else: self.shell.safe_execfile(filename, self.shell.user_ns, self.shell.user_ns) diff --git a/IPython/lib/demo.py b/IPython/lib/demo.py index a094c10..621e44a 100644 --- a/IPython/lib/demo.py +++ b/IPython/lib/demo.py @@ -184,7 +184,6 @@ import shlex import sys from IPython.utils import io -from IPython.utils.io import file_read from IPython.utils.text import marquee from IPython.utils import openpy __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError'] @@ -380,7 +379,8 @@ class Demo(object): filename = self.shell.mktempfile(self.src_blocks[index]) self.shell.hooks.editor(filename,1) - new_block = file_read(filename) + with open(filename, 'r') as f: + new_block = f.read() # update the source and colored block self.src_blocks[index] = new_block self.src_blocks_colored[index] = self.ip_colorize(new_block) diff --git a/IPython/utils/io.py b/IPython/utils/io.py index 90aa59f..77e315d 100644 --- a/IPython/utils/io.py +++ b/IPython/utils/io.py @@ -154,22 +154,6 @@ class Tee(object): self.close() -def file_read(filename): - """Read a file and close it. Returns the file source.""" - fobj = open(filename,'r'); - source = fobj.read(); - fobj.close() - return source - - -def file_readlines(filename): - """Read a file and close it. Returns the file source using readlines().""" - fobj = open(filename,'r'); - lines = fobj.readlines(); - fobj.close() - return lines - - def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): """Take multiple lines of input.