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)
python类getsourcelines()的实例源码
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
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
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')
python_filter_generator.py 文件源码
项目:ParaViewGeophysics
作者: banesullivan
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
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
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))
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
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
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__'))
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)
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))
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))
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)
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)
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)
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)
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))
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
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
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:
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
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)
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,
)
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)
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__]
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__]
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)
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
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
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