29 from DebugConfig import ConfigQtNames, ConfigVarTypeStrings |
29 from DebugConfig import ConfigQtNames, ConfigVarTypeStrings |
30 from FlexCompleter import Completer |
30 from FlexCompleter import Completer |
31 from DebugUtilities import prepareJsonCommand |
31 from DebugUtilities import prepareJsonCommand |
32 from BreakpointWatch import Breakpoint, Watch |
32 from BreakpointWatch import Breakpoint, Watch |
33 |
33 |
34 if sys.version_info[0] == 2: |
34 from DebugUtilities import getargvalues, formatargvalues |
35 from inspect import getargvalues, formatargvalues |
|
36 else: |
|
37 unichr = chr |
|
38 from DebugUtilities import getargvalues, formatargvalues |
|
39 |
35 |
40 DebugClientInstance = None |
36 DebugClientInstance = None |
41 |
37 |
42 ############################################################################### |
38 ############################################################################### |
43 |
|
44 |
|
45 def DebugClientRawInput(prompt="", echo=True): |
|
46 """ |
|
47 Replacement for the standard raw_input() builtin (Python 2) and |
|
48 the standard input() builtin (Python 3). |
|
49 |
|
50 This function works with the split debugger. |
|
51 |
|
52 @param prompt prompt to be shown |
|
53 @type str |
|
54 @param echo flag indicating echoing of the input |
|
55 @type bool |
|
56 @return result of the raw_input()/input() call |
|
57 @rtype str |
|
58 """ |
|
59 if DebugClientInstance is None or not DebugClientInstance.redirect: |
|
60 return DebugClientOrigRawInput(prompt) |
|
61 |
|
62 return DebugClientInstance.raw_input(prompt, echo) |
|
63 |
39 |
64 |
40 |
65 def DebugClientInput(prompt=""): |
41 def DebugClientInput(prompt=""): |
66 """ |
42 """ |
67 Replacement for the standard input() builtin (Python 2). |
43 Replacement for the standard input() builtin. |
68 |
44 |
69 This function works with the split debugger. |
45 This function works with the split debugger. |
70 |
46 |
71 @param prompt prompt to be shown |
47 @param prompt prompt to be shown |
72 @type str |
48 @type str |
76 if DebugClientInstance is None or not DebugClientInstance.redirect: |
52 if DebugClientInstance is None or not DebugClientInstance.redirect: |
77 return DebugClientOrigInput(prompt) |
53 return DebugClientOrigInput(prompt) |
78 |
54 |
79 return DebugClientInstance.input(prompt) |
55 return DebugClientInstance.input(prompt) |
80 |
56 |
81 # Use our own input() and on Python 2 raw_input(). |
57 # Use our own input(). |
82 if sys.version_info[0] == 2: |
58 try: |
83 try: |
59 DebugClientOrigInput = __builtins__.__dict__['input'] |
84 DebugClientOrigRawInput = __builtins__.__dict__['raw_input'] |
60 __builtins__.__dict__['input'] = DebugClientInput |
85 __builtins__.__dict__['raw_input'] = DebugClientRawInput |
61 except (AttributeError, KeyError): |
86 except (AttributeError, KeyError): |
62 import __main__ |
87 import __main__ |
63 DebugClientOrigInput = __main__.__builtins__.__dict__['input'] |
88 DebugClientOrigRawInput = __main__.__builtins__.__dict__['raw_input'] |
64 __main__.__builtins__.__dict__['input'] = DebugClientInput |
89 __main__.__builtins__.__dict__['raw_input'] = DebugClientRawInput |
|
90 |
|
91 try: |
|
92 DebugClientOrigInput = __builtins__.__dict__['input'] |
|
93 __builtins__.__dict__['input'] = DebugClientInput |
|
94 except (AttributeError, KeyError): |
|
95 import __main__ |
|
96 DebugClientOrigInput = __main__.__builtins__.__dict__['input'] |
|
97 __main__.__builtins__.__dict__['input'] = DebugClientInput |
|
98 else: |
|
99 try: |
|
100 DebugClientOrigInput = __builtins__.__dict__['input'] |
|
101 __builtins__.__dict__['input'] = DebugClientRawInput |
|
102 except (AttributeError, KeyError): |
|
103 import __main__ |
|
104 DebugClientOrigInput = __main__.__builtins__.__dict__['input'] |
|
105 __main__.__builtins__.__dict__['input'] = DebugClientRawInput |
|
106 |
65 |
107 ############################################################################### |
66 ############################################################################### |
108 |
67 |
109 |
68 |
110 def DebugClientFork(): |
69 def DebugClientFork(): |
272 if m: |
231 if m: |
273 self.__coding = m.group(1) |
232 self.__coding = m.group(1) |
274 return |
233 return |
275 self.__coding = default |
234 self.__coding = default |
276 |
235 |
277 def raw_input(self, prompt, echo): |
236 def input(self, prompt, echo=True): |
278 """ |
237 """ |
279 Public method to implement raw_input() / input() using the event loop. |
238 Public method to implement input() using the event loop. |
280 |
239 |
281 @param prompt the prompt to be shown (string) |
240 @param prompt the prompt to be shown (string) |
282 @param echo Flag indicating echoing of the input (boolean) |
241 @param echo Flag indicating echoing of the input (boolean) |
283 @return the entered string |
242 @return the entered string |
284 """ |
243 """ |
287 "echo": echo, |
246 "echo": echo, |
288 }) |
247 }) |
289 self.eventLoop(True) |
248 self.eventLoop(True) |
290 return self.rawLine |
249 return self.rawLine |
291 |
250 |
292 def input(self, prompt): |
|
293 """ |
|
294 Public method to implement input() (Python 2) using the event loop. |
|
295 |
|
296 @param prompt the prompt to be shown (string) |
|
297 @return the entered string evaluated as a Python expresion |
|
298 """ |
|
299 return eval(self.raw_input(prompt, True)) |
|
300 |
|
301 def sessionClose(self, terminate=True): |
251 def sessionClose(self, terminate=True): |
302 """ |
252 """ |
303 Public method to close the session with the debugger and optionally |
253 Public method to close the session with the debugger and optionally |
304 terminate. |
254 terminate. |
305 |
255 |
306 @param terminate flag indicating to terminate (boolean) |
256 @param terminate flag indicating to terminate (boolean) |
307 """ |
257 """ |
308 try: |
258 try: |
309 self.set_quit() |
259 self.set_quit() |
310 except Exception: |
260 except Exception: # secok |
311 pass |
261 pass |
312 |
262 |
313 self.debugging = False |
263 self.debugging = False |
314 |
264 |
315 # make sure we close down our end of the socket |
265 # make sure we close down our end of the socket |
331 @param mode kind of code to be generated (string, exec or eval) |
281 @param mode kind of code to be generated (string, exec or eval) |
332 @return compiled code object (None in case of errors) |
282 @return compiled code object (None in case of errors) |
333 """ |
283 """ |
334 with codecs.open(filename, encoding=self.__coding) as fp: |
284 with codecs.open(filename, encoding=self.__coding) as fp: |
335 statement = fp.read() |
285 statement = fp.read() |
336 |
|
337 if sys.version_info[0] == 2: |
|
338 lines = statement.splitlines(True) |
|
339 for lineno, line in enumerate(lines[:2]): |
|
340 lines[lineno] = self.coding_re.sub('', line) |
|
341 |
|
342 statement = unicode('').join(lines) # __IGNORE_WARNING__ |
|
343 |
286 |
344 try: |
287 try: |
345 code = compile(statement + '\n', filename, mode) |
288 code = compile(statement + '\n', filename, mode) |
346 except SyntaxError: |
289 except SyntaxError: |
347 exctype, excval, exctb = sys.exc_info() |
290 exctype, excval, exctb = sys.exc_info() |
380 printerr(str(err)) |
323 printerr(str(err)) |
381 return |
324 return |
382 |
325 |
383 method = commandDict["method"] |
326 method = commandDict["method"] |
384 params = commandDict["params"] |
327 params = commandDict["params"] |
385 if "filename" in params and sys.version_info[0] == 2: |
|
386 params["filename"] = params["filename"].encode( |
|
387 sys.getfilesystemencoding()) |
|
388 |
328 |
389 if method == "RequestVariables": |
329 if method == "RequestVariables": |
390 self.__dumpVariables( |
330 self.__dumpVariables( |
391 params["frameNumber"], params["scope"], params["filters"]) |
331 params["frameNumber"], params["scope"], params["filters"]) |
392 |
332 |
406 self.sendJsonCommand("ResponseStack", { |
346 self.sendJsonCommand("ResponseStack", { |
407 "stack": stack, |
347 "stack": stack, |
408 }) |
348 }) |
409 |
349 |
410 elif method == "RequestCapabilities": |
350 elif method == "RequestCapabilities": |
411 clientType = "Python2" if sys.version_info[0] == 2 else "Python3" |
351 clientType = "Python3" |
412 self.sendJsonCommand("ResponseCapabilities", { |
352 self.sendJsonCommand("ResponseCapabilities", { |
413 "capabilities": self.__clientCapabilities(), |
353 "capabilities": self.__clientCapabilities(), |
414 "clientType": clientType |
354 "clientType": clientType |
415 }) |
355 }) |
416 |
356 |
581 if params["erase"]: |
521 if params["erase"]: |
582 self.prof.erase() |
522 self.prof.erase() |
583 self.debugMod.__dict__['__file__'] = sys.argv[0] |
523 self.debugMod.__dict__['__file__'] = sys.argv[0] |
584 sys.modules['__main__'] = self.debugMod |
524 sys.modules['__main__'] = self.debugMod |
585 script = '' |
525 script = '' |
586 if sys.version_info[0] == 2: |
526 with codecs.open(sys.argv[0], encoding=self.__coding) as fp: |
587 script = 'execfile({0!r})'.format(sys.argv[0]) |
527 script = fp.read() |
588 else: |
528 if script and not script.endswith('\n'): |
589 with codecs.open(sys.argv[0], encoding=self.__coding) as fp: |
529 script += '\n' |
590 script = fp.read() |
|
591 if script and not script.endswith('\n'): |
|
592 script += '\n' |
|
593 |
530 |
594 if script: |
531 if script: |
595 self.running = sys.argv[0] |
532 self.running = sys.argv[0] |
596 res = 0 |
533 res = 0 |
597 try: |
534 try: |
631 else: |
568 else: |
632 self.buffer = '' |
569 self.buffer = '' |
633 |
570 |
634 try: |
571 try: |
635 if self.running is None: |
572 if self.running is None: |
636 exec(code, self.debugMod.__dict__) |
573 exec(code, self.debugMod.__dict__) # secok |
637 else: |
574 else: |
638 if self.currentThread is None: |
575 if self.currentThread is None: |
639 # program has terminated |
576 # program has terminated |
640 self.running = None |
577 self.running = None |
641 _globals = self.debugMod.__dict__ |
578 _globals = self.debugMod.__dict__ |
659 ## reset sys.stdout to our redirector |
596 ## reset sys.stdout to our redirector |
660 ## (unconditionally) |
597 ## (unconditionally) |
661 if "sys" in _globals: |
598 if "sys" in _globals: |
662 __stdout = _globals["sys"].stdout |
599 __stdout = _globals["sys"].stdout |
663 _globals["sys"].stdout = self.writestream |
600 _globals["sys"].stdout = self.writestream |
664 exec(code, _globals, _locals) |
601 exec(code, _globals, _locals) # secok |
665 _globals["sys"].stdout = __stdout |
602 _globals["sys"].stdout = __stdout |
666 elif "sys" in _locals: |
603 elif "sys" in _locals: |
667 __stdout = _locals["sys"].stdout |
604 __stdout = _locals["sys"].stdout |
668 _locals["sys"].stdout = self.writestream |
605 _locals["sys"].stdout = self.writestream |
669 exec(code, _globals, _locals) |
606 exec(code, _globals, _locals) # secok |
670 _locals["sys"].stdout = __stdout |
607 _locals["sys"].stdout = __stdout |
671 else: |
608 else: |
672 exec(code, _globals, _locals) |
609 exec(code, _globals, _locals) # secok |
673 |
610 |
674 self.currentThread.storeFrameLocals(self.framenr) |
611 self.currentThread.storeFrameLocals(self.framenr) |
675 except SystemExit as exc: |
612 except SystemExit as exc: |
676 self.progTerminated(exc.code) |
613 self.progTerminated(exc.code) |
677 except Exception: |
614 except Exception: |
1370 |
1307 |
1371 @param fn filename (string) |
1308 @param fn filename (string) |
1372 @return the converted filename (string) |
1309 @return the converted filename (string) |
1373 """ |
1310 """ |
1374 if os.path.isabs(fn): |
1311 if os.path.isabs(fn): |
1375 if sys.version_info[0] == 2: |
|
1376 fn = fn.decode(sys.getfilesystemencoding()) |
|
1377 |
|
1378 return fn |
1312 return fn |
1379 |
1313 |
1380 # Check the cache. |
1314 # Check the cache. |
1381 if fn in self._fncache: |
1315 if fn in self._fncache: |
1382 return self._fncache[fn] |
1316 return self._fncache[fn] |
1385 for p in sys.path: |
1319 for p in sys.path: |
1386 afn = os.path.abspath(os.path.join(p, fn)) |
1320 afn = os.path.abspath(os.path.join(p, fn)) |
1387 nafn = os.path.normcase(afn) |
1321 nafn = os.path.normcase(afn) |
1388 |
1322 |
1389 if os.path.exists(nafn): |
1323 if os.path.exists(nafn): |
1390 if sys.version_info[0] == 2: |
|
1391 afn = afn.decode(sys.getfilesystemencoding()) |
|
1392 |
|
1393 self._fncache[fn] = afn |
1324 self._fncache[fn] = afn |
1394 d = os.path.dirname(afn) |
1325 d = os.path.dirname(afn) |
1395 if (d not in sys.path) and (d not in self.dircache): |
1326 if (d not in sys.path) and (d not in self.dircache): |
1396 self.dircache.append(d) |
1327 self.dircache.append(d) |
1397 return afn |
1328 return afn |
1588 |
1519 |
1589 return var, "" |
1520 return var, "" |
1590 |
1521 |
1591 def __formatQtVariable(self, value, qttype): |
1522 def __formatQtVariable(self, value, qttype): |
1592 """ |
1523 """ |
1593 Private method to produce a formatted output of a simple Qt4/Qt5 type. |
1524 Private method to produce a formatted output of a simple Qt5 type. |
1594 |
1525 |
1595 @param value variable to be formatted |
1526 @param value variable to be formatted |
1596 @param qttype type of the Qt variable to be formatted (string) |
1527 @param qttype type of the Qt variable to be formatted (string) |
1597 @return A tuple consisting of a list of formatted variables. Each |
1528 @return A tuple consisting of a list of formatted variables. Each |
1598 variable entry is a tuple of three elements, the variable name, |
1529 variable entry is a tuple of three elements, the variable name, |
1599 its type and value. |
1530 its type and value. |
1600 """ |
1531 """ |
1601 varlist = [] |
1532 varlist = [] |
1602 if qttype == 'QChar': |
1533 if qttype == 'QChar': |
1603 varlist.append( |
1534 varlist.append( |
1604 ("", "QChar", "{0}".format(unichr(value.unicode())))) |
1535 ("", "QChar", "{0}".format(chr(value.unicode())))) |
1605 varlist.append(("", "int", "{0:d}".format(value.unicode()))) |
1536 varlist.append(("", "int", "{0:d}".format(value.unicode()))) |
1606 elif qttype == 'QByteArray': |
1537 elif qttype == 'QByteArray': |
1607 varlist.append( |
1538 varlist.append( |
1608 ("bytes", "QByteArray", "{0}".format(bytes(value))[2:-1])) |
1539 ("bytes", "QByteArray", "{0}".format(bytes(value))[2:-1])) |
1609 varlist.append( |
1540 varlist.append( |
1792 if 0 in filterList and str(key)[:2] == '__': |
1723 if 0 in filterList and str(key)[:2] == '__': |
1793 continue |
1724 continue |
1794 |
1725 |
1795 # special handling for '__builtins__' (it's way too big) |
1726 # special handling for '__builtins__' (it's way too big) |
1796 if key == '__builtins__': |
1727 if key == '__builtins__': |
1797 rvalue = '<module __builtin__ (built-in)>' |
1728 rvalue = '<module builtins (built-in)>' |
1798 valtype = 'module' |
1729 valtype = 'module' |
1799 if ConfigVarTypeStrings.index(valtype) in filterList: |
1730 if ConfigVarTypeStrings.index(valtype) in filterList: |
1800 continue |
1731 continue |
1801 else: |
1732 else: |
1802 isQt = False |
1733 isQt = False |