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

Skip to content

Commit 544e2c1

Browse files
committed
fix tests
1 parent 1e0b610 commit 544e2c1

3 files changed

Lines changed: 58 additions & 44 deletions

File tree

IPython/testing/ipunittest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import re
3939
import sys
4040
import unittest
41+
import builtins
4142
from doctest import DocTestFinder, DocTestRunner, TestResults
4243
from IPython.terminal.interactiveshell import InteractiveShell
4344

tests/test_magic.py

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,15 @@ def test_numpy_reset_array_undec():
327327
assert "a" not in _ip.user_ns
328328

329329

330-
def test_reset_out():
330+
@pytest.fixture()
331+
def underscore_not_in_builtins():
332+
import builtins
333+
334+
if "_" in builtins.__dict__:
335+
del builtins.__dict__["_"]
336+
337+
338+
def test_reset_out(underscore_not_in_builtins):
331339
"Test '%reset out' magic"
332340
_ip.run_cell("parrot = 'dead'", store_history=True)
333341
# test '%reset -f out', make an Out prompt
@@ -508,7 +516,7 @@ def test_time3():
508516
"run = 0\n"
509517
"run += 1")
510518

511-
def test_multiline_time():
519+
def test_multiline_time(underscore_not_in_builtins):
512520
"""Make sure last statement from time return a value."""
513521
ip = get_ipython()
514522
ip.user_ns.pop('run', None)
@@ -919,58 +927,63 @@ def test_env_set_whitespace(self):
919927
self.assertRaises(UsageError, lambda: _ip.run_line_magic("env", "var A=B"))
920928

921929

922-
class CellMagicTestCase(TestCase):
923930

924-
def check_ident(self, magic):
925-
# Manually called, we get the result
926-
out = _ip.run_cell_magic(magic, "a", "b")
927-
assert out == ("a", "b")
928-
# Via run_cell, it goes into the user's namespace via displayhook
929-
_ip.run_cell("%%" + magic + " c\nd\n")
930-
assert _ip.user_ns["_"] == ("c", "d\n")
931+
def check_ident(magic):
932+
# Manually called, we get the result
933+
out = _ip.run_cell_magic(magic, "a", "b")
934+
assert out == ("a", "b")
935+
# Via run_cell, it goes into the user's namespace via displayhook
936+
_ip.run_cell("%%" + magic + " c\nd\n")
937+
assert _ip.user_ns["_"] == ("c", "d\n")
931938

932-
def test_cell_magic_func_deco(self):
933-
"Cell magic using simple decorator"
934-
@register_cell_magic
935-
def cellm(line, cell):
936-
return line, cell
937939

938-
self.check_ident('cellm')
940+
def test_cell_magic_func_deco(underscore_not_in_builtins):
941+
"Cell magic using simple decorator"
939942

940-
def test_cell_magic_reg(self):
941-
"Cell magic manually registered"
942-
def cellm(line, cell):
943-
return line, cell
943+
@register_cell_magic
944+
def cellm(line, cell):
945+
return line, cell
944946

945-
_ip.register_magic_function(cellm, 'cell', 'cellm2')
946-
self.check_ident('cellm2')
947+
check_ident("cellm")
947948

948-
def test_cell_magic_class(self):
949-
"Cell magics declared via a class"
950-
@magics_class
951-
class MyMagics(Magics):
952949

953-
@cell_magic
954-
def cellm3(self, line, cell):
955-
return line, cell
950+
def test_cell_magic_reg(underscore_not_in_builtins):
951+
"Cell magic manually registered"
956952

957-
_ip.register_magics(MyMagics)
958-
self.check_ident('cellm3')
953+
def cellm(line, cell):
954+
return line, cell
955+
956+
_ip.register_magic_function(cellm, "cell", "cellm2")
957+
check_ident("cellm2")
959958

960-
def test_cell_magic_class2(self):
961-
"Cell magics declared via a class, #2"
962-
@magics_class
963-
class MyMagics2(Magics):
964959

965-
@cell_magic('cellm4')
966-
def cellm33(self, line, cell):
967-
return line, cell
960+
def test_cell_magic_class(underscore_not_in_builtins):
961+
"Cell magics declared via a class"
962+
963+
@magics_class
964+
class MyMagics(Magics):
965+
@cell_magic
966+
def cellm3(self, line, cell):
967+
return line, cell
968+
969+
_ip.register_magics(MyMagics)
970+
check_ident("cellm3")
971+
972+
973+
def test_cell_magic_class2(underscore_not_in_builtins):
974+
"Cell magics declared via a class, #2"
975+
976+
@magics_class
977+
class MyMagics2(Magics):
978+
@cell_magic("cellm4")
979+
def cellm33(self, line, cell):
980+
return line, cell
968981

969-
_ip.register_magics(MyMagics2)
970-
self.check_ident('cellm4')
971-
# Check that nothing is registered as 'cellm33'
972-
c33 = _ip.find_cell_magic('cellm33')
973-
assert c33 == None
982+
_ip.register_magics(MyMagics2)
983+
check_ident("cellm4")
984+
# Check that nothing is registered as 'cellm33'
985+
c33 = _ip.find_cell_magic("cellm33")
986+
assert c33 == None
974987

975988
def test_file():
976989
"""Basic %%writefile"""
@@ -1265,7 +1278,7 @@ async def test_script_streams_continiously(capsys):
12651278
ip = get_ipython()
12661279
# Windows is slow to start up a thread on CI
12671280
is_windows = os.name == "nt"
1268-
step = 3 if is_windows else 1
1281+
step = 3 if is_windows else 0.3
12691282
code = dedent(
12701283
f"""\
12711284
import time

0 commit comments

Comments
 (0)