Skip to content

Commit df8f36b

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

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
"""Colab-specific kernel customizations."""
15+
import logging
16+
17+
logging.basicConfig(level=logging.DEBUG)
1518

1619
from google.colab import _shell
1720
from google.colab import _shell_customizations
@@ -29,9 +32,25 @@ def _shell_class_default(self):
2932
def do_inspect(self, code, cursor_pos, detail_level=0, *args, **kwargs):
3033
name = tokenutil.token_at_cursor(code, cursor_pos)
3134
info = self.shell.object_inspect(name)
35+
logging.warning('do_inspect: %s', name)
3236

3337
data = {}
3438
if info['found']:
39+
logging.warning('do_inspect found: %s', name)
40+
info_text = self.shell.object_inspect_text(
41+
name, detail_level=detail_level
42+
)
43+
# Only include info_text if less than 1 MiB. We have seen frontend lockup
44+
# issues when this is very large. See b/401357469 for more details.
45+
# 1 MiB is chosen as an arbitary starting point.
46+
if len(info_text) < 2**20:
47+
logging.warning('do_inspect adding text/plain: %s', info_text)
48+
data['text/plain'] = info_text
49+
else:
50+
logging.warning(
51+
'do_inspect text/plain is too large: %s', len(info_text)
52+
)
53+
3554
# Provide the structured inspection information to allow the frontend to
3655
# format as desired.
3756
argspec = info.get('argspec')
@@ -51,7 +70,7 @@ def do_inspect(self, code, cursor_pos, detail_level=0, *args, **kwargs):
5170
'metadata': {},
5271
'found': info['found'],
5372
}
54-
73+
logging.warning('do_inspect: data mimes: %s', ', '.join(data.keys()))
5574
return reply_content
5675

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

google/colab/_shell.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""Colab-specific shell customizations."""
1515

1616
import datetime
17+
import logging
1718
import os
1819
import pathlib
1920
import sys
@@ -238,6 +239,44 @@ def _getattr_property(obj, attrname):
238239
# Nothing helped, fall back.
239240
return getattr(obj, attrname)
240241

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

@@ -259,7 +298,7 @@ def object_inspect(self, oname, detail_level=0):
259298
e, ''.join(traceback.format_tb(sys.exc_info()[2]))
260299
)
261300
)
262-
result = oinspect.InfoDict()
301+
result = oinspect.object_info()
263302
else:
264303
result = super(Shell, self).object_inspect(
265304
oname, detail_level=detail_level

0 commit comments

Comments
 (0)