From a2d83e4158d221f765cf85f3ce163a81b6a47cff 2011-10-08 15:53:51 From: Thomas Kluyver Date: 2011-10-08 15:53:51 Subject: [PATCH] Fix almost all IPython.core tests for Python 3. --- diff --git a/IPython/core/oinspect.py b/IPython/core/oinspect.py index 8d22e09..80b9c2f 100644 --- a/IPython/core/oinspect.py +++ b/IPython/core/oinspect.py @@ -344,8 +344,7 @@ class Inspector: if init_ds is not None: lines.append(head("Constructor Docstring:")) lines.append(indent(init_ds)) - elif (type(obj) is types.InstanceType or isinstance(obj,object)) \ - and hasattr(obj,'__call__'): + elif hasattr(obj,'__call__'): call_ds = getdoc(obj.__call__) if call_ds: lines.append(head("Calling Docstring:")) diff --git a/IPython/core/tests/test_handlers.py b/IPython/core/tests/test_handlers.py index 7696620..016764a 100644 --- a/IPython/core/tests/test_handlers.py +++ b/IPython/core/tests/test_handlers.py @@ -12,6 +12,7 @@ from IPython.core import autocall from IPython.testing import decorators as dec from IPython.testing import tools as tt from IPython.testing.globalipapp import get_ipython +from IPython.utils import py3compat #----------------------------------------------------------------------------- # Globals diff --git a/IPython/core/tests/test_history.py b/IPython/core/tests/test_history.py index bb73be5..64dff11 100644 --- a/IPython/core/tests/test_history.py +++ b/IPython/core/tests/test_history.py @@ -77,9 +77,9 @@ def test_history(): # Cross testing: check that magic %save can get previous session. testfilename = os.path.realpath(os.path.join(tmpdir, "test.py")) ip.magic_save(testfilename + " ~1/1-3") - testfile = open(testfilename, "r") - nt.assert_equal(testfile.read().decode("utf-8"), - "# coding: utf-8\n" + "\n".join(hist)) + with py3compat.open(testfilename) as testfile: + nt.assert_equal(testfile.read(), + u"# coding: utf-8\n" + u"\n".join(hist)) # Duplicate line numbers - check that it doesn't crash, and # gets a new session diff --git a/IPython/core/tests/test_iplib.py b/IPython/core/tests/test_iplib.py index 5e6d5d6..e7b7c08 100644 --- a/IPython/core/tests/test_iplib.py +++ b/IPython/core/tests/test_iplib.py @@ -15,6 +15,7 @@ import nose.tools as nt # our own packages from IPython.testing import decorators as dec from IPython.testing.globalipapp import get_ipython +from IPython.utils import py3compat #----------------------------------------------------------------------------- # Globals diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 504e509..102f05d 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -216,11 +216,6 @@ def test_paste(): original_clip = hooks.clipboard_get try: - # This try/except with an emtpy except clause is here only because - # try/yield/finally is invalid syntax in Python 2.4. This will be - # removed when we drop 2.4-compatibility, and the emtpy except below - # will be changed to a finally. - # Run tests with fake clipboard function user_ns.pop('x', None) paste('x=1') @@ -263,7 +258,6 @@ def test_paste(): yield nt.assert_equal(out, code+"\n## -- End pasted text --\n") finally: - # This should be in a finally clause, instead of the bare except above. # Restore original hook hooks.clipboard_get = original_clip diff --git a/IPython/core/tests/test_run.py b/IPython/core/tests/test_run.py index 7cbbb54..f3c197d 100644 --- a/IPython/core/tests/test_run.py +++ b/IPython/core/tests/test_run.py @@ -21,7 +21,7 @@ from nose import SkipTest from IPython.testing import decorators as dec from IPython.testing import tools as tt -from IPython.utils.py3compat import doctest_refactor_print +from IPython.utils import py3compat #----------------------------------------------------------------------------- # Test functions begin @@ -85,7 +85,7 @@ def doctest_run_builtins(): ....: """ -@doctest_refactor_print +@py3compat.doctest_refactor_print def doctest_reset_del(): """Test that resetting doesn't cause errors in __del__ methods. @@ -168,7 +168,7 @@ class TestMagicRunSimple(tt.TempFileMixin): " def __del__(self):\n" " print 'object A deleted'\n" "a = A()\n") - self.mktmp(src) + self.mktmp(py3compat.doctest_refactor_print(src)) tt.ipexec_validate(self.fname, 'object A deleted') @dec.skip_known_failure @@ -186,7 +186,7 @@ class TestMagicRunSimple(tt.TempFileMixin): " ip.magic('run %s')\n" " except NameError, e:\n" " print i;break\n" % empty.fname) - self.mktmp(src) + self.mktmp(py3compat.doctest_refactor_print(src)) _ip.magic('run %s' % self.fname) _ip.run_cell('ip == get_ipython()') tt.assert_equals(_ip.user_ns['i'], 5) @@ -200,11 +200,11 @@ class TestMagicRunSimple(tt.TempFileMixin): "%%run '%s' C-third\n") % (tc, tc, tc) self.mktmp(src, '.ipy') out = """\ -ARGV 1-: [u'C-first'] -ARGV 1-: [u'C-second'] +ARGV 1-: [{u}'C-first'] +ARGV 1-: [{u}'C-second'] tclass.py: deleting object: C-first -ARGV 1-: [u'C-third'] +ARGV 1-: [{u}'C-third'] tclass.py: deleting object: C-second tclass.py: deleting object: C-third """ - tt.ipexec_validate(self.fname, out) + tt.ipexec_validate(self.fname, py3compat.u_format(out)) diff --git a/IPython/testing/_paramtestpy3.py b/IPython/testing/_paramtestpy3.py index 200ca0d..661fcad 100644 --- a/IPython/testing/_paramtestpy3.py +++ b/IPython/testing/_paramtestpy3.py @@ -46,9 +46,11 @@ class ParametricTestCase(unittest.TestCase): # For normal tests, we just call the base class and return that if isgenerator(testMethod): def adapter(next_test): - return unittest.FunctionTestCase(next_test, - self.setUp, - self.tearDown) + ftc = unittest.FunctionTestCase(next_test, + self.setUp, + self.tearDown) + self._nose_case = ftc # Nose 1.0 rejects the test without this + return ftc return IterCallableSuite(testMethod(),adapter).run(result) else: diff --git a/IPython/testing/tools.py b/IPython/testing/tools.py index abe2be3..6c634aa 100644 --- a/IPython/testing/tools.py +++ b/IPython/testing/tools.py @@ -48,6 +48,7 @@ from IPython.config.loader import Config from IPython.utils.process import find_cmd, getoutputerror from IPython.utils.text import list_strings from IPython.utils.io import temp_pyfile +from IPython.utils.py3compat import PY3 from . import decorators as dec from . import skipdoctest @@ -209,7 +210,7 @@ def ipexec(fname, options=None): _ip = get_ipython() test_dir = os.path.dirname(__file__) - ipython_cmd = find_cmd('ipython') + ipython_cmd = find_cmd('ipython3' if PY3 else 'ipython') # Absolute path for filename full_fname = os.path.join(test_dir, fname) full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname) diff --git a/IPython/utils/_process_posix.py b/IPython/utils/_process_posix.py index 0a5ca87..ff6bd25 100644 --- a/IPython/utils/_process_posix.py +++ b/IPython/utils/_process_posix.py @@ -25,6 +25,7 @@ from IPython.external import pexpect from .autoattr import auto_attr from ._process_common import getoutput from IPython.utils import text +from IPython.utils import py3compat #----------------------------------------------------------------------------- # Function definitions @@ -33,8 +34,9 @@ from IPython.utils import text def _find_cmd(cmd): """Find the full path to a command using which.""" - return sp.Popen(['/usr/bin/env', 'which', cmd], + path = sp.Popen(['/usr/bin/env', 'which', cmd], stdout=sp.PIPE).communicate()[0] + return py3compat.bytes_to_str(path) class ProcessHandler(object): diff --git a/IPython/utils/pickleshare.py b/IPython/utils/pickleshare.py index 1afcb50..2a1658a 100755 --- a/IPython/utils/pickleshare.py +++ b/IPython/utils/pickleshare.py @@ -67,7 +67,7 @@ class PickleShareDB(collections.MutableMapping): return self.cache[fil][0] try: # The cached item has expired, need to read - obj = pickle.load(fil.open()) + obj = pickle.load(fil.open("rb")) except: raise KeyError(key) @@ -80,7 +80,7 @@ class PickleShareDB(collections.MutableMapping): parent = fil.parent if parent and not parent.isdir(): parent.makedirs() - pickled = pickle.dump(value,fil.open('w')) + pickled = pickle.dump(value,fil.open('wb')) try: self.cache[fil] = (value,fil.mtime) except OSError,e: