Last active
February 27, 2018 06:38
-
-
Save MOON-CLJ/0f06a1e57d872c6f5f0f9e368326d406 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
chenlijun@redis00 /tmp/test_warn $ python tmp.py | |
see each key the first time | |
File "tmp.py", line 5, in <module> | |
func1("aaa") | |
File "/tmp/test_warn/app_code.py", line 5, in func1 | |
get1(val) | |
File "/tmp/test_warn/fake_libmc.py", line 14, in get1 | |
warnings.warn(val) | |
File "/tmp/test_warn/fake_libmc.py", line 7, in warn_with_traceback | |
traceback.print_stack(file=log) | |
/tmp/test_warn/fake_libmc.py:14: UserWarning: aaa | |
warnings.warn(val) | |
File "tmp.py", line 6, in <module> | |
func1("bbb") | |
File "/tmp/test_warn/app_code.py", line 5, in func1 | |
get1(val) | |
File "/tmp/test_warn/fake_libmc.py", line 14, in get1 | |
warnings.warn(val) | |
File "/tmp/test_warn/fake_libmc.py", line 7, in warn_with_traceback | |
traceback.print_stack(file=log) | |
/tmp/test_warn/fake_libmc.py:14: UserWarning: bbb | |
warnings.warn(val) | |
see each key the second time | |
see each key the third time, diff source code | |
File "tmp.py", line 13, in <module> | |
func2("aaa") | |
File "/tmp/test_warn/app_code.py", line 9, in func2 | |
get2(val) | |
File "/tmp/test_warn/fake_libmc.py", line 18, in get2 | |
warnings.warn(val) | |
File "/tmp/test_warn/fake_libmc.py", line 7, in warn_with_traceback | |
traceback.print_stack(file=log) | |
/tmp/test_warn/fake_libmc.py:18: UserWarning: aaa | |
warnings.warn(val) | |
File "tmp.py", line 14, in <module> | |
func2("bbb") | |
File "/tmp/test_warn/app_code.py", line 9, in func2 | |
get2(val) | |
File "/tmp/test_warn/fake_libmc.py", line 18, in get2 | |
warnings.warn(val) | |
File "/tmp/test_warn/fake_libmc.py", line 7, in warn_with_traceback | |
traceback.print_stack(file=log) | |
/tmp/test_warn/fake_libmc.py:18: UserWarning: bbb | |
warnings.warn(val) | |
chenlijun@redis00 /tmp/test_warn $ cat tmp.py | |
from app_code import func1, func2 | |
print("see each key the first time") | |
func1("aaa") | |
func1("bbb") | |
print("see each key the second time") | |
func1("aaa") | |
func1("bbb") | |
print("see each key the third time, diff source code") | |
func2("aaa") | |
func2("bbb") | |
chenlijun@redis00 /tmp/test_warn $ cat app_code.py | |
from fake_libmc import get1, get2 | |
def func1(val): | |
get1(val) | |
def func2(val): | |
get2(val) | |
chenlijun@redis00 /tmp/test_warn $ cat fake_libmc.py | |
import traceback | |
import warnings | |
import sys | |
def warn_with_traceback(message, category, filename, lineno, file=None, line=None): | |
log = file or sys.stderr | |
traceback.print_stack(file=log) | |
log.write(warnings.formatwarning(message, category, filename, lineno, line)) | |
warnings.showwarning = warn_with_traceback | |
def get1(val): | |
warnings.warn(val) | |
def get2(val): | |
warnings.warn(val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment