Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1''' 

2Entry point module (keep at root): 

3 

4This module starts the debugger. 

5''' 

6import sys 

7from contextlib import contextmanager 

8import weakref 

9 

10from _pydevd_bundle.pydevd_collect_try_except_info import collect_return_info 

11 

12if sys.version_info[:2] < (2, 6): 

13 raise RuntimeError('The PyDev.Debugger requires Python 2.6 onwards to be run. If you need to use an older Python version, use an older version of the debugger.') 

14 

15import itertools 

16import atexit 

17import os 

18import traceback 

19from functools import partial 

20from collections import defaultdict 

21 

22from _pydevd_bundle.pydevd_constants import IS_JYTH_LESS25, IS_PYCHARM, get_thread_id, get_current_thread_id, \ 

23 dict_keys, dict_iter_items, DebugInfoHolder, PYTHON_SUSPEND, STATE_SUSPEND, STATE_RUN, get_frame, xrange, \ 

24 clear_cached_thread_id, INTERACTIVE_MODE_AVAILABLE, SHOW_DEBUG_INFO_ENV, IS_PY34_OR_GREATER, IS_PY36_OR_GREATER, \ 

25 IS_PY2, NULL, NO_FTRACE, dummy_excepthook, IS_CPYTHON, GOTO_HAS_RESPONSE 

26from _pydev_bundle import fix_getpass 

27from _pydev_bundle import pydev_imports, pydev_log 

28from _pydev_bundle._pydev_filesystem_encoding import getfilesystemencoding 

29from _pydev_bundle.pydev_is_thread_alive import is_thread_alive 

30from _pydev_imps._pydev_saved_modules import threading 

31from _pydev_imps._pydev_saved_modules import time 

32from _pydev_imps._pydev_saved_modules import thread 

33from _pydevd_bundle import pydevd_io, pydevd_vm_type 

34import pydevd_tracing 

35from _pydevd_bundle import pydevd_utils 

36from _pydevd_bundle import pydevd_vars 

37from _pydev_bundle.pydev_override import overrides 

38from _pydevd_bundle.pydevd_breakpoints import ExceptionBreakpoint, set_fallback_excepthook, disable_excepthook 

39from _pydevd_bundle.pydevd_comm import CMD_SET_BREAK, CMD_SET_NEXT_STATEMENT, CMD_STEP_INTO, CMD_STEP_OVER, \ 

40 CMD_STEP_RETURN, CMD_STEP_INTO_MY_CODE, CMD_THREAD_SUSPEND, CMD_RUN_TO_LINE, \ 

41 CMD_ADD_EXCEPTION_BREAK, CMD_SMART_STEP_INTO, InternalConsoleExec, NetCommandFactory, \ 

42 PyDBDaemonThread, _queue, ReaderThread, GetGlobalDebugger, get_global_debugger, \ 

43 set_global_debugger, WriterThread, pydevd_log, \ 

44 start_client, start_server, InternalGetBreakpointException, InternalSendCurrExceptionTrace, \ 

45 InternalSendCurrExceptionTraceProceeded, CommunicationRole, run_as_pydevd_daemon_thread 

46from _pydevd_bundle.pydevd_custom_frames import CustomFramesContainer, custom_frames_container_init 

47from _pydevd_bundle.pydevd_frame_utils import add_exception_to_frame, remove_exception_from_frame 

48from _pydevd_bundle.pydevd_kill_all_pydevd_threads import kill_all_pydev_threads 

49from _pydevd_bundle.pydevd_trace_dispatch import ( 

50 trace_dispatch as _trace_dispatch, global_cache_skips, global_cache_frame_skips, show_tracing_warning) 

51from _pydevd_frame_eval.pydevd_frame_eval_main import ( 

52 frame_eval_func, dummy_trace_dispatch, show_frame_eval_warning) 

53from _pydevd_bundle.pydevd_additional_thread_info import set_additional_thread_info 

54from _pydevd_bundle.pydevd_utils import save_main_module 

55from pydevd_concurrency_analyser.pydevd_concurrency_logger import ThreadingLogger, AsyncioLogger, send_message, cur_time 

56from pydevd_concurrency_analyser.pydevd_thread_wrappers import wrap_threads, wrap_asyncio 

57from pydevd_file_utils import get_fullname, rPath, get_package_dir 

58import pydev_ipython # @UnusedImport 

59from _pydevd_bundle.pydevd_dont_trace_files import DONT_TRACE 

60from pydevd_file_utils import get_abs_path_real_path_and_base_from_frame, NORM_PATHS_AND_BASE_CONTAINER 

61 

62get_file_type = DONT_TRACE.get 

63 

64__version_info__ = (1, 4, 0) 

65__version_info_str__ = [] 

66for v in __version_info__: 

67 __version_info_str__.append(str(v)) 

68 

69__version__ = '.'.join(__version_info_str__) 

70 

71#IMPORTANT: pydevd_constants must be the 1st thing defined because it'll keep a reference to the original sys._getframe 

72 

73 

74def install_breakpointhook(pydevd_breakpointhook=None): 

75 if pydevd_breakpointhook is None: 

76 from _pydevd_bundle.pydevd_breakpointhook import breakpointhook 

77 pydevd_breakpointhook = breakpointhook 

78 if sys.version_info >= (3, 7): 

79 # There are some choices on how to provide the breakpoint hook. Namely, we can provide a 

80 # PYTHONBREAKPOINT which provides the import path for a method to be executed or we 

81 # can override sys.breakpointhook. 

82 # pydevd overrides sys.breakpointhook instead of providing an environment variable because 

83 # it's possible that the debugger starts the user program but is not available in the 

84 # PYTHONPATH (and would thus fail to be imported if PYTHONBREAKPOINT was set to pydevd.settrace). 

85 # Note that the implementation still takes PYTHONBREAKPOINT in account (so, if it was provided 

86 # by someone else, it'd still work). 

87 sys.breakpointhook = pydevd_breakpointhook 

88 

89# Install the breakpoint hook at import time. 

90install_breakpointhook() 

91 

92SUPPORT_PLUGINS = not IS_JYTH_LESS25 

93PluginManager = None 

94if SUPPORT_PLUGINS: 

95 from _pydevd_bundle.pydevd_plugin_utils import PluginManager 

96 

97 

98threadingEnumerate = threading.enumerate 

99threadingCurrentThread = threading.currentThread 

100 

101original_excepthook = sys.__excepthook__ 

102 

103try: 

104 'dummy'.encode('utf-8') # Added because otherwise Jython 2.2.1 wasn't finding the encoding (if it wasn't loaded in the main thread). 

105except: 

106 pass 

107 

108 

109connected = False 

110bufferStdOutToServer = False 

111bufferStdErrToServer = False 

112remote = False 

113forked = False 

114 

115file_system_encoding = getfilesystemencoding() 

116 

117 

118#======================================================================================================================= 

119# PyDBCommandThread 

120#======================================================================================================================= 

121class PyDBCommandThread(PyDBDaemonThread): 

122 

123 def __init__(self, py_db): 

124 PyDBDaemonThread.__init__(self) 

125 self._py_db_command_thread_event = py_db._py_db_command_thread_event 

126 self.py_db = py_db 

127 self.setName('pydevd.CommandThread') 

128 

129 @overrides(PyDBDaemonThread._on_run) 

130 def _on_run(self): 

131 # Delay a bit this initialization to wait for the main program to start. 

132 time.sleep(0.3) 

133 

134 if self.killReceived: 

135 return 

136 

137 try: 

138 while not self.killReceived: 

139 try: 

140 self.py_db.process_internal_commands() 

141 except: 

142 pydevd_log(0, 'Finishing debug communication...(2)') 

143 self._py_db_command_thread_event.clear() 

144 self._py_db_command_thread_event.wait(0.3) 

145 except: 

146 pydev_log.debug(sys.exc_info()[0]) 

147 # only got this error in interpreter shutdown 

148 # pydevd_log(0, 'Finishing debug communication...(3)') 

149 

150 

151#======================================================================================================================= 

152# CheckOutputThread 

153# Non-daemon thread: guarantees that all data is written even if program is finished 

154#======================================================================================================================= 

155class CheckOutputThread(PyDBDaemonThread): 

156 

157 def __init__(self, py_db): 

158 PyDBDaemonThread.__init__(self) 

159 self.py_db = py_db 

160 self.setName('pydevd.CheckAliveThread') 

161 self.daemon = False 

162 

163 @overrides(PyDBDaemonThread._on_run) 

164 def _on_run(self): 

165 while not self.killReceived: 

166 time.sleep(0.3) 

167 if not self.py_db.has_threads_alive() and self.py_db.writer.empty(): 

168 try: 

169 pydev_log.debug("No threads alive, finishing debug session") 

170 self.py_db.finish_debugging_session() 

171 kill_all_pydev_threads() 

172 except: 

173 traceback.print_exc() 

174 

175 self.wait_pydb_threads_to_finish() 

176 

177 self.killReceived = True 

178 

179 self.py_db.check_output_redirect() 

180 

181 def wait_pydb_threads_to_finish(self, timeout=0.5): 

182 pydev_log.debug("Waiting for pydb daemon threads to finish") 

183 pydb_daemon_threads = self.created_pydb_daemon_threads 

184 started_at = time.time() 

185 while time.time() < started_at + timeout: 

186 if len(pydb_daemon_threads) == 1 and pydb_daemon_threads.get(self, None): 

187 return 

188 time.sleep(0.01) 

189 pydev_log.debug("The following pydb threads may not finished correctly: %s" 

190 % ', '.join([t.getName() for t in pydb_daemon_threads if t is not self])) 

191 

192 def do_kill_pydev_thread(self): 

193 self.killReceived = True 

194 

195 

196class TrackedLock(object): 

197 """The lock that tracks if it has been acquired by the current thread 

198 """ 

199 def __init__(self): 

200 self._lock = thread.allocate_lock() 

201 # thread-local storage 

202 self._tls = threading.local() 

203 self._tls.is_lock_acquired = False 

204 

205 def acquire(self): 

206 self._lock.acquire() 

207 self._tls.is_lock_acquired = True 

208 

209 def release(self): 

210 self._lock.release() 

211 self._tls.is_lock_acquired = False 

212 

213 def __enter__(self): 

214 self.acquire() 

215 

216 def __exit__(self, exc_type, exc_val, exc_tb): 

217 self.release() 

218 

219 def is_acquired_by_current_thread(self): 

220 return self._tls.is_lock_acquired 

221 

222class AbstractSingleNotificationBehavior(object): 

223 ''' 

224 The basic usage should be: 

225 

226 # Increment the request time for the suspend. 

227 single_notification_behavior.increment_suspend_time() 

228 

229 # Notify that this is a pause request (when a pause, not a breakpoint). 

230 single_notification_behavior.on_pause() 

231 

232 # Mark threads to be suspended. 

233 set_suspend(...) 

234 

235 # On do_wait_suspend, use notify_thread_suspended: 

236 def do_wait_suspend(...): 

237 with single_notification_behavior.notify_thread_suspended(thread_id): 

238 ... 

239 ''' 

240 

241 __slots__ = [ 

242 '_last_resume_notification_time', 

243 '_last_suspend_notification_time', 

244 '_lock', 

245 '_next_request_time', 

246 '_suspend_time_request', 

247 '_suspended_thread_ids', 

248 '_pause_requested', 

249 ] 

250 

251 NOTIFY_OF_PAUSE_TIMEOUT = .5 

252 

253 def __init__(self): 

254 self._next_request_time = partial(next, itertools.count()) 

255 self._last_suspend_notification_time = -1 

256 self._last_resume_notification_time = -1 

257 self._suspend_time_request = self._next_request_time() 

258 self._lock = thread.allocate_lock() 

259 self._suspended_thread_ids = set() 

260 self._pause_requested = False 

261 

262 def send_suspend_notification(self, thread_id, stop_reason): 

263 raise AssertionError('abstract: subclasses must override.') 

264 

265 def send_resume_notification(self, thread_id): 

266 raise AssertionError('abstract: subclasses must override.') 

267 

268 def increment_suspend_time(self): 

269 with self._lock: 

270 self._suspend_time_request = self._next_request_time() 

271 

272 def on_pause(self): 

273 # Upon a pause, we should force sending new suspend notifications 

274 # if no notification is sent after some time and there's some thread already stopped. 

275 with self._lock: 

276 self._pause_requested = True 

277 global_suspend_time = self._suspend_time_request 

278 run_as_pydevd_daemon_thread(self._notify_after_timeout, global_suspend_time) 

279 

280 def _notify_after_timeout(self, global_suspend_time): 

281 time.sleep(self.NOTIFY_OF_PAUSE_TIMEOUT) 

282 with self._lock: 

283 if self._suspended_thread_ids: 

284 if global_suspend_time > self._last_suspend_notification_time: 

285 self._last_suspend_notification_time = global_suspend_time 

286 # Notify about any thread which is currently suspended. 

287 self.send_suspend_notification(next(iter(self._suspended_thread_ids)), CMD_THREAD_SUSPEND) 

288 

289 @contextmanager 

290 def notify_thread_suspended(self, thread_id, stop_reason): 

291 with self._lock: 

292 pause_requested = self._pause_requested 

293 if pause_requested: 

294 # When a suspend notification is sent, reset the pause flag. 

295 self._pause_requested = False 

296 

297 self._suspended_thread_ids.add(thread_id) 

298 

299 # CMD_THREAD_SUSPEND should always be a side-effect of a break, so, only 

300 # issue for a CMD_THREAD_SUSPEND if a pause is pending. 

301 if stop_reason != CMD_THREAD_SUSPEND or pause_requested: 

302 if self._suspend_time_request > self._last_suspend_notification_time: 

303 self._last_suspend_notification_time = self._suspend_time_request 

304 self.send_suspend_notification(thread_id, stop_reason) 

305 try: 

306 yield # At this point the thread must be actually suspended. 

307 finally: 

308 # on resume (step, continue all): 

309 with self._lock: 

310 self._suspended_thread_ids.remove(thread_id) 

311 if self._last_resume_notification_time < self._last_suspend_notification_time: 

312 self._last_resume_notification_time = self._last_suspend_notification_time 

313 self.send_resume_notification(thread_id) 

314 

315 

316class ThreadsSuspendedSingleNotification(AbstractSingleNotificationBehavior): 

317 

318 __slots__ = AbstractSingleNotificationBehavior.__slots__ + [ 

319 'multi_threads_single_notification', '_py_db'] 

320 

321 def __init__(self, py_db): 

322 AbstractSingleNotificationBehavior.__init__(self) 

323 # If True, pydevd will send a single notification when all threads are suspended/resumed. 

324 self.multi_threads_single_notification = False 

325 self._py_db = weakref.ref(py_db) 

326 

327 @overrides(AbstractSingleNotificationBehavior.send_resume_notification) 

328 def send_resume_notification(self, thread_id): 

329 py_db = self._py_db() 

330 if py_db is not None: 

331 py_db.writer.add_command(py_db.cmd_factory.make_thread_resume_single_notification(thread_id)) 

332 

333 @overrides(AbstractSingleNotificationBehavior.send_suspend_notification) 

334 def send_suspend_notification(self, thread_id, stop_reason): 

335 py_db = self._py_db() 

336 if py_db is not None: 

337 py_db.writer.add_command(py_db.cmd_factory.make_thread_suspend_single_notification(thread_id, stop_reason)) 

338 

339 @overrides(AbstractSingleNotificationBehavior.notify_thread_suspended) 

340 @contextmanager 

341 def notify_thread_suspended(self, thread_id, stop_reason): 

342 if self.multi_threads_single_notification: 

343 with AbstractSingleNotificationBehavior.notify_thread_suspended(self, thread_id, stop_reason): 

344 yield 

345 else: 

346 yield 

347 

348 

349# noinspection SpellCheckingInspection 

350def stoptrace(): 

351 """Stops tracing in the current process and undoes all monkey-patches done by the debugger.""" 

352 global connected 

353 

354 if connected: 

355 pydevd_tracing.restore_sys_set_trace_func() 

356 sys.settrace(None) 

357 try: 

358 # Not available in Jython! 

359 threading.settrace(None) # Disable tracing for all future threads. 

360 except: 

361 pass 

362 

363 from _pydev_bundle.pydev_monkey import undo_patch_thread_modules 

364 undo_patch_thread_modules() 

365 

366 debugger = get_global_debugger() 

367 

368 if debugger: 

369 

370 debugger.set_trace_for_frame_and_parents(get_frame(), disable=True) 

371 debugger.exiting() 

372 

373 kill_all_pydev_threads() 

374 

375 connected = False 

376 

377 

378#======================================================================================================================= 

379# PyDB 

380#======================================================================================================================= 

381class PyDB(object): 

382 """ Main debugging class 

383 Lots of stuff going on here: 

384 

385 PyDB starts two threads on startup that connect to remote debugger (RDB) 

386 The threads continuously read & write commands to RDB. 

387 PyDB communicates with these threads through command queues. 

388 Every RDB command is processed by calling process_net_command. 

389 Every PyDB net command is sent to the net by posting NetCommand to WriterThread queue 

390 

391 Some commands need to be executed on the right thread (suspend/resume & friends) 

392 These are placed on the internal command queue. 

393 """ 

394 

395 

396 def __init__(self, set_as_global=True): 

397 if set_as_global: 

398 set_global_debugger(self) 

399 pydevd_tracing.replace_sys_set_trace_func() 

400 

401 self.reader = None 

402 self.writer = None 

403 self.output_checker_thread = None 

404 self.py_db_command_thread = None 

405 self.quitting = None 

406 self.cmd_factory = NetCommandFactory() 

407 self._cmd_queue = defaultdict(_queue.Queue) # Key is thread id or '*', value is Queue 

408 

409 self.breakpoints = {} 

410 

411 # mtime to be raised when breakpoints change 

412 self.mtime = 0 

413 

414 self.file_to_id_to_line_breakpoint = {} 

415 self.file_to_id_to_plugin_breakpoint = {} 

416 

417 # Note: breakpoints dict should not be mutated: a copy should be created 

418 # and later it should be assigned back (to prevent concurrency issues). 

419 self.break_on_uncaught_exceptions = {} 

420 self.break_on_caught_exceptions = {} 

421 

422 self.ready_to_run = False 

423 self._main_lock = TrackedLock() 

424 self._lock_running_thread_ids = thread.allocate_lock() 

425 self._py_db_command_thread_event = threading.Event() 

426 if set_as_global: 

427 CustomFramesContainer._py_db_command_thread_event = self._py_db_command_thread_event 

428 

429 self._finish_debugging_session = False 

430 self._termination_event_set = False 

431 self.signature_factory = None 

432 self.SetTrace = pydevd_tracing.SetTrace 

433 self.skip_on_exceptions_thrown_in_same_context = False 

434 self.ignore_exceptions_thrown_in_lines_with_ignore_exception = True 

435 

436 # Suspend debugger even if breakpoint condition raises an exception. 

437 # May be changed with CMD_PYDEVD_JSON_CONFIG. 

438 self.skip_suspend_on_breakpoint_exception = () # By default suspend on any Exception. 

439 self.skip_print_breakpoint_exception = () # By default print on any Exception. 

440 

441 # By default user can step into properties getter/setter/deleter methods 

442 self.disable_property_trace = False 

443 self.disable_property_getter_trace = False 

444 self.disable_property_setter_trace = False 

445 self.disable_property_deleter_trace = False 

446 

447 #this is a dict of thread ids pointing to thread ids. Whenever a command is passed to the java end that 

448 #acknowledges that a thread was created, the thread id should be passed here -- and if at some time we do not 

449 #find that thread alive anymore, we must remove it from this list and make the java side know that the thread 

450 #was killed. 

451 self._running_thread_ids = {} 

452 self._set_breakpoints_with_id = False 

453 

454 # This attribute holds the file-> lines which have an @IgnoreException. 

455 self.filename_to_lines_where_exceptions_are_ignored = {} 

456 

457 #working with plugins (lazily initialized) 

458 self.plugin = None 

459 self.has_plugin_line_breaks = False 

460 self.has_plugin_exception_breaks = False 

461 self.thread_analyser = None 

462 self.asyncio_analyser = None 

463 

464 # matplotlib support in debugger and debug console 

465 self.mpl_in_use = False 

466 self.mpl_hooks_in_debug_console = False 

467 self.mpl_modules_for_patching = {} 

468 

469 self._filename_to_not_in_scope = {} 

470 self.first_breakpoint_reached = False 

471 self.is_filter_enabled = pydevd_utils.is_filter_enabled() 

472 self.is_filter_libraries = pydevd_utils.is_filter_libraries() 

473 self.show_return_values = False 

474 self.remove_return_values_flag = False 

475 self.redirect_output = False 

476 

477 # this flag disables frame evaluation even if it's available 

478 self.use_frame_eval = True 

479 self.stop_on_start = False 

480 

481 # If True, pydevd will send a single notification when all threads are suspended/resumed. 

482 self._threads_suspended_single_notification = ThreadsSuspendedSingleNotification(self) 

483 

484 self._local_thread_trace_func = threading.local() 

485 

486 # sequence id of `CMD_PROCESS_CREATED` command -> threading.Event 

487 self.process_created_msg_received_events = dict() 

488 # the role PyDB plays in the communication with IDE 

489 self.communication_role = None 

490 

491 self.collect_return_info = collect_return_info 

492 

493 # If True, pydevd will stop on assertion errors in tests. 

494 self.stop_on_failed_tests = False 

495 

496 def get_thread_local_trace_func(self): 

497 try: 

498 thread_trace_func = self._local_thread_trace_func.thread_trace_func 

499 except AttributeError: 

500 thread_trace_func = self.trace_dispatch 

501 return thread_trace_func 

502 

503 def enable_tracing(self, thread_trace_func=None, apply_to_all_threads=False): 

504 ''' 

505 Enables tracing. 

506 

507 If in regular mode (tracing), will set the tracing function to the tracing 

508 function for this thread -- by default it's `PyDB.trace_dispatch`, but after 

509 `PyDB.enable_tracing` is called with a `thread_trace_func`, the given function will 

510 be the default for the given thread. 

511 ''' 

512 set_fallback_excepthook() 

513 if self.frame_eval_func is not None: 

514 self.frame_eval_func() 

515 pydevd_tracing.SetTrace(self.dummy_trace_dispatch) 

516 

517 if IS_CPYTHON and apply_to_all_threads: 

518 pydevd_tracing.set_trace_to_threads(self.dummy_trace_dispatch) 

519 return 

520 

521 if thread_trace_func is None: 

522 thread_trace_func = self.get_thread_local_trace_func() 

523 else: 

524 self._local_thread_trace_func.thread_trace_func = thread_trace_func 

525 

526 pydevd_tracing.SetTrace(thread_trace_func) 

527 if IS_CPYTHON and apply_to_all_threads: 

528 pydevd_tracing.set_trace_to_threads(thread_trace_func) 

529 

530 def disable_tracing(self): 

531 pydevd_tracing.SetTrace(None) 

532 

533 def on_breakpoints_changed(self, removed=False): 

534 ''' 

535 When breakpoints change, we have to re-evaluate all the assumptions we've made so far. 

536 ''' 

537 if not self.ready_to_run: 

538 # No need to do anything if we're still not running. 

539 return 

540 

541 self.mtime += 1 

542 if not removed: 

543 # When removing breakpoints we can leave tracing as was, but if a breakpoint was added 

544 # we have to reset the tracing for the existing functions to be re-evaluated. 

545 self.set_tracing_for_untraced_contexts() 

546 

547 def set_tracing_for_untraced_contexts(self, ignore_current_thread=False): 

548 # Enable the tracing for existing threads (because there may be frames being executed that 

549 # are currently untraced). 

550 ignore_thread = None 

551 if ignore_current_thread: 

552 ignore_thread = threading.current_thread() 

553 

554 ignore_thread_ids = set( 

555 t.ident for t in threadingEnumerate() 

556 if getattr(t, 'is_pydev_daemon_thread', False) or getattr(t, 'pydev_do_not_trace', False) 

557 ) 

558 

559 if IS_CPYTHON: 

560 # Note: use sys._current_frames instead of threading.enumerate() because this way 

561 # we also see C/C++ threads, not only the ones visible to the threading module. 

562 tid_to_frame = sys._current_frames() 

563 

564 for thread_id, frame in tid_to_frame.items(): 

565 if thread_id not in ignore_thread_ids: 

566 self.set_trace_for_frame_and_parents(frame) 

567 else: 

568 threads = threadingEnumerate() 

569 try: 

570 for t in threads: 

571 if t.ident in ignore_thread_ids or t is ignore_thread: 

572 continue 

573 

574 additional_info = set_additional_thread_info(t) 

575 frame = additional_info.get_topmost_frame(t) 

576 try: 

577 if frame is not None: 

578 self.set_trace_for_frame_and_parents(frame) 

579 finally: 

580 frame = None 

581 finally: 

582 frame = None 

583 t = None 

584 threads = None 

585 additional_info = None 

586 

587 @property 

588 def multi_threads_single_notification(self): 

589 return self._threads_suspended_single_notification.multi_threads_single_notification 

590 

591 @multi_threads_single_notification.setter 

592 def multi_threads_single_notification(self, notify): 

593 self._threads_suspended_single_notification.multi_threads_single_notification = notify 

594 

595 def get_plugin_lazy_init(self): 

596 if self.plugin is None and SUPPORT_PLUGINS: 

597 self.plugin = PluginManager(self) 

598 return self.plugin 

599 

600 def in_project_scope(self, filename): 

601 return pydevd_utils.in_project_roots(filename) 

602 

603 def is_ignored_by_filters(self, filename): 

604 return pydevd_utils.is_ignored_by_filter(filename) 

605 

606 def is_exception_trace_in_project_scope(self, trace): 

607 return pydevd_utils.is_exception_trace_in_project_scope(trace) 

608 

609 def is_top_level_trace_in_project_scope(self, trace): 

610 return pydevd_utils.is_top_level_trace_in_project_scope(trace) 

611 

612 def is_test_item_or_set_up_caller(self, frame): 

613 return pydevd_utils.is_test_item_or_set_up_caller(frame) 

614 

615 def set_unit_tests_debugging_mode(self): 

616 self.stop_on_failed_tests = True 

617 

618 def has_threads_alive(self): 

619 for t in pydevd_utils.get_non_pydevd_threads(): 

620 if isinstance(t, PyDBDaemonThread): 

621 pydev_log.error_once( 

622 'Error in debugger: Found PyDBDaemonThread not marked with is_pydev_daemon_thread=True.\n') 

623 

624 if is_thread_alive(t): 

625 if not t.isDaemon() or hasattr(t, "__pydevd_main_thread"): 

626 return True 

627 

628 return False 

629 

630 def finish_debugging_session(self): 

631 self._finish_debugging_session = True 

632 

633 

634 def initialize_network(self, sock): 

635 try: 

636 sock.settimeout(None) # infinite, no timeouts from now on - jython does not have it 

637 except: 

638 pass 

639 self.writer = WriterThread(sock) 

640 self.reader = ReaderThread(sock) 

641 self.writer.start() 

642 self.reader.start() 

643 

644 time.sleep(0.1) # give threads time to start 

645 

646 def connect(self, host, port): 

647 if host: 

648 self.communication_role = CommunicationRole.CLIENT 

649 s = start_client(host, port) 

650 else: 

651 self.communication_role = CommunicationRole.SERVER 

652 s = start_server(port) 

653 

654 self.initialize_network(s) 

655 

656 

657 def get_internal_queue(self, thread_id): 

658 """ returns internal command queue for a given thread. 

659 if new queue is created, notify the RDB about it """ 

660 if thread_id.startswith('__frame__'): 

661 thread_id = thread_id[thread_id.rfind('|') + 1:] 

662 return self._cmd_queue[thread_id] 

663 

664 def post_internal_command(self, int_cmd, thread_id): 

665 """ if thread_id is *, post to the '*' queue""" 

666 queue = self.get_internal_queue(thread_id) 

667 queue.put(int_cmd) 

668 

669 def enable_output_redirection(self, redirect_stdout, redirect_stderr): 

670 global bufferStdOutToServer 

671 global bufferStdErrToServer 

672 

673 bufferStdOutToServer = redirect_stdout 

674 bufferStdErrToServer = redirect_stderr 

675 self.redirect_output = redirect_stdout or redirect_stderr 

676 if bufferStdOutToServer: 

677 init_stdout_redirect() 

678 if bufferStdErrToServer: 

679 init_stderr_redirect() 

680 

681 def check_output_redirect(self): 

682 global bufferStdOutToServer 

683 global bufferStdErrToServer 

684 

685 if bufferStdOutToServer: 

686 init_stdout_redirect() 

687 

688 if bufferStdErrToServer: 

689 init_stderr_redirect() 

690 

691 

692 def init_matplotlib_in_debug_console(self): 

693 # import hook and patches for matplotlib support in debug console 

694 from _pydev_bundle.pydev_import_hook import import_hook_manager 

695 for module in dict_keys(self.mpl_modules_for_patching): 

696 import_hook_manager.add_module_name(module, self.mpl_modules_for_patching.pop(module)) 

697 

698 def init_matplotlib_support(self): 

699 # prepare debugger for integration with matplotlib GUI event loop 

700 from pydev_ipython.matplotlibtools import activate_matplotlib, activate_pylab, activate_pyplot, do_enable_gui 

701 # enable_gui_function in activate_matplotlib should be called in main thread. Unlike integrated console, 

702 # in the debug console we have no interpreter instance with exec_queue, but we run this code in the main 

703 # thread and can call it directly. 

704 class _MatplotlibHelper: 

705 _return_control_osc = False 

706 

707 def return_control(): 

708 # Some of the input hooks (e.g. Qt4Agg) check return control without doing 

709 # a single operation, so we don't return True on every 

710 # call when the debug hook is in place to allow the GUI to run 

711 _MatplotlibHelper._return_control_osc = not _MatplotlibHelper._return_control_osc 

712 return _MatplotlibHelper._return_control_osc 

713 

714 from pydev_ipython.inputhook import set_return_control_callback 

715 set_return_control_callback(return_control) 

716 

717 self.mpl_modules_for_patching = {"matplotlib": lambda: activate_matplotlib(do_enable_gui), 

718 "matplotlib.pyplot": activate_pyplot, 

719 "pylab": activate_pylab } 

720 

721 def _activate_mpl_if_needed(self): 

722 if len(self.mpl_modules_for_patching) > 0: 

723 for module in dict_keys(self.mpl_modules_for_patching): 

724 if module in sys.modules: 

725 activate_function = self.mpl_modules_for_patching.pop(module) 

726 activate_function() 

727 self.mpl_in_use = True 

728 

729 def _call_mpl_hook(self): 

730 try: 

731 from pydev_ipython.inputhook import get_inputhook 

732 inputhook = get_inputhook() 

733 if inputhook: 

734 inputhook() 

735 except: 

736 pass 

737 

738 

739 def notify_thread_created(self, thread_id, thread, use_lock=True): 

740 if self.writer is None: 

741 # Protect about threads being created before the communication structure is in place 

742 # (note that they will appear later on anyways as pydevd does reconcile live/dead threads 

743 # when processing internal commands, albeit it may take longer and in general this should 

744 # not be usual as it's expected that the debugger is live before other threads are created). 

745 return 

746 

747 with self._lock_running_thread_ids if use_lock else NULL: 

748 if thread_id in self._running_thread_ids: 

749 return 

750 

751 additional_info = set_additional_thread_info(thread) 

752 if additional_info.pydev_notify_kill: 

753 # After we notify it should be killed, make sure we don't notify it's alive (on a racing condition 

754 # this could happen as we may notify before the thread is stopped internally). 

755 return 

756 

757 self._running_thread_ids[thread_id] = thread 

758 

759 self.writer.add_command(self.cmd_factory.make_thread_created_message(thread)) 

760 

761 def notify_thread_not_alive(self, thread_id, use_lock=True): 

762 """ if thread is not alive, cancel trace_dispatch processing """ 

763 if self.writer is None: 

764 return 

765 

766 with self._lock_running_thread_ids if use_lock else NULL: 

767 thread = self._running_thread_ids.pop(thread_id, None) 

768 if thread is None: 

769 return 

770 

771 was_notified = thread.additional_info.pydev_notify_kill 

772 if not was_notified: 

773 thread.additional_info.pydev_notify_kill = True 

774 

775 self.writer.add_command(self.cmd_factory.make_thread_killed_message(thread_id)) 

776 

777 def process_internal_commands(self): 

778 '''This function processes internal commands 

779 ''' 

780 with self._main_lock: 

781 self.check_output_redirect() 

782 

783 program_threads_alive = {} 

784 all_threads = threadingEnumerate() 

785 program_threads_dead = [] 

786 with self._lock_running_thread_ids: 

787 for t in all_threads: 

788 if getattr(t, 'is_pydev_daemon_thread', False): 

789 pass # I.e.: skip the DummyThreads created from pydev daemon threads 

790 elif isinstance(t, PyDBDaemonThread): 

791 pydev_log.error_once('Error in debugger: Found PyDBDaemonThread not marked with is_pydev_daemon_thread=True.\n') 

792 

793 elif is_thread_alive(t): 

794 if not self._running_thread_ids: 

795 # Fix multiprocessing debug with breakpoints in both main and child processes 

796 # (https://youtrack.jetbrains.com/issue/PY-17092) When the new process is created, the main 

797 # thread in the new process already has the attribute 'pydevd_id', so the new thread doesn't 

798 # get new id with its process number and the debugger loses access to both threads. 

799 # Therefore we should update thread_id for every main thread in the new process. 

800 

801 # Fix it for all existing threads. 

802 for existing_thread in all_threads: 

803 old_thread_id = get_thread_id(existing_thread) 

804 clear_cached_thread_id(t) 

805 

806 thread_id = get_thread_id(t) 

807 if thread_id != old_thread_id: 

808 if pydevd_vars.has_additional_frames_by_id(old_thread_id): 

809 frames_by_id = pydevd_vars.get_additional_frames_by_id(old_thread_id) 

810 pydevd_vars.add_additional_frame_by_id(thread_id, frames_by_id) 

811 

812 thread_id = get_thread_id(t) 

813 program_threads_alive[thread_id] = t 

814 

815 self.notify_thread_created(thread_id, t, use_lock=False) 

816 

817 # Compute and notify about threads which are no longer alive. 

818 thread_ids = list(self._running_thread_ids.keys()) 

819 for thread_id in thread_ids: 

820 if thread_id not in program_threads_alive: 

821 program_threads_dead.append(thread_id) 

822 

823 for thread_id in program_threads_dead: 

824 self.notify_thread_not_alive(thread_id, use_lock=False) 

825 

826 # Without self._lock_running_thread_ids 

827 if len(program_threads_alive) == 0: 

828 self.finish_debugging_session() 

829 for t in all_threads: 

830 if hasattr(t, 'do_kill_pydev_thread'): 

831 t.do_kill_pydev_thread() 

832 else: 

833 # Actually process the commands now (make sure we don't have a lock for _lock_running_thread_ids 

834 # acquired at this point as it could lead to a deadlock if some command evaluated tried to 

835 # create a thread and wait for it -- which would try to notify about it getting that lock). 

836 curr_thread_id = get_current_thread_id(threadingCurrentThread()) 

837 

838 for thread_id in (curr_thread_id, '*'): 

839 queue = self.get_internal_queue(thread_id) 

840 

841 # some commands must be processed by the thread itself... if that's the case, 

842 # we will re-add the commands to the queue after executing. 

843 cmds_to_add_back = [] 

844 

845 try: 

846 while True: 

847 int_cmd = queue.get(False) 

848 

849 if not self.mpl_hooks_in_debug_console and isinstance(int_cmd, InternalConsoleExec): 

850 # add import hooks for matplotlib patches if only debug console was started 

851 try: 

852 self.init_matplotlib_in_debug_console() 

853 self.mpl_in_use = True 

854 except: 

855 pydevd_log(2, "Matplotlib support in debug console failed", traceback.format_exc()) 

856 self.mpl_hooks_in_debug_console = True 

857 

858 if int_cmd.can_be_executed_by(curr_thread_id): 

859 pydevd_log(2, "processing internal command ", str(int_cmd)) 

860 int_cmd.do_it(self) 

861 else: 

862 pydevd_log(2, "NOT processing internal command ", str(int_cmd)) 

863 cmds_to_add_back.append(int_cmd) 

864 

865 except _queue.Empty: # @UndefinedVariable 

866 # this is how we exit 

867 for int_cmd in cmds_to_add_back: 

868 queue.put(int_cmd) 

869 

870 def consolidate_breakpoints(self, file, id_to_breakpoint, breakpoints): 

871 break_dict = {} 

872 for breakpoint_id, pybreakpoint in dict_iter_items(id_to_breakpoint): 

873 break_dict[pybreakpoint.line] = pybreakpoint 

874 

875 breakpoints[file] = break_dict 

876 self.clear_skip_caches() 

877 

878 def clear_skip_caches(self): 

879 global_cache_skips.clear() 

880 global_cache_frame_skips.clear() 

881 

882 def add_break_on_exception( 

883 self, 

884 exception, 

885 condition, 

886 expression, 

887 notify_on_handled_exceptions, 

888 notify_on_unhandled_exceptions, 

889 notify_on_first_raise_only, 

890 ignore_libraries=False 

891 ): 

892 try: 

893 eb = ExceptionBreakpoint( 

894 exception, 

895 condition, 

896 expression, 

897 notify_on_handled_exceptions, 

898 notify_on_unhandled_exceptions, 

899 notify_on_first_raise_only, 

900 ignore_libraries 

901 ) 

902 except ImportError: 

903 pydev_log.error("Error unable to add break on exception for: %s (exception could not be imported)\n" % (exception,)) 

904 return None 

905 

906 if eb.notify_on_unhandled_exceptions: 

907 cp = self.break_on_uncaught_exceptions.copy() 

908 cp[exception] = eb 

909 if DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS > 0: 

910 pydev_log.error("Exceptions to hook on terminate: %s\n" % (cp,)) 

911 self.break_on_uncaught_exceptions = cp 

912 

913 if eb.notify_on_handled_exceptions: 

914 cp = self.break_on_caught_exceptions.copy() 

915 cp[exception] = eb 

916 if DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS > 0: 

917 pydev_log.error("Exceptions to hook always: %s\n" % (cp,)) 

918 self.break_on_caught_exceptions = cp 

919 

920 return eb 

921 

922 def _mark_suspend(self, thread, stop_reason): 

923 info = set_additional_thread_info(thread) 

924 info.suspend_type = PYTHON_SUSPEND 

925 thread.stop_reason = stop_reason 

926 if info.pydev_step_cmd == -1: 

927 # If the step command is not specified, set it to step into 

928 # to make sure it'll break as soon as possible. 

929 info.pydev_step_cmd = CMD_STEP_INTO 

930 

931 # Mark as suspend as the last thing. 

932 info.pydev_state = STATE_SUSPEND 

933 

934 return info 

935 

936 def set_suspend(self, thread, stop_reason, suspend_other_threads=False, is_pause=False): 

937 ''' 

938 :param thread: 

939 The thread which should be suspended. 

940 

941 :param stop_reason: 

942 Reason why the thread was suspended. 

943 

944 :param suspend_other_threads: 

945 Whether to force other threads to be suspended (i.e.: when hitting a breakpoint 

946 with a suspend all threads policy). 

947 

948 :param is_pause: 

949 If this is a pause to suspend all threads, any thread can be considered as the 'main' 

950 thread paused. 

951 ''' 

952 self._threads_suspended_single_notification.increment_suspend_time() 

953 if is_pause: 

954 self._threads_suspended_single_notification.on_pause() 

955 

956 info = self._mark_suspend(thread, stop_reason) 

957 

958 if is_pause: 

959 # Must set tracing after setting the state to suspend. 

960 frame = info.get_topmost_frame(thread) 

961 if frame is not None: 

962 try: 

963 self.set_trace_for_frame_and_parents(frame) 

964 finally: 

965 frame = None 

966 

967 # If conditional breakpoint raises any exception during evaluation send the details to the client. 

968 if stop_reason == CMD_SET_BREAK and info.conditional_breakpoint_exception is not None: 

969 conditional_breakpoint_exception_tuple = info.conditional_breakpoint_exception 

970 info.conditional_breakpoint_exception = None 

971 self._send_breakpoint_condition_exception(thread, conditional_breakpoint_exception_tuple) 

972 

973 if not suspend_other_threads and self.multi_threads_single_notification: 

974 # In the mode which gives a single notification when all threads are 

975 # stopped, stop all threads whenever a set_suspend is issued. 

976 suspend_other_threads = True 

977 

978 if suspend_other_threads: 

979 # Suspend all other threads. 

980 all_threads = pydevd_utils.get_non_pydevd_threads() 

981 for t in all_threads: 

982 if getattr(t, 'pydev_do_not_trace', None): 

983 pass # skip some other threads, i.e. ipython history saving thread from debug console 

984 else: 

985 if t is thread: 

986 continue 

987 info = self._mark_suspend(t, CMD_THREAD_SUSPEND) 

988 frame = info.get_topmost_frame(t) 

989 

990 # Reset the time as in this case this was not the main thread suspended. 

991 if frame is not None: 

992 try: 

993 self.set_trace_for_frame_and_parents(frame) 

994 finally: 

995 frame = None 

996 

997 

998 def _send_breakpoint_condition_exception(self, thread, conditional_breakpoint_exception_tuple): 

999 """If conditional breakpoint raises an exception during evaluation 

1000 send exception details to java 

1001 """ 

1002 thread_id = get_thread_id(thread) 

1003 # conditional_breakpoint_exception_tuple - should contain 2 values (exception_type, stacktrace) 

1004 if conditional_breakpoint_exception_tuple and len(conditional_breakpoint_exception_tuple) == 2: 

1005 exc_type, stacktrace = conditional_breakpoint_exception_tuple 

1006 int_cmd = InternalGetBreakpointException(thread_id, exc_type, stacktrace) 

1007 self.post_internal_command(int_cmd, thread_id) 

1008 

1009 

1010 def send_caught_exception_stack(self, thread, arg, curr_frame_id): 

1011 """Sends details on the exception which was caught (and where we stopped) to the java side. 

1012 

1013 arg is: exception type, description, traceback object 

1014 """ 

1015 thread_id = get_thread_id(thread) 

1016 int_cmd = InternalSendCurrExceptionTrace(thread_id, arg, curr_frame_id) 

1017 self.post_internal_command(int_cmd, thread_id) 

1018 

1019 

1020 def send_caught_exception_stack_proceeded(self, thread): 

1021 """Sends that some thread was resumed and is no longer showing an exception trace. 

1022 """ 

1023 thread_id = get_thread_id(thread) 

1024 int_cmd = InternalSendCurrExceptionTraceProceeded(thread_id) 

1025 self.post_internal_command(int_cmd, thread_id) 

1026 self.process_internal_commands() 

1027 

1028 def send_process_created_message(self): 

1029 """Sends a message that a new process has been created. 

1030 """ 

1031 cmd = self.cmd_factory.make_process_created_message() 

1032 self.writer.add_command(cmd) 

1033 

1034 def send_process_will_be_substituted(self): 

1035 """When `PyDB` works in server mode this method sends a message that a 

1036 new process is going to be created. After that it waits for the 

1037 response from the IDE to be sure that the IDE received this message. 

1038 Waiting for the response is required because the current process might 

1039 become substituted before it actually sends the message and the IDE 

1040 will not try to connect to `PyDB` in this case. 

1041 

1042 When `PyDB` works in client mode this method does nothing because the 

1043 substituted process will try to connect to the IDE itself. 

1044 """ 

1045 if self.communication_role == CommunicationRole.SERVER: 

1046 if self._main_lock.is_acquired_by_current_thread(): 

1047 # if `_main_lock` is acquired by the current thread then `event.wait()` would stuck 

1048 # because the corresponding call of `event.set()` is made under the same `_main_lock` 

1049 pydev_log.debug("Skip sending process substitution notification\n") 

1050 return 

1051 

1052 cmd = self.cmd_factory.make_process_created_message() 

1053 # register event before putting command to the message queue 

1054 event = threading.Event() 

1055 self.process_created_msg_received_events[cmd.seq] = event 

1056 self.writer.add_command(cmd) 

1057 event.wait() 

1058 

1059 def set_next_statement(self, frame, event, func_name, next_line): 

1060 stop = False 

1061 response_msg = "" 

1062 old_line = frame.f_lineno 

1063 if event == 'line' or event == 'exception': 

1064 #If we're already in the correct context, we have to stop it now, because we can act only on 

1065 #line events -- if a return was the next statement it wouldn't work (so, we have this code 

1066 #repeated at pydevd_frame). 

1067 

1068 curr_func_name = frame.f_code.co_name 

1069 

1070 #global context is set with an empty name 

1071 if curr_func_name in ('?', '<module>'): 

1072 curr_func_name = '' 

1073 

1074 if func_name == '*' or curr_func_name == func_name: 

1075 line = next_line 

1076 frame.f_trace = self.trace_dispatch 

1077 frame.f_lineno = line 

1078 stop = True 

1079 else: 

1080 response_msg = "jump is available only within the bottom frame" 

1081 return stop, old_line, response_msg 

1082 

1083 def cancel_async_evaluation(self, thread_id, frame_id): 

1084 self._main_lock.acquire() 

1085 try: 

1086 all_threads = threadingEnumerate() 

1087 for t in all_threads: 

1088 if getattr(t, 'is_pydev_daemon_thread', False) and hasattr(t, 'cancel_event') and hasattr(t, 'thread_id') and \ 

1089 t.thread_id == thread_id and t.frame_id == frame_id: 

1090 t.cancel_event.set() 

1091 except: 

1092 traceback.print_exc() 

1093 finally: 

1094 self._main_lock.release() 

1095 

1096 def do_wait_suspend(self, thread, frame, event, arg, send_suspend_message=True, is_unhandled_exception=False): #@UnusedVariable 

1097 """ busy waits until the thread state changes to RUN 

1098 it expects thread's state as attributes of the thread. 

1099 Upon running, processes any outstanding Stepping commands. 

1100 

1101 :param is_unhandled_exception: 

1102 If True we should use the line of the exception instead of the current line in the frame 

1103 as the paused location on the top-level frame (exception info must be passed on 'arg'). 

1104 """ 

1105 self.process_internal_commands() 

1106 thread_stack_str = '' # @UnusedVariable -- this is here so that `make_get_thread_stack_message` 

1107 # can retrieve it later. 

1108 

1109 thread_id = get_current_thread_id(thread) 

1110 stop_reason = thread.stop_reason 

1111 suspend_type = thread.additional_info.trace_suspend_type 

1112 

1113 if send_suspend_message: 

1114 # Send the suspend message 

1115 message = thread.additional_info.pydev_message 

1116 thread.additional_info.trace_suspend_type = 'trace' # Reset to trace mode for next call. 

1117 frame_to_lineno = {} 

1118 if is_unhandled_exception: 

1119 # arg must be the exception info (tuple(exc_type, exc, traceback)) 

1120 tb = arg[2] 

1121 while tb is not None: 

1122 frame_to_lineno[tb.tb_frame] = tb.tb_lineno 

1123 tb = tb.tb_next 

1124 cmd = self.cmd_factory.make_thread_suspend_message(thread_id, frame, stop_reason, message, suspend_type, frame_to_lineno=frame_to_lineno) 

1125 frame_to_lineno.clear() 

1126 thread_stack_str = cmd.thread_stack_str # @UnusedVariable -- `make_get_thread_stack_message` uses it later. 

1127 self.writer.add_command(cmd) 

1128 

1129 with CustomFramesContainer.custom_frames_lock: # @UndefinedVariable 

1130 from_this_thread = [] 

1131 

1132 for frame_id, custom_frame in dict_iter_items(CustomFramesContainer.custom_frames): 

1133 if custom_frame.thread_id == thread.ident: 

1134 # print >> sys.stderr, 'Frame created: ', frame_id 

1135 self.writer.add_command(self.cmd_factory.make_custom_frame_created_message(frame_id, custom_frame.name)) 

1136 self.writer.add_command(self.cmd_factory.make_thread_suspend_message(frame_id, custom_frame.frame, CMD_THREAD_SUSPEND, "", suspend_type)) 

1137 

1138 from_this_thread.append(frame_id) 

1139 

1140 with self._threads_suspended_single_notification.notify_thread_suspended(thread_id, stop_reason): 

1141 self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread) 

1142 

1143 def _do_wait_suspend(self, thread, frame, event, arg, suspend_type, from_this_thread): 

1144 info = thread.additional_info 

1145 

1146 if info.pydev_state == STATE_SUSPEND and not self._finish_debugging_session: 

1147 # before every stop check if matplotlib modules were imported inside script code 

1148 self._activate_mpl_if_needed() 

1149 

1150 while info.pydev_state == STATE_SUSPEND and not self._finish_debugging_session: 

1151 if self.mpl_in_use: 

1152 # call input hooks if only matplotlib is in use 

1153 self._call_mpl_hook() 

1154 

1155 self.process_internal_commands() 

1156 time.sleep(0.01) 

1157 

1158 self.cancel_async_evaluation(get_current_thread_id(thread), str(id(frame))) 

1159 

1160 # process any stepping instructions 

1161 if info.pydev_step_cmd == CMD_STEP_INTO or info.pydev_step_cmd == CMD_STEP_INTO_MY_CODE: 

1162 info.pydev_step_stop = None 

1163 info.pydev_smart_step_context.smart_step_stop = None 

1164 

1165 elif info.pydev_step_cmd == CMD_STEP_OVER: 

1166 info.pydev_step_stop = frame 

1167 info.pydev_smart_step_context.smart_step_stop = None 

1168 self.set_trace_for_frame_and_parents(frame) 

1169 

1170 elif info.pydev_step_cmd == CMD_SMART_STEP_INTO: 

1171 self.set_trace_for_frame_and_parents(frame) 

1172 info.pydev_step_stop = None 

1173 info.pydev_smart_step_context.smart_step_stop = frame 

1174 

1175 elif info.pydev_step_cmd == CMD_RUN_TO_LINE or info.pydev_step_cmd == CMD_SET_NEXT_STATEMENT: 

1176 self.set_trace_for_frame_and_parents(frame) 

1177 stop = False 

1178 response_msg = "" 

1179 old_line = frame.f_lineno 

1180 if not IS_PYCHARM: 

1181 stop, _, response_msg = self.set_next_statement(frame, event, info.pydev_func_name, info.pydev_next_line) 

1182 if stop: 

1183 # Set next did not work... 

1184 info.pydev_step_cmd = -1 

1185 info.pydev_state = STATE_SUSPEND 

1186 thread.stop_reason = CMD_THREAD_SUSPEND 

1187 # return to the suspend state and wait for other command (without sending any 

1188 # additional notification to the client). 

1189 self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread) 

1190 return 

1191 else: 

1192 try: 

1193 stop, old_line, response_msg = self.set_next_statement(frame, event, info.pydev_func_name, info.pydev_next_line) 

1194 except ValueError as e: 

1195 response_msg = "%s" % e 

1196 finally: 

1197 if GOTO_HAS_RESPONSE: 

1198 seq = info.pydev_message 

1199 cmd = self.cmd_factory.make_set_next_stmnt_status_message(seq, stop, response_msg) 

1200 self.writer.add_command(cmd) 

1201 info.pydev_message = '' 

1202 

1203 if stop: 

1204 cmd = self.cmd_factory.make_thread_run_message(get_current_thread_id(thread), info.pydev_step_cmd) 

1205 self.writer.add_command(cmd) 

1206 info.pydev_state = STATE_SUSPEND 

1207 thread.stop_reason = CMD_SET_NEXT_STATEMENT 

1208 self.do_wait_suspend(thread, frame, event, arg) 

1209 return 

1210 else: 

1211 info.pydev_step_cmd = -1 

1212 info.pydev_state = STATE_SUSPEND 

1213 thread.stop_reason = CMD_THREAD_SUSPEND 

1214 # return to the suspend state and wait for other command 

1215 self.do_wait_suspend(thread, frame, event, arg, send_suspend_message=False) 

1216 return 

1217 

1218 elif info.pydev_step_cmd == CMD_STEP_RETURN: 

1219 back_frame = frame.f_back 

1220 if back_frame is not None: 

1221 # steps back to the same frame (in a return call it will stop in the 'back frame' for the user) 

1222 info.pydev_step_stop = frame 

1223 self.set_trace_for_frame_and_parents(frame) 

1224 else: 

1225 # No back frame?!? -- this happens in jython when we have some frame created from an awt event 

1226 # (the previous frame would be the awt event, but this doesn't make part of 'jython', only 'java') 

1227 # so, if we're doing a step return in this situation, it's the same as just making it run 

1228 info.pydev_step_stop = None 

1229 info.pydev_step_cmd = -1 

1230 info.pydev_state = STATE_RUN 

1231 

1232 del frame 

1233 cmd = self.cmd_factory.make_thread_run_message(get_current_thread_id(thread), info.pydev_step_cmd) 

1234 self.writer.add_command(cmd) 

1235 

1236 with CustomFramesContainer.custom_frames_lock: 

1237 # The ones that remained on last_running must now be removed. 

1238 for frame_id in from_this_thread: 

1239 # print >> sys.stderr, 'Removing created frame: ', frame_id 

1240 self.writer.add_command(self.cmd_factory.make_thread_killed_message(frame_id)) 

1241 

1242 def stop_on_unhandled_exception(self, thread, frame, frames_byid, arg): 

1243 pydev_log.debug("We are stopping in post-mortem\n") 

1244 thread_id = get_thread_id(thread) 

1245 pydevd_vars.add_additional_frame_by_id(thread_id, frames_byid) 

1246 exctype, value, tb = arg 

1247 tb = pydevd_utils.get_top_level_trace_in_project_scope(tb) 

1248 if sys.excepthook != dummy_excepthook: 

1249 original_excepthook(exctype, value, tb) 

1250 disable_excepthook() # Avoid printing the exception for the second time. 

1251 try: 

1252 try: 

1253 add_exception_to_frame(frame, arg) 

1254 self.set_suspend(thread, CMD_ADD_EXCEPTION_BREAK) 

1255 self.do_wait_suspend(thread, frame, 'exception', arg, is_unhandled_exception=True) 

1256 except KeyboardInterrupt as e: 

1257 raise e 

1258 except: 

1259 pydev_log.error("We've got an error while stopping in post-mortem: %s\n" % (arg[0],)) 

1260 finally: 

1261 remove_exception_from_frame(frame) 

1262 pydevd_vars.remove_additional_frame_by_id(thread_id) 

1263 frame = None 

1264 

1265 def set_trace_for_frame_and_parents(self, frame, **kwargs): 

1266 disable = kwargs.pop('disable', False) 

1267 assert not kwargs 

1268 

1269 while frame is not None: 

1270 try: 

1271 # Make fast path faster! 

1272 abs_path_real_path_and_base = NORM_PATHS_AND_BASE_CONTAINER[frame.f_code.co_filename] 

1273 except: 

1274 abs_path_real_path_and_base = get_abs_path_real_path_and_base_from_frame(frame) 

1275 

1276 # Don't change the tracing on debugger-related files 

1277 file_type = get_file_type(abs_path_real_path_and_base[-1]) 

1278 

1279 if file_type is None: 

1280 if disable: 

1281 if frame.f_trace is not None and frame.f_trace is not NO_FTRACE: 

1282 frame.f_trace = NO_FTRACE 

1283 

1284 elif frame.f_trace is not self.trace_dispatch: 

1285 frame.f_trace = self.trace_dispatch 

1286 

1287 frame = frame.f_back 

1288 

1289 del frame 

1290 

1291 def _create_pydb_command_thread(self): 

1292 curr_pydb_command_thread = self.py_db_command_thread 

1293 if curr_pydb_command_thread is not None: 

1294 curr_pydb_command_thread.do_kill_pydev_thread() 

1295 

1296 new_pydb_command_thread = self.py_db_command_thread = PyDBCommandThread(self) 

1297 new_pydb_command_thread.start() 

1298 

1299 def _create_check_output_thread(self): 

1300 curr_output_checker_thread = self.output_checker_thread 

1301 if curr_output_checker_thread is not None: 

1302 curr_output_checker_thread.do_kill_pydev_thread() 

1303 

1304 output_checker_thread = self.output_checker_thread = CheckOutputThread(self) 

1305 output_checker_thread.start() 

1306 

1307 def start_auxiliary_daemon_threads(self): 

1308 self._create_pydb_command_thread() 

1309 self._create_check_output_thread() 

1310 

1311 def prepare_to_run(self, enable_tracing_from_start=True): 

1312 ''' Shared code to prepare debugging by installing traces and registering threads ''' 

1313 self._create_pydb_command_thread() 

1314 if self.redirect_output or self.signature_factory is not None or self.thread_analyser is not None: 

1315 # we need all data to be sent to IDE even after program finishes 

1316 self._create_check_output_thread() 

1317 # turn off frame evaluation for concurrency visualization 

1318 self.frame_eval_func = None 

1319 

1320 self.patch_threads() 

1321 if enable_tracing_from_start: 

1322 pydevd_tracing.SetTrace(self.trace_dispatch) 

1323 

1324 if show_tracing_warning or show_frame_eval_warning: 

1325 cmd = self.cmd_factory.make_show_warning_message("cython") 

1326 self.writer.add_command(cmd) 

1327 

1328 def patch_threads(self): 

1329 try: 

1330 # not available in jython! 

1331 threading.settrace(self.trace_dispatch) # for all future threads 

1332 except: 

1333 pass 

1334 

1335 from _pydev_bundle.pydev_monkey import patch_thread_modules 

1336 patch_thread_modules() 

1337 

1338 def run(self, file, globals=None, locals=None, is_module=False, set_trace=True): 

1339 module_name = None 

1340 entry_point_fn = '' 

1341 if is_module: 

1342 # When launching with `python -m <module>`, python automatically adds 

1343 # an empty path to the PYTHONPATH which resolves files in the current 

1344 # directory, so, depending how pydevd itself is launched, we may need 

1345 # to manually add such an entry to properly resolve modules in the 

1346 # current directory 

1347 if '' not in sys.path: 

1348 sys.path.insert(0, '') 

1349 file, _, entry_point_fn = file.partition(':') 

1350 module_name = file 

1351 filename = get_fullname(file) 

1352 if filename is None: 

1353 mod_dir = get_package_dir(module_name) 

1354 if mod_dir is None: 

1355 sys.stderr.write("No module named %s\n" % file) 

1356 return 

1357 else: 

1358 filename = get_fullname("%s.__main__" % module_name) 

1359 if filename is None: 

1360 sys.stderr.write("No module named %s\n" % file) 

1361 return 

1362 else: 

1363 file = filename 

1364 else: 

1365 file = filename 

1366 mod_dir = os.path.dirname(filename) 

1367 main_py = os.path.join(mod_dir, '__main__.py') 

1368 main_pyc = os.path.join(mod_dir, '__main__.pyc') 

1369 if filename.endswith('__init__.pyc'): 

1370 if os.path.exists(main_pyc): 

1371 filename = main_pyc 

1372 elif os.path.exists(main_py): 

1373 filename = main_py 

1374 elif filename.endswith('__init__.py'): 

1375 if os.path.exists(main_pyc) and not os.path.exists(main_py): 

1376 filename = main_pyc 

1377 elif os.path.exists(main_py): 

1378 filename = main_py 

1379 

1380 sys.argv[0] = filename 

1381 

1382 if os.path.isdir(file): 

1383 new_target = os.path.join(file, '__main__.py') 

1384 if os.path.isfile(new_target): 

1385 file = new_target 

1386 

1387 m = None 

1388 if globals is None: 

1389 m = save_main_module(file, 'pydevd') 

1390 globals = m.__dict__ 

1391 try: 

1392 globals['__builtins__'] = __builtins__ 

1393 except NameError: 

1394 pass # Not there on Jython... 

1395 

1396 if locals is None: 

1397 locals = globals 

1398 

1399 # Predefined (writable) attributes: __name__ is the module's name; 

1400 # __doc__ is the module's documentation string, or None if unavailable; 

1401 # __file__ is the pathname of the file from which the module was loaded, 

1402 # if it was loaded from a file. The __file__ attribute is not present for 

1403 # C modules that are statically linked into the interpreter; for extension modules 

1404 # loaded dynamically from a shared library, it is the pathname of the shared library file. 

1405 

1406 

1407 # I think this is an ugly hack, bug it works (seems to) for the bug that says that sys.path should be the same in 

1408 # debug and run. 

1409 if sys.path[0] != '' and m is not None and m.__file__.startswith(sys.path[0]): 

1410 # print >> sys.stderr, 'Deleting: ', sys.path[0] 

1411 del sys.path[0] 

1412 

1413 if not is_module: 

1414 # now, the local directory has to be added to the pythonpath 

1415 # sys.path.insert(0, os.getcwd()) 

1416 # Changed: it's not the local directory, but the directory of the file launched 

1417 # The file being run must be in the pythonpath (even if it was not before) 

1418 sys.path.insert(0, os.path.split(rPath(file))[0]) 

1419 

1420 if set_trace: 

1421 

1422 while not self.ready_to_run: 

1423 time.sleep(0.1) # busy wait until we receive run command 

1424 

1425 if self.break_on_caught_exceptions or self.has_plugin_line_breaks or self.has_plugin_exception_breaks \ 

1426 or self.signature_factory: 

1427 # disable frame evaluation if there are exception breakpoints with 'On raise' activation policy 

1428 # or if there are plugin exception breakpoints or if collecting run-time types is enabled 

1429 self.frame_eval_func = None 

1430 

1431 # call prepare_to_run when we already have all information about breakpoints 

1432 self.prepare_to_run() 

1433 

1434 t = threadingCurrentThread() 

1435 thread_id = get_current_thread_id(t) 

1436 

1437 if self.thread_analyser is not None: 

1438 wrap_threads() 

1439 self.thread_analyser.set_start_time(cur_time()) 

1440 send_message("threading_event", 0, t.getName(), thread_id, "thread", "start", file, 1, None, parent=get_thread_id(t)) 

1441 

1442 if self.asyncio_analyser is not None: 

1443 if IS_PY36_OR_GREATER: 

1444 wrap_asyncio() 

1445 # we don't have main thread in asyncio graph, so we should add a fake event 

1446 send_message("asyncio_event", 0, "Task", "Task", "thread", "stop", file, 1, frame=None, parent=None) 

1447 

1448 try: 

1449 if INTERACTIVE_MODE_AVAILABLE: 

1450 self.init_matplotlib_support() 

1451 except: 

1452 sys.stderr.write("Matplotlib support in debugger failed\n") 

1453 traceback.print_exc() 

1454 

1455 if hasattr(sys, 'exc_clear'): 

1456 # we should clean exception information in Python 2, before user's code execution 

1457 sys.exc_clear() 

1458 

1459 # Notify that the main thread is created. 

1460 self.notify_thread_created(thread_id, t) 

1461 

1462 if self.stop_on_start: 

1463 info = set_additional_thread_info(t) 

1464 t.additional_info.pydev_step_cmd = CMD_STEP_INTO_MY_CODE 

1465 

1466 # Note: important: set the tracing right before calling _exec. 

1467 if set_trace: 

1468 self.enable_tracing() 

1469 

1470 return self._exec(is_module, entry_point_fn, module_name, file, globals, locals) 

1471 

1472 def _exec(self, is_module, entry_point_fn, module_name, file, globals, locals): 

1473 ''' 

1474 This function should have frames tracked by unhandled exceptions (the `_exec` name is important). 

1475 ''' 

1476 if not is_module: 

1477 pydev_imports.execfile(file, globals, locals) # execute the script 

1478 else: 

1479 # treat ':' as a separator between module and entry point function 

1480 # if there is no entry point we run we same as with -m switch. Otherwise we perform 

1481 # an import and execute the entry point 

1482 if entry_point_fn: 

1483 mod = __import__(module_name, level=0, fromlist=[entry_point_fn], globals=globals, locals=locals) 

1484 func = getattr(mod, entry_point_fn) 

1485 func() 

1486 else: 

1487 # Run with the -m switch 

1488 import runpy 

1489 if hasattr(runpy, '_run_module_as_main'): 

1490 # Newer versions of Python actually use this when the -m switch is used. 

1491 if sys.version_info[:2] <= (2, 6): 

1492 runpy._run_module_as_main(module_name, set_argv0=False) 

1493 else: 

1494 runpy._run_module_as_main(module_name, alter_argv=False) 

1495 else: 

1496 runpy.run_module(module_name) 

1497 return globals 

1498 

1499 def exiting(self): 

1500 # noinspection PyBroadException 

1501 try: 

1502 sys.stdout.flush() 

1503 except: 

1504 pass 

1505 # noinspection PyBroadException 

1506 try: 

1507 sys.stderr.flush() 

1508 except: 

1509 pass 

1510 self.check_output_redirect() 

1511 cmd = self.cmd_factory.make_exit_message() 

1512 self.writer.add_command(cmd) 

1513 

1514 def wait_for_commands(self, globals): 

1515 self._activate_mpl_if_needed() 

1516 

1517 thread = threading.currentThread() 

1518 from _pydevd_bundle import pydevd_frame_utils 

1519 frame = pydevd_frame_utils.Frame(None, -1, pydevd_frame_utils.FCode("Console", 

1520 os.path.abspath(os.path.dirname(__file__))), globals, globals) 

1521 thread_id = get_current_thread_id(thread) 

1522 pydevd_vars.add_additional_frame_by_id(thread_id, {id(frame): frame}) 

1523 

1524 cmd = self.cmd_factory.make_show_console_message(thread_id, frame) 

1525 self.writer.add_command(cmd) 

1526 

1527 while True: 

1528 if self.mpl_in_use: 

1529 # call input hooks if only matplotlib is in use 

1530 self._call_mpl_hook() 

1531 self.process_internal_commands() 

1532 time.sleep(0.01) 

1533 

1534 trace_dispatch = _trace_dispatch 

1535 frame_eval_func = frame_eval_func 

1536 dummy_trace_dispatch = dummy_trace_dispatch 

1537 

1538 # noinspection SpellCheckingInspection 

1539 @staticmethod 

1540 def stoptrace(): 

1541 """A proxy method for calling :func:`stoptrace` from the modules where direct import 

1542 is impossible because, for example, a circular dependency.""" 

1543 PyDBDaemonThread.created_pydb_daemon_threads = {} 

1544 stoptrace() 

1545 

1546 

1547def set_debug(setup): 

1548 setup['DEBUG_RECORD_SOCKET_READS'] = True 

1549 setup['DEBUG_TRACE_BREAKPOINTS'] = 1 

1550 setup['DEBUG_TRACE_LEVEL'] = 3 

1551 

1552 

1553def enable_qt_support(qt_support_mode): 

1554 from _pydev_bundle import pydev_monkey_qt 

1555 pydev_monkey_qt.patch_qt(qt_support_mode) 

1556 

1557 

1558def dump_threads(stream=None): 

1559 ''' 

1560 Helper to dump thread info (default is printing to stderr). 

1561 ''' 

1562 pydevd_utils.dump_threads(stream) 

1563 

1564 

1565def usage(do_exit=True, exit_code=0): 

1566 sys.stdout.write('Usage:\n') 

1567 sys.stdout.write('\tpydevd.py --port N [(--client hostname) | --server] --file executable [file_options]\n') 

1568 if do_exit: 

1569 sys.exit(exit_code) 

1570 

1571 

1572class _CustomWriter(object): 

1573 

1574 def __init__(self, out_ctx, wrap_stream, wrap_buffer, on_write=None): 

1575 ''' 

1576 :param out_ctx: 

1577 1=stdout and 2=stderr 

1578 

1579 :param wrap_stream: 

1580 Either sys.stdout or sys.stderr. 

1581 

1582 :param bool wrap_buffer: 

1583 If True the buffer attribute (which wraps writing bytes) should be 

1584 wrapped. 

1585 

1586 :param callable(str) on_write: 

1587 May be a custom callable to be called when to write something. 

1588 If not passed the default implementation will create an io message 

1589 and send it through the debugger. 

1590 ''' 

1591 self.encoding = getattr(wrap_stream, 'encoding', os.environ.get('PYTHONIOENCODING', 'utf-8')) 

1592 self._out_ctx = out_ctx 

1593 if wrap_buffer: 

1594 self.buffer = _CustomWriter(out_ctx, wrap_stream, wrap_buffer=False, on_write=on_write) 

1595 self._on_write = on_write 

1596 

1597 def flush(self): 

1598 pass # no-op here 

1599 

1600 def write(self, s): 

1601 if self._on_write is not None: 

1602 self._on_write(s) 

1603 return 

1604 

1605 if s: 

1606 if IS_PY2: 

1607 # Need s in bytes 

1608 if isinstance(s, unicode): 

1609 # Note: python 2.6 does not accept the "errors" keyword. 

1610 s = s.encode('utf-8', 'replace') 

1611 else: 

1612 # Need s in str 

1613 if isinstance(s, bytes): 

1614 s = s.decode(self.encoding, errors='replace') 

1615 

1616 py_db = get_global_debugger() 

1617 if py_db is not None: 

1618 # Note that the actual message contents will be a xml with utf-8, although 

1619 # the entry is str on py3 and bytes on py2. 

1620 cmd = py_db.cmd_factory.make_io_message(s, self._out_ctx) 

1621 py_db.writer.add_command(cmd) 

1622 

1623 

1624def init_stdout_redirect(on_write=None): 

1625 if not hasattr(sys, '_pydevd_out_buffer_'): 

1626 wrap_buffer = True if not IS_PY2 else False 

1627 original = sys.stdout 

1628 sys._pydevd_out_buffer_ = _CustomWriter(1, original, wrap_buffer, on_write) 

1629 sys.stdout_original = original 

1630 sys.stdout = pydevd_io.IORedirector(original, sys._pydevd_out_buffer_, wrap_buffer) #@UndefinedVariable 

1631 

1632def init_stderr_redirect(on_write=None): 

1633 if not hasattr(sys, '_pydevd_err_buffer_'): 

1634 wrap_buffer = True if not IS_PY2 else False 

1635 original = sys.stderr 

1636 sys._pydevd_err_buffer_ = _CustomWriter(2, original, wrap_buffer, on_write) 

1637 sys.stderr_original = original 

1638 sys.stderr = pydevd_io.IORedirector(original, sys._pydevd_err_buffer_, wrap_buffer) #@UndefinedVariable 

1639 

1640 

1641#======================================================================================================================= 

1642# settrace 

1643#======================================================================================================================= 

1644def settrace( 

1645 host=None, 

1646 stdoutToServer=False, 

1647 stderrToServer=False, 

1648 port=5678, 

1649 suspend=True, 

1650 trace_only_current_thread=False, 

1651 overwrite_prev_trace=False, 

1652 patch_multiprocessing=False, 

1653 stop_at_frame=None, 

1654 ): 

1655 '''Sets the tracing function with the pydev debug function and initializes needed facilities. 

1656 

1657 @param host: the user may specify another host, if the debug server is not in the same machine (default is the local 

1658 host) 

1659 

1660 @param stdoutToServer: when this is true, the stdout is passed to the debug server 

1661 

1662 @param stderrToServer: when this is true, the stderr is passed to the debug server 

1663 so that they are printed in its console and not in this process console. 

1664 

1665 @param port: specifies which port to use for communicating with the server (note that the server must be started 

1666 in the same port). @note: currently it's hard-coded at 5678 in the client 

1667 

1668 @param suspend: whether a breakpoint should be emulated as soon as this function is called. 

1669 

1670 @param trace_only_current_thread: determines if only the current thread will be traced or all current and future 

1671 threads will also have the tracing enabled. 

1672 

1673 @param overwrite_prev_trace: deprecated 

1674 

1675 @param patch_multiprocessing: if True we'll patch the functions which create new processes so that launched 

1676 processes are debugged. 

1677 

1678 @param stop_at_frame: if passed it'll stop at the given frame, otherwise it'll stop in the function which 

1679 called this method. 

1680 ''' 

1681 _set_trace_lock.acquire() 

1682 try: 

1683 _locked_settrace( 

1684 host, 

1685 stdoutToServer, 

1686 stderrToServer, 

1687 port, 

1688 suspend, 

1689 trace_only_current_thread, 

1690 patch_multiprocessing, 

1691 stop_at_frame, 

1692 ) 

1693 finally: 

1694 _set_trace_lock.release() 

1695 

1696 

1697 

1698_set_trace_lock = thread.allocate_lock() 

1699 

1700def _locked_settrace( 

1701 host, 

1702 stdoutToServer, 

1703 stderrToServer, 

1704 port, 

1705 suspend, 

1706 trace_only_current_thread, 

1707 patch_multiprocessing, 

1708 stop_at_frame, 

1709 ): 

1710 if patch_multiprocessing: 

1711 try: 

1712 from _pydev_bundle import pydev_monkey 

1713 except: 

1714 pass 

1715 else: 

1716 pydev_monkey.patch_new_process_functions() 

1717 

1718 global connected 

1719 global bufferStdOutToServer 

1720 global bufferStdErrToServer 

1721 

1722 # Reset created PyDB daemon threads after fork - parent threads don't exist in a child process. 

1723 PyDBDaemonThread.created_pydb_daemon_threads = {} 

1724 

1725 if not connected: 

1726 pydevd_vm_type.setup_type() 

1727 

1728 if SetupHolder.setup is None: 

1729 setup = { 

1730 'client': host, # dispatch expects client to be set to the host address when server is False 

1731 'server': False, 

1732 'port': int(port), 

1733 'multiprocess': patch_multiprocessing, 

1734 } 

1735 SetupHolder.setup = setup 

1736 

1737 debugger = PyDB() 

1738 pydev_log.debug("pydev debugger: process %d is connecting\n" % os.getpid()) 

1739 debugger.connect(host, port) # Note: connect can raise error. 

1740 

1741 # Mark connected only if it actually succeeded. 

1742 connected = True 

1743 bufferStdOutToServer = stdoutToServer 

1744 bufferStdErrToServer = stderrToServer 

1745 

1746 if bufferStdOutToServer: 

1747 init_stdout_redirect() 

1748 

1749 if bufferStdErrToServer: 

1750 init_stderr_redirect() 

1751 

1752 patch_stdin(debugger) 

1753 

1754 t = threadingCurrentThread() 

1755 additional_info = set_additional_thread_info(t) 

1756 

1757 while not debugger.ready_to_run: 

1758 time.sleep(0.1) # busy wait until we receive run command 

1759 

1760 # Set the tracing only 

1761 debugger.set_trace_for_frame_and_parents(get_frame().f_back) 

1762 

1763 CustomFramesContainer.custom_frames_lock.acquire() # @UndefinedVariable 

1764 try: 

1765 for _frameId, custom_frame in dict_iter_items(CustomFramesContainer.custom_frames): 

1766 debugger.set_trace_for_frame_and_parents(custom_frame.frame) 

1767 finally: 

1768 CustomFramesContainer.custom_frames_lock.release() # @UndefinedVariable 

1769 

1770 debugger.start_auxiliary_daemon_threads() 

1771 

1772 debugger.enable_tracing(apply_to_all_threads=True) 

1773 

1774 if not trace_only_current_thread: 

1775 # Trace future threads? 

1776 debugger.patch_threads() 

1777 

1778 # As this is the first connection, also set tracing for any untraced threads 

1779 debugger.set_tracing_for_untraced_contexts(ignore_current_thread=True) 

1780 

1781 # Stop the tracing as the last thing before the actual shutdown for a clean exit. 

1782 atexit.register(stoptrace) 

1783 

1784 else: 

1785 # ok, we're already in debug mode, with all set, so, let's just set the break 

1786 debugger = get_global_debugger() 

1787 

1788 debugger.set_trace_for_frame_and_parents(get_frame().f_back) 

1789 

1790 t = threadingCurrentThread() 

1791 additional_info = set_additional_thread_info(t) 

1792 

1793 debugger.enable_tracing() 

1794 

1795 if not trace_only_current_thread: 

1796 # Trace future threads? 

1797 debugger.patch_threads() 

1798 

1799 # Suspend as the last thing after all tracing is in place. 

1800 if suspend: 

1801 if stop_at_frame is not None: 

1802 # If the step was set we have to go to run state and 

1803 # set the proper frame for it to stop. 

1804 additional_info.pydev_state = STATE_RUN 

1805 additional_info.pydev_step_cmd = CMD_STEP_OVER 

1806 additional_info.pydev_step_stop = stop_at_frame 

1807 additional_info.suspend_type = PYTHON_SUSPEND 

1808 else: 

1809 # Ask to break as soon as possible. 

1810 debugger.set_suspend(t, CMD_SET_BREAK) 

1811 

1812 

1813class Dispatcher(object): 

1814 def __init__(self): 

1815 self.port = None 

1816 

1817 def connect(self, host, port): 

1818 self.host = host 

1819 self.port = port 

1820 self.client = start_client(self.host, self.port) 

1821 self.reader = DispatchReader(self) 

1822 self.reader.pydev_do_not_trace = False # We run reader in the same thread so we don't want to loose tracing. 

1823 self.reader.run() 

1824 

1825 def close(self): 

1826 try: 

1827 self.reader.do_kill_pydev_thread() 

1828 except : 

1829 pass 

1830 

1831class DispatchReader(ReaderThread): 

1832 def __init__(self, dispatcher): 

1833 self.dispatcher = dispatcher 

1834 ReaderThread.__init__(self, self.dispatcher.client) 

1835 

1836 @overrides(ReaderThread._on_run) 

1837 def _on_run(self): 

1838 dummy_thread = threading.currentThread() 

1839 dummy_thread.is_pydev_daemon_thread = False 

1840 return ReaderThread._on_run(self) 

1841 

1842 def handle_except(self): 

1843 ReaderThread.handle_except(self) 

1844 

1845 def process_command(self, cmd_id, seq, text): 

1846 if cmd_id == 99: 

1847 self.dispatcher.port = int(text) 

1848 self.killReceived = True 

1849 

1850 

1851def _should_use_existing_connection(setup): 

1852 ''' 

1853 The new connection dispatch approach is used by PyDev when the `multiprocess` option is set, 

1854 the existing connection approach is used by PyCharm when the `multiproc` option is set. 

1855 ''' 

1856 return setup.get('multiproc', False) 

1857 

1858 

1859def dispatch(): 

1860 setup = SetupHolder.setup 

1861 host = setup['client'] 

1862 port = setup['port'] 

1863 if _should_use_existing_connection(setup): 

1864 dispatcher = Dispatcher() 

1865 try: 

1866 dispatcher.connect(host, port) 

1867 port = dispatcher.port 

1868 finally: 

1869 dispatcher.close() 

1870 return host, port 

1871 

1872 

1873def settrace_forked(): 

1874 ''' 

1875 When creating a fork from a process in the debugger, we need to reset the whole debugger environment! 

1876 ''' 

1877 from _pydevd_bundle.pydevd_constants import GlobalDebuggerHolder 

1878 GlobalDebuggerHolder.global_dbg = None 

1879 

1880 from _pydevd_frame_eval.pydevd_frame_eval_main import clear_thread_local_info 

1881 host, port = dispatch() 

1882 

1883 import pydevd_tracing 

1884 pydevd_tracing.restore_sys_set_trace_func() 

1885 

1886 if port is not None: 

1887 global connected 

1888 connected = False 

1889 global forked 

1890 forked = True 

1891 

1892 custom_frames_container_init() 

1893 

1894 if clear_thread_local_info is not None: 

1895 clear_thread_local_info() 

1896 

1897 settrace( 

1898 host, 

1899 port=port, 

1900 suspend=False, 

1901 trace_only_current_thread=False, 

1902 overwrite_prev_trace=True, 

1903 patch_multiprocessing=True, 

1904 ) 

1905 

1906#======================================================================================================================= 

1907# SetupHolder 

1908#======================================================================================================================= 

1909class SetupHolder: 

1910 

1911 setup = None 

1912 

1913 

1914def apply_debugger_options(setup_options): 

1915 """ 

1916 

1917 :type setup_options: dict[str, bool] 

1918 """ 

1919 default_options = {'save-signatures': False, 'qt-support': ''} 

1920 default_options.update(setup_options) 

1921 setup_options = default_options 

1922 

1923 debugger = GetGlobalDebugger() 

1924 if setup_options['save-signatures']: 

1925 if pydevd_vm_type.get_vm_type() == pydevd_vm_type.PydevdVmType.JYTHON: 

1926 sys.stderr.write("Collecting run-time type information is not supported for Jython\n") 

1927 else: 

1928 # Only import it if we're going to use it! 

1929 from _pydevd_bundle.pydevd_signature import SignatureFactory 

1930 debugger.signature_factory = SignatureFactory() 

1931 

1932 if setup_options['qt-support']: 

1933 enable_qt_support(setup_options['qt-support']) 

1934 

1935 

1936def patch_stdin(debugger): 

1937 from _pydev_bundle.pydev_stdin import DebugConsoleStdIn 

1938 orig_stdin = sys.stdin 

1939 sys.stdin = DebugConsoleStdIn(debugger, orig_stdin) 

1940 

1941 

1942def handle_keyboard_interrupt(): 

1943 debugger = get_global_debugger() 

1944 

1945 if not debugger: 

1946 return 

1947 

1948 debugger.disable_tracing() 

1949 _, value, tb = sys.exc_info() 

1950 

1951 while tb: 

1952 filename = tb.tb_frame.f_code.co_filename 

1953 if debugger.in_project_scope(filename) and '_pydevd' not in filename: 

1954 break 

1955 tb = tb.tb_next 

1956 if tb: 

1957 limit = 1 

1958 tb_next = tb.tb_next 

1959 

1960 # When stopping the suspended debugger, traceback can contain two stack traces with the same frame. 

1961 if tb_next and tb_next.tb_frame is tb.tb_frame: 

1962 tb_next = None 

1963 

1964 while tb_next: 

1965 filename = tb_next.tb_frame.f_code.co_filename 

1966 if get_file_type(os.path.basename(filename)) or '_pydevd' in filename: 

1967 break 

1968 limit += 1 

1969 if tb_next.tb_next and tb_next.tb_next.tb_frame is not tb_next.tb_frame: 

1970 tb_next = tb_next.tb_next 

1971 else: 

1972 break 

1973 try: 

1974 value = value.with_traceback(tb) 

1975 except AttributeError: 

1976 value.__traceback__ = tb 

1977 value.__cause__ = None 

1978 traceback.print_exception(type(value), value, tb, limit=limit) 

1979 

1980 disable_excepthook() 

1981 

1982 

1983# Dispatch on_debugger_modules_loaded here, after all primary debugger modules are loaded 

1984from _pydevd_bundle.pydevd_extension_api import DebuggerEventHandler 

1985from _pydevd_bundle import pydevd_extension_utils 

1986 

1987for handler in pydevd_extension_utils.extensions_of_type(DebuggerEventHandler): 

1988 handler.on_debugger_modules_loaded(debugger_version=__version__) 

1989#======================================================================================================================= 

1990# main 

1991#======================================================================================================================= 

1992def main(): 

1993 

1994 # parse the command line. --file is our last argument that is required 

1995 try: 

1996 from _pydevd_bundle.pydevd_command_line_handling import process_command_line 

1997 setup = process_command_line(sys.argv) 

1998 SetupHolder.setup = setup 

1999 except ValueError: 

2000 traceback.print_exc() 

2001 usage(exit_code=1) 

2002 

2003 # noinspection PyUnboundLocalVariable 

2004 if setup['help']: 

2005 usage() 

2006 

2007 if setup['print-in-debugger-startup']: 

2008 try: 

2009 pid = ' (pid: %s)' % os.getpid() 

2010 except: 

2011 pid = '' 

2012 sys.stderr.write("pydev debugger: starting%s\n" % pid) 

2013 

2014 fix_getpass.fix_getpass() 

2015 

2016 pydev_log.debug("Executing file %s" % setup['file']) 

2017 pydev_log.debug("arguments: %s"% str(sys.argv)) 

2018 

2019 

2020 pydevd_vm_type.setup_type(setup.get('vm_type', None)) 

2021 

2022 if SHOW_DEBUG_INFO_ENV: 

2023 set_debug(setup) 

2024 

2025 DebugInfoHolder.DEBUG_RECORD_SOCKET_READS = setup.get('DEBUG_RECORD_SOCKET_READS', DebugInfoHolder.DEBUG_RECORD_SOCKET_READS) 

2026 DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS = setup.get('DEBUG_TRACE_BREAKPOINTS', DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS) 

2027 DebugInfoHolder.DEBUG_TRACE_LEVEL = setup.get('DEBUG_TRACE_LEVEL', DebugInfoHolder.DEBUG_TRACE_LEVEL) 

2028 

2029 port = setup['port'] 

2030 host = setup['client'] 

2031 f = setup['file'] 

2032 fix_app_engine_debug = False 

2033 

2034 debugger = PyDB() 

2035 

2036 try: 

2037 from _pydev_bundle import pydev_monkey 

2038 except: 

2039 pass #Not usable on jython 2.1 

2040 else: 

2041 if setup['multiprocess']: # PyDev 

2042 pydev_monkey.patch_new_process_functions() 

2043 

2044 elif setup['multiproc']: # PyCharm 

2045 pydev_log.debug("Started in multiproc mode\n") 

2046 

2047 dispatcher = Dispatcher() 

2048 try: 

2049 dispatcher.connect(host, port) 

2050 if dispatcher.port is not None: 

2051 port = dispatcher.port 

2052 pydev_log.debug("Received port %d\n" % port) 

2053 pydev_log.debug("pydev debugger: process %d is connecting\n" % os.getpid()) 

2054 

2055 try: 

2056 pydev_monkey.patch_new_process_functions() 

2057 except: 

2058 pydev_log.error("Error patching process functions\n") 

2059 traceback.print_exc() 

2060 else: 

2061 pydev_log.error("pydev debugger: couldn't get port for new debug process\n") 

2062 finally: 

2063 dispatcher.close() 

2064 else: 

2065 try: 

2066 pydev_monkey.patch_new_process_functions_with_warning() 

2067 except: 

2068 pydev_log.error("Error patching process functions\n") 

2069 traceback.print_exc() 

2070 

2071 # Only do this patching if we're not running with multiprocess turned on. 

2072 if f.find('dev_appserver.py') != -1: 

2073 if os.path.basename(f).startswith('dev_appserver.py'): 

2074 appserver_dir = os.path.dirname(f) 

2075 version_file = os.path.join(appserver_dir, 'VERSION') 

2076 if os.path.exists(version_file): 

2077 try: 

2078 stream = open(version_file, 'r') 

2079 try: 

2080 for line in stream.read().splitlines(): 

2081 line = line.strip() 

2082 if line.startswith('release:'): 

2083 line = line[8:].strip() 

2084 version = line.replace('"', '') 

2085 version = version.split('.') 

2086 if int(version[0]) > 1: 

2087 fix_app_engine_debug = True 

2088 

2089 elif int(version[0]) == 1: 

2090 if int(version[1]) >= 7: 

2091 # Only fix from 1.7 onwards 

2092 fix_app_engine_debug = True 

2093 break 

2094 finally: 

2095 stream.close() 

2096 except: 

2097 traceback.print_exc() 

2098 

2099 try: 

2100 # In the default run (i.e.: run directly on debug mode), we try to patch stackless as soon as possible 

2101 # on a run where we have a remote debug, we may have to be more careful because patching stackless means 

2102 # that if the user already had a stackless.set_schedule_callback installed, he'd loose it and would need 

2103 # to call it again (because stackless provides no way of getting the last function which was registered 

2104 # in set_schedule_callback). 

2105 # 

2106 # So, ideally, if there's an application using stackless and the application wants to use the remote debugger 

2107 # and benefit from stackless debugging, the application itself must call: 

2108 # 

2109 # import pydevd_stackless 

2110 # pydevd_stackless.patch_stackless() 

2111 # 

2112 # itself to be able to benefit from seeing the tasklets created before the remote debugger is attached. 

2113 from _pydevd_bundle import pydevd_stackless 

2114 pydevd_stackless.patch_stackless() 

2115 except: 

2116 # It's ok not having stackless there... 

2117 try: 

2118 sys.exc_clear() # the exception information should be cleaned in Python 2 

2119 except: 

2120 pass 

2121 

2122 is_module = setup['module'] 

2123 patch_stdin(debugger) 

2124 

2125 if fix_app_engine_debug: 

2126 sys.stderr.write("pydev debugger: google app engine integration enabled\n") 

2127 curr_dir = os.path.dirname(__file__) 

2128 app_engine_startup_file = os.path.join(curr_dir, 'pydev_app_engine_debug_startup.py') 

2129 

2130 sys.argv.insert(1, '--python_startup_script=' + app_engine_startup_file) 

2131 import json 

2132 setup['pydevd'] = __file__ 

2133 sys.argv.insert(2, '--python_startup_args=%s' % json.dumps(setup),) 

2134 sys.argv.insert(3, '--automatic_restart=no') 

2135 sys.argv.insert(4, '--max_module_instances=1') 

2136 

2137 # Run the dev_appserver 

2138 debugger.run(setup['file'], None, None, is_module, set_trace=False) 

2139 else: 

2140 if setup['save-threading']: 

2141 debugger.thread_analyser = ThreadingLogger() 

2142 if setup['save-asyncio']: 

2143 if IS_PY34_OR_GREATER: 

2144 debugger.asyncio_analyser = AsyncioLogger() 

2145 

2146 apply_debugger_options(setup) 

2147 

2148 try: 

2149 debugger.connect(host, port) 

2150 except: 

2151 sys.stderr.write("Could not connect to %s: %s\n" % (host, port)) 

2152 traceback.print_exc() 

2153 sys.exit(1) 

2154 

2155 global connected 

2156 connected = True # Mark that we're connected when started from inside ide. 

2157 try: 

2158 globals = debugger.run(setup['file'], None, None, is_module) 

2159 except KeyboardInterrupt as e: 

2160 handle_keyboard_interrupt() 

2161 raise 

2162 

2163 if setup['cmd-line']: 

2164 debugger.wait_for_commands(globals) 

2165 

2166if __name__ == '__main__': 

2167 main()