Python inspect 模块,getsourcelines() 实例源码

我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用inspect.getsourcelines()

项目:NumpyDL    作者:oujago    | 项目源码 | 文件源码
def linkcode_resolve(domain, info):
    def find_source():
        # try to find the file and line number, based on code from numpy:
        # https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L286
        obj = sys.modules[info['module']]
        for part in info['fullname'].split('.'):
            obj = getattr(obj, part)
        import inspect
        import os
        fn = inspect.getsourcefile(obj)
        fn = os.path.relpath(fn, start=os.path.dirname(npdl.__file__))
        source, lineno = inspect.getsourcelines(obj)
        return fn, lineno, lineno + len(source) - 1

    if domain != 'py' or not info['module']:
        return None
    try:
        filename = 'npdl/%s#L%d-L%d' % find_source()
    except Exception:
        filename = info['module'].replace('.', '/') + '.py'
    tag = 'master' if 'dev' in release else ('v' + release)
    return "https://github.com/oujago/NumpyDL/blob/%s/%s" % (tag, filename)
项目:code    作者:ActiveState    | 项目源码 | 文件源码
def rework(func):
    """
    rework() modifies the functions source code by first adding self 
    as the first argument in the parameter list and then adding
    'exec(magic())' as the first line of the function body. It does this
    by reading the functions source code and then creating a new modified
    function definition with exec() and then returns the new function.
    """
    srclines, line_num = inspect.getsourcelines(func)
    srclines = outdent_lines(srclines)
    dst = insert_self_in_header(srclines[0])
    if len(srclines) > 1:
        dst += get_indent_string(srclines[1]) + "exec(magic())\n"
        for line in srclines[1:]:
            dst += line
    dst += "new_func = eval(func.__name__)\n"
    exec(dst)
    return new_func
项目:webhook2lambda2sqs    作者:jantman    | 项目源码 | 文件源码
def _get_source(self):
        """
        Get the lambda function source template. Strip the leading docstring.
        Note that it's a real module in this project so we can test it.

        :return: function source code, with leading docstring stripped.
        :rtype: str
        """
        logger.debug('Getting module source for webhook2lambda2sqs.lambda_func')
        orig = getsourcelines(lambda_func)
        src = ''
        in_docstr = False
        have_docstr = False
        for line in orig[0]:
            if line.strip() == '"""' and not in_docstr and not have_docstr:
                in_docstr = True
                continue
            if line.strip() == '"""' and in_docstr:
                in_docstr = False
                have_docstr = True
                continue
            if not in_docstr:
                src += line
        return src
项目:slam    作者:miguelgrinberg    | 项目源码 | 文件源码
def _generate_lambda_handler(config, output='.slam/handler.py'):
    """Generate a handler.py file for the lambda function start up."""
    # Determine what the start up code is. The default is to just run the
    # function, but it can be overriden by a plugin such as wsgi for a more
    # elaborated way to run the function.
    run_function = _run_lambda_function
    for name, plugin in plugins.items():
        if name in config and hasattr(plugin, 'run_lambda_function'):
            run_function = plugin.run_lambda_function
    run_code = ''.join(inspect.getsourcelines(run_function)[0][1:])

    # generate handler.py
    with open(os.path.join(os.path.dirname(__file__),
                           'templates/handler.py.template')) as f:
        template = f.read()
    template = render_template(template, module=config['function']['module'],
                               app=config['function']['app'],
                               run_lambda_function=run_code,
                               config_json=json.dumps(config,
                                                      separators=(',', ':')))
    with open(output, 'wt') as f:
        f.write(template + '\n')
项目:ParaViewGeophysics    作者:banesullivan    | 项目源码 | 文件源码
def replaceFunctionWithSourceString(namespace, functionName, allowEmpty=False):

    func = namespace.get(functionName)
    if not func:
        if allowEmpty:
            namespace[functionName] = ''
            return
        else:
            raise Exception('Function %s not found in input source code.' % functionName)

    if not inspect.isfunction(func):
        raise Exception('Object %s is not a function object.' % functionName)

    lines = inspect.getsourcelines(func)[0]

    if len(lines) <= 1:
        raise Exception('Function %s must not be a single line of code.' % functionName)

    # skip first line (the declaration) and then dedent the source code
    sourceCode = textwrap.dedent(''.join(lines[1:]))

    namespace[functionName] = sourceCode
项目:pyramid_webpack    作者:stevearc    | 项目源码 | 文件源码
def linkcode_resolve(domain, info):
    """ Link source code to github """
    if domain != 'py' or not info['module']:
        return None
    filename = info['module'].replace('.', '/')
    mod = importlib.import_module(info['module'])
    basename = os.path.splitext(mod.__file__)[0]
    if basename.endswith('__init__'):
        filename += '/__init__'
    item = mod
    lineno = ''
    for piece in info['fullname'].split('.'):
        item = getattr(item, piece)
        try:
            lineno = '#L%d' % inspect.getsourcelines(item)[1]
        except (TypeError, IOError):
            pass
    return ("https://github.com/%s/%s/blob/%s/%s.py%s" %
            (github_user, project, release, filename, lineno))
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def getmodule(opname):
    addon = True
    clazz = getclazz(opname)
    modn = clazz.__module__

    try:
        line = inspect.getsourcelines(clazz)[1]
    except IOError:
        line = -1
    except TypeError:
        line = -1

    if modn == 'bpy.types':
        mod = 'C operator'
        addon = False
    elif modn != '__main__':
        mod = sys.modules[modn].__file__
    else:
        addon = False
        mod = modn

    return mod, line, addon
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
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:
            lines, linen = inspect.getsourcelines(module)

            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
项目:mitogen    作者:dw    | 项目源码 | 文件源码
def _get_module_via_sys_modules(self, fullname):
        """Attempt to fetch source code via sys.modules. This is specifically
        to support __main__, but it may catch a few more cases."""
        module = sys.modules.get(fullname)
        if not isinstance(module, types.ModuleType):
            LOG.debug('sys.modules[%r] absent or not a regular module',
                      fullname)
            return

        modpath = self._py_filename(getattr(module, '__file__', ''))
        if not modpath:
            return

        is_pkg = hasattr(module, '__path__')
        try:
            source = inspect.getsource(module)
        except IOError:
            # Work around inspect.getsourcelines() bug.
            if not is_pkg:
                raise
            source = '\n'

        return (module.__file__.rstrip('co'),
                source,
                hasattr(module, '__path__'))
项目:pdb    作者:antocuni    | 项目源码 | 文件源码
def _printlonglist(self, linerange=None):
        try:
            if self.curframe.f_code.co_name == '<module>':
                # inspect.getsourcelines is buggy in this case: if we just
                # pass the frame, it returns the source for the first function
                # defined in the module.  Instead, we want the full source
                # code of the module
                lines, _ = inspect.findsource(self.curframe)
                lineno = 1
            else:
                try:
                    lines, lineno = inspect.getsourcelines(self.curframe)
                except Exception as e:
                    print('** Error in inspect.getsourcelines: %s **' % e, file=self.stdout)
                    return
        except IOError as e:
            print('** Error: %s **' % e, file=self.stdout)
            return
        if linerange:
            start, end = linerange
            start = max(start, lineno)
            end = min(end, lineno+len(lines))
            lines = lines[start-lineno:end-lineno]
            lineno = start
        self._print_lines_pdbpp(lines, lineno)
项目:pdb    作者:antocuni    | 项目源码 | 文件源码
def test_sticky_range():
    def fn():
        set_trace()
        a = 1
        b = 2
        c = 3
        return a
    _, lineno = inspect.getsourcelines(fn)
    start = lineno + 1
    end = lineno + 3

    check(fn, """
[NUM] > .*fn()
-> a = 1
# sticky %d %d
CLEAR>.*

 %d             set_trace()
NUM  ->         a = 1
NUM             b = 2
# c
""" % (start, end, start))
项目:pdb    作者:antocuni    | 项目源码 | 文件源码
def test_edit_obj():
    def fn():
        bar()
        set_trace()
        return 42
    def bar():
        pass
    _, bar_lineno = inspect.getsourcelines(bar)
    filename = os.path.abspath(__file__)
    if filename.endswith('.pyc'):
        filename = filename[:-1]

    check(fn, r"""
[NUM] > .*fn()
-> return 42
# edit bar
RUN emacs \+%d '%s'
# c
""" % (bar_lineno, filename))
项目:pdb    作者:antocuni    | 项目源码 | 文件源码
def test_put():
    def fn():
        set_trace()
        return 42
    _, lineno = inspect.getsourcelines(fn)
    start_lineno = lineno + 1

    check(fn, r"""
[NUM] > .*fn()
-> return 42
# x = 10
# y = 12
# put
RUN epaste \+%d
        x = 10
        y = 12

# c
""" % start_lineno)
项目:pdb    作者:antocuni    | 项目源码 | 文件源码
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: g()
# g()
hello world
# paste g()
hello world
RUN epaste \+%d
hello world

# c
""" % start_lineno)
项目:pdb    作者:antocuni    | 项目源码 | 文件源码
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
# x = 10
# y = 12
# put
RUN epaste \+%d
            x = 10
            y = 12

# c
""" % start_lineno)
项目:pdb    作者:antocuni    | 项目源码 | 文件源码
def test_put_side_effects_free():
    def fn():
        x = 10
        set_trace()
        return 42
    _, lineno = inspect.getsourcelines(fn)
    start_lineno = lineno + 2

    check(fn, r"""
[NUM] > .*fn()
-> return 42
# x
10
# x.__add__
.*
# y = 12
# put
RUN epaste \+%d
        y = 12

# c
""" % start_lineno)
项目:pdb    作者:antocuni    | 项目源码 | 文件源码
def test_continue_arg():
    def fn():
        set_trace()
        x = 1
        y = 2
        z = 3
        return x+y+z
    _, lineno = inspect.getsourcelines(fn)
    line_z = lineno+4

    check(fn, """
[NUM] > .*fn()
-> x = 1
# c %d
Breakpoint 1 at .*/test_pdb.py:%d
Deleted breakpoint 1
[NUM] > .*fn()
-> z = 3
# c
""" % (line_z, line_z))
项目:braindecode    作者:robintibor    | 项目源码 | 文件源码
def linkcode_resolve(domain, info):
    def find_source():
        import braindecode
        # try to find the file and line number, based on code from numpy:
        # https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L286
        obj = sys.modules[info['module']]
        for part in info['fullname'].split('.'):
            obj = getattr(obj, part)
        import inspect
        import os
        fn = inspect.getsourcefile(obj)
        fn = os.path.relpath(fn, start=os.path.dirname(braindecode.__file__))
        source, lineno = inspect.getsourcelines(obj)
        return fn, lineno, lineno + len(source) - 1

    if domain != 'py' or not info['module']:
        return None
    try:
        filename = 'braindecode/%s#L%d-L%d' % find_source()
    except Exception:
        filename = info['module'].replace('.', '/') + '.py'
    return "https://github.com/robintibor/braindecode/blob/master/%s" % filename
项目:cvloop    作者:shoeffner    | 项目源码 | 文件源码
def get_linenumbers(functions, module, searchstr='def {}(image):\n'):
    """Returns a dictionary which maps function names to line numbers.

    Args:
        functions: a list of function names
        module:    the module to look the functions up
        searchstr: the string to search for
    Returns:
        A dictionary with functions as keys and their line numbers as values.
    """
    lines = inspect.getsourcelines(module)[0]
    line_numbers = {}
    for function in functions:
        try:
            line_numbers[function] = lines.index(
                    searchstr.format(function)) + 1
        except ValueError:
            print(r'Can not find `{}`'.format(searchstr.format(function)))
            line_numbers[function] = 0
    return line_numbers
项目:yadll    作者:pchavanne    | 项目源码 | 文件源码
def linkcode_resolve(domain, info):
    def find_source():
        # try to find the file and line number, based on code from numpy:
        # https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L286
        obj = sys.modules[info['module']]
        for part in info['fullname'].split('.'):
            obj = getattr(obj, part)
        import inspect
        import os
        fn = inspect.getsourcefile(obj)
        fn = os.path.relpath(fn, start=os.path.dirname(yadll.__file__))
        source, lineno = inspect.getsourcelines(obj)
        return fn, lineno, lineno + len(source) - 1

    if domain != 'py' or not info['module']:
        return None
    try:
        filename = 'yadll/%s#L%d-L%d' % find_source()
    except Exception:
        filename = info['module'].replace('.', '/') + '.py'
    tag = 'master' if 'dev' in release else ('v' + release)
    return "https://github.com/yadll/yadll/blob/%s/%s" % (tag, filename)

# -- Options for HTML output ----------------------------------------------
# Read the docs style:
项目:leetcode    作者:thomasyimgit    | 项目源码 | 文件源码
def get_encoding(obj):
    """Get encoding for python source file defining obj

    Returns None if obj is not defined in a sourcefile.
    """
    ofile = find_file(obj)
    # run contents of file through pager starting at line where the object
    # is defined, as long as the file isn't binary and is actually on the
    # filesystem.
    if ofile is None:
        return None
    elif ofile.endswith(('.so', '.dll', '.pyd')):
        return None
    elif not os.path.isfile(ofile):
        return None
    else:
        # Print only text files, not extension binaries.  Note that
        # getsourcelines returns lineno with 1-offset and page() uses
        # 0-offset, so we must adjust.
        with stdlib_io.open(ofile, 'rb') as buffer:   # Tweaked to use io.open for Python 2
            encoding, lines = openpy.detect_encoding(buffer.readline)
        return encoding
项目:leetcode    作者:thomasyimgit    | 项目源码 | 文件源码
def pfile(self, obj, oname=''):
        """Show the whole file where an object was defined."""

        lineno = find_source_lines(obj)
        if lineno is None:
            self.noinfo('file', oname)
            return

        ofile = find_file(obj)
        # run contents of file through pager starting at line where the object
        # is defined, as long as the file isn't binary and is actually on the
        # filesystem.
        if ofile.endswith(('.so', '.dll', '.pyd')):
            print('File %r is binary, not printing.' % ofile)
        elif not os.path.isfile(ofile):
            print('File %r does not exist, not printing.' % ofile)
        else:
            # Print only text files, not extension binaries.  Note that
            # getsourcelines returns lineno with 1-offset and page() uses
            # 0-offset, so we must adjust.
            page.page(self.format(openpy.read_py_file(ofile, skip_encoding_cookie=False)), lineno - 1)
项目:caproto    作者:NSLS-II    | 项目源码 | 文件源码
def generate_asv_metadata(self, function_to_benchmark, start_dt, end_dt):
        'Generate metadata for asv from pytest_benchmark information'
        callspec = self.node.callspec
        params = OrderedDict(
            (arg, callspec.params[arg])
            for arg in callspec.metafunc.funcargnames
            if arg in callspec.params
        )

        code = ''.join(inspect.getsourcelines(function_to_benchmark)[0])

        if params:
            name, _, param_string = self.name.partition('[')
            param_string = param_string.rstrip(']')
        else:
            name, param_string = self.name, ''

        return dict(start_dt=start_dt,
                    end_dt=end_dt,
                    code=code,
                    params=params,
                    name=name,
                    param_string=param_string,
                    )
项目:NNBuilder    作者:aeloyq    | 项目源码 | 文件源码
def linkcode_resolve(domain, info):
    def find_source():
        obj = sys.modules[info['module']]
        for part in info['fullname'].split('.'):
            obj = getattr(obj, part)
        import inspect
        import os
        fn = inspect.getsourcefile(obj)
        fn = os.path.relpath(fn, start=os.path.dirname(nnbuilder.__file__))
        source, lineno = inspect.getsourcelines(obj)
        return fn, lineno, lineno + len(source) - 1

    if domain != 'py' or not info['module']:
        return None
    try:
        filename = 'nnbuilder/%s#L%d-L%d' % find_source()
    except Exception:
        filename = info['module'].replace('.', '/') + '.py'
    return "https://github.com/aeloyq/NNBuilder/tree/master/%s" % (filename)
项目:ml-utils    作者:LinxiFan    | 项目源码 | 文件源码
def interpret_fraction(f):
    assert inspect.isfunction(f)
    members = dict(inspect.getmembers(f))
    global_dict = members["__globals__"]
    source_filename = inspect.getsourcefile(f)
    f_ast = ast.parse(inspect.getsource(f))
    _, starting_line = inspect.getsourcelines(f)
    # ast_demo.increment_lineno(f_ast, starting_line - 1)
    # print("AST:", ast_demo.dump(f_ast))
    visitor = FractionInterpreter()
    new_ast = visitor.visit(f_ast)
    # print(ast_demo.dump(new_ast))
    ast.fix_missing_locations(new_ast)
    co = compile(new_ast, '<ast_demo>', 'exec')
    fake_locals = {}
    # exec will define the new function into fake_locals scope
    # this is to avoid conflict with vars in the real locals()
    # https://stackoverflow.com/questions/24733831/using-a-function-defined-in-an-execed-string-in-python-3
    exec(co, None, fake_locals)
    # new_f = locals()[visitor._new_func_name(f.__name__)]
    return fake_locals[f.__name__]
项目:ml-utils    作者:LinxiFan    | 项目源码 | 文件源码
def mess_control(f, *, debug=0):
    assert inspect.isfunction(f)
    members = dict(inspect.getmembers(f))
    global_dict = members["__globals__"]
    source_filename = inspect.getsourcefile(f)
    f_ast = ast.parse(inspect.getsource(f))
    _, starting_line = inspect.getsourcelines(f)
    # ast_demo.increment_lineno(f_ast, starting_line - 1)
    if debug:
        print("AST:", ast.dump(f_ast))
    visitor = ControlMess()
    new_ast = visitor.visit(f_ast)
    if debug:
        print('NEW AST:', ast.dump(new_ast))
    ast.fix_missing_locations(new_ast)
    co = compile(new_ast, '<ast_demo>', 'exec')
    fake_locals = {}
    # exec will define the new function into fake_locals scope
    # this is to avoid conflict with vars in the real locals()
    # https://stackoverflow.com/questions/24733831/using-a-function-defined-in-an-execed-string-in-python-3
    exec(co, None, fake_locals)
    # new_f = locals()[visitor._new_func_name(f.__name__)]
    return fake_locals[f.__name__]
项目:Chiaki-Nanami    作者:Ikusaba-san    | 项目源码 | 文件源码
def github_source(self, ctx, *, command: BotCommand = None):
        """Displays the github link for the source code of a particular command.

        Keep in mind that although this is less spammy. It may be inaccurate or
        even in a completely different place if last-minute changes were applied
        to the code and weren't pushed to GitHub.

        If you truly want the most accurate code, use `{prefix}source`.
        """
        source_url = f'https://github.com/Ikusaba-san/Chiaki-Nanami/tree/dev'
        if command is None: 
            return await ctx.send(source_url)

        src = command.callback.__code__
        lines, firstlineno = inspect.getsourcelines(src)
        lastline = firstlineno + len(lines) - 1
        # We don't use the built-in commands so we can eliminate this branch
        location = os.path.relpath(src.co_filename).replace('\\', '/')

        url = f'<{source_url}/{location}#L{firstlineno}-L{lastline}>'
        await ctx.send(url)
项目:Chiaki-Nanami    作者:Ikusaba-san    | 项目源码 | 文件源码
def source(self, ctx, *, command: BotCommand):
        """Displays the source code for a particular command.

        There is a per-user, 2 times per 5 seconds cooldown in order to prevent spam.
        """
        paginator = commands.Paginator(prefix='```py')
        for line in inspect.getsourcelines(command.callback)[0]:
            # inspect.getsourcelines returns the lines with the newlines at the
            # end. However, the paginator will add it's own newlines when joining
            # up the lines. We don't want to have double lines. So we have to
            # strip off the ends.
            #
            # Also, because we prefix each page with a code block (```), we need
            # to make sure that other triple-backticks don't prematurely end the
            # block.
            paginator.add_line(line.rstrip().replace('`', '\u200b`'))

        for p in paginator.pages:
            await ctx.send(p)

    # Credits to Reina
项目:splunk_ta_ps4_f1_2016    作者:jonathanvarley    | 项目源码 | 文件源码
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
项目:SRL-Python    作者:SimpleRegex    | 项目源码 | 文件源码
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
项目:TA-SyncKVStore    作者:georgestarcher    | 项目源码 | 文件源码
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
项目:cb-defense-splunk-app    作者:carbonblack    | 项目源码 | 文件源码
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
项目:SpaghettIDE    作者:Bleu-royal    | 项目源码 | 文件源码
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
项目:djaio    作者:Sberned    | 项目源码 | 文件源码
def _get_linepath(item):
    _filename = inspect.getfile(item)
    _source = open(_filename).readlines()
    lineno = _source.index(inspect.getsourcelines(item)[0][0]) + 1
    return '{}:{}'.format(_filename, lineno)
项目:deep-residual-networks-pyfunt    作者:dnlcrl    | 项目源码 | 文件源码
def print_infos(solver):
    print('Model: \n%s' % solver.model)

    print('Solver: \n%s' % solver)

    print('Data Augmentation Function: \n')
    print(''.join(['\t' + i for i in inspect.getsourcelines(data_augm)[0]]))
    print('Custom Weight Decay Update Rule: \n')
    print(''.join(['\t' + i for i in inspect.getsourcelines(custom_update_decay)[0]]))
项目:Harmonbot    作者:Harmon758    | 项目源码 | 文件源码
def source(self, command : str = None):
        '''
        Displays my full source code or for a specific command
        To display the source code of a subcommand you have to separate it by
        periods, e.g. tag.create for the create subcommand of the tag command
        Based on [R. Danny](https://github.com/Rapptz/RoboDanny)'s source command
        '''
        source_url = "https://github.com/Harmon758/Harmonbot"
        if command is None:
            await self.bot.embed_reply(source_url)
            return
        code_path = command.split('.')
        obj = self.bot
        for cmd in code_path:
            try:
                obj = obj.get_command(cmd)
                if obj is None:
                    await self.bot.embed_reply("Could not find the command " + cmd)
                    return
            except AttributeError:
                await self.bot.embed_reply("{0.name} command has no subcommands".format(obj))
                return
        # since we found the command we're looking for, presumably anyway, let's
        # try to access the code itself
        src = obj.callback.__code__
        lines, firstlineno = inspect.getsourcelines(src)
        ## if not obj.callback.__module__.startswith("discord"):
        # not a built-in command
        location = os.path.relpath(src.co_filename).replace('\\', '/')
        ## else:
        ##  location = obj.callback.__module__.replace('.', '/') + ".py"
        ##  source_url = "https://github.com/Rapptz/discord.py"
        final_url = '<{}/blob/master/Discord/{}#L{}-L{}>'.format(source_url, location, firstlineno, firstlineno + len(lines) - 1)
        await self.bot.embed_reply(final_url)
项目:catalearn    作者:Catalearn    | 项目源码 | 文件源码
def get_source_code(func):
    source_lines = inspect.getsourcelines(func)[0]
    source_lines = format(source_lines)
    if source_lines[0][0] == '@':
        # if the first line is a decorator, remove it
        source_lines = source_lines[1:]  
    source = ''.join(source_lines)
    return source
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def formatrepr(self):
        tblines = []
        addline = tblines.append
        stack = [self.request._pyfuncitem.obj]
        stack.extend(map(lambda x: x.func, self.fixturestack))
        msg = self.msg
        if msg is not None:
            # the last fixture raise an error, let's present
            # it at the requesting side
            stack = stack[:-1]
        for function in stack:
            fspath, lineno = getfslineno(function)
            try:
                lines, _ = inspect.getsourcelines(get_real_func(function))
            except (IOError, IndexError, TypeError):
                error_msg = "file %s, line %s: source code not available"
                addline(error_msg % (fspath, lineno+1))
            else:
                addline("file %s, line %s" % (fspath, lineno+1))
                for i, line in enumerate(lines):
                    line = line.rstrip()
                    addline("  " + line)
                    if line.lstrip().startswith('def'):
                        break

        if msg is None:
            fm = self.request._fixturemanager
            available = []
            parentid = self.request._pyfuncitem.parent.nodeid
            for name, fixturedefs in fm._arg2fixturedefs.items():
                faclist = list(fm._matchfactories(fixturedefs, parentid))
                if faclist and name not in available:
                    available.append(name)
            msg = "fixture %r not found" % (self.argname,)
            msg += "\n available fixtures: %s" %(", ".join(sorted(available)),)
            msg += "\n use 'pytest --fixtures [testpath]' for help on them."

        return FixtureLookupErrorRepr(fspath, lineno, tblines, msg, self.argname)
项目:cpyparsing    作者:evhub    | 项目源码 | 文件源码
def makeTestSuite():
    import inspect
    suite = TestSuite()
    suite.addTest( PyparsingTestInit() )

    test_case_classes = ParseTestCase.__subclasses__()
    # put classes in order as they are listed in the source code
    test_case_classes.sort(key=lambda cls: inspect.getsourcelines(cls)[1])

    test_case_classes.remove(PyparsingTestInit)
    # test_case_classes.remove(ParseASMLTest)
    test_case_classes.remove(EnablePackratParsing)
    if IRON_PYTHON_ENV:
        test_case_classes.remove(OriginalTextForTest)

    suite.addTests(T() for T in test_case_classes)

    if TEST_USING_PACKRAT:
        # retest using packrat parsing (disable those tests that aren't compatible)
        suite.addTest( EnablePackratParsing() )

        unpackrattables = [ PyparsingTestInit, EnablePackratParsing, RepeaterTest, ]

        # add tests to test suite a second time, to run with packrat parsing
        # (leaving out those that we know wont work with packrat)
        packratTests = [t.__class__() for t in suite._tests
                            if t.__class__ not in unpackrattables]
        suite.addTests( packratTests )

    return suite
项目:Parallel.GAMIT    作者:demiangomez    | 项目源码 | 文件源码
def __get_source(self, func):
        """Fetches source of the function"""
        hashf = hash(func)
        if hashf not in self.__sourcesHM:
            #get lines of the source and adjust indent
            sourcelines = inspect.getsourcelines(func)[0]
            #remove indentation from the first line
            sourcelines[0] = sourcelines[0].lstrip()
            self.__sourcesHM[hashf] = "".join(sourcelines)
        return self.__sourcesHM[hashf]
项目:auger    作者:laffra    | 项目源码 | 文件源码
def dump(self, filename, module, functions):
        self.output_ = []
        self.dump_tests(filename, functions)
        for line in inspect.getsourcelines(module)[0]:
            line = line.replace('\n', '')
            if line.startswith('import '):
                self.imports_.add((line.replace('import ', ''),))
            if line.startswith('from '):
                self.imports_.add(tuple(line.replace('from ', '').replace('import ','').split(' ')))
        return '\n'.join(self.format_imports() + self.output_)
项目:slam    作者:miguelgrinberg    | 项目源码 | 文件源码
def test_generate_lambda_handler(self):
        cli._generate_lambda_handler(
            {'function': {'module': 'my_module', 'app': 'my_app'}},
            output='_slam.yaml')
        with open('_slam.yaml') as f:
            handler = f.read()
        os.remove('_slam.yaml')
        self.assertIn('from my_module import my_app', handler)
        self.assertIn(''.join(inspect.getsourcelines(
            cli._run_lambda_function)[0][1:]), handler)
项目:devsecops-example-helloworld    作者:boozallen    | 项目源码 | 文件源码
def testAssertWarnsRegexContext(self):
        # Same as above, but with assertWarnsRegex as a context manager
        def _runtime_warn(msg):
            warnings.warn(msg, RuntimeWarning)
        _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1]
        with self.assertWarnsRegex(RuntimeWarning, "o+") as cm:
            _runtime_warn("foox")
        self.assertIsInstance(cm.warning, RuntimeWarning)
        self.assertEqual(cm.warning.args[0], "foox")
        self.assertIn("_test_unittest2_with.py", cm.filename)
        self.assertEqual(cm.lineno, _runtime_warn_lineno + 1)
        # Failure when no warning is triggered
        with self.assertRaises(self.failureException):
            with self.assertWarnsRegex(RuntimeWarning, "o+"):
                pass
        # Failure when another warning is triggered
        with catch_warnings():
            # Force default filter (in case tests are run with -We)
            warnings.simplefilter("default", RuntimeWarning)
            with self.assertRaises(self.failureException):
                with self.assertWarnsRegex(DeprecationWarning, "o+"):
                    _runtime_warn("foox")
        # Failure when message doesn't match
        with self.assertRaises(self.failureException):
            with self.assertWarnsRegex(RuntimeWarning, "o+"):
                _runtime_warn("barz")
        # A little trickier: we ask RuntimeWarnings to be raised, and then
        # check for some of them.  It is implementation-defined whether
        # non-matching RuntimeWarnings are simply re-raised, or produce a
        # failureException.
        with catch_warnings():
            warnings.simplefilter("error", RuntimeWarning)
            with self.assertRaises((RuntimeWarning, self.failureException)):
                with self.assertWarnsRegex(RuntimeWarning, "o+"):
                    _runtime_warn("barz")
项目:triage    作者:dssg    | 项目源码 | 文件源码
def get_line_no(self, obj):
        """Gets the source line number of this object. None if `obj` code cannot be found.
        """
        try:
            lineno = getsourcelines(obj)[1]
        except:
            # no code found
            lineno = None
        return lineno
项目:keras-text    作者:raghakot    | 项目源码 | 文件源码
def get_line_no(self, obj):
        """Gets the source line number of this object. None if `obj` code cannot be found.
        """
        try:
            lineno = getsourcelines(obj)[1]
        except:
            # no code found
            lineno = None
        return lineno
项目:PyFunt    作者:dnlcrl    | 项目源码 | 文件源码
def print_infos(solver):
    print('Model: \n%s' % solver.model)

    print('Solver: \n%s' % solver)

    print('Data Augmentation Function: \n')
    print(''.join(['\t' + i for i in inspect.getsourcelines(data_augm)[0]]))
    print('Custom Weight Decay Update Rule: \n')
    print(''.join(['\t' + i for i in inspect.getsourcelines(custom_update_decay)[0]]))
项目:keras    作者:GeekLiB    | 项目源码 | 文件源码
def class_to_source_link(cls):
    module_name = cls.__module__
    assert module_name[:6] == 'keras.'
    path = module_name.replace('.', '/')
    path += '.py'
    line = inspect.getsourcelines(cls)[-1]
    link = 'https://github.com/fchollet/keras/blob/master/' + path + '#L' + str(line)
    return '[[source]](' + link + ')'
项目:cupy    作者:cupy    | 项目源码 | 文件源码
def linkcode_resolve(domain, info):
    if domain != 'py' or not info['module']:
        return None

    rtd_version = os.environ.get('READTHEDOCS_VERSION')
    if rtd_version == 'latest':
        tag = 'master'
    else:
        tag = 'v{}'.format(__version__)

    # Import the object from module path
    obj = _import_object_from_name(info['module'], info['fullname'])

    # If it's not defined in the internal module, return None.
    mod = inspect.getmodule(obj)
    if mod is None:
        return None
    if not (mod.__name__ == 'cupy' or mod.__name__.startswith('cupy.')):
        return None

    # Get the source file name and line number at which obj is defined.
    try:
        filename = inspect.getsourcefile(obj)
    except TypeError:
        # obj is not a module, class, function, ..etc.
        return None

    # inspect can return None for cython objects
    if filename is None:
        return None

    # Get the source line number
    _, linenum = inspect.getsourcelines(obj)
    assert isinstance(linenum, six.integer_types)

    filename = os.path.realpath(filename)
    relpath = _get_source_relative_path(filename)

    return 'https://github.com/cupy/cupy/blob/{}/{}#L{}'.format(
        tag, relpath, linenum)
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def validate_module(self, module):
        lines, linen = inspect.getsourcelines(module)

        fre = re.compile(r'\s*def\s+(t_[a-zA-Z_0-9]*)\(')
        sre = re.compile(r'\s*(t_[a-zA-Z_0-9]*)\s*=')

        counthash = {}
        linen += 1
        for line in lines:
            m = fre.match(line)
            if not m:
                m = sre.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.error('%s:%d: Rule %s redefined. Previously defined on line %d', filename, linen, name, prev)
                    self.error = True
            linen += 1

# -----------------------------------------------------------------------------
# lex(module)
#
# Build all of the regular expression rules from definitions in the supplied module
# -----------------------------------------------------------------------------