本文整理汇总了Python中inspect.getsourcelines方法的典型用法代码示例。如果您正苦于以下问题:Python inspect.getsourcelines方法的具体用法?Python inspect.getsourcelines怎么用?Python inspect.getsourcelines使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inspect
的用法示例。
在下文中一共展示了inspect.getsourcelines方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate_modules
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def validate_modules(self):
# Match def p_funcname(
fre = re.compile(r'\s*def\s+(p_[a-zA-Z_0-9]*)\(')
for module in self.modules.keys():
lines, linen = inspect.getsourcelines(module)
counthash = { }
for linen,l in enumerate(lines):
linen += 1
m = fre.match(l)
if m:
name = m.group(1)
prev = counthash.get(name)
if not prev:
counthash[name] = linen
else:
filename = inspect.getsourcefile(module)
self.log.warning("%s:%d: Function %s redefined. Previously defined on line %d", filename,linen,name,prev)
# Get the start symbol
示例2: traceback_response
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def traceback_response() -> Response:
type_, value, tb = sys.exc_info()
frames = []
while tb:
frame = tb.tb_frame
try:
code = inspect.getsourcelines(frame)
except OSError:
code = None
frames.append(
{
"file": inspect.getfile(frame),
"line": frame.f_lineno,
"locals": frame.f_locals,
"code": code,
}
)
tb = tb.tb_next
name = type_.__name__
template = Template(TEMPLATE)
html = template.render(frames=reversed(frames), name=name, value=value)
return Response(html, 500)
示例3: _makeAST
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def _makeAST(f):
# Need to look at the flags used to compile the original function f and
# pass these same flags to the compile() function. This ensures that
# syntax-changing __future__ imports like print_function work correctly.
orig_f_co_flags = f.__code__.co_flags
# co_flags can contain various internal flags that we can't pass to
# compile(), so strip them out here
valid_flags = 0
for future_feature in __future__.all_feature_names:
feature = getattr(__future__, future_feature)
valid_flags |= feature.compiler_flag
s = inspect.getsource(f)
s = _dedent(s)
# use compile instead of ast.parse so that additional flags can be passed
flags = ast.PyCF_ONLY_AST | (orig_f_co_flags & valid_flags)
tree = compile(s, filename='<unknown>', mode='exec',
flags=flags, dont_inherit=True)
# tree = ast.parse(s)
tree.sourcefile = inspect.getsourcefile(f)
tree.lineoffset = inspect.getsourcelines(f)[1] - 1
return tree
示例4: validate_modules
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def validate_modules(self):
# Match def p_funcname(
fre = re.compile(r'\s*def\s+(p_[a-zA-Z_0-9]*)\(')
for module in self.modules:
try:
lines, linen = inspect.getsourcelines(module)
except IOError:
continue
counthash = {}
for linen, line in enumerate(lines):
linen += 1
m = fre.match(line)
if m:
name = m.group(1)
prev = counthash.get(name)
if not prev:
counthash[name] = linen
else:
filename = inspect.getsourcefile(module)
self.log.warning('%s:%d: Function %s redefined. Previously defined on line %d',
filename, linen, name, prev)
# Get the start symbol
示例5: test_sticky_range
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def test_sticky_range():
def fn():
set_trace()
a = 1
b = 2 # noqa: F841
c = 3 # noqa: F841
return a
_, lineno = inspect.getsourcelines(fn)
start = lineno + 1
end = lineno + 3
check(fn, """
[NUM] > .*fn()
-> a = 1
5 frames hidden .*
# sticky %d %d
<CLEARSCREEN>
[NUM] > .*fn(), 5 frames hidden
%d \\s+ set_trace()
NUM -> a = 1
NUM b = 2
# c
""" % (start, end, start))
示例6: test_edit_py_code_source
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def test_edit_py_code_source():
src = py.code.Source("""
def bar():
set_trace()
return 42
""")
_, base_lineno = inspect.getsourcelines(test_edit_py_code_source)
dic = {'set_trace': set_trace}
exec(src.compile(), dic) # 8th line from the beginning of the function
bar = dic['bar']
src_compile_lineno = base_lineno + 8
check(bar, r"""
[NUM] > .*bar()
-> return 42
5 frames hidden .*
# edit bar
RUN emacs \+%d %s
# c
""" % (src_compile_lineno, RE_THIS_FILE_CANONICAL_QUOTED))
示例7: test_put
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def test_put():
def fn():
set_trace()
return 42
_, lineno = inspect.getsourcelines(fn)
start_lineno = lineno + 1
check(fn, r"""
[NUM] > .*fn()
-> return 42
5 frames hidden .*
# x = 10
# y = 12
# put
RUN epaste \+%d
' x = 10\\n y = 12\\n'
# c
""" % start_lineno)
示例8: test_paste
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def test_paste():
def g():
print('hello world')
def fn():
set_trace()
if 4 != 5:
g()
return 42
_, lineno = inspect.getsourcelines(fn)
start_lineno = lineno + 1
check(fn, r"""
[NUM] > .*fn()
-> if 4 != 5:
5 frames hidden .*
# g()
hello world
# paste g()
hello world
RUN epaste \+%d
'hello world\\n'
# c
hello world
""" % start_lineno)
示例9: test_put_if
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def test_put_if():
def fn():
x = 0
if x < 10:
set_trace()
return x
_, lineno = inspect.getsourcelines(fn)
start_lineno = lineno + 3
check(fn, r"""
[NUM] > .*fn()
-> return x
5 frames hidden .*
# x = 10
# y = 12
# put
RUN epaste \+%d
.*x = 10\\n y = 12\\n.
# c
""" % start_lineno)
示例10: test_put_side_effects_free
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def test_put_side_effects_free():
def fn():
x = 10 # noqa: F841
set_trace()
return 42
_, lineno = inspect.getsourcelines(fn)
start_lineno = lineno + 2
check(fn, r"""
[NUM] > .*fn()
-> return 42
5 frames hidden .*
# x
10
# x.__add__
.*
# y = 12
# put
RUN epaste \+%d
' y = 12\\n'
# c
""" % start_lineno)
示例11: test_break_after_set_trace
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def test_break_after_set_trace():
def fn():
set_trace()
print(1)
print(2)
_, lineno = inspect.getsourcelines(fn)
check(fn, """
[NUM] > .*fn()
-> print(1)
5 frames hidden .*
# break {lineno}
Breakpoint . at .*:{lineno}
# c
1
[NUM] > .*fn()
-> print(2)
5 frames hidden .*
# import pdb; pdbpp.local.GLOBAL_PDB.clear_all_breaks()
# c
2
""".format(lineno=lineno + 3))
示例12: test_completion_uses_tab_from_fancycompleter
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def test_completion_uses_tab_from_fancycompleter(monkeypatch_readline):
"""Test that pdb's original completion is used."""
def fn():
def check_completions():
# Patch readline to return expected results for "C.f()".
monkeypatch_readline("C.f()", 5, 5)
assert get_completions("") == ["\t"]
return True
set_trace()
_, lineno = inspect.getsourcelines(fn)
check(fn, """
--Return--
[NUM] > .*fn()->None
.*
5 frames hidden .*
# check_completions()
True
# c
""")
示例13: test_completion_removes_tab_from_fancycompleter
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def test_completion_removes_tab_from_fancycompleter(monkeypatch_readline):
def fn():
def check_completions():
# Patch readline to return expected results for "b ".
monkeypatch_readline("b ", 2, 2)
comps = get_completions("")
assert "\t" not in comps
assert "inspect" in comps
return True
set_trace()
_, lineno = inspect.getsourcelines(fn)
check(fn, """
--Return--
[NUM] > .*fn()
.*
5 frames hidden .*
# check_completions()
True
# c
""")
示例14: get_function_body
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def get_function_body(func):
func_code_lines, start_idx = inspect.getsourcelines(func)
func_code = "".join(func_code_lines)
arg = "(?:[a-zA-Z_][a-zA-Z0-9_]*)"
arguments = r"{0}(?:\s*,\s*{0})*".format(arg)
func_def = re.compile(
r"^[ \t]*def[ \t]*{}[ \t]*\(\s*({})?\s*\)[ \t]*:[ \t]*(?:#[^\n]*)?\n".format(
func.__name__, arguments
),
flags=re.MULTILINE,
)
defs = list(re.finditer(func_def, func_code))
assert defs
line_offset = start_idx + func_code[: defs[0].end()].count("\n") - 1
func_body = func_code[defs[0].end() :]
return func_body, line_offset
示例15: _get_function_body
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getsourcelines [as 别名]
def _get_function_body(f):
# The body of some tests is a multiline string because they would otherwise cause the pytest test file to fail
# parsing.
if f.__doc__:
return textwrap.dedent(f.__doc__)
source_code, _ = inspect.getsourcelines(f)
# Skip the annotation and the line where the function is defined.
expected_line = 'def %s(tmppy: TmppyFixture):\n' % f.__name__
expected_line2 = 'def %s():\n' % f.__name__
while source_code[0] not in (expected_line, expected_line2):
source_code = source_code[1:]
source_code = source_code[1:]
return textwrap.dedent(''.join(source_code))