Lines Matching refs:self
121 def __repr__(self):
122 return self
127 # Mutate self to be the "real path".
135 def check(self):
136 if not os.path.exists(self):
137 print('Error:', self.orig, 'does not exist')
141 sys.path[0] = os.path.dirname(self)
144 def filename(self):
145 return self
148 def namespace(self):
151 __file__=self,
156 def code(self):
157 with io.open_code(self) as fp:
158 return f"exec(compile({fp.read()!r}, {self!r}, 'exec'))"
162 def check(self):
164 self._details
170 def _details(self):
172 return runpy._get_module_details(self)
175 def filename(self):
176 return self.code.co_filename
179 def code(self):
180 name, spec, code = self._details
184 def _spec(self):
185 name, spec, code = self._details
189 def namespace(self):
192 __file__=os.path.normcase(os.path.abspath(self.filename)),
193 __package__=self._spec.parent,
194 __loader__=self._spec.loader,
195 __spec__=self._spec,
211 def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
213 bdb.Bdb.__init__(self, skip=skip)
214 cmd.Cmd.__init__(self, completekey, stdin, stdout)
217 self.use_rawinput = 0
218 self.prompt = '(Pdb) '
219 self.aliases = {}
220 self.displaying = {}
221 self.mainpyfile = ''
222 self._wait_for_mainpyfile = False
223 self.tb_lineno = {}
231 self.allow_kbdint = False
232 self.nosigint = nosigint
235 self.rcLines = []
239 self.rcLines.extend(rcFile)
244 self.rcLines.extend(rcFile)
248 self.commands = {} # associates a command list to breakpoint numbers
249 self.commands_doprompt = {} # for each bp num, tells if the prompt
251 self.commands_silent = {} # for each bp num, tells if the stack trace
253 self.commands_defining = False # True while in the process of defining
255 self.commands_bnum = None # The breakpoint number for which we are
258 def sigint_handler(self, signum, frame):
259 if self.allow_kbdint:
261 self.message("\nProgram interrupted. (Use 'cont' to resume).")
262 self.set_step()
263 self.set_trace(frame)
265 def reset(self):
266 bdb.Bdb.reset(self)
267 self.forget()
269 def forget(self):
270 self.lineno = None
271 self.stack = []
272 self.curindex = 0
273 self.curframe = None
274 self.tb_lineno.clear()
276 def setup(self, f, tb):
277 self.forget()
278 self.stack, self.curindex = self.get_stack(f, tb)
284 self.tb_lineno[tb.tb_frame] = lineno
286 self.curframe = self.stack[self.curindex][0]
290 self.curframe_locals = self.curframe.f_locals
291 return self.execRcLines()
294 def execRcLines(self):
295 if not self.rcLines:
298 rcLines = self.rcLines
301 self.rcLines = []
305 if self.onecmd(line):
309 self.rcLines += reversed(rcLines)
314 def user_call(self, frame, argument_list):
317 if self._wait_for_mainpyfile:
319 if self.stop_here(frame):
320 self.message('--Call--')
321 self.interaction(frame, None)
323 def user_line(self, frame):
325 if self._wait_for_mainpyfile:
326 if (self.mainpyfile != self.canonic(frame.f_code.co_filename)
329 self._wait_for_mainpyfile = False
330 if self.bp_commands(frame):
331 self.interaction(frame, None)
333 def bp_commands(self, frame):
339 # self.currentbp is set in bdb in Bdb.break_here if a breakpoint was hit
340 if getattr(self, "currentbp", False) and \
341 self.currentbp in self.commands:
342 currentbp = self.currentbp
343 self.currentbp = 0
344 lastcmd_back = self.lastcmd
345 self.setup(frame, None)
346 for line in self.commands[currentbp]:
347 self.onecmd(line)
348 self.lastcmd = lastcmd_back
349 if not self.commands_silent[currentbp]:
350 self.print_stack_entry(self.stack[self.curindex])
351 if self.commands_doprompt[currentbp]:
352 self._cmdloop()
353 self.forget()
357 def user_return(self, frame, return_value):
359 if self._wait_for_mainpyfile:
362 self.message('--Return--')
363 self.interaction(frame, None)
365 def user_exception(self, frame, exc_info):
368 if self._wait_for_mainpyfile:
380 self.message('%s%s' % (prefix,
382 self.interaction(frame, exc_traceback)
385 def _cmdloop(self):
390 self.allow_kbdint = True
391 self.cmdloop()
392 self.allow_kbdint = False
395 self.message('--KeyboardInterrupt--')
398 def preloop(self):
399 displaying = self.displaying.get(self.curframe)
402 newvalue = self._getval_except(expr)
408 self.message('display %s: %r [old: %r]' %
411 def interaction(self, frame, traceback):
420 if self.setup(frame, traceback):
423 self.forget()
425 self.print_stack_entry(self.stack[self.curindex])
426 self._cmdloop()
427 self.forget()
429 def displayhook(self, obj):
435 self.message(repr(obj))
437 def default(self, line):
439 locals = self.curframe_locals
440 globals = self.curframe.f_globals
447 sys.stdin = self.stdin
448 sys.stdout = self.stdout
449 sys.displayhook = self.displayhook
456 self._error_exc()
458 def precmd(self, line):
463 while args[0] in self.aliases:
464 line = self.aliases[args[0]]
479 self.cmdqueue.append(next)
483 def onecmd(self, line):
490 if not self.commands_defining:
491 return cmd.Cmd.onecmd(self, line)
493 return self.handle_command_def(line)
495 def handle_command_def(self, line):
497 cmd, arg, line = self.parseline(line)
501 self.commands_silent[self.commands_bnum] = True
504 self.cmdqueue = []
506 cmdlist = self.commands[self.commands_bnum]
513 func = getattr(self, 'do_' + cmd)
515 func = self.default
517 if func.__name__ in self.commands_resuming:
518 self.commands_doprompt[self.commands_bnum] = False
519 self.cmdqueue = []
525 def message(self, msg):
526 print(msg, file=self.stdout)
528 def error(self, msg):
529 print('***', msg, file=self.stdout)
534 def _complete_location(self, text, line, begidx, endidx):
541 ret = self._complete_expression(text, line, begidx, endidx)
553 def _complete_bpnumber(self, text, line, begidx, endidx):
560 def _complete_expression(self, text, line, begidx, endidx):
562 if not self.curframe:
567 ns = {**self.curframe.f_globals, **self.curframe_locals}
589 def do_commands(self, arg):
632 self.error("Usage: commands [bnum]\n ...\n end")
635 self.get_bpbynumber(bnum)
637 self.error('cannot set commands: %s' % err)
640 self.commands_bnum = bnum
642 if bnum in self.commands:
643 old_command_defs = (self.commands[bnum],
644 self.commands_doprompt[bnum],
645 self.commands_silent[bnum])
648 self.commands[bnum] = []
649 self.commands_doprompt[bnum] = True
650 self.commands_silent[bnum] = False
652 prompt_back = self.prompt
653 self.prompt = '(com) '
654 self.commands_defining = True
656 self.cmdloop()
660 self.commands[bnum] = old_command_defs[0]
661 self.commands_doprompt[bnum] = old_command_defs[1]
662 self.commands_silent[bnum] = old_command_defs[2]
664 del self.commands[bnum]
665 del self.commands_doprompt[bnum]
666 del self.commands_silent[bnum]
667 self.error('command definition aborted, old commands restored')
669 self.commands_defining = False
670 self.prompt = prompt_back
674 def do_break(self, arg, temporary = 0):
690 if self.breaks: # There's at least one
691 self.message("Num Type Disp Enb Where")
694 self.message(bp.bpformat())
711 f = self.lookupmodule(filename)
713 self.error('%r not found from sys.path' % filename)
721 self.error('Bad lineno: %s' % arg)
730 self.curframe.f_globals,
731 self.curframe_locals)
745 (ok, filename, ln) = self.lineinfo(arg)
747 self.error('The specified object %r is not a function '
753 filename = self.defaultFile()
755 line = self.checkline(filename, lineno)
758 err = self.set_break(filename, line, temporary, cond, funcname)
760 self.error(err)
762 bp = self.get_breaks(filename, line)[-1]
763 self.message("Breakpoint %d at %s:%d" %
767 def defaultFile(self):
769 filename = self.curframe.f_code.co_filename
770 if filename == '<string>' and self.mainpyfile:
771 filename = self.mainpyfile
779 def do_tbreak(self, arg):
784 self.do_break(arg, 1)
788 def lineinfo(self, identifier):
803 if parts[0] == 'self':
808 fname = self.defaultFile()
814 f = self.lookupmodule(parts[0])
821 def checkline(self, filename, lineno):
829 frame = getattr(self, 'curframe', None)
833 self.message('End of file')
839 self.error('Blank or comment')
843 def do_enable(self, arg):
851 bp = self.get_bpbynumber(i)
853 self.error(err)
856 self.message('Enabled %s' % bp)
860 def do_disable(self, arg):
871 bp = self.get_bpbynumber(i)
873 self.error(err)
876 self.message('Disabled %s' % bp)
880 def do_condition(self, arg):
893 bp = self.get_bpbynumber(args[0].strip())
895 self.error('Breakpoint number expected')
897 self.error(err)
901 self.message('Breakpoint %d is now unconditional.' % bp.number)
903 self.message('New condition set for breakpoint %d.' % bp.number)
907 def do_ignore(self, arg):
922 bp = self.get_bpbynumber(args[0].strip())
924 self.error('Breakpoint number expected')
926 self.error(err)
934 self.message('Will ignore next %s of breakpoint %d.' %
937 self.message('Will stop next time breakpoint %d is reached.'
942 def do_clear(self, arg):
957 self.clear_all_breaks()
959 self.message('Deleted %s' % bp)
971 bplist = self.get_breaks(filename, lineno)[:]
972 err = self.clear_break(filename, lineno)
974 self.error(err)
977 self.message('Deleted %s' % bp)
982 bp = self.get_bpbynumber(i)
984 self.error(err)
986 self.clear_bpbynumber(i)
987 self.message('Deleted %s' % bp)
993 def do_where(self, arg):
999 self.print_stack_trace()
1003 def _select_frame(self, number):
1004 assert 0 <= number < len(self.stack)
1005 self.curindex = number
1006 self.curframe = self.stack[self.curindex][0]
1007 self.curframe_locals = self.curframe.f_locals
1008 self.print_stack_entry(self.stack[self.curindex])
1009 self.lineno = None
1011 def do_up(self, arg):
1016 if self.curindex == 0:
1017 self.error('Oldest frame')
1022 self.error('Invalid frame count (%s)' % arg)
1027 newframe = max(0, self.curindex - count)
1028 self._select_frame(newframe)
1031 def do_down(self, arg):
1036 if self.curindex + 1 == len(self.stack):
1037 self.error('Newest frame')
1042 self.error('Invalid frame count (%s)' % arg)
1045 newframe = len(self.stack) - 1
1047 newframe = min(len(self.stack) - 1, self.curindex + count)
1048 self._select_frame(newframe)
1051 def do_until(self, arg):
1063 self.error('Error in argument: %r' % arg)
1065 if lineno <= self.curframe.f_lineno:
1066 self.error('"until" line number is smaller than current '
1071 self.set_until(self.curframe, lineno)
1075 def do_step(self, arg):
1081 self.set_step()
1085 def do_next(self, arg):
1090 self.set_next(self.curframe)
1094 def do_run(self, arg):
1107 self.error('Cannot run %s: %s' % (arg, e))
1115 def do_return(self, arg):
1119 self.set_return(self.curframe)
1123 def do_continue(self, arg):
1127 if not self.nosigint:
1130 signal.signal(signal.SIGINT, self.sigint_handler)
1137 self.set_continue()
1141 def do_jump(self, arg):
1152 if self.curindex + 1 != len(self.stack):
1153 self.error('You can only jump within the bottom frame')
1158 self.error("The 'jump' command requires a line number")
1163 self.curframe.f_lineno = arg
1164 self.stack[self.curindex] = self.stack[self.curindex][0], arg
1165 self.print_stack_entry(self.stack[self.curindex])
1167 self.error('Jump failed: %s' % e)
1170 def do_debug(self, arg):
1177 globals = self.curframe.f_globals
1178 locals = self.curframe_locals
1179 p = Pdb(self.completekey, self.stdin, self.stdout)
1180 p.prompt = "(%s) " % self.prompt.strip()
1181 self.message("ENTERING RECURSIVE DEBUGGER")
1185 self._error_exc()
1186 self.message("LEAVING RECURSIVE DEBUGGER")
1187 sys.settrace(self.trace_dispatch)
1188 self.lastcmd = p.lastcmd
1192 def do_quit(self, arg):
1196 self._user_requested_quit = True
1197 self.set_quit()
1203 def do_EOF(self, arg):
1207 self.message('')
1208 self._user_requested_quit = True
1209 self.set_quit()
1212 def do_args(self, arg):
1216 co = self.curframe.f_code
1217 dict = self.curframe_locals
1224 self.message('%s = %r' % (name, dict[name]))
1226 self.message('%s = *** undefined ***' % (name,))
1229 def do_retval(self, arg):
1233 if '__return__' in self.curframe_locals:
1234 self.message(repr(self.curframe_locals['__return__']))
1236 self.error('Not yet returned!')
1239 def _getval(self, arg):
1241 return eval(arg, self.curframe.f_globals, self.curframe_locals)
1243 self._error_exc()
1246 def _getval_except(self, arg, frame=None):
1249 return eval(arg, self.curframe.f_globals, self.curframe_locals)
1257 def _error_exc(self):
1259 self.error(traceback.format_exception_only(*exc_info)[-1].strip())
1261 def _msg_val_func(self, arg, func):
1263 val = self._getval(arg)
1267 self.message(func(val))
1269 self._error_exc()
1271 def do_p(self, arg):
1275 self._msg_val_func(arg, repr)
1277 def do_pp(self, arg):
1281 self._msg_val_func(arg, pprint.pformat)
1287 def do_list(self, arg):
1302 self.lastcmd = 'list'
1317 self.error('Error in argument: %r' % arg)
1319 elif self.lineno is None or arg == '.':
1320 first = max(1, self.curframe.f_lineno - 5)
1322 first = self.lineno + 1
1325 filename = self.curframe.f_code.co_filename
1329 tmp = self.curframe.f_globals.get("__file__")
1332 breaklist = self.get_file_breaks(filename)
1334 lines = linecache.getlines(filename, self.curframe.f_globals)
1335 self._print_lines(lines[first-1:last], first, breaklist,
1336 self.curframe)
1337 self.lineno = min(last, len(lines))
1339 self.message('[EOF]')
1344 def do_longlist(self, arg):
1348 filename = self.curframe.f_code.co_filename
1349 breaklist = self.get_file_breaks(filename)
1351 lines, lineno = self._getsourcelines(self.curframe)
1353 self.error(err)
1355 self._print_lines(lines, lineno, breaklist, self.curframe)
1358 def do_source(self, arg):
1363 obj = self._getval(arg)
1367 lines, lineno = self._getsourcelines(obj)
1369 self.error(err)
1371 self._print_lines(lines, lineno)
1375 def _print_lines(self, lines, start, breaks=(), frame=None):
1379 exc_lineno = self.tb_lineno.get(frame, -1)
1394 self.message(s + '\t' + line.rstrip())
1396 def do_whatis(self, arg):
1401 value = self._getval(arg)
1412 self.message('Method %s' % code.co_name)
1420 self.message('Function %s' % code.co_name)
1424 self.message('Class %s.%s' % (value.__module__, value.__qualname__))
1427 self.message(type(value))
1431 def do_display(self, arg):
1440 self.message('Currently displaying:')
1441 for item in self.displaying.get(self.curframe, {}).items():
1442 self.message('%s: %r' % item)
1444 val = self._getval_except(arg)
1445 self.displaying.setdefault(self.curframe, {})[arg] = val
1446 self.message('display %s: %r' % (arg, val))
1450 def do_undisplay(self, arg):
1459 del self.displaying.get(self.curframe, {})[arg]
1461 self.error('not displaying %s' % arg)
1463 self.displaying.pop(self.curframe, None)
1465 def complete_undisplay(self, text, line, begidx, endidx):
1466 return [e for e in self.displaying.get(self.curframe, {})
1469 def do_interact(self, arg):
1475 ns = {**self.curframe.f_globals, **self.curframe_locals}
1478 def do_alias(self, arg):
1499 # Print instance variables in self
1500 alias ps pi self
1504 keys = sorted(self.aliases.keys())
1506 self.message("%s = %s" % (alias, self.aliases[alias]))
1508 if args[0] in self.aliases and len(args) == 1:
1509 self.message("%s = %s" % (args[0], self.aliases[args[0]]))
1511 self.aliases[args[0]] = ' '.join(args[1:])
1513 def do_unalias(self, arg):
1519 if args[0] in self.aliases:
1520 del self.aliases[args[0]]
1522 def complete_unalias(self, text, line, begidx, endidx):
1523 return [a for a in self.aliases if a.startswith(text)]
1537 def print_stack_trace(self):
1539 for frame_lineno in self.stack:
1540 self.print_stack_entry(frame_lineno)
1544 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
1546 if frame is self.curframe:
1550 self.message(prefix +
1551 self.format_stack_entry(frame_lineno, prompt_prefix))
1555 def do_help(self, arg):
1563 return cmd.Cmd.do_help(self, arg)
1566 topic = getattr(self, 'help_' + arg)
1569 command = getattr(self, 'do_' + arg)
1571 self.error('No help for %r' % arg)
1574 self.error('No help for %r; please do not run Python with -OO '
1578 self.error('No help for %r; __doc__ string missing' % arg)
1580 self.message(command.__doc__.rstrip())
1584 def help_exec(self):
1594 self.message((self.help_exec.__doc__ or '').strip())
1596 def help_pdb(self):
1601 def lookupmodule(self, filename):
1610 if os.path.exists(f) and self.canonic(f) == self.mainpyfile:
1625 def _run(self, target: Union[_ModuleTarget, _ScriptTarget]):
1631 self._wait_for_mainpyfile = True
1632 self._user_requested_quit = False
1634 self.mainpyfile = self.canonic(target.filename)
1643 self.run(target.code)
1646 def _getsourcelines(self, obj):