Skip to content

Commit 99b0651

Browse files
committed
Remove leftover ipython_version check and add IPython magics test (Closes #407)
1 parent 384b340 commit 99b0651

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎.github/workflows/lint_python.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ jobs:
7474
python-version: ${{ fromJSON(format('["{0}", "{1}"]', format('{0}.0-alpha - {0}.X', matrix.python.major_dot_minor), matrix.python.major_dot_minor))[startsWith(matrix.python.major_dot_minor, 'pypy')] }}
7575
architecture: x64
7676
- run: pip install --upgrade pip wheel
77-
- run: pip install pytest
77+
- run: pip install pytest ipython
7878
- run: pip install --editable .
7979
- run: pip install numpy pylab-sdk
8080
- run: make test

‎Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ test:
1919
$(PYTHON) test/test_exit_code.py
2020
$(PYTHON) test/test_mprof.py
2121
$(PYTHON) test/test_async.py
22+
$(PYTHON) test/test_ipython.py
2223
mprof run test/test_func.py
2324

2425
develop:

‎memory_profiler.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -997,10 +997,7 @@ def mprun(self, parameter_s='', cell=None):
997997
output = stdout_trap.getvalue()
998998
output = output.rstrip()
999999

1000-
if ipython_version < '0.11':
1001-
page(output, screen_lines=self.shell.rc.screen_length)
1002-
else:
1003-
page(output)
1000+
page(output)
10041001
print(message, )
10051002

10061003
text_file = opts.T[0]

‎test/test_ipython.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
from unittest.mock import patch
3+
4+
from IPython.core.interactiveshell import InteractiveShell
5+
from memory_profiler import LineProfiler, MemitResult, load_ipython_extension
6+
7+
8+
def sample_func(n):
9+
arr = [1] * n
10+
return len(arr)
11+
12+
13+
class TestIPythonMagics(unittest.TestCase):
14+
@classmethod
15+
def setUpClass(cls):
16+
cls.ip = InteractiveShell.instance()
17+
load_ipython_extension(cls.ip)
18+
cls.ip.user_ns['sample_func'] = sample_func
19+
20+
def test_mprun(self):
21+
with patch('IPython.core.page.page') as mock_page:
22+
prof = self.ip.run_line_magic('mprun', '-r -f sample_func sample_func(1000)')
23+
self.assertIsInstance(prof, LineProfiler)
24+
self.assertTrue(mock_page.called)
25+
output = mock_page.call_args[0][0]
26+
self.assertIn('Filename:', output)
27+
self.assertIn('def sample_func(n):', output)
28+
29+
def test_memit(self):
30+
res = self.ip.run_line_magic('memit', '-o -q [1] * 10000')
31+
self.assertIsInstance(res, MemitResult)
32+
self.assertGreater(len(res.mem_usage), 0)
33+
self.assertIn('peak memory:', str(res))
34+
35+
36+
if __name__ == '__main__':
37+
unittest.main()

0 commit comments

Comments
 (0)