eric6/DebugClients/Python/DebugClientBase.py

changeset 7637
c878e8255972
parent 7635
0cdead130a81
child 7639
422fd05e9c91
equal deleted inserted replaced
7636:61566f35ab22 7637:c878e8255972
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 39
44 40
45 def DebugClientRawInput(prompt="", echo=True): 41 def DebugClientRawInput(prompt=""):
46 """ 42 """
47 Replacement for the standard raw_input() builtin (Python 2) and 43 Replacement for the standard input() builtin.
48 the standard input() builtin (Python 3).
49 44
50 This function works with the split debugger. 45 This function works with the split debugger.
51 46
52 @param prompt prompt to be shown 47 @param prompt prompt to be shown
53 @type str 48 @type str
54 @param echo flag indicating echoing of the input
55 @type bool
56 @return result of the raw_input()/input() call 49 @return result of the raw_input()/input() call
57 @rtype str 50 @rtype str
58 """ 51 """
59 if DebugClientInstance is None or not DebugClientInstance.redirect: 52 if DebugClientInstance is None or not DebugClientInstance.redirect:
60 return DebugClientOrigRawInput(prompt)
61
62 return DebugClientInstance.raw_input(prompt, echo)
63
64
65 def DebugClientInput(prompt=""):
66 """
67 Replacement for the standard input() builtin (Python 2).
68
69 This function works with the split debugger.
70
71 @param prompt prompt to be shown
72 @type str
73 @return result of the input() call
74 @rtype str
75 """
76 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'] = DebugClientRawInput
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'] = DebugClientRawInput
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):
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 """
285 self.sendJsonCommand("RequestRaw", { 244 self.sendJsonCommand("RequestRaw", {
286 "prompt": prompt, 245 "prompt": prompt,
287 "echo": echo, 246 "echo": True,
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)) # secok
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
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:
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
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(

eric ide

mercurial