pyflyby 1.9.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyflyby might be problematic. Click here for more details.

Files changed (54) hide show
  1. pyflyby/__init__.py +56 -0
  2. pyflyby/__main__.py +9 -0
  3. pyflyby/_autoimp.py +2114 -0
  4. pyflyby/_cmdline.py +531 -0
  5. pyflyby/_comms.py +221 -0
  6. pyflyby/_dbg.py +1339 -0
  7. pyflyby/_docxref.py +379 -0
  8. pyflyby/_file.py +738 -0
  9. pyflyby/_flags.py +230 -0
  10. pyflyby/_format.py +182 -0
  11. pyflyby/_idents.py +233 -0
  12. pyflyby/_import_sorting.py +165 -0
  13. pyflyby/_importclns.py +642 -0
  14. pyflyby/_importdb.py +588 -0
  15. pyflyby/_imports2s.py +639 -0
  16. pyflyby/_importstmt.py +662 -0
  17. pyflyby/_interactive.py +2605 -0
  18. pyflyby/_livepatch.py +793 -0
  19. pyflyby/_log.py +199 -0
  20. pyflyby/_modules.py +515 -0
  21. pyflyby/_parse.py +1441 -0
  22. pyflyby/_py.py +2078 -0
  23. pyflyby/_util.py +459 -0
  24. pyflyby/_version.py +7 -0
  25. pyflyby/autoimport.py +20 -0
  26. pyflyby/importdb.py +19 -0
  27. pyflyby-1.9.4.data/data/etc/pyflyby/canonical.py +10 -0
  28. pyflyby-1.9.4.data/data/etc/pyflyby/common.py +27 -0
  29. pyflyby-1.9.4.data/data/etc/pyflyby/forget.py +10 -0
  30. pyflyby-1.9.4.data/data/etc/pyflyby/mandatory.py +10 -0
  31. pyflyby-1.9.4.data/data/etc/pyflyby/numpy.py +156 -0
  32. pyflyby-1.9.4.data/data/etc/pyflyby/std.py +335 -0
  33. pyflyby-1.9.4.data/data/libexec/pyflyby/colordiff +34 -0
  34. pyflyby-1.9.4.data/data/libexec/pyflyby/diff-colorize +148 -0
  35. pyflyby-1.9.4.data/data/share/doc/pyflyby/LICENSE.txt +23 -0
  36. pyflyby-1.9.4.data/data/share/doc/pyflyby/TODO.txt +115 -0
  37. pyflyby-1.9.4.data/data/share/doc/pyflyby/testing.txt +13 -0
  38. pyflyby-1.9.4.data/data/share/emacs/site-lisp/pyflyby.el +108 -0
  39. pyflyby-1.9.4.data/scripts/collect-exports +76 -0
  40. pyflyby-1.9.4.data/scripts/collect-imports +58 -0
  41. pyflyby-1.9.4.data/scripts/find-import +38 -0
  42. pyflyby-1.9.4.data/scripts/list-bad-xrefs +34 -0
  43. pyflyby-1.9.4.data/scripts/prune-broken-imports +34 -0
  44. pyflyby-1.9.4.data/scripts/pyflyby-diff +34 -0
  45. pyflyby-1.9.4.data/scripts/reformat-imports +27 -0
  46. pyflyby-1.9.4.data/scripts/replace-star-imports +37 -0
  47. pyflyby-1.9.4.data/scripts/tidy-imports +191 -0
  48. pyflyby-1.9.4.data/scripts/transform-imports +47 -0
  49. pyflyby-1.9.4.dist-info/LICENSE.txt +23 -0
  50. pyflyby-1.9.4.dist-info/METADATA +507 -0
  51. pyflyby-1.9.4.dist-info/RECORD +54 -0
  52. pyflyby-1.9.4.dist-info/WHEEL +5 -0
  53. pyflyby-1.9.4.dist-info/entry_points.txt +3 -0
  54. pyflyby-1.9.4.dist-info/top_level.txt +1 -0
pyflyby/_dbg.py ADDED
@@ -0,0 +1,1339 @@
1
+ # pyflyby/_dbg.py.
2
+ # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2018 Karl Chen.
3
+ # License: MIT http://opensource.org/licenses/MIT
4
+
5
+
6
+ import builtins
7
+ from contextlib import contextmanager
8
+ import errno
9
+ from functools import wraps
10
+ import os
11
+ import pwd
12
+ import signal
13
+ import sys
14
+ import time
15
+ from types import CodeType, FrameType, TracebackType
16
+
17
+ from collections.abc import Callable
18
+
19
+ from pyflyby._file import Filename
20
+
21
+
22
+ """
23
+ Used by wait_for_debugger_to_attach to record whether we're waiting to attach,
24
+ and if so what.
25
+ """
26
+ _waiting_for_debugger = None
27
+
28
+
29
+ _ORIG_SYS_EXCEPTHOOK = sys.excepthook
30
+
31
+ def _reset_excepthook():
32
+ if _ORIG_SYS_EXCEPTHOOK:
33
+ sys.excepthook = _ORIG_SYS_EXCEPTHOOK
34
+ return True
35
+ return False
36
+
37
+
38
+ def _override_excepthook(hook):
39
+ """
40
+ Override sys.excepthook with `hook` but also support resetting.
41
+
42
+ Users should call this function instead of directly overiding
43
+ sys.excepthook. This is helpful in resetting sys.excepthook in certain cases.
44
+ """
45
+ global _ORIG_SYS_EXCEPTHOOK
46
+ _ORIG_SYS_EXCEPTHOOK = hook
47
+ sys.excepthook = hook
48
+
49
+
50
+ class _NoTtyError(Exception):
51
+ pass
52
+
53
+
54
+ _memoized_dev_tty_fd = Ellipsis
55
+ def _dev_tty_fd():
56
+ """
57
+ Return a file descriptor opened to /dev/tty.
58
+ Memoized.
59
+ """
60
+ global _memoized_dev_tty_fd
61
+ if _memoized_dev_tty_fd is Ellipsis:
62
+ try:
63
+ _memoized_dev_tty_fd = os.open("/dev/tty", os.O_RDWR)
64
+ except OSError:
65
+ _memoized_dev_tty_fd = None
66
+ if _memoized_dev_tty_fd is None:
67
+ raise _NoTtyError
68
+ return _memoized_dev_tty_fd
69
+
70
+
71
+ def tty_is_usable():
72
+ """
73
+ Return whether /dev/tty is usable.
74
+
75
+ In interactive sessions, /dev/tty is usable; in non-interactive sessions,
76
+ /dev/tty is not usable::
77
+
78
+ $ ssh -t localhost py -q pyflyby._dbg.tty_is_usable
79
+ True
80
+
81
+ $ ssh -T localhost py -q pyflyby._dbg.tty_is_usable
82
+ False
83
+
84
+ tty_is_usable() is useful for deciding whether we are in an interactive
85
+ terminal. In an interactive terminal we can enter the debugger directly;
86
+ in a non-interactive terminal, we need to wait_for_debugger_to_attach.
87
+
88
+ Note that this is different from doing e.g. isatty(0). isatty would
89
+ return False if a program was piped, even though /dev/tty is usable.
90
+ """
91
+ try:
92
+ _dev_tty_fd()
93
+ return True
94
+ except _NoTtyError:
95
+ return False
96
+
97
+
98
+ @contextmanager
99
+ def _FdCtx(target_fd, src_fd):
100
+ assert target_fd != src_fd
101
+ saved_fd = os.dup(target_fd)
102
+ assert saved_fd > 2, "saved_fd == %d" % (saved_fd,)
103
+ assert saved_fd != target_fd and saved_fd != src_fd
104
+ os.dup2(src_fd, target_fd)
105
+ try:
106
+ yield
107
+ finally:
108
+ os.dup2(saved_fd, target_fd)
109
+
110
+
111
+ _in_StdioCtx = []
112
+
113
+ @contextmanager
114
+ def _StdioCtx(tty="/dev/tty"):
115
+ '''
116
+ Within the context, force fd {0, 1, 2}, sys.__{stdin,stdout,stderr}__,
117
+ sys.{stdin,stdout,stderr} to fd. This allows us to use the debugger even
118
+ if stdio is otherwise redirected.
119
+
120
+ :type tty:
121
+ ``int`` or ``str``
122
+ :param tty:
123
+ Tty to use. Either a file descriptor or a name of a tty.
124
+ '''
125
+ from ._interactive import UpdateIPythonStdioCtx
126
+ to_close = None
127
+ if isinstance(tty, int):
128
+ fd = tty
129
+ elif isinstance(tty, str):
130
+ if tty == "/dev/tty":
131
+ fd = _dev_tty_fd()
132
+ else:
133
+ fd = os.open(tty, os.O_RDWR)
134
+ to_close = fd
135
+ else:
136
+ raise TypeError("_StdioCtx(): tty should be an int or str")
137
+ if _in_StdioCtx and _in_StdioCtx[-1] == fd:
138
+ # Same context; do nothing.
139
+ assert to_close is None
140
+ yield
141
+ return
142
+ if not fd > 2:
143
+ raise ValueError("_StdioCtx: unsafe to use fd<=2; fd==%d" % (fd,))
144
+ _in_StdioCtx.append(fd)
145
+ saved_stdin = sys.stdin
146
+ saved_stdin__ = sys.__stdin__
147
+ saved_stdout = sys.stdout
148
+ saved_stdout__ = sys.__stdout__
149
+ saved_stderr = sys.stderr
150
+ saved_stderr__ = sys.__stderr__
151
+ try:
152
+ sys.stdout.flush(); sys.__stdout__.flush()
153
+ sys.stderr.flush(); sys.__stderr__.flush()
154
+ from ._util import nested
155
+ with nested(_FdCtx(0, fd), _FdCtx(1, fd), _FdCtx(2, fd)):
156
+ with nested(os.fdopen(0, 'r'),
157
+ os.fdopen(1, 'w'),
158
+ os.fdopen(2, 'w', 1)) as (fd0, fd1, fd2):
159
+ sys.stdin = sys.__stdin__ = fd0
160
+ sys.stdout = sys.__stdout__ = fd1
161
+ sys.stderr = sys.__stderr__ = fd2
162
+ # Update IPython's stdin/stdout/stderr temporarily.
163
+ with UpdateIPythonStdioCtx():
164
+ yield
165
+ finally:
166
+ assert _in_StdioCtx and _in_StdioCtx[-1] == fd
167
+ _in_StdioCtx.pop(-1)
168
+ sys.stdin = saved_stdin
169
+ sys.__stdin__ = saved_stdin__
170
+ sys.stdout = saved_stdout
171
+ sys.__stdout__ = saved_stdout__
172
+ sys.stderr = saved_stderr
173
+ sys.__stderr__ = saved_stderr__
174
+ if to_close is not None:
175
+ try:
176
+ os.close(to_close)
177
+ except (OSError, IOError):
178
+ pass
179
+
180
+
181
+ @contextmanager
182
+ def _ExceptHookCtx():
183
+ '''
184
+ Context manager that restores ``sys.excepthook`` upon exit.
185
+ '''
186
+ saved_excepthook = sys.excepthook
187
+ try:
188
+ # TODO: should we set sys.excepthook = sys.__excepthook__ ?
189
+ yield
190
+ finally:
191
+ sys.excepthook = saved_excepthook
192
+
193
+
194
+ @contextmanager
195
+ def _DisplayHookCtx():
196
+ '''
197
+ Context manager that resets ``sys.displayhook`` to the default value upon
198
+ entry, and restores the pre-context value upon exit.
199
+ '''
200
+ saved_displayhook = sys.displayhook
201
+ try:
202
+ sys.displayhook = sys.__displayhook__
203
+ yield
204
+ finally:
205
+ sys.displayhook = saved_displayhook
206
+
207
+
208
+ def print_traceback(*exc_info):
209
+ """
210
+ Print a traceback, using IPython's ultraTB if possible.
211
+
212
+ Output goes to /dev/tty.
213
+
214
+ :param exc_info:
215
+ 3 arguments as returned by sys.exc_info().
216
+ """
217
+ from pyflyby._interactive import print_verbose_tb
218
+ if not exc_info:
219
+ exc_info = sys.exc_info()
220
+ with _StdioCtx():
221
+ print_verbose_tb(*exc_info)
222
+
223
+
224
+ @contextmanager
225
+ def _DebuggerCtx(tty="/dev/tty"):
226
+ """
227
+ A context manager that sets up the environment (stdio, sys hooks) for a
228
+ debugger, initializes IPython if necessary, and creates a debugger instance.
229
+
230
+ :return:
231
+ Context manager that yields a Pdb instance.
232
+ """
233
+ from pyflyby._interactive import new_IPdb_instance
234
+ with _StdioCtx(tty):
235
+ with _ExceptHookCtx():
236
+ with _DisplayHookCtx():
237
+ pdb = new_IPdb_instance()
238
+ pdb.reset()
239
+ yield pdb
240
+
241
+
242
+ def _get_caller_frame():
243
+ '''
244
+ Get the closest frame from outside this module.
245
+
246
+ :rtype:
247
+ ``FrameType``
248
+ '''
249
+ this_filename = _get_caller_frame.__code__.co_filename
250
+ f = sys._getframe()
251
+ while (f.f_back and (
252
+ f.f_code.co_filename == this_filename or
253
+ (f.f_back.f_back and
254
+ f.f_code.co_filename == "<string>" and
255
+ f.f_code.co_name == "<module>" and
256
+ f.f_back.f_code.co_filename == this_filename))):
257
+ f = f.f_back
258
+ if f.f_code.co_filename == "<string>" and f.f_code.co_name == "<module>":
259
+ # Skip an extra string eval frame for attaching a debugger.
260
+ # TODO: pass in a frame or maximum number of string frames to skip.
261
+ # We shouldn't skip "<string>" if it comes from regular user code.
262
+ f = f.f_back
263
+ return f
264
+
265
+
266
+ def _prompt_continue_waiting_for_debugger():
267
+ """
268
+ Prompt while exiting the debugger to get user opinion on keeping the
269
+ process waiting for debugger to attach.
270
+ """
271
+ count_invalid = 0
272
+ max_invalid_entries = 3
273
+ while count_invalid < max_invalid_entries:
274
+ sys.stdout.flush()
275
+ response = input("Keep the process running for debugger to be "
276
+ "attached later ? (y)es/(n)o\n")
277
+
278
+ if response.lower() in ('n', 'no'):
279
+ global _waiting_for_debugger
280
+ _waiting_for_debugger = None
281
+ break
282
+
283
+ if response.lower() not in ('y', 'yes'):
284
+ print("Invalid response: {}".format(repr(response)))
285
+ count_invalid += 1
286
+ else:
287
+ break
288
+ else:
289
+ print("Exiting after {} invalid responses.".format(max_invalid_entries))
290
+ _waiting_for_debugger = None
291
+ # Sleep for a fraction of second for the print statements to get printed.
292
+ time.sleep(0.01)
293
+
294
+
295
+ def _debug_exception(*exc_info, **kwargs):
296
+ """
297
+ Debug an exception -- print a stack trace and enter the debugger.
298
+
299
+ Suitable to be assigned to sys.excepthook.
300
+ """
301
+ from pyflyby._interactive import print_verbose_tb
302
+ tty = kwargs.pop("tty", "/dev/tty")
303
+ debugger_attached = kwargs.pop("debugger_attached", False)
304
+ if kwargs:
305
+ raise TypeError("debug_exception(): unexpected kwargs %s"
306
+ % (', '.join(sorted(kwargs.keys()))))
307
+ if not exc_info:
308
+ exc_info = sys.exc_info()
309
+ if len(exc_info) == 1 and type(exc_info[0]) is tuple:
310
+ exc_info = exc_info[0]
311
+ if len(exc_info) == 1 and type(exc_info[0]) is TracebackType:
312
+ # Allow the input to be just the traceback. The exception instance is
313
+ # only used for printing the traceback. It's not needed by the
314
+ # debugger.
315
+ # We don't know the exception in this case. For now put "", "". This
316
+ # will cause print_verbose_tb to include a line with just a colon.
317
+ # TODO: avoid that line.
318
+ exc_info = ("", "", exc_info)
319
+
320
+ with _DebuggerCtx(tty=tty) as pdb:
321
+ if debugger_attached:
322
+ # If debugger is attached to the process made waiting by
323
+ # 'wait_for_debugger_to_attach', check with the user whether to
324
+ # keep the process waiting for debugger to attach.
325
+ pdb.postloop = _prompt_continue_waiting_for_debugger
326
+ print_verbose_tb(*exc_info)
327
+ pdb.interaction(None, exc_info[2])
328
+
329
+
330
+ def _debug_code(arg, globals=None, locals=None, auto_import=True, tty="/dev/tty"):
331
+ """
332
+ Run code under the debugger.
333
+
334
+ :type arg:
335
+ ``str``, ``Callable``, ``CodeType``, ``PythonStatement``, ``PythonBlock``,
336
+ ``FileText``
337
+ """
338
+ if globals is None or locals is None:
339
+ caller_frame = _get_caller_frame()
340
+ if globals is None:
341
+ globals = caller_frame.f_globals
342
+ if locals is None:
343
+ locals = caller_frame.f_locals
344
+ del caller_frame
345
+ with _DebuggerCtx(tty=tty) as pdb:
346
+ print("Entering debugger. Use 'n' to step, 'c' to run, 'q' to stop.")
347
+ print("")
348
+ from ._parse import PythonStatement, PythonBlock, FileText
349
+
350
+ if isinstance(arg, (str, PythonStatement, PythonBlock, FileText)):
351
+ # Compile the block so that we can get the right compile mode.
352
+ arg = PythonBlock(arg)
353
+ # TODO: enter text into linecache
354
+ autoimp_arg = arg
355
+ code = arg.compile()
356
+ elif isinstance(arg, CodeType):
357
+ autoimp_arg = arg
358
+ code = arg
359
+ elif isinstance(arg, Callable):
360
+ # TODO: check argspec to make sure it's a zero-arg callable.
361
+ code = arg.__code__
362
+ autoimp_arg = code
363
+ else:
364
+ raise TypeError(
365
+ "debug_code(): expected a string/callable/lambda; got a %s"
366
+ % (type(arg).__name__,))
367
+ if auto_import:
368
+ from ._autoimp import auto_import as auto_import_f
369
+ auto_import_f(autoimp_arg, [globals, locals])
370
+ return pdb.runeval(code, globals=globals, locals=locals)
371
+
372
+
373
+ _CURRENT_FRAME = object()
374
+
375
+
376
+ def debugger(*args, **kwargs):
377
+ '''
378
+ Entry point for debugging.
379
+
380
+ ``debugger()`` can be used in the following ways::
381
+
382
+ 1. Breakpoint mode, entering debugger in executing code::
383
+ >> def foo():
384
+ .. bar()
385
+ .. debugger()
386
+ .. baz()
387
+
388
+ This allow stepping through code after the debugger() call - i.e. between
389
+ bar() and baz(). This is similar to 'import pdb; pdb.set_trace()'::
390
+
391
+ 2. Debug a python statement::
392
+
393
+ >> def foo(x):
394
+ .. ...
395
+ >> X = 5
396
+
397
+ >> debugger("foo(X)")
398
+
399
+ The auto-importer is run on the given python statement.
400
+
401
+ 3. Debug a callable::
402
+
403
+ >> def foo(x=5):
404
+ .. ...
405
+
406
+ >> debugger(foo)
407
+ >> debugger(lambda: foo(6))
408
+
409
+ 4. Debug an exception::
410
+
411
+ >> try:
412
+ .. ...
413
+ .. except:
414
+ .. debugger(sys.exc_info())
415
+
416
+
417
+ If the process is waiting on for a debugger to attach to debug a frame or
418
+ exception traceback, then calling debugger(None) will debug that target.
419
+ If it is frame, then the user can step through code. If it is an
420
+ exception traceback, then the debugger will operate in post-mortem mode
421
+ with no stepping allowed. The process will continue running after this
422
+ debug session's "continue".
423
+
424
+ ``debugger()`` is suitable to be called interactively, from scripts, in
425
+ sys.excepthook, and in signal handlers.
426
+
427
+ :param args:
428
+ What to debug:
429
+ - If a string or callable, then run it under the debugger.
430
+ - If a frame, then debug the frame.
431
+ - If a traceback, then debug the traceback.
432
+ - If a 3-tuple as returned by sys.exc_info(), then debug the traceback.
433
+ - If the process is waiting to for a debugger to attach, then attach
434
+ the debugger there. This is only relevant when an external process
435
+ is attaching a debugger.
436
+ - If nothing specified, then enter the debugger at the statement
437
+ following the call to debug().
438
+ :kwarg tty:
439
+ Tty to connect to. If ``None`` (default): if /dev/tty is usable, then
440
+ use it; else call wait_for_debugger_to_attach() instead (unless
441
+ wait_for_attach==False).
442
+ :kwarg on_continue:
443
+ Function to call upon exiting the debugger and continuing with regular
444
+ execution.
445
+ :kwarg wait_for_attach:
446
+ Whether to wait for a remote terminal to attach (with 'py -d PID').
447
+ If ``True``, then always wait for a debugger to attach.
448
+ If ``False``, then never wait for a debugger to attach; debug in the
449
+ current terminal.
450
+ If unset, then defaults to true only when ``tty`` is unspecified and
451
+ /dev/tty is not usable.
452
+ :kwarg background:
453
+ If ``False``, then pause execution to debug.
454
+ If ``True``, then fork a process and wait for a debugger to attach in the
455
+ forked child.
456
+ '''
457
+ from ._parse import PythonStatement, PythonBlock, FileText
458
+ if len(args) in {1, 2}:
459
+ arg = args[0]
460
+ elif len(args) == 0:
461
+ arg = None
462
+ else:
463
+ arg = args
464
+ tty = kwargs.pop("tty" , None)
465
+ on_continue = kwargs.pop("on_continue" , lambda: None)
466
+ globals = kwargs.pop("globals" , None)
467
+ locals = kwargs.pop("locals" , None)
468
+ wait_for_attach = kwargs.pop("wait_for_attach", Ellipsis)
469
+ background = kwargs.pop("background" , False)
470
+ _debugger_attached = False
471
+ if kwargs:
472
+ raise TypeError("debugger(): unexpected kwargs %s"
473
+ % (', '.join(sorted(kwargs))))
474
+ if arg is None and tty is not None and wait_for_attach != True:
475
+ # If _waiting_for_debugger is not None, then attach to that
476
+ # (whether it's a frame, traceback, etc).
477
+ global _waiting_for_debugger
478
+ arg = _waiting_for_debugger
479
+ _debugger_attached = True
480
+ if arg is None:
481
+ # Debug current frame.
482
+ arg = _CURRENT_FRAME
483
+ if arg is _CURRENT_FRAME:
484
+ arg = _get_caller_frame()
485
+ if background:
486
+ # Fork a process and wait for a debugger to attach in the background.
487
+ # Todo: implement on_continue()
488
+ wait_for_debugger_to_attach(arg, background=True)
489
+ return
490
+ if wait_for_attach == True:
491
+ wait_for_debugger_to_attach(arg)
492
+ return
493
+ if tty is None:
494
+ if tty_is_usable():
495
+ tty = "/dev/tty"
496
+ elif wait_for_attach != False:
497
+ # If the tty isn't usable, then default to waiting for the
498
+ # debugger to attach from another (interactive) terminal.
499
+ # Todo: implement on_continue()
500
+ # TODO: capture globals/locals when relevant.
501
+ wait_for_debugger_to_attach(arg)
502
+ return
503
+ if isinstance(
504
+ arg, (str, PythonStatement, PythonBlock, FileText, CodeType, Callable)
505
+ ):
506
+ _debug_code(arg, globals=globals, locals=locals, tty=tty)
507
+ on_continue()
508
+ return
509
+ if (isinstance(arg, TracebackType) or
510
+ type(arg) is tuple and len(arg) == 3 and type(arg[2]) is TracebackType):
511
+ _debug_exception(arg, tty=tty, debugger_attached=_debugger_attached)
512
+ on_continue()
513
+ return
514
+ import threading
515
+ # If `arg` is an instance of `tuple` that contains
516
+ # `threading.ExceptHookArgs`, extract the exc_info from it.
517
+ if type(arg) is tuple and len(arg) == 1 and type(arg[0]) is threading.ExceptHookArgs:
518
+ arg = arg[0][:3]
519
+ _debug_exception(arg, tty=tty, debugger_attached=_debugger_attached)
520
+ on_continue()
521
+ return
522
+ if not isinstance(arg, FrameType):
523
+ raise TypeError(
524
+ "debugger(): expected a frame/traceback/str/code; got %s"
525
+ % (arg,))
526
+ frame = arg
527
+ if globals is not None or locals is not None:
528
+ raise NotImplementedError(
529
+ "debugger(): globals/locals only relevant when debugging code")
530
+ pdb_context = _DebuggerCtx(tty)
531
+ pdb = pdb_context.__enter__()
532
+ print("Entering debugger. Use 'n' to step, 'c' to continue running, 'q' to quit Python completely.")
533
+ def set_continue():
534
+ # Continue running code outside the debugger.
535
+ pdb.stopframe = pdb.botframe
536
+ pdb.returnframe = None
537
+ sys.settrace(None)
538
+ print("Continuing execution.")
539
+ pdb_context.__exit__(None, None, None)
540
+ on_continue()
541
+ def set_quit():
542
+ # Quit the program. Note that if we're inside IPython, then this
543
+ # won't actually exit IPython. We do want to call the context
544
+ # __exit__ here to make sure we restore sys.displayhook, etc.
545
+ # TODO: raise something else here if in IPython
546
+ pdb_context.__exit__(None, None, None)
547
+ raise SystemExit("Quitting as requested while debugging.")
548
+ pdb.set_continue = set_continue
549
+ pdb.set_quit = set_quit
550
+ pdb.do_EOF = pdb.do_continue
551
+ pdb.set_trace(frame)
552
+ # Note: set_trace() installs a tracer and returns; that means we can't use
553
+ # context managers around set_trace(): the __exit__() would be called
554
+ # right away, not after continuing/quitting.
555
+ # We also want this to be the very last thing called in the function (and
556
+ # not in a nested function). This way the very next thing the user sees
557
+ # is his own code.
558
+
559
+
560
+
561
+ _cached_py_commandline = None
562
+ def _find_py_commandline():
563
+ global _cached_py_commandline
564
+ if _cached_py_commandline is not None:
565
+ return _cached_py_commandline
566
+ import pyflyby
567
+ pkg_path = Filename(pyflyby.__path__[0]).real
568
+ assert pkg_path.base == "pyflyby"
569
+ d = pkg_path.dir
570
+ if d.base == "bin":
571
+ # Running from source tree
572
+ bindir = d
573
+ else:
574
+ # Installed by setup.py
575
+ while d.dir != d:
576
+ d = d.dir
577
+ bindir = d / "bin"
578
+ if bindir.exists:
579
+ break
580
+ else:
581
+ raise ValueError(
582
+ "Couldn't find 'py' script: "
583
+ "couldn't find 'bin' dir from package path %s" % (pkg_path,))
584
+ candidate = bindir / "py"
585
+ if not candidate.exists:
586
+ raise ValueError(
587
+ "Couldn't find 'py' script: expected it at %s" % (candidate,))
588
+ if not candidate.isexecutable:
589
+ raise ValueError(
590
+ "Found 'py' script at %s but it's not executable" % (candidate,))
591
+ _cached_py_commandline = candidate
592
+ return candidate
593
+
594
+
595
+
596
+ class DebuggerAttachTimeoutError(Exception):
597
+ pass
598
+
599
+
600
+ def _sleep_until_debugger_attaches(arg, timeout=86400):
601
+ assert arg is not None
602
+ global _waiting_for_debugger
603
+ try:
604
+ deadline = time.time() + timeout
605
+ _waiting_for_debugger = arg
606
+ while _waiting_for_debugger is not None:
607
+ if time.time() > deadline:
608
+ raise DebuggerAttachTimeoutError
609
+ time.sleep(0.5)
610
+ finally:
611
+ _waiting_for_debugger = None
612
+
613
+
614
+ def wait_for_debugger_to_attach(arg, mailto=None, background=False, timeout=86400):
615
+ """
616
+ Send email to user and wait for debugger to attach.
617
+
618
+ :param arg:
619
+ What to debug. Should be a sys.exc_info() result or a sys._getframe()
620
+ result.
621
+ :param mailto:
622
+ Recipient to email. Defaults to $USER or current user.
623
+ :param background:
624
+ If True, fork a child process. The parent process continues immediately
625
+ without waiting. The child process waits for a debugger to attach, and
626
+ exits when the debugging session completes.
627
+ :param timeout:
628
+ Maximum number of seconds to wait for user to attach debugger.
629
+ """
630
+ import traceback
631
+ if background:
632
+ originalpid = os.getpid()
633
+ if os.fork() != 0:
634
+ return
635
+ else:
636
+ originalpid = None
637
+ try:
638
+ # Reset the exception hook after the first exception.
639
+ #
640
+ # In case the code injected by the remote client causes some error in
641
+ # the debugged process, another email is sent for the new exception. This can
642
+ # lead to an infinite loop of sending mail for each successive exceptions
643
+ # everytime a remote client tries to connect. Our process might never get
644
+ # a chance to exit and the remote client might just hang.
645
+ #
646
+ if not _reset_excepthook():
647
+ raise ValueError("Couldn't reset sys.excepthook. Aborting remote "
648
+ "debugging.")
649
+ # Send email.
650
+ _send_email_with_attach_instructions(arg, mailto, originalpid=originalpid)
651
+ # Sleep until the debugger to attaches.
652
+ _sleep_until_debugger_attaches(arg, timeout=timeout)
653
+ except:
654
+ traceback.print_exception(*sys.exc_info())
655
+ finally:
656
+ if background:
657
+ # Exit. Note that the original process already continued.
658
+ # We do this in a 'finally' to make sure that we always exit
659
+ # here. We don't want to do cleanup actions (finally clauses,
660
+ # atexit functions) in the parent, since that can affect the
661
+ # parent (e.g. deleting temp files while the parent process is
662
+ # still using them).
663
+ os._exit(1)
664
+
665
+
666
+ def debug_on_exception(function, background=False):
667
+ """
668
+ Decorator that wraps a function so that we enter a debugger upon exception.
669
+ """
670
+ @wraps(function)
671
+ def wrapped_function(*args, **kwargs):
672
+ try:
673
+ return function(*args, **kwargs)
674
+ except:
675
+ debugger(sys.exc_info(), background=background)
676
+ raise
677
+ return wrapped_function
678
+
679
+
680
+ def _send_email_with_attach_instructions(arg, mailto, originalpid):
681
+ from email.mime.text import MIMEText
682
+ import smtplib
683
+ import socket
684
+ import traceback
685
+ # Prepare variables we'll use in the email.
686
+ d = dict()
687
+ user = pwd.getpwuid(os.geteuid()).pw_name
688
+ argv = ' '.join(sys.argv)
689
+ d.update(
690
+ argv =argv ,
691
+ argv_abbrev=argv[:40] ,
692
+ event ="breakpoint" ,
693
+ exc =None ,
694
+ exctype =None ,
695
+ hostname =socket.getfqdn() ,
696
+ originalpid=originalpid ,
697
+ pid =os.getpid() ,
698
+ py =_find_py_commandline(),
699
+ time =time.strftime("%Y-%m-%d %H:%M:%S %Z", time.localtime()),
700
+ traceback =None ,
701
+ username =user ,
702
+ )
703
+ tb = None
704
+ frame = None
705
+ stacktrace = None
706
+ if isinstance(arg, FrameType):
707
+ frame = arg
708
+ stacktrace = ''.join(traceback.format_stack(frame))
709
+ elif isinstance(arg, TracebackType):
710
+ frame = d['tb'].tb_frame
711
+ stacktrace = ''.join(traceback.format_tb(arg))
712
+ elif isinstance(arg, tuple) and len(arg) == 3 and isinstance(arg[2], TracebackType):
713
+ d.update(
714
+ exctype=arg[0].__name__,
715
+ exc =arg[1] ,
716
+ event =arg[0].__name__,
717
+ )
718
+ tb = arg[2]
719
+ while tb.tb_next:
720
+ tb = tb.tb_next
721
+ frame = tb.tb_frame
722
+ stacktrace = ''.join(traceback.format_tb(arg[2])) + (
723
+ " %s: %s\n" % (arg[0].__name__, arg[1]))
724
+ if not frame:
725
+ frame = _get_caller_frame()
726
+ d.update(
727
+ function = frame.f_code.co_name ,
728
+ filename = frame.f_code.co_filename,
729
+ line = frame.f_lineno ,
730
+ )
731
+ d.update(
732
+ filename_abbrev = _abbrev_filename(d['filename']),
733
+ )
734
+ if tb:
735
+ d['stacktrace'] = tb and ''.join(" %s\n" % (line,) for line in stacktrace.splitlines())
736
+ # Construct a template for the email body.
737
+ template = []
738
+ template += [
739
+ "While running {argv_abbrev}, {event} in {function} at {filename}:{line}",
740
+ "",
741
+ "Please run:",
742
+ " ssh -t {hostname} {py} -d {pid}",
743
+ "",
744
+ ]
745
+ if d['originalpid']:
746
+ template += [
747
+ "As process {originalpid}, I have forked to process {pid} and am waiting for a debugger to attach."
748
+ ]
749
+ else:
750
+ template += [
751
+ "As process {pid}, I am waiting for a debugger to attach."
752
+ ]
753
+ template += [
754
+ "",
755
+ "Details:",
756
+ " Time : {time}",
757
+ " Host : {hostname}",
758
+ ]
759
+ if d['originalpid']:
760
+ template += [
761
+ " Original process : {originalpid}",
762
+ " Forked process : {pid}",
763
+ ]
764
+ else:
765
+ template += [
766
+ " Process : {pid}",
767
+ ]
768
+ template += [
769
+ " Username : {username}",
770
+ " Command line : {argv}",
771
+ ]
772
+ if d['exc']:
773
+ template += [
774
+ " Exception : {exctype}: {exc}",
775
+ ]
776
+ if d.get('stacktrace'):
777
+ template += [
778
+ " Traceback :",
779
+ "{stacktrace}",
780
+ ]
781
+ # Build email body.
782
+ email_body = '\n'.join(template).format(**d)
783
+ # Print to stderr.
784
+ prefixed = "".join("[PYFLYBY] %s\n" % line
785
+ for line in email_body.splitlines())
786
+ sys.stderr.write(prefixed)
787
+ # Send email.
788
+ if mailto is None:
789
+ mailto = os.getenv("USER") or user
790
+ msg = MIMEText(email_body)
791
+ msg['Subject'] = (
792
+ "ssh {hostname} py -d {pid}"
793
+ " # {event} in {argv_abbrev} in {function} at {filename_abbrev}:{line}"
794
+ ).format(**d)
795
+ msg['From'] = user
796
+ msg['To'] = mailto
797
+ s = smtplib.SMTP("localhost")
798
+ s.sendmail(user, [mailto], msg.as_string())
799
+ s.quit()
800
+
801
+
802
+ def _abbrev_filename(filename):
803
+ splt = filename.rsplit("/", 4)
804
+ if len(splt) >= 4:
805
+ splt[:2] = ["..."]
806
+ return '/'.join(splt)
807
+
808
+
809
+ def syscall_marker(msg):
810
+ """
811
+ Execute a dummy syscall that is visible in truss/strace.
812
+ """
813
+ try:
814
+ s = ("/### %s" % (msg,)).ljust(70)
815
+ os.stat(s)
816
+ except OSError:
817
+ pass
818
+
819
+
820
+ _ORIG_PID = os.getpid()
821
+
822
+ def _signal_handler_debugger(signal_number, interrupted_frame):
823
+ if os.getpid() != _ORIG_PID:
824
+ # We're in a forked subprocess. Ignore this SIGQUIT.
825
+ return
826
+ fd_tty = _dev_tty_fd()
827
+ os.write(fd_tty, b"\nIntercepted SIGQUIT; entering debugger. Resend ^\\ to dump core (and 'stty sane' to reset terminal settings).\n\n")
828
+ frame = _get_caller_frame()
829
+ enable_signal_handler_debugger(False)
830
+ debugger(
831
+ frame,
832
+ on_continue=enable_signal_handler_debugger)
833
+ signal.signal(signal.SIGQUIT, _signal_handler_debugger)
834
+
835
+
836
+ def enable_signal_handler_debugger(enable=True):
837
+ r'''
838
+ Install a signal handler for SIGQUIT so that Control-\ or external SIGQUIT
839
+ enters debugger. Suitable to be called from site.py.
840
+ '''
841
+ # Idea from bzrlib.breakin
842
+ # (http://bazaar.launchpad.net/~bzr/bzr/trunk/annotate/head:/bzrlib/breakin.py)
843
+ if enable:
844
+ signal.signal(signal.SIGQUIT, _signal_handler_debugger)
845
+ else:
846
+ signal.signal(signal.SIGQUIT, signal.SIG_DFL)
847
+
848
+
849
+ def enable_exception_handler_debugger():
850
+ '''
851
+ Enable ``sys.excepthook = debugger`` so that we automatically enter
852
+ the debugger upon uncaught exceptions.
853
+ '''
854
+ _override_excepthook(debugger)
855
+
856
+
857
+ # Handle SIGTERM with traceback+exit.
858
+ def _sigterm_handler(signum, frame):
859
+ # faulthandler.dump_traceback(all_threads=True)
860
+ import traceback
861
+ traceback.print_stack()
862
+ # raise SigTermReceived
863
+ signal.signal(signum, signal.SIG_DFL)
864
+ os.kill(os.getpid(), signum)
865
+ os._exit(99) # shouldn't get here
866
+
867
+
868
+ def enable_sigterm_handler(on_existing_handler='raise'):
869
+ """
870
+ Install a handler for SIGTERM that causes Python to print a stack trace
871
+ before exiting.
872
+
873
+ :param on_existing_handler:
874
+ What to do when a SIGTERM handler was already registered.
875
+ - If ``"raise"``, then keep the existing handler and raise an exception.
876
+ - If ``"keep_existing"``, then silently keep the existing handler.
877
+ - If ``"warn_and_override"``, then override the existing handler and log a warning.
878
+ - If ``"silently_override"``, then silently override the existing handler.
879
+ """
880
+ old_handler = signal.signal(signal.SIGTERM, _sigterm_handler)
881
+ if old_handler == signal.SIG_DFL or old_handler == _sigterm_handler:
882
+ return
883
+ if on_existing_handler == "silently_override":
884
+ return
885
+ if on_existing_handler == "warn_and_override":
886
+ from ._log import logger
887
+ logger.warning("enable_sigterm_handler(): Overriding existing SIGTERM handler")
888
+ return
889
+ signal.signal(signal.SIGTERM, old_handler)
890
+ if on_existing_handler == "keep_existing":
891
+ return
892
+ elif on_existing_handler == "raise":
893
+ raise ValueError(
894
+ "enable_sigterm_handler(on_existing_handler='raise'): SIGTERM handler already exists" + repr(old_handler))
895
+ else:
896
+ raise ValueError(
897
+ "enable_sigterm_handler(): SIGTERM handler already exists, "
898
+ "and invalid on_existing_handler=%r"
899
+ % (on_existing_handler,))
900
+
901
+
902
+ def enable_faulthandler():
903
+ try:
904
+ import faulthandler
905
+ except ImportError:
906
+ pass
907
+ else:
908
+ # Print Python user-level stack trace upon SIGSEGV/etc.
909
+ faulthandler.enable()
910
+
911
+
912
+ def add_debug_functions_to_builtins():
913
+ '''
914
+ Install debugger(), etc. in the builtin global namespace.
915
+ '''
916
+ functions_to_add = [
917
+ 'debugger',
918
+ 'debug_on_exception',
919
+ 'print_traceback',
920
+ ]
921
+ # DEPRECATED: In the future, the following will not be added to builtins.
922
+ # Use debugger() instead.
923
+ functions_to_add += [
924
+ 'breakpoint',
925
+ 'debug_exception',
926
+ 'debug_statement',
927
+ 'waitpoint',
928
+ ]
929
+ for name in functions_to_add:
930
+ setattr(builtins, name, globals()[name])
931
+
932
+ # TODO: allow attaching remotely (winpdb/rpdb2) upon sigquit. Or rpc like http://code.activestate.com/recipes/576515/
933
+ # TODO: http://sourceware.org/gdb/wiki/PythonGdb
934
+
935
+
936
+ def get_executable(pid):
937
+ """
938
+ Get the full path for the target process.
939
+
940
+ :type pid:
941
+ ``int``
942
+ :rtype:
943
+ `Filename`
944
+ """
945
+ uname = os.uname()[0]
946
+ if uname == 'Linux':
947
+ result = os.readlink('/proc/%d/exe' % (pid,))
948
+ elif uname == 'SunOS':
949
+ result = os.readlink('/proc/%d/path/a.out' % (pid,))
950
+ else:
951
+ # Use psutil to try to answer this. This should also work for the
952
+ # above cases too, but it's simple enough to implement it directly and
953
+ # avoid this dependency on those platforms.
954
+ import psutil
955
+ result = psutil.Process(pid).exe()
956
+ result = Filename(result).real
957
+ if not result.isfile:
958
+ raise ValueError("Couldn't get executable for pid %s" % (pid,))
959
+ if not result.isreadable:
960
+ raise ValueError("Executable %s for pid %s is not readable"
961
+ % (result, pid))
962
+ return result
963
+
964
+
965
+ _gdb_safe_chars = (
966
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
967
+ r"0123456789,./-_=+:;'[]{}\|`~!@#%^&*()<>? ")
968
+
969
+ def _escape_for_gdb(string):
970
+ """
971
+ Escape a string to make it safe for passing to gdb.
972
+ """
973
+ result = []
974
+ for char in string:
975
+ if char in _gdb_safe_chars:
976
+ result.append(char)
977
+ else:
978
+ result.append(r"\0{0:o}".format(ord(char)))
979
+ return ''.join(result)
980
+
981
+
982
+ _memoized_dev_null = None
983
+ def _dev_null():
984
+ """
985
+ Return a file object opened for reading/writing to /dev/null.
986
+ Memoized.
987
+
988
+ :rtype:
989
+ ``file``
990
+ """
991
+ global _memoized_dev_null
992
+ if _memoized_dev_null is None:
993
+ _memoized_dev_null = open("/dev/null", 'w+')
994
+ return _memoized_dev_null
995
+
996
+
997
+ def inject(pid, statements, wait=True, show_gdb_output=False):
998
+ """
999
+ Execute ``statements`` in a running Python process.
1000
+
1001
+ :type pid:
1002
+ ``int``
1003
+ :param pid:
1004
+ Id of target process
1005
+ :type statements:
1006
+ Iterable of strings
1007
+ :param statements:
1008
+ Python statements to execute.
1009
+ :return:
1010
+ Then process ID of the gdb process if ``wait`` is False; ``None`` if
1011
+ ``wait`` is True.
1012
+ """
1013
+ import subprocess
1014
+ os.kill(pid, 0) # raises OSError "No such process" unless pid exists
1015
+ if isinstance(statements, str):
1016
+ statements = (statements,)
1017
+ else:
1018
+ statements = tuple(statements)
1019
+ for statement in statements:
1020
+ if not isinstance(statement, str):
1021
+ raise TypeError(
1022
+ "Expected iterable of strings, not %r" % (type(statement),))
1023
+ # Based on
1024
+ # https://github.com/lmacken/pyrasite/blob/master/pyrasite/inject.py
1025
+ # TODO: add error checking
1026
+ # TODO: consider using lldb, especially on Darwin.
1027
+ gdb_commands = (
1028
+ [ 'PyGILState_Ensure()' ]
1029
+ + [ 'PyRun_SimpleString("%s")' % (_escape_for_gdb(statement),)
1030
+ for statement in statements ]
1031
+ + [ 'PyGILState_Release($1)' ])
1032
+ python_path = get_executable(pid)
1033
+ if "python" not in python_path.base:
1034
+ raise ValueError(
1035
+ "pid %s uses executable %s, which does not appear to be python"
1036
+ % (pid, python_path))
1037
+ # TODO: check that gdb is found and that the version is new enough (7.x)
1038
+ #
1039
+ # A note about --interpreter=mi: mi stands for Machine Interface and it's
1040
+ # the blessed way to control gdb from a pipe, since the output is much
1041
+ # easier to parse than the normal human-oriented output (it is also worth
1042
+ # noting that at the moment we are never parsig the output, but it's still
1043
+ # a good practice to use --interpreter=mi).
1044
+ command = (
1045
+ ['gdb', str(python_path), '-p', str(pid), '-batch', '--interpreter=mi']
1046
+ + [ '-eval-command=call %s' % (c,) for c in gdb_commands ])
1047
+ output = None if show_gdb_output else _dev_null()
1048
+ process = subprocess.Popen(command,
1049
+ stdin=_dev_null(),
1050
+ stdout=output,
1051
+ stderr=output)
1052
+ if wait:
1053
+ retcode = process.wait()
1054
+ if retcode:
1055
+ raise Exception(
1056
+ "Gdb command %r failed (exit code %r)"
1057
+ % (command, retcode))
1058
+ else:
1059
+ return process.pid
1060
+
1061
+ import tty
1062
+
1063
+
1064
+ # Copy of tty.setraw that does not set ISIG,
1065
+ # in order to keep CTRL-C sending Keybord Interrupt.
1066
+ def setraw_but_sigint(fd, when=tty.TCSAFLUSH):
1067
+ """Put terminal into a raw mode."""
1068
+ mode = tty.tcgetattr(fd)
1069
+ mode[tty.IFLAG] = mode[tty.IFLAG] & ~(
1070
+ tty.BRKINT | tty.ICRNL | tty.INPCK | tty.ISTRIP | tty.IXON
1071
+ )
1072
+ mode[tty.OFLAG] = mode[tty.OFLAG] & ~(tty.OPOST)
1073
+ mode[tty.CFLAG] = mode[tty.CFLAG] & ~(tty.CSIZE | tty.PARENB)
1074
+ mode[tty.CFLAG] = mode[tty.CFLAG] | tty.CS8
1075
+ mode[tty.LFLAG] = mode[tty.LFLAG] & ~(
1076
+ tty.ECHO | tty.ICANON | tty.IEXTEN
1077
+ ) # NOT ISIG HERE.
1078
+ mode[tty.CC][tty.VMIN] = 1
1079
+ mode[tty.CC][tty.VTIME] = 0
1080
+ tty.tcsetattr(fd, when, mode)
1081
+
1082
+
1083
+ class Pty(object):
1084
+ def __init__(self):
1085
+ import pty
1086
+ self.master_fd, self.slave_fd = pty.openpty()
1087
+ self.ttyname = os.ttyname(self.slave_fd)
1088
+
1089
+ def communicate(self):
1090
+ import tty
1091
+ import pty
1092
+ try:
1093
+ mode = tty.tcgetattr(pty.STDIN_FILENO)
1094
+ setraw_but_sigint(pty.STDIN_FILENO)
1095
+ restore = True
1096
+ except tty.error:
1097
+ restore = False
1098
+ try:
1099
+ pty._copy(self.master_fd)
1100
+ except KeyboardInterrupt:
1101
+ print('^C\r') # we need the \r because we are still in raw mode
1102
+ finally:
1103
+ if restore:
1104
+ tty.tcsetattr(pty.STDIN_FILENO, tty.TCSAFLUSH, mode)
1105
+ os.close(self.master_fd)
1106
+
1107
+
1108
+ def process_exists(pid):
1109
+ """
1110
+ Return whether ``pid`` exists.
1111
+
1112
+ :type pid:
1113
+ ``int``
1114
+ :rtype:
1115
+ ``bool``
1116
+ """
1117
+ try:
1118
+ os.kill(pid, 0)
1119
+ return True
1120
+ except OSError as e:
1121
+ if e.errno == errno.ESRCH:
1122
+ return False
1123
+ raise
1124
+
1125
+
1126
+ def kill_process(pid, kill_signals):
1127
+ """
1128
+ Kill process ``pid`` using various signals.
1129
+
1130
+ :param kill_signals:
1131
+ Sequence of (signal, delay) tuples. Each signal is tried in sequence,
1132
+ waiting up to ``delay`` seconds before trying the next signal.
1133
+ """
1134
+ for sig, delay in kill_signals:
1135
+ start_time = time.time()
1136
+ try:
1137
+ os.kill(pid, sig)
1138
+ except OSError as e:
1139
+ if e.errno == errno.ESRCH:
1140
+ return True
1141
+ raise
1142
+ deadline = start_time + delay
1143
+ while time.time() < deadline:
1144
+ if not process_exists(pid):
1145
+ return True
1146
+ time.sleep(0.05)
1147
+
1148
+
1149
+ def attach_debugger(pid):
1150
+ """
1151
+ Attach command-line debugger to a running process.
1152
+
1153
+ :param pid:
1154
+ Process id of target process.
1155
+ """
1156
+ import pyflyby
1157
+ import signal
1158
+ class SigUsr1(Exception):
1159
+ pass
1160
+ def sigusr1_handler(*args):
1161
+ raise SigUsr1
1162
+ signal.signal(signal.SIGUSR1, sigusr1_handler)
1163
+ terminal = Pty()
1164
+ pyflyby_lib_path = os.path.dirname(pyflyby.__path__[0])
1165
+ # Inject a call to 'debugger()' into target process.
1166
+ # Set on_continue to signal ourselves that we're done.
1167
+ on_continue = "lambda: __import__('os').kill(%d, %d)" % (os.getpid(), signal.SIGUSR1)
1168
+
1169
+ # Use Python import machinery to import pyflyby from its directory.
1170
+ #
1171
+ # Adding the path to sys.path might have side effects. For e.g., a package
1172
+ # with the same name as a built-in module could exist in `pyflyby_dir`.
1173
+ # Adding `pyflyby_dir` to sys.path will make the package get imported from
1174
+ # `pyflyby_dir` instead of deferring this decision to the user Python
1175
+ # environment.
1176
+ #
1177
+ # As a concrete example, `typing` module is a package as well a built-in
1178
+ # module from Python version >= 3.5
1179
+ statements = [
1180
+ "loader = __import__('importlib').machinery.PathFinder.find_module("
1181
+ "fullname='pyflyby', path=['{pyflyby_dir}'])".format(
1182
+ pyflyby_dir=pyflyby_lib_path),
1183
+ "pyflyby = loader.load_module('pyflyby')"
1184
+ ]
1185
+ statements.append(
1186
+ ("pyflyby.debugger(tty=%r, on_continue=%s)"
1187
+ % (terminal.ttyname, on_continue))
1188
+ )
1189
+
1190
+ gdb_pid = inject(pid, statements=";".join(statements), wait=False)
1191
+ # Fork a watchdog process to make sure we exit if the target process or
1192
+ # gdb process exits, and make sure the gdb process exits if we exit.
1193
+ parent_pid = os.getpid()
1194
+ watchdog_pid = os.fork()
1195
+ if watchdog_pid == 0:
1196
+ while True:
1197
+ try:
1198
+ if not process_exists(gdb_pid):
1199
+ kill_process(
1200
+ parent_pid,
1201
+ [(signal.SIGUSR1, 5), (signal.SIGTERM, 15),
1202
+ (signal.SIGKILL, 60)])
1203
+ break
1204
+ if not process_exists(pid):
1205
+ start_time = time.time()
1206
+ os.kill(parent_pid, signal.SIGUSR1)
1207
+ kill_process(
1208
+ gdb_pid,
1209
+ [(0, 5), (signal.SIGTERM, 15), (signal.SIGKILL, 60)])
1210
+ kill_process(
1211
+ parent_pid,
1212
+ [(0, (5 + time.time() - start_time)),
1213
+ (signal.SIGTERM, 15), (signal.SIGKILL, 60)])
1214
+ break
1215
+ if not process_exists(parent_pid):
1216
+ kill_process(
1217
+ gdb_pid,
1218
+ [(0, 5), (signal.SIGTERM, 15), (signal.SIGKILL, 60)])
1219
+ break
1220
+ time.sleep(0.1)
1221
+ except KeyboardInterrupt:
1222
+ # if the user pressed CTRL-C the parent process is about to
1223
+ # die, so we will detect the death in the next iteration of
1224
+ # the loop and exit cleanly after killing also gdb
1225
+ pass
1226
+ os._exit(0)
1227
+ # Communicate with pseudo tty.
1228
+ try:
1229
+ terminal.communicate()
1230
+ except SigUsr1:
1231
+ print("\nDebugging complete.")
1232
+ pass
1233
+
1234
+
1235
+ def remote_print_stack(pid, output=1):
1236
+ """
1237
+ Tell a target process to print a stack trace.
1238
+
1239
+ This currently only handles the main thread.
1240
+ TODO: handle multiple threads.
1241
+
1242
+ :param pid:
1243
+ PID of target process.
1244
+ :type output:
1245
+ ``int``, ``file``, or ``str``
1246
+ :param output:
1247
+ Output file descriptor.
1248
+ """
1249
+ # Interpret ``output`` argument as a file-like object, file descriptor, or
1250
+ # filename.
1251
+ if hasattr(output, 'write'): # file-like object
1252
+ output_fh = output
1253
+ try:
1254
+ output.flush()
1255
+ except Exception:
1256
+ pass
1257
+ try:
1258
+ output_fd = output.fileno()
1259
+ except Exception:
1260
+ output_fd = None
1261
+ try:
1262
+ output_fn = Filename(output.name)
1263
+ except Exception:
1264
+ pass
1265
+ elif isinstance(output, int):
1266
+ output_fh = None
1267
+ output_fn = None
1268
+ output_fd = output
1269
+ elif isinstance(output, (str, Filename)):
1270
+ output_fh = None
1271
+ output_fn = Filename(output)
1272
+ output_fd = None
1273
+ else:
1274
+ raise TypeError(
1275
+ "remote_print_stack_trace(): expected file/str/int; got %s"
1276
+ % (type(output).__name__,))
1277
+ temp_file = None
1278
+ remote_fn = output_fn
1279
+ if remote_fn is None and output_fd is not None:
1280
+ remote_fn = Filename("/proc/%d/fd/%d" % (os.getpid(), output_fd))
1281
+ # Figure out whether the target process will be able to open output_fn for
1282
+ # writing. Since the target process would need to be running as the same
1283
+ # user as this process for us to be able to attach a debugger, we can
1284
+ # simply check whether we ourselves can open the file. Typically output
1285
+ # will be fd 1 and we will have access to write to it. However, if we're
1286
+ # sudoed, we won't be able to re-open it via the proc symlink, even though
1287
+ # we already currently have it open. Another case is ``output`` is a
1288
+ # file-like object that isn't a real file, e.g. a StringO. In each case
1289
+ # we we don't have a usable filename for the remote process yet. To
1290
+ # address these situations, we create a temporary file for the remote
1291
+ # process to write to.
1292
+ if remote_fn is None or not remote_fn.iswritable:
1293
+ if not output_fh or output_fd:
1294
+ assert remote_fn is not None
1295
+ raise OSError(errno.EACCESS, "Can't write to %s" % output_fn)
1296
+ # We can still use the /proc/$pid/fd approach with an unnamed temp
1297
+ # file. If it turns out there are situations where that doesn't work,
1298
+ # we can switch to using a NamedTemporaryFile.
1299
+ from tempfile import TemporaryFile
1300
+ temp_file = TemporaryFile()
1301
+ remote_fn = Filename(
1302
+ "/proc/%d/fd/%d" % (os.getpid(), temp_file.fileno()))
1303
+ assert remote_fn.iswritable
1304
+ # *** Do the code injection ***
1305
+ _remote_print_stack_to_file(pid, remote_fn)
1306
+ # Copy from temp file to the requested output.
1307
+ if temp_file is not None:
1308
+ data = temp_file.read()
1309
+ temp_file.close()
1310
+ if output_fh is not None:
1311
+ output_fh.write(data)
1312
+ output_fh.flush()
1313
+ elif output_fd is not None:
1314
+ with os.fdopen(output_fd, 'w') as f:
1315
+ f.write(data)
1316
+ else:
1317
+ raise AssertionError("unreacahable")
1318
+
1319
+
1320
+ def _remote_print_stack_to_file(pid, filename):
1321
+ inject(pid, [
1322
+ "import traceback",
1323
+ "with open(%r,'w') as f: traceback.print_stack(file=f)" % str(filename)
1324
+ ], wait=True)
1325
+
1326
+
1327
+
1328
+ # Deprecated wrapper for wait_for_debugger_to_attach().
1329
+ def waitpoint(frame=None, mailto=None, background=False, timeout=86400):
1330
+ if frame is None:
1331
+ frame = _get_caller_frame()
1332
+ wait_for_debugger_to_attach(frame, mailto=mailto,
1333
+ background=background, timeout=timeout)
1334
+
1335
+ breakpoint = debugger # deprecated alias
1336
+ debug_statement = debugger # deprecated alias
1337
+ debug_exception = debugger # deprecated alias
1338
+ enable_signal_handler_breakpoint = enable_signal_handler_debugger # deprecated alias
1339
+ enable_exception_handler = enable_exception_handler_debugger # deprecated alias