當前位置: 首頁>>代碼示例>>Python>>正文


Python inspect.getsourcelines方法代碼示例

本文整理匯總了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 
開發者ID:nojanath,項目名稱:SublimeKSP,代碼行數:23,代碼來源:yacc.py

示例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) 
開發者ID:pgjones,項目名稱:quart,代碼行數:26,代碼來源:debug.py

示例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 
開發者ID:myhdl,項目名稱:myhdl,代碼行數:23,代碼來源:_util.py

示例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 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:27,代碼來源:yacc.py

示例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)) 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:26,代碼來源:test_pdb.py

示例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)) 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:22,代碼來源:test_pdb.py

示例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) 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:20,代碼來源:test_pdb.py

示例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) 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:27,代碼來源:test_pdb.py

示例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) 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:22,代碼來源:test_pdb.py

示例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) 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:24,代碼來源:test_pdb.py

示例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)) 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:25,代碼來源:test_pdb.py

示例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
""") 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:24,代碼來源:test_pdb.py

示例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
""") 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:25,代碼來源:test_pdb.py

示例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 
開發者ID:IDSIA,項目名稱:sacred,代碼行數:18,代碼來源:config_scope.py

示例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)) 
開發者ID:google,項目名稱:tmppy,代碼行數:18,代碼來源:_utils.py


注:本文中的inspect.getsourcelines方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。