Skip to content

Commit be891d6

Browse files
committed
Change the way the capturer is invoked
1 parent 48e68b3 commit be891d6

File tree

5 files changed

+72
-30
lines changed

5 files changed

+72
-30
lines changed

src/importnb/capture.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
try:
2-
from .utils import __IPYTHON__, export
2+
from .utils import export, __IPYTHON__
33
except:
4-
from utils import __IPYTHON__, export
4+
from utils import export, __IPYTHON__
5+
__all__ = 'capture_output',
56

6-
if False and __IPYTHON__:
7+
if __IPYTHON__:
78
from IPython.utils.capture import capture_output
89
else:
910
from contextlib import redirect_stdout, ExitStack
@@ -60,4 +61,5 @@ def stderr(self): return self._stderr and self._stderr.getvalue() or ''
6061

6162
if __name__ == '__main__':
6263
export('capture.ipynb', '../importnb/capture.py')
63-
__import__('doctest').testmod()
64+
__import__('doctest').testmod()
65+

src/importnb/loader.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ def lazy_loader_cls(loader):
6464
return inspect.getclosurevars(loader).nonlocals.get('cls', loader)
6565
return loader
6666

67+
e = ExitStack()
68+
6769
class Notebook(SourceFileLoader, ExitStack):
6870
"""A SourceFileLoader for notebooks that provides line number debugginer in the JSON source."""
6971
EXTENSION_SUFFIXES = '.ipynb',
7072

7173
def __init__(
72-
self, fullname=None, path=None, *, stdout=False, stderr=False, display=True, lazy=False
74+
self, fullname=None, path=None, *, stdout=False, stderr=False, display=False, lazy=False
7375
):
7476
SourceFileLoader.__init__(self, fullname, path)
7577
ExitStack.__init__(self)
@@ -78,14 +80,18 @@ def __init__(
7880

7981
def __enter__(self, position=0):
8082
add_path_hooks(type(self), self.EXTENSION_SUFFIXES, position=position, lazy=self._lazy)
81-
stack = super().__enter__()
82-
return stack.enter_context(capture_output(
83-
stdout=self._stdout, stderr=self._stderr, display=self._display
84-
))
83+
if self._capture:
84+
stack = super().__enter__()
85+
return stack.enter_context(capture_output(
86+
stdout=self._stdout, stderr=self._stderr, display=self._display
87+
))
8588

86-
def __exit__(self, *excepts): remove_one_path_hook(type(self))
89+
@property
90+
def _capture(self): return any((self._stdout, self._stderr, self._display))
91+
def __exit__(self, *excepts):
92+
remove_one_path_hook(type(self))
8793

88-
def exec_module(self, module): super().exec_module(module)
94+
if self._capture: super().__exit__(*excepts)
8995

9096
def source_to_code(Notebook, data, path):
9197
with __import__('io').BytesIO(data) as stream:
@@ -111,4 +117,5 @@ def unload_ipython_extension(ip=None):
111117

112118
if __name__ == '__main__':
113119
export('loader.ipynb', '../importnb/loader.py')
114-
__import__('doctest').testmod()
120+
__import__('doctest').testmod()
121+

src/importnb/tests/test_importnb.ipynb

+13
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@
158158
"import sys"
159159
]
160160
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": null,
164+
"metadata": {},
165+
"outputs": [],
166+
"source": [
167+
"def test_capturer():\n",
168+
" from importnb.capture import capture_output\n",
169+
" if __IPYTHON__:\n",
170+
" from IPython.utils.capture import capture_output as ipython_version\n",
171+
" assert capture_output is ipython_version"
172+
]
173+
},
161174
{
162175
"cell_type": "code",
163176
"execution_count": 10,

src/notebooks/capture.ipynb

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": 4,
66
"metadata": {},
77
"outputs": [],
88
"source": [
99
"try:\n",
10-
" from .utils import __IPYTHON__, export\n",
10+
" from .utils import export, __IPYTHON__\n",
1111
"except:\n",
12-
" from utils import __IPYTHON__, export"
12+
" from utils import export, __IPYTHON__\n",
13+
"__all__ = 'capture_output',"
1314
]
1415
},
1516
{
1617
"cell_type": "code",
17-
"execution_count": 2,
18+
"execution_count": 5,
1819
"metadata": {},
1920
"outputs": [],
2021
"source": [
21-
"if False and __IPYTHON__:\n",
22+
"if __IPYTHON__:\n",
2223
" from IPython.utils.capture import capture_output\n",
2324
"else:\n",
2425
" from contextlib import redirect_stdout, ExitStack\n",
@@ -75,7 +76,7 @@
7576
},
7677
{
7778
"cell_type": "code",
78-
"execution_count": 3,
79+
"execution_count": 6,
7980
"metadata": {
8081
"scrolled": false
8182
},
@@ -85,6 +86,13 @@
8586
" export('capture.ipynb', '../importnb/capture.py')\n",
8687
" __import__('doctest').testmod()"
8788
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": []
8896
}
8997
],
9098
"metadata": {

src/notebooks/loader.ipynb

+24-12
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
},
136136
{
137137
"cell_type": "code",
138-
"execution_count": 5,
138+
"execution_count": 6,
139139
"metadata": {},
140140
"outputs": [],
141141
"source": [
@@ -144,7 +144,7 @@
144144
" EXTENSION_SUFFIXES = '.ipynb',\n",
145145
" \n",
146146
" def __init__(\n",
147-
" self, fullname=None, path=None, *, stdout=False, stderr=False, display=True, lazy=False\n",
147+
" self, fullname=None, path=None, *, stdout=False, stderr=False, display=False, lazy=False\n",
148148
" ): \n",
149149
" SourceFileLoader.__init__(self, fullname, path)\n",
150150
" ExitStack.__init__(self)\n",
@@ -153,14 +153,19 @@
153153
" \n",
154154
" def __enter__(self, position=0): \n",
155155
" add_path_hooks(type(self), self.EXTENSION_SUFFIXES, position=position, lazy=self._lazy)\n",
156-
" stack = super().__enter__()\n",
157-
" return stack.enter_context(capture_output(\n",
158-
" stdout=self._stdout, stderr=self._stderr, display=self._display\n",
159-
" ))\n",
160-
" \n",
161-
" def __exit__(self, *excepts): remove_one_path_hook(type(self))\n",
156+
" if self._capture:\n",
157+
" stack = super().__enter__()\n",
158+
" return stack.enter_context(capture_output(\n",
159+
" stdout=self._stdout, stderr=self._stderr, display=self._display\n",
160+
" ))\n",
162161
" \n",
163-
" def exec_module(self, module): super().exec_module(module) \n",
162+
" @property\n",
163+
" def _capture(self): return any((self._stdout, self._stderr, self._display))\n",
164+
" \n",
165+
" def __exit__(self, *excepts): \n",
166+
" remove_one_path_hook(type(self))\n",
167+
" \n",
168+
" if self._capture: super().__exit__(*excepts)\n",
164169
" \n",
165170
" def source_to_code(Notebook, data, path):\n",
166171
" with __import__('io').BytesIO(data) as stream:\n",
@@ -176,7 +181,7 @@
176181
},
177182
{
178183
"cell_type": "code",
179-
"execution_count": 6,
184+
"execution_count": 7,
180185
"metadata": {},
181186
"outputs": [],
182187
"source": [
@@ -203,7 +208,7 @@
203208
},
204209
{
205210
"cell_type": "code",
206-
"execution_count": 7,
211+
"execution_count": 8,
207212
"metadata": {},
208213
"outputs": [],
209214
"source": [
@@ -222,7 +227,7 @@
222227
},
223228
{
224229
"cell_type": "code",
225-
"execution_count": 8,
230+
"execution_count": 9,
226231
"metadata": {
227232
"scrolled": false
228233
},
@@ -232,6 +237,13 @@
232237
" export('loader.ipynb', '../importnb/loader.py')\n",
233238
" __import__('doctest').testmod()"
234239
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": null,
244+
"metadata": {},
245+
"outputs": [],
246+
"source": []
235247
}
236248
],
237249
"metadata": {

0 commit comments

Comments
 (0)