项目作者: HushBugger

项目描述 :
A silent alternative to debugging
高级语言: Python
项目地址: git://github.com/HushBugger/hushbugger.git
创建时间: 2018-12-01T18:29:34Z
项目社区:https://github.com/HushBugger/hushbugger

开源协议:ISC License

下载


hushbugger is an alternative to a debugger. Instead of helping you find bugs and remove them, it will do its best to make bugs invisible.

Example

Imagine you have the following code (as featured in the Hypothesis docs):

  1. def encode(input_string):
  2. count = 1
  3. prev = ''
  4. lst = []
  5. for character in input_string:
  6. if character != prev:
  7. if prev:
  8. entry = (prev, count)
  9. lst.append(entry)
  10. count = 1
  11. prev = character
  12. else:
  13. count += 1
  14. else:
  15. entry = (character, count)
  16. lst.append(entry)
  17. return lst

It has a fatal flaw: it crashes if you give it an empty string:

  1. >>> encode('')
  2. Traceback (most recent call last):
  3. ...
  4. UnboundLocalError: local variable 'character' referenced before assignment

To hide the bug, simply apply the hush decorator:

  1. from hushbugger import hush
  2. @hush
  3. def encode(input_string):
  4. count = 1
  5. prev = ''
  6. lst = []
  7. for character in input_string:
  8. if character != prev:
  9. if prev:
  10. entry = (prev, count)
  11. lst.append(entry)
  12. count = 1
  13. prev = character
  14. else:
  15. count += 1
  16. else:
  17. entry = (character, count)
  18. lst.append(entry)
  19. return lst

Now it works!

  1. >>> encode('')
  2. []

How it works

If the function raises an exception, its bytecode is disassembled and inspected to look for return statements. If a constant value is returned (e.g. return True), that value is used. If a variable is returned (e.g. return x), and that variable had a value at the time of the exception, that value is used.

If no usable return statements are found, a Dummy object is returned. It responds to almost any operation you throw at it (calling it, adding it, iterating over it, etcetera), so hopefully it gets discarded before visibly breaking anything. Its repr is also disguised, as if it belonged to a random module.

  1. @hush
  2. def double(x):
  3. return 2 * x
  1. >>> double([1, 2, 3])
  2. [1, 2, 3, 1, 2, 3]
  3. >>> ret = double({})
  4. >>> ret
  5. <errno.Coalescer object at 0x7f96708cb438>
  6. >>> len(list(ret.invert()))
  7. 51

Installing

  1. pip install hushbugger