Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ef4479

Browse files
committed
remove testing.tools.monkeypatch
use mock.patch.object instead part 1 of reducing dependence on testing.tools
1 parent ce85574 commit 0ef4479

5 files changed

Lines changed: 38 additions & 41 deletions

File tree

IPython/core/tests/test_run.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
import textwrap
2424
import unittest
2525

26+
try:
27+
from unittest.mock import patch
28+
except ImportError:
29+
from mock import patch
30+
2631
import nose.tools as nt
2732
from nose import SkipTest
2833

@@ -448,7 +453,7 @@ def test_prun_submodule_with_relative_import(self):
448453
def with_fake_debugger(func):
449454
@functools.wraps(func)
450455
def wrapper(*args, **kwds):
451-
with tt.monkeypatch(debugger.Pdb, 'run', staticmethod(eval)):
456+
with patch.object(debugger.Pdb, 'run', staticmethod(eval)):
452457
return func(*args, **kwds)
453458
return wrapper
454459

IPython/html/widgets/tests/test_interaction.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
from __future__ import print_function
77

8-
from collections import OrderedDict
8+
try:
9+
from unittest.mock import patch
10+
except ImportError:
11+
from mock import patch
912

1013
import nose.tools as nt
11-
import IPython.testing.tools as tt
1214

1315
from IPython.kernel.comm import Comm
1416
from IPython.html import widgets
@@ -354,7 +356,7 @@ def f(kwarg='default', annotate='default', default='default'):
354356

355357
@nt.with_setup(clear_display)
356358
def test_decorator_kwarg():
357-
with tt.monkeypatch(interaction, 'display', record_display):
359+
with patch.object(interaction, 'display', record_display):
358360
@interact(a=5)
359361
def foo(a):
360362
pass
@@ -373,7 +375,7 @@ def show(self, x):
373375

374376
f = Foo()
375377

376-
with tt.monkeypatch(interaction, 'display', record_display):
378+
with patch.object(interaction, 'display', record_display):
377379
g = interact(f.show, x=(1,10))
378380
nt.assert_equal(len(displayed), 1)
379381
w = displayed[0].children[0]
@@ -384,7 +386,7 @@ def show(self, x):
384386

385387
@nt.with_setup(clear_display)
386388
def test_decorator_no_call():
387-
with tt.monkeypatch(interaction, 'display', record_display):
389+
with patch.object(interaction, 'display', record_display):
388390
@interact
389391
def foo(a='default'):
390392
pass
@@ -399,7 +401,7 @@ def foo(a='default'):
399401
def test_call_interact():
400402
def foo(a='default'):
401403
pass
402-
with tt.monkeypatch(interaction, 'display', record_display):
404+
with patch.object(interaction, 'display', record_display):
403405
ifoo = interact(foo)
404406
nt.assert_equal(len(displayed), 1)
405407
w = displayed[0].children[0]
@@ -412,7 +414,7 @@ def foo(a='default'):
412414
def test_call_interact_kwargs():
413415
def foo(a='default'):
414416
pass
415-
with tt.monkeypatch(interaction, 'display', record_display):
417+
with patch.object(interaction, 'display', record_display):
416418
ifoo = interact(foo, a=10)
417419
nt.assert_equal(len(displayed), 1)
418420
w = displayed[0].children[0]
@@ -425,7 +427,7 @@ def foo(a='default'):
425427
def test_call_decorated_on_trait_change():
426428
"""test calling @interact decorated functions"""
427429
d = {}
428-
with tt.monkeypatch(interaction, 'display', record_display):
430+
with patch.object(interaction, 'display', record_display):
429431
@interact
430432
def foo(a='default'):
431433
d['a'] = a
@@ -449,7 +451,7 @@ def foo(a='default'):
449451
def test_call_decorated_kwargs_on_trait_change():
450452
"""test calling @interact(foo=bar) decorated functions"""
451453
d = {}
452-
with tt.monkeypatch(interaction, 'display', record_display):
454+
with patch.object(interaction, 'display', record_display):
453455
@interact(a='kwarg')
454456
def foo(a='default'):
455457
d['a'] = a

IPython/lib/tests/test_latextools.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# encoding: utf-8
22
"""Tests for IPython.utils.path.py"""
33

4-
#-----------------------------------------------------------------------------
5-
# Copyright (C) 2008-2011 The IPython Development Team
6-
#
7-
# Distributed under the terms of the BSD License. The full license is in
8-
# the file COPYING, distributed as part of this software.
9-
#-----------------------------------------------------------------------------
4+
# Copyright (c) IPython Development Team.
5+
# Distributed under the terms of the Modified BSD License.
6+
7+
try:
8+
from unittest.mock import patch
9+
except ImportError:
10+
from mock import patch
1011

1112
import nose.tools as nt
1213

1314
from IPython.lib import latextools
1415
from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
15-
from IPython.testing.tools import monkeypatch
1616
from IPython.utils.process import FindCmdError
1717

1818

@@ -29,7 +29,7 @@ def mock_find_cmd(arg):
2929
if arg == command:
3030
raise FindCmdError
3131

32-
with monkeypatch(latextools, "find_cmd", mock_find_cmd):
32+
with patch.object(latextools, "find_cmd", mock_find_cmd):
3333
nt.assert_equals(latextools.latex_to_png_dvipng("whatever", True),
3434
None)
3535

@@ -46,7 +46,7 @@ def mock_kpsewhich(filename):
4646
for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]:
4747
yield (latextools.latex_to_png_dvipng, s, wrap)
4848

49-
with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
49+
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
5050
yield (latextools.latex_to_png_dvipng, s, wrap)
5151

5252
@skipif_not_matplotlib
@@ -61,7 +61,7 @@ def mock_kpsewhich(filename):
6161
for (s, wrap) in [("$x^2$", False), ("x^2", True)]:
6262
yield (latextools.latex_to_png_mpl, s, wrap)
6363

64-
with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
64+
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
6565
yield (latextools.latex_to_png_mpl, s, wrap)
6666

6767
@skipif_not_matplotlib
@@ -78,7 +78,7 @@ def mock_kpsewhich(filename):
7878
assert False, ("kpsewhich should not be called "
7979
"(called with {0})".format(filename))
8080

81-
with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
81+
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
8282
nt.assert_equals(
8383
'\n'.join(latextools.genelatex("body text", False)),
8484
r'''\documentclass{article}
@@ -100,7 +100,7 @@ def mock_kpsewhich(filename):
100100
nt.assert_equals(filename, "breqn.sty")
101101
return "path/to/breqn.sty"
102102

103-
with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
103+
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
104104
nt.assert_equals(
105105
'\n'.join(latextools.genelatex("x^2", True)),
106106
r'''\documentclass{article}
@@ -125,7 +125,7 @@ def mock_kpsewhich(filename):
125125
nt.assert_equals(filename, "breqn.sty")
126126
return None
127127

128-
with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
128+
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
129129
nt.assert_equals(
130130
'\n'.join(latextools.genelatex("x^2", True)),
131131
r'''\documentclass{article}

IPython/testing/tools.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -444,17 +444,6 @@ def make_tempfile(name):
444444
os.unlink(name)
445445

446446

447-
@contextmanager
448-
def monkeypatch(obj, name, attr):
449-
"""
450-
Context manager to replace attribute named `name` in `obj` with `attr`.
451-
"""
452-
orig = getattr(obj, name)
453-
setattr(obj, name, attr)
454-
yield
455-
setattr(obj, name, orig)
456-
457-
458447
def help_output_test(subcommand=''):
459448
"""test that `ipython [subcommand] -h` works"""
460449
cmd = get_ipython_cmd() + [subcommand, '-h']

jupyter_console/tests/test_image_handler.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
#-----------------------------------------------------------------------------
2-
# Copyright (C) 2012 The IPython Development Team
3-
#
4-
# Distributed under the terms of the BSD License. The full license is in
5-
# the file COPYING, distributed as part of this software.
6-
#-----------------------------------------------------------------------------
1+
# Copyright (c) IPython Development Team.
2+
# Distributed under the terms of the Modified BSD License.
73

84
import os
95
import sys
106
import unittest
117
import base64
128

9+
try:
10+
from unittest.mock import patch
11+
except ImportError:
12+
from mock import patch
13+
1314
from IPython.kernel import KernelClient
1415
from IPython.terminal.console.interactiveshell import ZMQTerminalInteractiveShell
1516
from IPython.utils.tempdir import TemporaryDirectory
@@ -54,7 +55,7 @@ def fake_open(arg):
5455
open_called_with.append(arg)
5556
return Struct(show=lambda: show_called_with.append(None))
5657

57-
with monkeypatch(PIL.Image, 'open', fake_open):
58+
with patch.object(PIL.Image, 'open', fake_open):
5859
self.shell.handle_image_PIL(self.data, self.mime)
5960

6061
self.assertEqual(len(open_called_with), 1)

0 commit comments

Comments
 (0)