6
6
from nbclient import NotebookClient
7
7
from nbclient .exceptions import CellExecutionError
8
8
from nbformat .v4 import new_code_cell
9
+ import cloudpickle
9
10
10
11
from .exceptions import (
11
12
TestbookCellTagNotFoundError ,
@@ -39,7 +40,7 @@ def ref(self, name: str) -> Union[TestbookObjectReference, Any]:
39
40
# Check if exists
40
41
self .inject (name , pop = True )
41
42
try :
42
- self .inject (f"import json; json .dumps({ name } )" , pop = True )
43
+ self .inject (f"import cloudpickle; cloudpickle .dumps({ name } )" , pop = True )
43
44
return self .value (name )
44
45
except Exception :
45
46
return TestbookObjectReference (self , name )
@@ -248,9 +249,8 @@ def inject(
248
249
249
250
def value (self , code : str ) -> Any :
250
251
"""
251
- Execute given code in the kernel and return JSON serializeable result.
252
+ Execute given code in the kernel and returns the serializeable result.
252
253
253
- If the result is not JSON serializeable, it raises `TestbookAttributeError`.
254
254
This error object will also contain an attribute called `save_varname` which
255
255
can be used to create a reference object with :meth:`ref`.
256
256
@@ -277,34 +277,24 @@ def value(self, code: str) -> Any:
277
277
'code provided does not produce execute_result'
278
278
)
279
279
280
- save_varname = random_varname ()
281
-
282
- inject_code = f"""
283
- import json
284
- from IPython import get_ipython
285
- from IPython.display import JSON
286
-
287
- { save_varname } = get_ipython().last_execution_result.result
288
-
289
- json.dumps({ save_varname } )
290
- JSON({{"value" : { save_varname } }})
291
- """
292
-
293
- try :
294
- outputs = self .inject (inject_code , pop = True ).outputs
295
-
296
- if outputs [0 ].output_type == "error" :
297
- # will receive error when `allow_errors` is set to True
298
- raise TestbookRuntimeError (
299
- outputs [0 ].evalue , outputs [0 ].traceback , outputs [0 ].ename
300
- )
301
-
302
- return outputs [0 ].data ['application/json' ]['value' ]
303
-
304
- except TestbookRuntimeError :
305
- e = TestbookSerializeError ('could not JSON serialize output' )
306
- e .save_varname = save_varname
307
- raise e
280
+ import tempfile
281
+ with tempfile .NamedTemporaryFile () as tmp :
282
+ try :
283
+ inject_code = f"""
284
+ import cloudpickle
285
+ with open('{ tmp .name } ', 'wb') as f:
286
+ cloudpickle.dump(get_ipython().last_execution_result.result, f)
287
+ """
288
+ outputs = self .inject (inject_code , pop = True ).outputs
289
+ if len (outputs ) > 0 and outputs [0 ].output_type == "error" :
290
+ # will receive error when `allow_errors` is set to True
291
+ raise TestbookRuntimeError (
292
+ outputs [0 ].evalue , outputs [0 ].traceback , outputs [0 ].ename
293
+ )
294
+ with open (tmp .name , 'rb' ) as f :
295
+ return cloudpickle .load (f )
296
+ except TestbookRuntimeError :
297
+ raise TestbookSerializeError ('could not serialize output' )
308
298
309
299
@contextmanager
310
300
def patch (self , target , ** kwargs ):
0 commit comments