Skip to content

Commit 58b52d5

Browse files
devoidcolaboratory-team
authored andcommitted
No public description
PiperOrigin-RevId: 821837262
1 parent 839cdcd commit 58b52d5

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

google/colab/_inspector.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import math
2424
import re
2525
import types
26+
import warnings
2627

2728
from IPython.core import oinspect
2829
from IPython.utils import dir2
@@ -475,6 +476,17 @@ def _getdef(self, obj, oname=''):
475476
except: # pylint: disable=bare-except
476477
logging.exception('Exception raised in ColabInspector._getdef')
477478
def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
479+
"""Compute a dict with detailed information about an object."""
480+
if formatter is not None:
481+
warnings.warn(
482+
'The `formatter` keyword argument to `Inspector.info`'
483+
'is deprecated as of IPython 5.0 and will have no effects.',
484+
DeprecationWarning,
485+
stacklevel=2,
486+
)
487+
return self._info(obj, oname=oname, info=info, detail_level=detail_level)
488+
489+
def _info(self, obj, oname='', info=None, detail_level=0):
478490
"""Compute a dict with detailed information about an object.
479491
480492
This overrides the superclass method for two main purposes:
@@ -484,7 +496,6 @@ def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
484496
Args:
485497
obj: object to inspect.
486498
oname: (optional) string reference to this object
487-
formatter: (optional) custom docstring formatter
488499
info: (optional) previously computed information about obj
489500
detail_level: (optional) 0 or 1; 1 means "include more detail"
490501
@@ -572,8 +583,7 @@ def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
572583
if source is not None:
573584
out['source'] = source
574585
if 'source' not in out:
575-
formatter = formatter or (lambda x: x)
576-
docstring = formatter(getdoc(obj) or '<no docstring>')
586+
docstring = getdoc(obj) or '<no docstring>'
577587
if docstring:
578588
out['docstring'] = docstring
579589

google/colab/_kernel.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ def do_inspect(self, code, cursor_pos, detail_level=0, *args, **kwargs):
3232

3333
data = {}
3434
if info['found']:
35+
info_text = self.shell.object_inspect_text(
36+
name, detail_level=detail_level
37+
)
38+
# Only include info_text if less than 2 MiB. We have seen frontend lockup
39+
# issues when this is very large. See b/401357469 for more details.
40+
# 2 MiB is chosen as an arbitrary starting point.
41+
if len(info_text) < 2**20:
42+
data['text/plain'] = info_text
43+
else:
44+
self.log.warning(
45+
'do_inspect text/plain output omitted as it was too large:'
46+
' size %d bytes',
47+
len(info_text),
48+
)
49+
3550
# Provide the structured inspection information to allow the frontend to
3651
# format as desired.
3752
argspec = info.get('argspec')
@@ -51,7 +66,6 @@ def do_inspect(self, code, cursor_pos, detail_level=0, *args, **kwargs):
5166
'metadata': {},
5267
'found': info['found'],
5368
}
54-
5569
return reply_content
5670

5771
def complete_request(self, stream, ident, parent):

google/colab/_shell.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,38 @@ def _getattr_property(obj, attrname):
238238
# Nothing helped, fall back.
239239
return getattr(obj, attrname)
240240

241+
def object_inspect_text(self, oname, detail_level=0):
242+
"""Get object info as formatted text."""
243+
info = self._ofind(oname)
244+
if info['found']:
245+
try:
246+
info = self._object_find(oname)
247+
# We need to avoid arbitrary python objects remaining in info (and
248+
# potentially being serialized below); `obj` itself needs to be
249+
# removed, but retained for use below, and `parent` isn't used at all.
250+
obj = info.pop('obj', '')
251+
info.pop('parent', '')
252+
# Follow the InteractiveShell base class implementation and call
253+
# directly into the inspector's _get_info method.
254+
# pylint: disable=protected-access
255+
result = self.inspector._get_info(
256+
obj,
257+
oname,
258+
info=info,
259+
detail_level=detail_level,
260+
)
261+
except Exception as e: # pylint: disable=broad-except
262+
self.kernel.log.info(
263+
'Exception caught during object text inspection: '
264+
'{!r}\nTraceback:\n{}'.format(
265+
e, ''.join(traceback.format_tb(sys.exc_info()[2]))
266+
)
267+
)
268+
return ''
269+
else:
270+
return ''
271+
return result['text/plain']
272+
241273
def object_inspect(self, oname, detail_level=0):
242274
info = self._ofind(oname)
243275

@@ -259,7 +291,7 @@ def object_inspect(self, oname, detail_level=0):
259291
e, ''.join(traceback.format_tb(sys.exc_info()[2]))
260292
)
261293
)
262-
result = oinspect.InfoDict()
294+
result = oinspect.object_info()
263295
else:
264296
result = super(Shell, self).object_inspect(
265297
oname, detail_level=detail_level

0 commit comments

Comments
 (0)