MCPcopy Index your code
hub / github.com/python/cpython / runsource

Method runsource

Lib/_pyrepl/console.py:190–242  ·  view source on GitHub ↗
(self, source, filename="<input>", symbol="single")

Source from the content-addressed store, hash-verified

188 return None
189
190 def runsource(self, source, filename="<input>", symbol="single"):
191 try:
192 tree = self.compile.compiler(
193 source,
194 filename,
195 "exec",
196 ast.PyCF_ONLY_AST,
197 incomplete_input=False,
198 )
199 except SyntaxError as e:
200 # If it looks like pip install was entered (a common beginner
201 # mistake), provide a hint to use the system command prompt.
202 if re.match(r"^\s*(pip3?|py(thon3?)? -m pip) install.*", source):
203 e.add_note(
204 "The Python package manager (pip) can only be used"
205 " outside of the Python REPL.\n"
206 "Try the 'pip' command in a separate terminal or"
207 " command prompt."
208 )
209 self.showsyntaxerror(filename, source=source)
210 return False
211 except (OverflowError, ValueError):
212 self.showsyntaxerror(filename, source=source)
213 return False
214 if tree.body:
215 *_, last_stmt = tree.body
216 for stmt in tree.body:
217 wrapper = ast.Interactive if stmt is last_stmt else ast.Module
218 the_symbol = symbol if stmt is last_stmt else "exec"
219 item = wrapper([stmt])
220 try:
221 code = self.compile.compiler(item, filename, the_symbol)
222 linecache._register_code(code, source, filename)
223 except SyntaxError as e:
224 if e.args[0] == "'await' outside function":
225 python = os.path.basename(sys.executable)
226 e.add_note(
227 f"Try the asyncio REPL ({python} -m asyncio) to use"
228 f" top-level 'await' and run background asyncio tasks."
229 )
230 self.showsyntaxerror(filename, source=source)
231 return False
232 except (OverflowError, ValueError):
233 self.showsyntaxerror(filename, source=source)
234 return False
235
236 if code is None:
237 return True
238
239 result = self.runcode(code)
240 if result is self.STATEMENT_FAILED:
241 break
242 return False

Calls 5

showsyntaxerrorMethod · 0.95
runcodeMethod · 0.95
wrapperFunction · 0.50
matchMethod · 0.45
basenameMethod · 0.45