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

Method _extract_code

IPython/core/completer.py:2311–2360  ·  view source on GitHub ↗

Extract code from magics if any.

(self, line: str)

Source from the content-addressed store, hash-verified

2309 }
2310
2311 def _extract_code(self, line: str) -> str:
2312 """Extract code from magics if any."""
2313
2314 if not line:
2315 return line
2316 maybe_magic, *rest = line.split(maxsplit=1)
2317 if not rest:
2318 return line
2319 args = rest[0]
2320 known_magics = self.shell.magics_manager.lsmagic()
2321 line_magics = known_magics["line"]
2322 magic_name = maybe_magic.lstrip(self.magic_escape)
2323 if magic_name not in line_magics:
2324 return line
2325
2326 if not maybe_magic.startswith(self.magic_escape):
2327 all_variables = [*self.namespace.keys(), *self.global_namespace.keys()]
2328 if magic_name in all_variables:
2329 # short circuit if we see a line starting with say `time`
2330 # but time is defined as a variable (in addition to being
2331 # a magic). In these cases users need to use explicit `%time`.
2332 return line
2333
2334 magic_method = line_magics[magic_name]
2335
2336 try:
2337 if magic_name == "timeit":
2338 opts, stmt = magic_method.__self__.parse_options(
2339 args,
2340 "n:r:tcp:qov:",
2341 posix=False,
2342 strict=False,
2343 preserve_non_opts=True,
2344 )
2345 return stmt
2346 elif magic_name == "prun":
2347 opts, stmt = magic_method.__self__.parse_options(
2348 args, "D:l:rs:T:q", list_all=True, posix=False
2349 )
2350 return stmt
2351 elif hasattr(magic_method, "parser") and getattr(
2352 magic_method, "has_arguments", False
2353 ):
2354 # e.g. %debug, %time
2355 args, extra = magic_method.parser.parse_argstring(args, partial=True)
2356 return " ".join(extra)
2357 except UsageError:
2358 return line
2359
2360 return line
2361
2362 @context_matcher()
2363 def magic_matcher(self, context: CompletionContext) -> SimpleMatcherResult:

Callers 3

file_matcherMethod · 0.95
python_matcherMethod · 0.95
_completeMethod · 0.95

Calls 4

keysMethod · 0.80
parse_optionsMethod · 0.80
parse_argstringMethod · 0.80
lsmagicMethod · 0.45

Tested by

no test coverage detected