MCPcopy Index your code
hub / github.com/ipython/ipython / getsource

Function getsource

IPython/core/oinspect.py:232–291  ·  view source on GitHub ↗

Wrapper around inspect.getsource. This can be modified by other projects to provide customized source extraction. Parameters ---------- obj : object an object whose source code we will attempt to extract oname : str (optional) a name under which the object i

(obj, oname='')

Source from the content-addressed store, hash-verified

230
231
232def getsource(obj, oname='') -> Union[str,None]:
233 """Wrapper around inspect.getsource.
234
235 This can be modified by other projects to provide customized source
236 extraction.
237
238 Parameters
239 ----------
240 obj : object
241 an object whose source code we will attempt to extract
242 oname : str
243 (optional) a name under which the object is known
244
245 Returns
246 -------
247 src : unicode or None
248
249 """
250
251 if isinstance(obj, property):
252 sources = []
253 for attrname in ['fget', 'fset', 'fdel']:
254 fn = getattr(obj, attrname)
255 if fn is not None:
256 oname_prefix = ('%s.' % oname) if oname else ''
257 sources.append(''.join(('# ', oname_prefix, attrname)))
258 if inspect.isfunction(fn):
259 _src = getsource(fn)
260 if _src:
261 # assert _src is not None, "please mypy"
262 sources.append(dedent(_src))
263 else:
264 # Default str/repr only prints function name,
265 # pretty.pretty prints module name too.
266 sources.append(
267 '%s%s = %s\n' % (oname_prefix, attrname, pretty(fn))
268 )
269 if sources:
270 return '\n'.join(sources)
271 else:
272 return None
273
274 else:
275 # Get source for non-property objects.
276
277 obj = _get_wrapped(obj)
278
279 try:
280 src = inspect.getsource(obj)
281 except TypeError:
282 # The object itself provided no meaningful source, try looking for
283 # its class definition instead.
284 try:
285 src = inspect.getsource(obj.__class__)
286 except (OSError, TypeError):
287 return None
288 except OSError:
289 return None

Callers 3

format_filterFunction · 0.90
psourceMethod · 0.85
infoMethod · 0.85

Calls 3

prettyFunction · 0.90
dedentFunction · 0.85
_get_wrappedFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…