@@ -39,17 +39,13 @@ def ref(self, name: str) -> Union[TestbookObjectReference, Any]:
39
39
40
40
# Check if exists
41
41
self .inject (name , pop = True )
42
- try :
43
- self .inject (f"import cloudpickle; cloudpickle.dumps({ name } )" , pop = True )
44
- return self .value (name )
45
- except Exception :
46
- return TestbookObjectReference (self , name )
42
+ return TestbookObjectReference (self , name )
47
43
48
44
def get (self , item ):
49
- return self .ref (item )
45
+ return self .value (item )
50
46
51
47
def __getitem__ (self , item ):
52
- return self .ref (item )
48
+ return self .value (item )
53
49
54
50
@staticmethod
55
51
def _construct_call_code (
@@ -251,9 +247,6 @@ def value(self, code: str) -> Any:
251
247
"""
252
248
Execute given code in the kernel and returns the serializeable result.
253
249
254
- This error object will also contain an attribute called `save_varname` which
255
- can be used to create a reference object with :meth:`ref`.
256
-
257
250
Parameters
258
251
----------
259
252
code: str
@@ -276,14 +269,17 @@ def value(self, code: str) -> Any:
276
269
raise TestbookExecuteResultNotFoundError (
277
270
'code provided does not produce execute_result'
278
271
)
272
+
273
+ save_varname = random_varname ()
279
274
280
275
import tempfile
281
276
with tempfile .NamedTemporaryFile () as tmp :
282
277
try :
283
278
inject_code = f"""
284
279
import cloudpickle
280
+ { save_varname } = get_ipython().last_execution_result.result
285
281
with open('{ tmp .name } ', 'wb') as f:
286
- cloudpickle.dump(get_ipython().last_execution_result.result , f)
282
+ cloudpickle.dump({ save_varname } , f)
287
283
"""
288
284
outputs = self .inject (inject_code , pop = True ).outputs
289
285
if len (outputs ) > 0 and outputs [0 ].output_type == "error" :
@@ -294,7 +290,9 @@ def value(self, code: str) -> Any:
294
290
with open (tmp .name , 'rb' ) as f :
295
291
return cloudpickle .load (f )
296
292
except TestbookRuntimeError :
297
- raise TestbookSerializeError ('could not serialize output' )
293
+ e = TestbookSerializeError ('could not serialize output' )
294
+ e .save_varname = save_varname
295
+ raise e
298
296
299
297
@contextmanager
300
298
def patch (self , target , ** kwargs ):
0 commit comments