Skip to content

Commit df6cde6

Browse files
devoidcolaboratory-team
authored andcommitted
No public description
PiperOrigin-RevId: 821837262
1 parent be426fe commit df6cde6

3 files changed

Lines changed: 61 additions & 6 deletions

File tree

google/colab/_inspector.py

Lines changed: 14 additions & 4 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

@@ -619,7 +629,7 @@ def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
619629
if definition:
620630
out['definition'] = definition
621631

622-
if not oinspect.is_simple_callable(obj):
632+
# if not oinspect.is_simple_callable(obj):
623633
call_docstring = getdoc(obj.__call__)
624634
if call_docstring and call_docstring != _BASE_CALL_DOC:
625635
out['call_docstring'] = call_docstring

google/colab/_kernel.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
"""Colab-specific kernel customizations."""
15+
import logging
1516

1617
from google.colab import _shell
1718
from google.colab import _shell_customizations
@@ -29,9 +30,21 @@ def _shell_class_default(self):
2930
def do_inspect(self, code, cursor_pos, detail_level=0, *args, **kwargs):
3031
name = tokenutil.token_at_cursor(code, cursor_pos)
3132
info = self.shell.object_inspect(name)
33+
logging.debug('do_inspect: %s', name)
3234

3335
data = {}
3436
if info['found']:
37+
logging.debug('do_inspect found: %s', name)
38+
info_text = self.shell.object_inspect_text(
39+
name, detail_level=detail_level
40+
)
41+
# Only include info_text if less than 1 MiB. We have seen frontend lockup
42+
# issues when this is very large. See b/401357469 for more details.
43+
# 1 MiB is chosen as an arbitary starting point.
44+
if len(info_text) < 2**20:
45+
logging.debug('do_inspect adding text/plain: %s', info_text)
46+
data['text/plain'] = info_text
47+
3548
# Provide the structured inspection information to allow the frontend to
3649
# format as desired.
3750
argspec = info.get('argspec')
@@ -51,7 +64,7 @@ def do_inspect(self, code, cursor_pos, detail_level=0, *args, **kwargs):
5164
'metadata': {},
5265
'found': info['found'],
5366
}
54-
67+
logging.debug('do_inspect: data mimes: %s', ', '.join(data.keys()))
5568
return reply_content
5669

5770
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)