def find_source_lines(obj): """Find the line number in a file where an object was defined. This is essentially a robust wrapper around `inspect.getsourcelines`. Returns None if no file can be found. Parameters ---------- obj : any Python object Returns ------- lineno : int The line number where the object definition starts. """ # get source if obj was decorated with @decorator if safe_hasattr(obj, "__wrapped__"): obj = obj.__wrapped__ try: try: lineno = inspect.getsourcelines(obj)[1] except TypeError: # For instances, try the class object like getsource() does if hasattr(obj, "__class__"): lineno = inspect.getsourcelines(obj.__class__)[1] else: lineno = None except: # pylint:disable=bare-except return None return lineno
def _load_functions(self): """ Loads the run-/build-/init-functions """ import os import inspect from importlib.machinery import SourceFileLoader if self.definition.get('is_tensorrt_engine'): self.__initialized = False self._initfunction = self.tensorrt_init self._flowfunction = self.tensorrt_run self.engine_file = self.definition['path'] elif self.definition.get('is_autogenerated'): self.__initialized = True self._initfunction = lambda x, y, z: None self._flowfunction = self.worldadapter_flowfunction else: sourcefile = self.definition['path'] modulename = 'nodetypes.' + self.definition['category'].replace('/', '.') + os.path.basename(sourcefile)[:-3] module = SourceFileLoader(modulename, sourcefile).load_module() if self.definition.get('init_function_name'): self._initfunction = getattr(module, self.definition['init_function_name']) self.__initialized = False else: self._initfunction = lambda x, y, z: None self.__initialized = True if self.implementation == 'python': self._flowfunction = getattr(module, self.definition['run_function_name']) self.line_number = inspect.getsourcelines(self._flowfunction)[1] else: self._buildfunction = getattr(module, self.definition['build_function_name']) self.line_number = inspect.getsourcelines(self._buildfunction)[1]
def _get_custom_pkg_info(name, fn): """Retrieve information about the installed package from the install function. """ vals = dict((k, v) for k, v in inspect.getmembers(fn)) code = inspect.getsourcelines(fn) if vals["__name__"] == "decorator": fn = [x for x in fn.func_closure if not isinstance(x.cell_contents, str)][0].cell_contents vals = dict((k, v) for k, v in inspect.getmembers(fn)) code = inspect.getsourcelines(fn) version = "" for line in (l.strip() for l in code[0]): if line.find("version") >= 0 and line.find(" =") > 0: version = line.split()[-1].replace('"', '').replace("'", "") if version: break doc = vals.get("func_doc", "") descr, homepage = "", "" if doc is not None: descr = doc.split("\n")[0] for line in doc.split("\n"): if line.strip().startswith("http"): homepage = line.strip() return {"name": name.replace("install_", ""), "description": descr, "homepage_uri": homepage, "version": version}
def _analyzeGens(top, absnames): genlist = [] for g in top: if isinstance(g, _UserCode): tree = g elif isinstance(g, (_AlwaysComb, _AlwaysSeq, _Always)): f = g.func s = inspect.getsource(f) s = _dedent(s) tree = ast.parse(s) #print ast.dump(tree) tree.sourcefile = inspect.getsourcefile(f) tree.lineoffset = inspect.getsourcelines(f)[1]-1 tree.symdict = f.func_globals.copy() tree.callstack = [] # handle free variables tree.nonlocaldict = {} if f.func_code.co_freevars: for n, c in zip(f.func_code.co_freevars, f.func_closure): obj = _cell_deref(c) if isinstance(g, _AlwaysComb): if not ( isinstance(obj, (int, long, EnumType,_Signal)) or \ _isMem(obj) or _isTupleOfInts(obj) ): info = "File %s, line %s: " % (tree.sourcefile, tree.lineoffset) print type(obj) raise ConversionError(_error.UnsupportedType, n, info) tree.symdict[n] = obj # currently, only intbv as automatic nonlocals (until Python 3.0) if isinstance(obj, intbv): tree.nonlocaldict[n] = obj tree.name = absnames.get(id(g), str(_Label("BLOCK"))).upper() v = _FirstPassVisitor(tree) v.visit(tree) if isinstance(g, _AlwaysComb): v = _AnalyzeAlwaysCombVisitor(tree, g.senslist) elif isinstance(g, _AlwaysSeq): v = _AnalyzeAlwaysSeqVisitor(tree, g.senslist, g.reset, g.sigregs, g.varregs) else: v = _AnalyzeAlwaysDecoVisitor(tree, g.senslist) v.visit(tree) else: # @instance f = g.gen.gi_frame s = inspect.getsource(f) s = _dedent(s) tree = ast.parse(s) # print ast.dump(tree) tree.sourcefile = inspect.getsourcefile(f) tree.lineoffset = inspect.getsourcelines(f)[1]-1 tree.symdict = f.f_globals.copy() tree.symdict.update(f.f_locals) tree.nonlocaldict = {} tree.callstack = [] tree.name = absnames.get(id(g), str(_Label("BLOCK"))).upper() v = _FirstPassVisitor(tree) v.visit(tree) v = _AnalyzeBlockVisitor(tree) v.visit(tree) genlist.append(tree) return genlist
def action(args): qadata = qa_from_csv(qafile) if args.qa_file: print(path.abspath(qafile)) elif args.variables: _, d = qadata.popitem() print('\n'.join(d.keys())) elif args.names: cmpnd_codes = dict(COMPOUND_CODES) writer = csv.writer(sys.stdout, quoting=csv.QUOTE_NONNUMERIC) writer.writerow(['id', 'code', 'name']) for compound_id, d in qadata.items(): writer.writerow([compound_id, cmpnd_codes[compound_id], d['qa_compound']]) elif args.compound_id: try: for k,v in qadata[args.compound_id].items(): print('%s = %s' % (k,v)) except KeyError: print('"%s" is not a valid compound id; try listing compounds using the "-n/--names" option' % args.compound_id) sys.exit(1) elif args.list_calculations: for name, description in all_checks.items(): print '%-20s %s' % (name, description) elif args.show_calculation: fun = getattr(calculations, args.show_calculation) print ''.join(inspect.getsourcelines(fun)[0]) elif args.controls: for row in CONTROL_NAMES: print '%s\t%s' % row elif args.algorithm: print ''.join(inspect.getsourcelines(calculations.results)[0])
def pfile(self,obj,oname=''): """Show the whole file where an object was defined.""" try: try: lineno = inspect.getsourcelines(obj)[1] except TypeError: # For instances, try the class object like getsource() does if hasattr(obj,'__class__'): lineno = inspect.getsourcelines(obj.__class__)[1] # Adjust the inspected object so getabsfile() below works obj = obj.__class__ except: self.noinfo('file',oname) return # We only reach this point if object was successfully queried # run contents of file through pager starting at line # where the object is defined ofile = inspect.getabsfile(obj) 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(open(ofile).read()),lineno-1)
def _extract_methods_info(module): """Extract methods data from a given python modules.""" categories = [('uncategorized', -1)] categories += [ (line.strip().replace(_CATEGORY_TAG, ''), line_number) for line_number, line in enumerate(inspect.getsourcelines(module)[0]) if line.strip().startswith(_CATEGORY_TAG)] methods = [getattr(module, name) for name in dir(module)] methods = [method for method in methods if ( callable(method) and hasattr(method, '__module__') and method.__module__ == module.__name__ and method.__name__ != 'run')] methods_info = [] for method in methods: codelines, line_number = inspect.getsourcelines(method) methods_info.append({ 'name': method.__name__, 'doc': method.__doc__.strip() if method.__doc__ else '', 'codelines': [ line for line in codelines if line.strip() and '"""' not in line], 'method': method, 'category': [ category_name for category_name, category_line_number in categories if line_number > category_line_number][-1], }) return methods_info
def getSchemaForContentType(self): result = OrderedDict() schema = self.context.Schema() field_ids = schema.keys() field_ids.sort() for i in field_ids: field = schema[i] widget = field.widget field_py_file = inspect.getsourcefile(field.__class__) field_py_lineno = inspect.getsourcelines(field.__class__)[1] widget_py_file = inspect.getsourcefile(widget.__class__) widget_py_lineno = inspect.getsourcelines(widget.__class__)[1] label = widget.label condition = widget.getCondition() visibility = widget.visible result[i] = {'field' : field.__class__.__name__, 'field_py_file' : field_py_file, 'field_py_lineno' : field_py_lineno, 'widget' : widget.__class__.__name__, 'widget_py_file' : widget_py_file, 'widget_py_lineno' : widget_py_lineno, 'label' : label, 'condition' : condition, 'visibility' : visibility,} return result
def find_source_lines(obj): """Find the line number in a file where an object was defined. This is essentially a robust wrapper around `inspect.getsourcelines`. Returns None if no file can be found. Parameters ---------- obj : any Python object Returns ------- lineno : int The line number where the object definition starts. """ obj = _get_wrapped(obj) try: try: lineno = inspect.getsourcelines(obj)[1] except TypeError: # For instances, try the class object like getsource() does if hasattr(obj, '__class__'): lineno = inspect.getsourcelines(obj.__class__)[1] else: lineno = None except: return None return lineno
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]]))
def write_user_exception(filename, stream_err_output, num_lines_offset_trace=0): import sys import traceback import inspect (t, v, tb) = sys.exc_info() name = t.__name__ record_error = False if name in ['SyntaxError', 'IndentationError']: syntax_error_values = v.args user_line_number = syntax_error_values[1][1] - num_lines_offset_trace error_message = "%s: %s\n\tFile: %s, line %s column %s\n\t%s" % \ (name, syntax_error_values[0], syntax_error_values[1][0], user_line_number, syntax_error_values[1][2], syntax_error_values[1][3]) else: error_message = "%s: %s\n" % (name, v) user_line_number = None while 1: e_file_name = tb.tb_frame.f_code.co_filename if e_file_name.find(filename) > 0: record_error = True if not record_error: if not tb.tb_next: break tb = tb.tb_next continue line_number = tb.tb_lineno mod = inspect.getmodule(tb) if mod: lines, offset = inspect.getsourcelines(mod) line = lines[line_number - offset - 1] else: #Useful to catch exceptions with an invalid module (like syntax #errors) lines, offset = inspect.getsourcelines(tb.tb_frame) if (line_number - 1) >= len(lines): line = "Unknown Line" else: line = lines[line_number - 1] user_line_number = line_number - num_lines_offset_trace func_name = tb.tb_frame.f_code.co_name error_message += 'File %s, line %s, in %s\n\t%s\n' % \ (e_file_name, user_line_number, func_name, line) if not tb.tb_next: break tb = tb.tb_next if name in ['UnicodeEncodeError']: error_message += "\nTo print a unicode string in your udf use encode('utf-8'). Example: \n\tprint 'Example'.encode('utf-8')" if user_line_number: stream_err_output.write("%s\n" % user_line_number) stream_err_output.write("%s\n" % error_message)
def testcollect( globs, matchfun = lambda x: re.match( 'test', x)): from inspect import getfile, getsourcelines result = [x[ 1] for x in globs.items() if callable( x[ 1]) and matchfun( x[ 0])] result.sort( lambda x, y: cmp( (getfile( x), getsourcelines( x)[ -1]), (getfile( y), getsourcelines( y)[ -1]))) return result
def ok_to_add_method(self, member, parent): if inspect.getsourcefile(member) != inspect.getsourcefile(parent): return False # Use line inspection to work out whether the method is defined on this # klass. Possibly not the best way, but I can't think of another atm. lines, start_line = inspect.getsourcelines(member) parent_lines, parent_start_line = inspect.getsourcelines(parent) if start_line < parent_start_line or start_line > parent_start_line + len(parent_lines): return False return True
def _analyzeGens(top, absnames): genlist = [] for g in top: if isinstance(g, _UserCode): ast = g elif isinstance(g, (_AlwaysComb, _Always)): f = g.func s = inspect.getsource(f) # remove decorators s = re.sub(r"@.*", "", s) s = s.lstrip() ast = compiler.parse(s) # print ast ast.sourcefile = inspect.getsourcefile(f) ast.lineoffset = inspect.getsourcelines(f)[1]-1 ast.symdict = f.func_globals.copy() ast.callstack = [] # handle free variables if f.func_code.co_freevars: for n, c in zip(f.func_code.co_freevars, f.func_closure): obj = _cell_deref(c) if isinstance(g, _AlwaysComb): # print type(obj) assert isinstance(obj, (int, long, Signal)) or \ _isMem(obj) or _isTupleOfInts(obj) ast.symdict[n] = obj ast.name = absnames.get(id(g), str(_Label("BLOCK"))).upper() v = _NotSupportedVisitor(ast) compiler.walk(ast, v) if isinstance(g, _AlwaysComb): v = _AnalyzeAlwaysCombVisitor(ast, g.senslist) else: v = _AnalyzeAlwaysDecoVisitor(ast, g.senslist) compiler.walk(ast, v) else: # @instance f = g.gen.gi_frame s = inspect.getsource(f) # remove decorators s = re.sub(r"@.*", "", s) s = s.lstrip() ast = compiler.parse(s) # print ast ast.sourcefile = inspect.getsourcefile(f) ast.lineoffset = inspect.getsourcelines(f)[1]-1 ast.symdict = f.f_globals.copy() ast.symdict.update(f.f_locals) ast.callstack = [] ast.name = absnames.get(id(g), str(_Label("BLOCK"))).upper() v = _NotSupportedVisitor(ast) compiler.walk(ast, v) v = _AnalyzeBlockVisitor(ast) compiler.walk(ast, v) genlist.append(ast) return genlist
def source(self): fsource = inspect.getsourcelines(self.f)[0][2:] fsource = textwrap.dedent(''.join(fsource)) # find other functions defined in same module (e.g. plotting helpers) for k, v in self.f.func_globals.iteritems(): if hasattr(v, '__module__') and v.__module__ == self.f.__module__: # and if they are mentioned in the source code, list them, too. if k in fsource: fsource = textwrap.dedent(''.join(inspect.getsourcelines(v)[0])) + '\n' + fsource preamble = '# Note that this code might not run if you directly copy and paste it:\n# - Not all import statements are shown here\n# - `self` is a reference to a test instance, which allows access to\n# parameters such as the directory where the test is run etc.\n\n' return preamble + fsource
def checksum(self): if not self._checksum: m = hashlib.sha1() for ia in full_traverse(self.input_args): if isinstance(ia, target.Target): m.update(ia.checksum()) else: m.update(joblib.hash(ia).encode()) m.update('\n'.join(inspect.getsourcelines(self.user_outputs)[0]).encode('utf-8')) m.update('\n'.join(inspect.getsourcelines(self.user_run)[0]).encode('utf-8')) self._checksum = m.hexdigest() return self._checksum
def wrapper(*args, **kwargs): if self._debug: trace_obj = { 'name': func.__name__, 'documentation': inspect.getdoc(func), 'file': inspect.getfile(func), 'source_code': inspect.getsourcelines(func)[0], 'source_line': inspect.getsourcelines(func)[1], 'module': inspect.getmodule(func).__name__, 'call_timestamp': time.time() } self._trace.append(trace_obj) return func(*args, **kwargs)
def registrator(cfunc): """Register function as handler for pattern in corresponded map. Also, bind default func arguments. """ # Extract original function. if hasattr(cfunc, '__decorated__'): func = getattr(cfunc, '__decorated__') else: func = cfunc # Get the names and default values of a function's arguments. func_args, func_varargs, func_varkw, func_defaults = inspect.getargspec(func) func_bindings = {} if func_defaults is not None: # Populate func bindings with default function values. def assign_default_values(tree, values): for el in tree: value = values.next() if type(el) == types.ListType: assign_default_values(el, iter(value)) else: func_bindings[el] = Value(value) assign_default_values(func_args[-len(func_defaults):], iter(func_defaults)) # Merge func bindings with step definition bindings. func_bindings.update(bindings) # Show warnings. if self.verbose: if func_varargs in func_bindings: sys.stderr.write( "%s:%d : " "WARNING! You've used name of positional argument for bindings: '*%s'." "Did you really want this?\n" \ % (inspect.getsourcefile(func), inspect.getsourcelines()[1], func_varargs) ) if func_varkw in func_bindings: sys.stderr.write( "%s:%d : " "WARNING! You've used name of keyword argument for bindings: '**%s'." "Did you really want this?\n" \ % (inspect.getsourcefile(func), inspect.getsourcelines()[1], func_varargs) ) patterns[pattern] = (cfunc, func_args, func_bindings) return cfunc
def linkcode_resolve(domain, info): """A simple function to find matching source code.""" module_name = info["module"] fullname = info["fullname"] attribute_name = fullname.split(".")[-1] base_url = "https://github.com/JelteF/PyLaTeX/" if "+" in version: commit_hash = version.split(".")[-1][1:] base_url += "tree/%s/" % commit_hash else: base_url += "blob/v%s/" % version filename = module_name.replace(".", "/") + ".py" module = sys.modules.get(module_name) # Get the actual object try: actual_object = module for obj in fullname.split("."): parent = actual_object actual_object = getattr(actual_object, obj) except AttributeError: return None # Fix property methods by using their getter method if isinstance(actual_object, property): actual_object = actual_object.fget # Try to get the linenumber of the object try: source, start_line = inspect.getsourcelines(actual_object) except TypeError: # If it can not be found, try to find it anyway in the parents its # source code parent_source, parent_start_line = inspect.getsourcelines(parent) for i, line in enumerate(parent_source): if line.strip().startswith(attribute_name): start_line = parent_start_line + i end_line = start_line break else: return None else: end_line = start_line + len(source) - 1 line_anchor = "#L%d-L%d" % (start_line, end_line) return base_url + filename + line_anchor
def linkcode_resolve(domain, info): """A simple function to find matching source code.""" module_name = info['module'] fullname = info['fullname'] attribute_name = fullname.split('.')[-1] base_url = 'https://github.com/JelteF/PyLaTeX/blob/' if release.endswith('-dev'): base_url += 'master/' else: base_url += 'v' + version + '/' filename = module_name.replace('.', '/') + '.py' module = sys.modules.get(module_name) # Get the actual object try: actual_object = module for obj in fullname.split('.'): parent = actual_object actual_object = getattr(actual_object, obj) except AttributeError: return None # Fix property methods by using their getter method if isinstance(actual_object, property): actual_object = actual_object.fget # Try to get the linenumber of the object try: source, start_line = inspect.getsourcelines(actual_object) except TypeError: # If it can not be found, try to find it anyway in the parents its # source code parent_source, parent_start_line = inspect.getsourcelines(parent) for i, line in enumerate(parent_source): if line.strip().startswith(attribute_name): start_line = parent_start_line + i end_line = start_line break else: return None else: end_line = start_line + len(source) - 1 line_anchor = '#L%d-L%d' % (start_line, end_line) return base_url + filename + line_anchor
def source(self): import inspect import itertools lines = inspect.getsourcelines(self.__class__)[0] lines = [x for x in itertools.takewhile(lambda x: not x.strip().startswith('template'), lines)] return ''.join(lines)
def _template_decorator(self, func): """Registers template as expected by _create_template_function. The template data consists of: - the function object as it comes from the sandbox evaluation of the template declaration. - its code, modified as described in the comments of this method. - the path of the file containing the template definition. """ if not inspect.isfunction(func): raise Exception('`template` is a function decorator. You must ' 'use it as `@template` preceding a function declaration.') name = func.func_name if name in self.templates: raise KeyError( 'A template named "%s" was already declared in %s.' % (name, self.templates[name][2])) if name.islower() or name.isupper() or name[0].islower(): raise NameError('Template function names must be CamelCase.') lines, firstlineno = inspect.getsourcelines(func) first_op = None generator = tokenize.generate_tokens(iter(lines).next) # Find the first indent token in the source of this template function, # which corresponds to the beginning of the function body. for typ, s, begin, end, line in generator: if typ == tokenize.OP: first_op = True if first_op and typ == tokenize.INDENT: break if typ != tokenize.INDENT: # This should never happen. raise Exception('Could not find the first line of the template %s' % func.func_name) # The code of the template in moz.build looks like this: # m def Foo(args): # n FOO = 'bar' # n+1 (...) # # where, # - m is firstlineno - 1, # - n is usually m + 1, but in case the function signature takes more # lines, is really m + begin[0] - 1 # # We want that to be replaced with: # m if True: # n FOO = 'bar' # n+1 (...) # # (this is simpler than trying to deindent the function body) # So we need to prepend with n - 1 newlines so that line numbers # are unchanged. code = '\n' * (firstlineno + begin[0] - 3) + 'if True:\n' code += ''.join(lines[begin[0] - 1:]) self.templates[name] = func, code, self._context.current_path
def view_info(self, discr, context, request, content): introspector = request.registry.introspector template = 'unknown' intr = introspector.get('templates', discr) if intr is not None: # pragma: no cover template = intr['name'] intr = introspector.get('views', discr) if intr is None: # pragma: no cover return content view = intr['callable'] data = OrderedDict( (('name', intr['name']), ('route-name', intr['route_name']), ('view-factory', '%s.%s'%(view.__module__, view.__name__)), ('python-module', inspect.getmodule(view).__name__), ('python-module-location', inspect.getsourcefile(view)), ('python-module-line', inspect.getsourcelines(view)[-1]), ('renderer', template), ('context', '%s.%s'%(context.__class__.__module__, context.__class__.__name__)), ('context-path', request.resource_url(context)), )) content = text_('\n<!-- view:\n%s \n-->\n'\ '<div style="border: 2px solid red">%s</div>')%( json.dumps(data, indent=2), content) return content
def get_function_attributes(func): ''' Extract the function attributes from a Python function or object with *py_func* attribute, such as CPUOverloaded. Returns an instance of FunctionAttributes. ''' if hasattr(func, 'py_func'): func = func.py_func # This is a Overload object name, filename, lineno = DEFAULT_FUNCTION_ATTRIBUTES try: name = func.__name__ except AttributeError: pass # this "function" object isn't really a function try: possible_filename = inspect.getsourcefile(func) # Sometimes getsourcefile returns null if possible_filename is not None: filename = possible_filename except TypeError: pass # built-in function, or other object unsupported by inspect try: lines, lineno = inspect.getsourcelines(func) except (IOError, TypeError): pass # unable to read source code for function return FunctionAttributes(name, filename, lineno)
def _try_waiter (self, entry): """ Tries a waiting callback. Calls the callback, removes from _waiters, and returns True if all are satisfied. """ if entry not in self._waiters: # Already handled return callback, name, components, args_, kw_ = entry for c in components: if not self.hasComponent(c): return False self._waiters.remove(entry) try: if callback is not None: callback(*args_,**kw_) except: import traceback msg = "Exception while trying to notify " + name import inspect try: msg += " at " + inspect.getfile(callback) msg += ":" + str(inspect.getsourcelines(callback)[1]) except: pass log.exception(msg) return True
def _get_fileinfo(self, test): filename = inspect.getsourcefile(test.method()) linenum = inspect.getsourcelines(test.method()) try: # Fixme return "%s(%d)"% (os.path.basename(filename), linenum[1]) except TypeError: return str(test.method)
def uncompile(c): """ uncompile(codeobj) -> [source, filename, mode, flags, firstlineno, privateprefix] """ if c.co_flags & inspect.CO_NESTED or c.co_freevars: raise Unsupported('nested functions not supported') if c.co_name == '<lambda>': raise Unsupported('lambda functions not supported') if c.co_filename == '<string>': raise Unsupported('code without source file not supported') filename = inspect.getfile(c) try: lines, firstlineno = inspect.getsourcelines(c) except IOError: raise NoSource('source code not available') source = ''.join(lines) # __X is mangled to _ClassName__X in methods. Find this prefix: privateprefix = None for name in c.co_names: m = re.match('^(_[A-Za-z][A-Za-z0-9_]*)__.*$', name) if m: privateprefix = m.group(1) break return [source, filename, 'exec', c.co_flags & PyCF_MASK, firstlineno, privateprefix]
def coderef_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): text = utils.unescape(text) has_explicit_title, title, target = split_explicit_title(text) try: modname, name = target.rsplit('.', 1) except ValueError: raise Exception("Don't know how to import name %s" % target) mod = import_module(modname) try: value = getattr(mod, name, None) except AttributeError: raise Exception("No name '%s' in module '%s'" % (name, modname)) #~ raise Exception("20130908 %s " % lines) if isinstance(value, type): if value.__module__ != modname: raise Exception("20130908 %r != %r" % (value.__module__, modname)) url = srcref(mod) lines, line_no = inspect.getsourcelines(value) if line_no: url += "#" + str(line_no) if not has_explicit_title: pass pnode = nodes.reference(title, title, internal=False, refuri=url) return [pnode], []
def event_filter_to_descriptive_string(self, event_filter): """Find the source code of the callable or pretty-print the dictionary""" message = '' if callable(event_filter): file_name = '(unknown)' try: file_name = inspect.getsourcefile(event_filter) except TypeError: pass try: list_of_source_lines, line_no = inspect.getsourcelines(event_filter) except IOError: pass else: message = '{file_name}:{line_no}\n{hr}\n{event_filter}\n{hr}'.format( event_filter=''.join(list_of_source_lines).rstrip(), file_name=file_name, line_no=line_no, hr='-' * 20, ) if not message: message = '{hr}\n{event_filter}\n{hr}'.format( event_filter=pprint.pformat(event_filter), hr='-' * 20, ) return message
def dump_garbage(source=False): """ show us what's the garbage about """ import inspect, gc print "\nCollecting GARBAGE:" gc.collect() print "\nCollecting GARBAGE:" gc.collect() print "\nGARBAGE OBJECTS:" for x in gc.garbage: try: s = str(x) if len(s) > 80: s = "%s..." % s[:80] print "::", s print " type:", type(x) print " referrers:", len(gc.get_referrers(x)) print " is class:", inspect.isclass(type(x)) print " module:", inspect.getmodule(x) if source: lines, line_num = inspect.getsourcelines(type(x)) print " line num:", line_num for l in lines: print " line:", l.rstrip("\n") except: pass
def linkcode_resolve(domain, info): """Determine the URL corresponding to a Python object. Parameters ---------- domain : str Only useful when 'py'. info : dict With keys "module" and "fullname". Returns ------- url : str The code URL. Notes ----- This has been adapted to deal with our "verbose" decorator. Adapted from SciPy (doc/source/conf.py). """ import mne if domain != 'py': return None modname = info['module'] fullname = info['fullname'] submod = sys.modules.get(modname) if submod is None: return None obj = submod for part in fullname.split('.'): try: obj = getattr(obj, part) except Exception: return None # deal with our decorators properly while hasattr(obj, '__wrapped__'): obj = obj.__wrapped__ try: fn = inspect.getsourcefile(obj) except Exception: fn = None if not fn: try: fn = inspect.getsourcefile(sys.modules[obj.__module__]) except Exception: fn = None if not fn: return None fn = op.relpath(fn, start=op.dirname(mne.__file__)) fn = '/'.join(op.normpath(fn).split(os.sep)) # in case on Windows try: source, lineno = inspect.getsourcelines(obj) except Exception: lineno = None if lineno: linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) else: linespec = "" if 'dev' in mne.__version__: kind = 'master' else: kind = 'maint/%s' % ('.'.join(mne.__version__.split('.')[:2])) return "http://github.com/mne-tools/mne-python/blob/%s/mne/%s%s" % ( # noqa kind, fn, linespec)
def magic_edit(self, parameter_s='', last_call=['', '']): """Bring up an editor and execute the resulting code. Usage: %edit [options] [args] %edit runs IPython's editor hook. The default version of this hook is set to call the __IPYTHON__.rc.editor command. This is read from your environment variable $EDITOR. If this isn't found, it will default to vi under Linux/Unix and to notepad under Windows. See the end of this docstring for how to change the editor hook. You can also set the value of this editor via the command line option '-editor' or in your ipythonrc file. This is useful if you wish to use specifically for IPython an editor different from your typical default (and for Windows users who typically don't set environment variables). This command allows you to conveniently edit multi-line code right in your IPython session. If called without arguments, %edit opens up an empty editor with a temporary file and will execute the contents of this file when you close it (don't forget to save it!). Options: -n <number>: open the editor at a specified line number. By default, the IPython editor hook uses the unix syntax 'editor +N filename', but you can configure this by providing your own modified hook if your favorite editor supports line-number specifications with a different syntax. -p: this will call the editor with the same data as the previous time it was used, regardless of how long ago (in your current session) it was. -r: use 'raw' input. This option only applies to input taken from the user's history. By default, the 'processed' history is used, so that magics are loaded in their transformed version to valid Python. If this option is given, the raw input as typed as the command line is used instead. When you exit the editor, it will be executed by IPython's own processor. -x: do not execute the edited code immediately upon exit. This is mainly useful if you are editing programs which need to be called with command line arguments, which you can then do using %run. Arguments: If arguments are given, the following possibilites exist: - The arguments are numbers or pairs of colon-separated numbers (like 1 4:8 9). These are interpreted as lines of previous input to be loaded into the editor. The syntax is the same of the %macro command. - If the argument doesn't start with a number, it is evaluated as a variable and its contents loaded into the editor. You can thus edit any string which contains python code (including the result of previous edits). - If the argument is the name of an object (other than a string), IPython will try to locate the file where it was defined and open the editor at the point where it is defined. You can use `%edit function` to load an editor exactly at the point where 'function' is defined, edit it and have the file be executed automatically. If the object is a macro (see %macro for details), this opens up your specified editor with a temporary file containing the macro's data. Upon exit, the macro is reloaded with the contents of the file. Note: opening at an exact line is only supported under Unix, and some editors (like kedit and gedit up to Gnome 2.8) do not understand the '+NUMBER' parameter necessary for this feature. Good editors like (X)Emacs, vi, jed, pico and joe all do. - If the argument is not found as a variable, IPython will look for a file with that name (adding .py if necessary) and load it into the editor. It will execute its contents with execfile() when you exit, loading any code in the file into your interactive namespace. After executing your code, %edit will return as output the code you typed in the editor (except when it was an existing file). This way you can reload the code in further invocations of %edit as a variable, via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of the output. Note that %edit is also available through the alias %ed. This is an example of creating a simple function inside the editor and then modifying it. First, start up the editor: In [1]: ed Editing... done. Executing edited code... Out[1]: 'def foo():n print "foo() was defined in an editing session"n' We can then call the function foo(): In [2]: foo() foo() was defined in an editing session Now we edit foo. IPython automatically loads the editor with the (temporary) file where foo() was previously defined: In [3]: ed foo Editing... done. Executing edited code... And if we call foo() again we get the modified version: In [4]: foo() foo() has now been changed! Here is an example of how to edit a code snippet successive times. First we call the editor: In [5]: ed Editing... done. Executing edited code... hello Out[5]: "print 'hello'n" Now we call it again with the previous output (stored in _): In [6]: ed _ Editing... done. Executing edited code... hello world Out[6]: "print 'hello world'n" Now we call it with the output #8 (stored in _8, also as Out[8]): In [7]: ed _8 Editing... done. Executing edited code... hello again Out[7]: "print 'hello again'n" Changing the default editor hook: If you wish to write your own editor hook, you can put it in a configuration file which you load at startup time. The default hook is defined in the IPython.core.hooks module, and you can use that as a starting example for further modifications. That file also has general instructions on how to set a new hook for use once you've defined it.""" # FIXME: This function has become a convoluted mess. It needs a # ground-up rewrite with clean, simple logic. def make_filename(arg): "Make a filename from the given args" try: filename = get_py_filename(arg) except IOError: if args.endswith('.py'): filename = arg else: filename = None return filename # custom exceptions class DataIsObject(Exception): pass opts, args = self.parse_options(parameter_s, 'prn:') # Set a few locals from the options for convenience: opts_p = opts.has_key('p') opts_r = opts.has_key('r') # Default line number value lineno = opts.get('n', None) if lineno is not None: try: lineno = int(lineno) except: warn("The -n argument must be an integer.") return if opts_p: args = '_%s' % last_call[0] if not self.shell.user_ns.has_key(args): args = last_call[1] # use last_call to remember the state of the previous call, but don't # let it be clobbered by successive '-p' calls. try: last_call[0] = self.shell.displayhook.prompt_count if not opts_p: last_call[1] = parameter_s except: pass # by default this is done with temp files, except when the given # arg is a filename use_temp = True data = '' if args[0].isdigit(): # Mode where user specifies ranges of lines, like in %macro. # This means that you can't edit files whose names begin with # numbers this way. Tough. ranges = args.split() data = ''.join(self.extract_input_slices(ranges, opts_r)) elif args.endswith('.py'): filename = make_filename(args) use_temp = False elif args: try: # Load the parameter given as a variable. If not a string, # process it as an object instead (below) #print '*** args',args,'type',type(args) # dbg data = eval(args, self.shell.user_ns) if not isinstance(data, basestring): raise DataIsObject except (NameError, SyntaxError): # given argument is not a variable, try as a filename filename = make_filename(args) if filename is None: warn("Argument given (%s) can't be found as a variable " "or as a filename." % args) return use_temp = False except DataIsObject: # macros have a special edit function if isinstance(data, Macro): self._edit_macro(args, data) return # For objects, try to edit the file where they are defined try: filename = inspect.getabsfile(data) if 'fakemodule' in filename.lower() and inspect.isclass( data): # class created by %edit? Try to find source # by looking for method definitions instead, the # __module__ in those classes is FakeModule. attrs = [getattr(data, aname) for aname in dir(data)] for attr in attrs: if not inspect.ismethod(attr): continue filename = inspect.getabsfile(attr) if filename and 'fakemodule' not in filename.lower( ): # change the attribute to be the edit target instead data = attr break datafile = 1 except TypeError: filename = make_filename(args) datafile = 1 warn('Could not find file where `%s` is defined.\n' 'Opening a file named `%s`' % (args, filename)) # Now, make sure we can actually read the source (if it was in # a temp file it's gone by now). if datafile: try: if lineno is None: lineno = inspect.getsourcelines(data)[1] except IOError: filename = make_filename(args) if filename is None: warn('The file `%s` where `%s` was defined cannot ' 'be read.' % (filename, data)) return use_temp = False if use_temp: filename = self.shell.mktempfile(data) print('IPython will make a temporary file named:', filename) # Make sure we send to the client an absolute path, in case the working # directory of client and kernel don't match filename = os.path.abspath(filename) payload = { 'source': 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic', 'filename': filename, 'line_number': lineno } self.payload_manager.write_payload(payload)
def validate_function(fn: FunctionType, config: Configuration, module_type: ModuleType) -> FunctionValidationResult: """Validates the docstring of a function against its signature. Args: fn (FunctionType): The function to validate. config (Configuration): The configuration to use while validating. module_type (ModuleType): The module from which the function was extracted. Returns: FunctionValidationResult: The result of validating this function. """ log(f"Validating function: {fn}") result = FunctionValidationResult(fn, module_type) doc = inspect.getdoc(fn) if not doc: if config.fail_on_missing_docstring: result.result = ResultType.FAILED result.fail_reason = f"Function does not have a docstring" _, line_number = inspect.getsourcelines(fn) result.range = Range(line_number, line_number, 0, 0) else: result.result = ResultType.NO_DOC return result parser = config.get_parser() summary = parser.get_summary(doc, module_type) if not summary and config.fail_on_missing_summary: result.result = ResultType.FAILED result.fail_reason = f"Function does not have a summary" result.range = __get_docstring_range(fn, module_type, doc) return result sig = inspect.signature(fn) sig_parameters = [ Parameter(name, proxy.annotation) for name, proxy in sig.parameters.items() if name != "self" ] sig_return_type = type( None) if sig.return_annotation is None else sig.return_annotation try: doc_parameters = parser.get_parameters(doc, module_type) doc_return_type = parser.get_return_type(doc, module_type) except ParseException as e: result.result = ResultType.FAILED result.fail_reason = f"Unable to parse docstring: {str(e)}" result.range = __get_docstring_range(fn, module_type, doc) return result # Validate return type if sig_return_type != doc_return_type: result.result = ResultType.FAILED result.fail_reason = f"Return type differ. Expected (from signature) {sig_return_type}, but got (in docs) {doc_return_type}." result.range = __get_docstring_range(fn, module_type, doc) return result # Validate equal number of parameters if len(sig_parameters) != len(doc_parameters): result.result = ResultType.FAILED result.fail_reason = f"Number of arguments differ. Expected (from signature) {len(sig_parameters)} arguments, but found (in docs) {len(doc_parameters)}." result.range = __get_docstring_range(fn, module_type, doc) return result # Validate name and type of function parameters for sigparam, docparam in zip(sig_parameters, doc_parameters): if sigparam.name != docparam.name: result.result = ResultType.FAILED result.fail_reason = f"Argument name differ. Expected (from signature) '{sigparam.name}', but got (in docs) '{docparam.name}'" result.range = __get_docstring_range(fn, module_type, doc) return result # NOTE: Optional[str] == Union[str, None] # True if sigparam.type != docparam.type: result.result = ResultType.FAILED result.fail_reason = f"Argument type differ. Argument '{sigparam.name}' was expected (from signature) to have type '{sigparam.type}', but has (in docs) type '{docparam.type}'" result.range = __get_docstring_range(fn, module_type, doc) return result # Validate exceptions raised if config.fail_on_raises_section: try: sig_exceptions = get_exceptions_raised(fn, module_type) doc_exceptions = parser.get_exceptions_raised(doc) if len(sig_exceptions) != len(doc_exceptions): result.result = ResultType.FAILED result.fail_reason = f"Number of listed raised exceptions does not match actual. Doc: {doc_exceptions}, expected: {sig_exceptions}" result.range = __get_docstring_range(fn, module_type, doc) return result intersection = set(sig_exceptions) - set(doc_exceptions) if len(intersection) > 0: result.result = ResultType.FAILED result.fail_reason = f"Listed raised exceptions does not match actual. Docstring: {doc_exceptions}, expected: {sig_exceptions}" result.range = __get_docstring_range(fn, module_type, doc) return result except ParseException as e: result.result = ResultType.FAILED result.fail_reason = f"Unable to parse docstring: {str(e)}" result.range = __get_docstring_range(fn, module_type, doc) return result result.result = ResultType.OK return result
def run(self, opts): from calibre.utils.serialize import msgpack_dumps scripts = {} for x in ('console', 'gui'): for name in basenames[x]: if name in ('calibre-complete', 'calibre_postinstall'): continue scripts[name] = x dest = self.j(self.RESOURCES, 'scripts.calibre_msgpack') if self.newer(dest, self.j(self.SRC, 'calibre', 'linux.py')): self.info('\tCreating ' + self.b(dest)) with open(dest, 'wb') as f: f.write(msgpack_dumps(scripts)) from calibre.web.feeds.recipes.collection import \ serialize_builtin_recipes, iterate_over_builtin_recipe_files files = [x[1] for x in iterate_over_builtin_recipe_files()] dest = self.j(self.RESOURCES, 'builtin_recipes.xml') if self.newer(dest, files): self.info('\tCreating builtin_recipes.xml') xml = serialize_builtin_recipes() with open(dest, 'wb') as f: f.write(xml) recipe_icon_dir = self.a( self.j(self.RESOURCES, '..', 'recipes', 'icons')) dest = os.path.splitext(dest)[0] + '.zip' files += glob.glob(self.j(recipe_icon_dir, '*.png')) if self.newer(dest, files): self.info('\tCreating builtin_recipes.zip') with zipfile.ZipFile(dest, 'w', zipfile.ZIP_STORED) as zf: for n in sorted(files, key=self.b): with open(n, 'rb') as f: zf.writestr(self.b(n), f.read()) dest = self.j(self.RESOURCES, 'ebook-convert-complete.calibre_msgpack') files = [] for x in os.walk(self.j(self.SRC, 'calibre')): for f in x[-1]: if f.endswith('.py'): files.append(self.j(x[0], f)) if self.newer(dest, files): self.info('\tCreating ' + self.b(dest)) complete = {} from calibre.ebooks.conversion.plumber import supported_input_formats complete['input_fmts'] = set(supported_input_formats()) from calibre.web.feeds.recipes.collection import get_builtin_recipe_titles complete['input_recipes'] = [ t + '.recipe ' for t in get_builtin_recipe_titles() ] from calibre.customize.ui import available_output_formats complete['output'] = set(available_output_formats()) from calibre.ebooks.conversion.cli import create_option_parser from calibre.utils.logging import Log log = Log() # log.outputs = [] for inf in supported_input_formats(): if inf in ('zip', 'rar', 'oebzip'): continue for ouf in available_output_formats(): of = ouf if ouf == 'oeb' else 'dummy.' + ouf p = create_option_parser(('ec', 'dummy1.' + inf, of, '-h'), log)[0] complete[(inf, ouf)] = [ x + ' ' for x in get_opts_from_parser(p) ] with open(dest, 'wb') as f: f.write(msgpack_dumps(complete)) self.info('\tCreating template-functions.json') dest = self.j(self.RESOURCES, 'template-functions.json') function_dict = {} import inspect from calibre.utils.formatter_functions import formatter_functions for obj in formatter_functions().get_builtins().values(): eval_func = inspect.getmembers( obj, lambda x: inspect.ismethod(x) and x.__name__ == 'evaluate') try: lines = [ l[4:] for l in inspect.getsourcelines(eval_func[0][1])[0] ] except: continue lines = ''.join(lines) function_dict[obj.name] = lines import json json.dump(function_dict, open(dest, 'wb'), indent=4) self.info('\tCreating editor-functions.json') dest = self.j(self.RESOURCES, 'editor-functions.json') function_dict = {} from calibre.gui2.tweak_book.function_replace import builtin_functions for func in builtin_functions(): try: src = ''.join(inspect.getsourcelines(func)[0][1:]) except Exception: continue src = src.replace('def ' + func.func_name, 'def replace') imports = [ 'from %s import %s' % (x.__module__, x.__name__) for x in func.imports ] if imports: src = '\n'.join(imports) + '\n\n' + src function_dict[func.name] = src json.dump(function_dict, open(dest, 'wb'), indent=4) self.info('\tCreating user-manual-translation-stats.json') d = {} for lc, stats in iteritems( json.load( open( self.j(self.d(self.SRC), 'manual', 'locale', 'completed.json')))): total = sum(itervalues(stats)) d[lc] = stats['translated'] / float(total) json.dump(d, open( self.j(self.RESOURCES, 'user-manual-translation-stats.json'), 'wb'), indent=4)
def get_ln(fn): info = inspect.getsourcelines(fn) return info[1]
def test_controller_class_names(self): """This function checks that all controller class names end with either 'Handler', 'Page' or 'FileDownloader'. """ # A mapping of returned handler types to expected name endings. handler_type_to_name_endings_dict = { feconf.HANDLER_TYPE_HTML: 'Page', feconf.HANDLER_TYPE_JSON: 'Handler', feconf.HANDLER_TYPE_DOWNLOADABLE: 'FileDownloader', } num_handlers_checked = 0 for url in main.URLS: # URLS = MAPREDUCE_HANDLERS + other handlers. MAPREDUCE_HANDLERS # are tuples. So, below check is to pick only those which have # a RedirectRoute associated with it. if isinstance(url, main.routes.RedirectRoute): clazz = url.handler num_handlers_checked += 1 all_base_classes = [base_class.__name__ for base_class in (inspect.getmro(clazz))] # Check that it is a subclass of 'BaseHandler'. if 'BaseHandler' in all_base_classes: class_return_type = clazz.GET_HANDLER_ERROR_RETURN_TYPE # Check that any class with a get handler has a # GET_HANDLER_ERROR_RETURN_TYPE that's one of # the allowed values. if 'get' in clazz.__dict__.keys(): self.assertIn( class_return_type, handler_type_to_name_endings_dict) class_name = clazz.__name__ file_name = inspect.getfile(clazz) line_num = inspect.getsourcelines(clazz)[1] allowed_class_ending = handler_type_to_name_endings_dict[ class_return_type] # Check that the name of the class ends with # the proper word if it has a get function. if 'get' in clazz.__dict__.keys(): message = ( 'Please ensure that the name of this class ' 'ends with \'%s\'' % allowed_class_ending) error_message = ( '%s --> Line %s: %s' % (file_name, line_num, message)) self.assertTrue( class_name.endswith(allowed_class_ending), msg=error_message) # Check that the name of the class ends with 'Handler' # if it does not has a get function. else: message = ( 'Please ensure that the name of this class ' 'ends with \'Handler\'') error_message = ( '%s --> Line %s: %s' % (file_name, line_num, message)) self.assertTrue(class_name.endswith('Handler'), msg=error_message) self.assertGreater(num_handlers_checked, 150)
def classSrcHyperlink(klass): 'Return ReST-formatted line with hyperlinks to class headers (and implementation, if the corresponding .cpp file exists).' def findFirstLine(ff, pattern, regex=True): if regex: pat = re.compile(pattern) if not os.path.exists(ff): return -1 numLines = [ i for i, l in enumerate(open(ff).readlines()) if (pat.match(l) if regex else (l == pattern)) ] return (numLines[0] if numLines else -1) import woo.config, inspect, os, hashlib pysrc = inspect.getsourcefile(klass) if pysrc: # python class lineNo = inspect.getsourcelines(klass)[1] pysrc1 = os.path.basename(pysrc) # locate the file in the source tree (as a quick hack, just use filename match) matches = [] for root, dirnames, filenames in os.walk(woo.config.sourceRoot): relRoot = root[len(woo.config.sourceRoot) + 1:] if relRoot.startswith('build') or relRoot.startswith('debian'): continue # garbage matches += [relRoot + '/' + f for f in filenames if f == pysrc1] if len(matches) > 1: #print 'WARN: multiple files named %s, using the first one: %s'%(pysrc1,str(matches)) def fileHash(src): if sys.version_info[0] == 2: return hashlib.md5(open(src).read()).hexdigest() else: return hashlib.md5( open(src).read().encode('utf-8')).hexdigest() md0 = fileHash(pysrc) matches = [ m for m in matches if fileHash(woo.config.sourceRoot + '/' + m) == md0 ] if len(matches) == 0: print( 'WARN: no file named %s with matching md5 digest found in source tree?' % pysrc1) return None elif len(matches) > 1: print( 'WARN: multiple files named %s with the same md5 found in the source tree, using the one with shortest path.' % pysrc1) matches.sort(key=len) match = matches[0] else: match = matches[0] elif len(matches) == 1: match = matches[0] else: print('WARN: no python source %s found.' % (pysrc1)) return None return '[:woosrc:`%s <%s#L%s>`]' % (match, match, lineNo) if not klass._classTrait: return None f = klass._classTrait.file if f.startswith(woo.config.sourceRoot): commonRoot = woo.config.sourceRoot elif f.startswith(woo.config.buildRoot): commonRoot = woo.config.buildRoot elif not f.startswith('/'): commonRoot = '' else: print( 'File where class is defined (%s) does not start with source root (%s) or build root (%s)' % (f, woo.config.sourceRoot, woo.config.buildRoot)) return None # +1 removes underscore in woo/... f2 = f[len(commonRoot) + (1 if commonRoot else 0):] # if this header was copied into include/, get rid of that now m = re.match('include/woo/(.*)', f2) if m: f2 = m.group(1) if f2.endswith('.hpp'): hpp, cpp, lineIs = f2, f2[:-4] + '.cpp', 'hpp' elif f2.endswith('.cpp'): hpp, cpp, lineIs = f2[:-4] + '.hpp', f2, 'cpp' else: return None # should not happen, anyway hppLine = findFirstLine( woo.config.sourceRoot + '/' + hpp, r'^(.*\s)?(struct|class)\s+%s\s*({|:).*$' % klass.__name__) cppLine = findFirstLine(woo.config.sourceRoot + '/' + cpp, r'^(.*\s)?%s::.*$' % klass.__name__) ret = [] if hppLine > 0: ret += [':woosrc:`%s <%s#L%d>`' % (hpp, hpp, hppLine + 1)] else: if f2.endswith('.hpp'): ret += [ ':woosrc:`%s <%s#L%d>`' % (hpp, hpp, klass._classTrait.line) ] else: print('WARN: No header line found for %s in %s' % (klass.__name__, hpp)) if cppLine > 0: ret += [':woosrc:`%s <%s#L%d>`' % (cpp, cpp, cppLine + 1)] else: pass # print 'No impl. line found for %s in %s'%(klass.__name__,cpp) #ret=[':woosrc:`header <%s#L%d>`'%(f2,klass._classTrait.line)] #if f2.endswith('.hpp'): # cpp=woo.config.sourceRoot+'/'+f2[:-4]+'.cpp' # # print 'Trying ',cpp # if os.path.exists(cpp): # # find first line in the cpp file which contains KlassName:: -- that's most likely where the implementation begins # numLines=[i for i,l in enumerate(open(cpp).readlines()) if klass.__name__+'::' in l] # if numLines: ret.append(':woosrc:`implementation <%s#L%d>`'%(f2[:-4]+'.cpp',numLines[0]+1)) # # return just the cpp file if the implementation is probably not there at all? # # don't do it for now # # else: ret.append(':woosrc:`implementation <%s#L>`'%(f2[:-4]+'.cpp')) if ret: return '[ ' + ' , '.join(ret) + ' ]' else: return None
def on_window_flip(self, window): '''Internal method to be called when the window have just displayed an image. When an image is showed, we decrement our framecount. If framecount is come to 0, we are taking the screenshot. The screenshot is done in a temporary place, and is compared to the original one -> test ok/ko. If no screenshot is available in the results directory, a new one will be created. ''' from kivy.base import EventLoop from tempfile import mkstemp from os.path import join, exists from os import unlink, close from shutil import move, copy # don't save screenshot until we have enough frames. # log.debug('framecount %d' % self.framecount) # ! check if there is 'framecount', otherwise just # ! assume zero e.g. if handling runTouchApp manually self.framecount = getattr(self, 'framecount', 0) - 1 if self.framecount > 0: return # don't create screenshots if not requested manually if not make_screenshots: EventLoop.stop() return reffn = None match = False try: # just get a temporary name fd, tmpfn = mkstemp(suffix='.png', prefix='kivyunit-') close(fd) unlink(tmpfn) # get a filename for the current unit test self.test_counter += 1 test_uid = '%s-%d.png' % ( '_'.join(self.id().split('.')[-2:]), self.test_counter) # capture the screen log.info('Capturing screenshot for %s' % test_uid) tmpfn = window.screenshot(tmpfn) log.info('Capture saved at %s' % tmpfn) # search the file to compare to reffn = join(self.results_dir, test_uid) log.info('Compare with %s' % reffn) # get sourcecode import inspect frame = inspect.getouterframes(inspect.currentframe())[6] sourcecodetab, line = inspect.getsourcelines(frame[0]) line = frame[2] - line currentline = sourcecodetab[line] sourcecodetab[line] = '<span style="color: red;">%s</span>' % ( currentline) sourcecode = ''.join(sourcecodetab) sourcecodetab[line] = '>>>>>>>>\n%s<<<<<<<<\n' % currentline sourcecodeask = ''.join(sourcecodetab) if not exists(reffn): log.info('No image reference, move %s as ref ?' % test_uid) if self.interactive_ask_ref(sourcecodeask, tmpfn, self.id()): move(tmpfn, reffn) tmpfn = reffn log.info('Image used as reference') match = True else: log.info('Image discarded') else: from kivy.core.image import Image as CoreImage s1 = CoreImage(tmpfn, keep_data=True) sd1 = s1.image._data[0].data s2 = CoreImage(reffn, keep_data=True) sd2 = s2.image._data[0].data if sd1 != sd2: log.critical( '%s at render() #%d, images are different.' % ( self.id(), self.test_counter)) if self.interactive_ask_diff(sourcecodeask, tmpfn, reffn, self.id()): log.critical('user ask to use it as ref.') move(tmpfn, reffn) tmpfn = reffn match = True else: self.test_failed = True else: match = True # generate html from os.path import join, dirname, exists, basename from os import mkdir build_dir = join(dirname(__file__), 'build') if not exists(build_dir): mkdir(build_dir) copy(reffn, join(build_dir, 'ref_%s' % basename(reffn))) if tmpfn != reffn: copy(tmpfn, join(build_dir, 'test_%s' % basename(reffn))) with open(join(build_dir, 'index.html'), 'at') as fd: color = '#ffdddd' if not match else '#ffffff' fd.write('<div style="background-color: %s">' % color) fd.write('<h2>%s #%d</h2>' % (self.id(), self.test_counter)) fd.write('<table><tr><th>Reference</th>' '<th>Test</th>' '<th>Comment</th>') fd.write('<tr><td><img src="ref_%s"/></td>' % basename(reffn)) if tmpfn != reffn: fd.write('<td><img src="test_%s"/></td>' % basename(reffn)) else: fd.write('<td>First time, no comparison.</td>') fd.write('<td><pre>%s</pre></td>' % sourcecode) fd.write('</table></div>') finally: try: if reffn != tmpfn: unlink(tmpfn) except: pass EventLoop.stop()
def linkcode_resolve(domain, info): """ Determine the URL corresponding to Python object """ if domain != 'py': return None modname = info['module'] fullname = info['fullname'] submod = sys.modules.get(modname) if submod is None: return None obj = submod for part in fullname.split('.'): try: obj = getattr(obj, part) except Exception: return None # strip decorators, which would resolve to the source of the decorator # possibly an upstream bug in getsourcefile, bpo-1764286 try: unwrap = inspect.unwrap except AttributeError: pass else: obj = unwrap(obj) fn = None lineno = None # Make a poor effort at linking C extension types if isinstance(obj, type) and obj.__module__ == 'numpy': fn = _get_c_source_file(obj) if fn is None: try: fn = inspect.getsourcefile(obj) except Exception: fn = None if not fn: return None try: source, lineno = inspect.getsourcelines(obj) except Exception: lineno = None fn = relpath(fn, start=dirname(numpy.__file__)) if lineno: linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) else: linespec = "" if 'dev' in numpy.__version__: return "https://github.com/numpy/numpy/blob/master/numpy/%s%s" % ( fn, linespec) else: return "https://github.com/numpy/numpy/blob/v%s/numpy/%s%s" % ( numpy.__version__, fn, linespec)
def getsourcelines(object): # pylint: disable=redefined-builtin """TFDecorator-aware replacement for inspect.getsourcelines.""" return _inspect.getsourcelines(tf_decorator.unwrap(object)[1])
# Print the best Chromosomes from each Population current_population_fits = [] current_population_chromosomes = [] for i in range(mp_ga.num_populations): current_population_fits.append(best_chromosomes[i].fitness) current_population_chromosomes.append(best_chromosomes[i].genes) df_final = pd.DataFrame( { "name of test": str(name_of_test), "run time": run_time, "fit val": current_population_fits, "seed": seed, "eval start": start, "eval stop": stop, "complexity type": inspect.getsourcelines(fitness_function)[0][0], "k": k, "number of genes": num_genes, "number of populations": num_populations, "migration frequency": migration_frequency, "migration probability": migration_probability, "crossover probability": crossover_probability, "mutation probability": mutation_probability, "elitism ratio": elitism_ratio, "number of generations": generations, } ) df_final.to_excel(("test_logs/" + name_of_test + "_results.xlsx")) print("\nSaved parameters to " + name_of_test + "_results.xlsx")
def methodInfo(m): import inspect lines = inspect.getsourcelines(m) return "\nat %s:%s:\n %s" % (inspect.getsourcefile(m), lines[1], "\n".join(lines[0]))
def make_wrapper(func): """ make_wrapper function nested in iterate_jit decorator wraps specified func using apply_jit. """ # Get the input arguments from the function in_args = inspect.getfullargspec(func).args # Get the numba.jit arguments jit_args = inspect.getfullargspec(jit).args + ['nopython'] kwargs_for_jit = toolz.keyfilter(jit_args.__contains__, kwargs) # Any name that is a parameter # Boolean flag is given special treatment. # Identify those names here dd_key_list = list(Policy.default_data(metadata=True).keys()) allowed_parameters = dd_key_list allowed_parameters += list(arg[1:] for arg in dd_key_list) additional_parameters = [ arg for arg in in_args if arg in allowed_parameters ] additional_parameters += parameters # Remote duplicates all_parameters = list(set(additional_parameters)) src = inspect.getsourcelines(func)[0] # Discover the return arguments by walking # the AST of the function grn = GetReturnNode() all_out_args = None for node in ast.walk(ast.parse(''.join(src))): all_out_args = grn.visit(node) if all_out_args: break if not all_out_args: raise ValueError("Can't find return statement in function!") # Now create the apply-style possibly-jitted function applied_jitted_f = make_apply_function(func, list(reversed(all_out_args)), in_args, parameters=all_parameters, do_jit=DO_JIT, **kwargs_for_jit) def wrapper(*args, **kwargs): """ wrapper function nested in make_wrapper function nested in iterate_jit decorator. """ in_arrays = [] pm_or_pf = [] for farg in all_out_args + in_args: if hasattr(args[0], farg): in_arrays.append(getattr(args[0], farg)) pm_or_pf.append("pm") elif hasattr(args[1], farg): in_arrays.append(getattr(args[1], farg)) pm_or_pf.append("pf") # Create the high level function high_level_func = create_toplevel_function_string( all_out_args, list(in_args), pm_or_pf) func_code = compile(high_level_func, "<string>", "exec") fakeglobals = {} eval( func_code, # pylint: disable=eval-used {"applied_f": applied_jitted_f}, fakeglobals) high_level_fn = fakeglobals['hl_func'] ans = high_level_fn(*args, **kwargs) return ans return wrapper
def linkcode_resolve(domain, info): # NOQA: C901 """ Determine the URL corresponding to Python object Notes ----- From https://github.com/numpy/numpy/blob/v1.15.1/doc/source/conf.py, 7c49cfa on Jul 31. License BSD-3. https://github.com/numpy/numpy/blob/v1.15.1/LICENSE.txt """ if domain != "py": return None modname = info["module"] fullname = info["fullname"] submod = sys.modules.get(modname) if submod is None: return None obj = submod for part in fullname.split("."): try: obj = getattr(obj, part) except Exception: return None # strip decorators, which would resolve to the source of the decorator # possibly an upstream bug in getsourcefile, bpo-1764286 try: unwrap = inspect.unwrap except AttributeError: pass else: obj = unwrap(obj) try: fn = inspect.getsourcefile(obj) except Exception: fn = None if not fn: return None try: source, lineno = inspect.getsourcelines(obj) except Exception: lineno = None if lineno: linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) else: linespec = "" fn = relpath(fn, start=dirname(libtmux.__file__)) if "dev" in about["__version__"]: return "{}/blob/master/{}/{}{}".format( about["__github__"], about["__package_name__"], fn, linespec, ) else: return "{}/blob/v{}/{}/{}{}".format( about["__github__"], about["__version__"], about["__package_name__"], fn, linespec, )
def function_source(function): return ''.join(inspect.getsourcelines(function)[0])
track_max_fitness.append(max(current_population_fits)) track_final_chromosome.append(best_chrom) df_final = pd.DataFrame({ "name of test": [str(name_of_test)] * len(param_change), "run time": track_run_time, "fit vals": track_final_fit_value, "max fitness": track_max_fitness, "seed": [seed] * len(param_change), "eval start": [start] * len(param_change), "eval stop": [stop] * len(param_change), "complexity type": inspect.getsourcelines(fitness_function)[0][0] * len(param_change), "k": [k] * len(param_change), "number of genes": [num_genes] * len(param_change), "number of populations": [num_populations] * len(param_change), "migration frequency": [migration_frequency] * len(param_change), "migration probability": [migration_probability] * len(param_change), "crossover probability": [crossover_probability] * len(param_change), "mutation probability": [mutation_probability] * len(param_change), "elitism ratio": [elitism_ratio] * len(param_change), "number of generations": [generations] * len(param_change), }) df_final.to_excel(("test_logs/" + name_of_test + "_results.xlsx")) print("\nSaved parameters to " + name_of_test + "_results.xlsx") # save chromosomes separately
def __call__(self, request): """ Handler that mimics what CubicWebPublisher.main_handle_request and CubicWebPublisher.core_handle do """ req = request.cw_request vreg = request.registry['cubicweb.registry'] try: content = None try: with cw_to_pyramid(request): ctrlid, rset = self.appli.url_resolver.process( req, req.path) try: controller = vreg['controllers'].select( ctrlid, req, appli=self.appli) log.info( "REQUEST [%s] '%s' selected controller %s at %s:%s", ctrlid, req.path, controller, inspect.getsourcefile(controller.__class__), inspect.getsourcelines(controller.__class__)[1]) emit_to_debug_channel("vreg", { "vreg": vreg, }) emit_to_debug_channel( "controller", { "kind": ctrlid, "request": req, "path": req.path, "controller": controller, "config": self.appli.repo.config, }) except cubicweb.NoSelectableObject: log.warn("WARNING '%s' unauthorized request", req.path) raise httpexceptions.HTTPUnauthorized( req._('not authorized')) req.update_search_state() content = controller.publish(rset=rset) # XXX this auto-commit should be handled by the cw_request # cleanup or the pyramid transaction manager. # It is kept here to have the ValidationError handling bw # compatible if req.cnx: txuuid = req.cnx.commit() # commited = True if txuuid is not None: req.data['last_undoable_transaction'] = txuuid except cubicweb.web.ValidationError as ex: # XXX The validation_error_handler implementation is light, we # should redo it better in cw_to_pyramid, so it can be properly # handled when raised from a cubicweb view. # BUT the real handling of validation errors should be done # earlier in the controllers, not here. In the end, the # ValidationError should never by handled here. content = self.appli.validation_error_handler(req, ex) except cubicweb.web.RemoteCallFailed: # XXX The default pyramid error handler (or one that we provide # for this exception) should be enough # content = self.appli.ajax_error_handler(req, ex) raise if content is not None: request.response.body = content except LogOut as ex: # The actual 'logging out' logic should be in separated function # that is accessible by the pyramid views headers = security.forget(request) raise HTTPSeeOther(ex.url, headers=headers) except cubicweb.AuthenticationError: # Will occur upon access to req.cnx which is a # cubicweb.dbapi._NeedAuthAccessMock. if not content: content = vreg['views'].main_template(req, 'login') request.response.status_code = 403 request.response.body = content except cubicweb.web.NotFound as ex: view = vreg['views'].select('404', req) content = vreg['views'].main_template(req, view=view) request.response.status_code = ex.status request.response.body = content finally: # XXX CubicWebPyramidRequest.headers_out should # access directly the pyramid response headers. request.response.headers.clear() for k, v in req.headers_out.getAllRawHeaders(): for item in v: request.response.headers.add(k, item) return request.response
def get_dependencies(self): return inspect.getsourcelines(self.imports_loc)[0]
def to_ast(func, line=None): lines = inspect.getsourcelines(func)[0] if line: return ast.parse(lines[line-func.func_code.co_firstlineno]) else: return ast.parse(''.join(lines))
def hooks(): import functools import inspect list_op = [ '_%s_%s' % (h, m) for h in ['before', 'after'] for m in ['insert', 'update', 'delete'] ] tables = [] with_build_it = False for db_str in sorted(databases): db = databases[db_str] for t in db.tables: method_hooks = [] for op in list_op: functions = [] for f in getattr(db[t], op): if hasattr(f, '__call__'): try: if isinstance(f, (functools.partial)): f = f.func filename = inspect.getsourcefile(f) details = { 'funcname': f.__name__, 'filename': filename[len(request.folder):] if request.folder in filename else None, 'lineno': inspect.getsourcelines(f)[1] } if details[ 'filename']: # Built in functions as delete_uploaded_files are not editable details['url'] = URL( a='admin', c='default', f='edit', args=[ request['application'], details['filename'] ], vars={'lineno': details['lineno']}) if details['filename'] or with_build_it: functions.append(details) # compiled app and windows build don't support code inspection except: pass if len(functions): method_hooks.append({'name': op, 'functions': functions}) if len(method_hooks): tables.append({ 'name': "%s.%s" % (db_str, t), 'slug': IS_SLUG()("%s.%s" % (db_str, t))[0], 'method_hooks': method_hooks }) # Render ul_main = UL(_class='nav nav-list') for t in tables: ul_main.append(A(t['name'], _onclick="collapse('a_%s')" % t['slug'])) ul_t = UL(_class='nav nav-list', _id="a_%s" % t['slug'], _style='display:none') for op in t['method_hooks']: ul_t.append(LI(op['name'])) ul_t.append( UL([ LI( A(f['funcname'], _class="editor_filelink", _href=f['url'] if 'url' in f else None, **{'_data-lineno': f['lineno'] - 1})) for f in op['functions'] ])) ul_main.append(ul_t) return ul_main
def check_spelling_words(doc: Docstring) -> list: """ Check spelling of chosen words in doc. Parameters ---------- doc : numpydoc.validate.Docstring Docstring handler. Returns ------- list List of tuples with Modin error code and its description. Notes ----- Any special words enclosed in apostrophes(") are treated as python string constants and are not checked for spelling. """ if not doc.raw_doc: return [] components = set( ["Modin", "pandas", "NumPy", "Ray", "Dask"] + ["PyArrow", "OmniSci", "XGBoost", "Plasma"] ) check_words = "|".join(x.lower() for x in components) # comments work only with re.VERBOSE pattern = r""" (?: # non-capturing group [^-\\\w\/] # any symbol except: '-', '\', '/' and any from [a-zA-Z0-9_] | ^ # or line start ) ({check_words}) # words to check, example - "modin|pandas|numpy" (?: # non-capturing group [^-"\.\/\w\\] # any symbol except: '-', '"', '.', '\', '/' and any from [a-zA-Z0-9_] | \.\s # or '.' and any whitespace | \.$ # or '.' and line end | $ # or line end ) """.format( check_words=check_words ) results = [ set(re.findall(pattern, line, re.I | re.VERBOSE)) - components for line in doc.raw_doc.splitlines() ] docstring_start_line = None for idx, line in enumerate(inspect.getsourcelines(doc.code_obj)[0]): if '"""' in line or "'''" in line: docstring_start_line = doc.source_file_def_line + idx break errors = [] for line_idx, words_in_line in enumerate(results): for word in words_in_line: reference = [x for x in components if x.lower() == word.lower()][0] errors.append( ( "MD02", MODIN_ERROR_CODES["MD02"].format( line=docstring_start_line + line_idx, word=word, reference=reference, ), ) ) return errors
def aux(self, *args, **kwargs): class_ = type(self) print (inspect.getsourcelines(class_) == inspect.getsourcelines(type(self))) f(self, *args, **kwargs)
def tracer(self, frame, event, arg): #print Cstr("1:FRAME: " + str(frame)).tocyan() #print Cstr("2:EVENT: " + event).toblue() #print Cstr("3:ARG: " + str(arg)).tored() # # Reference: https://pymotw.com/2/sys/tracing.html # # NOTE: c_call, c_return, and c_exception are not supported # to be traced # if event in ["line", "return", "call", "exception"]: seconds = int(time.time() - self.tracer_start_time) lineno = frame.f_lineno funcname = frame.f_code.co_name filename = frame.f_code.co_filename # # For /usr/lib/python*/*.py, always print # its absolute path # if not filename.startswith("/usr/lib"): n = self.tracer_rpath_depth filename = '/'.join(filename.split('/')[n:]) flag = self._debug_query(funcname, filename) if not flag: return None try: thisframe = inspect.getsourcelines(frame) lineno_base = thisframe[1] lineno_offset = lineno - lineno_base lineco = thisframe[0][lineno_offset].rstrip() except: errmsg = "-- Oops, source code not found --" lineco = Cstr(errmsg).tomagenta() s = self.debug_utag s += Cstr('[').togray() s += Cstr(funcname).tomagenta() + '()' s += Cstr('@').togray() s += Cstr(filename).toblue() s += Cstr(':').togray() s += Cstr("%d" % lineno).togray() s += Cstr('|').togray() s += Cstr("%d" % seconds).tomagenta() s += Cstr(']').togray() if event == "line": s += '+ ' + lineco elif event == "return": s0 = s rtag = "<RETURN>" s += "++ %s ++ %s" % \ (Cstr(rtag).tocyan(), Cstr(str(arg)).tocyan()) if self.tracer_verbose >= 3: rtag = "<GLOBALS>" gvars = str(frame.f_globals) s += '\n' + s0 s += "++ %s ++ %s" % \ (Cstr(rtag).tocyan(), Cstr(gvars).tocyan()) if self.tracer_verbose >= 2: rtag = "<LOCALS>" try: arg_info = \ inspect.getargvalues(frame) lvars = str(arg_info.locals) except: lvars = "" s += '\n' + s0 s += "++ %s ++ %s" % \ (Cstr(rtag).tocyan(), Cstr(lvars).tocyan()) if self.tracer_verbose >= 1: rtag = "</CALLER>" sprev = self._get_caller(frame) s += '\n' + s0 s += "++ %s ++ %s" % \ (Cstr(rtag).tocyan(), Cstr(sprev).tocyan()) elif event == "call": s0 = s if self.tracer_verbose >= 1: rtag = "<CALLER> " sprev = self._get_caller(frame) s += "++ %s ++ %s" % \ (Cstr(rtag).tocyan(), Cstr(sprev).tocyan()) s += '\n' + s0 try: arg_info = inspect.getargvalues(frame) funcargs = str(arg_info.locals) except: funcargs = "" fd4 = 'def ' + funcname + funcargs + ':' s += '++ ' + Cstr(fd4).tocyan() elif event == "exception": s += '++ ' + Cstr(str(arg)).tored() else: pass if self.tracer_sleep_nsecs != 0: time.sleep(self.tracer_sleep_nsecs) sys.stderr.write("%s\n" % s) return self.tracer
from inspectiontest import Child, Parent import inspect child = Child() test_func = child.parent_test print('Is Method: %s' % inspect.ismethod(test_func)) print('Is Function: %s' % inspect.isfunction(test_func)) print(test_func.__self__) lines = inspect.getsourcelines(test_func) source_code = "".join(lines[0]) print(source_code)
def get_setup_cache_key(func): if func is None: return None return '{0}:{1}'.format(inspect.getsourcefile(func), inspect.getsourcelines(func)[1])
def __init__(self, fn): code, firstline = inspect.getsourcelines(fn) self.filepath = inspect.getfile(fn).replace(Config.get_project_path(), '') self.start_line = firstline self.end_line = firstline + len(code) - 1
def get_function_source(ft) -> str: "Returns link to `ft` in source code." lineno = inspect.getsourcelines(ft)[1] return get_source_link(inspect.getmodule(ft), lineno)
# Reading data # =========== # In this part we'll get the crisis years from a published IMF data set. # # First let's get the standard country codes coresponding to these countries using dracula. # <codecell> import inspect from dracula.extractor import Extractor import dracula extractor = Extractor() country_codes = {} countries = extractor.grab_metadata("countries") print(inspect.getsourcelines(dracula.wb.parser.parse_multiple_countries_alone)) for country in countries: #print(dir(country)) country_codes[country.name] = country.code print(country_codes) # <markdowncell> # Manual fixing # <codecell> country_codes["Serbia, Republic of"] = 'SRB' country_codes['Brunei'] = 'BRN' country_codes[u"Lao People’s Dem. Rep."] = 'LAO' country_codes['Venezuela'] = 'VEN'
# RESULTS OF THIS TEST DEPEND ON THE CPYTHON # VERSION AND PYTHON ENVIRONMENT USED # TO RUN not_impl_mods_gen.py """ output += gen_methods() output += f""" cpymods = {gen_modules()!r} libdir = {os.path.abspath("../Lib/").encode('utf8')!r} """ # Copy the source code of functions we will reuse in the generated script for fn in [attr_is_not_inherited, extra_info, dir_of_mod_or_error]: output += "".join(inspect.getsourcelines(fn)[0]) + "\n\n" # Prevent missing variable linter errors from compare() expected_methods = {} cpymods = {} libdir = "" # This function holds the source code that will be run under RustPython def compare(): import inspect import io import os import re import sys import warnings
df_final = pd.DataFrame({ "name of test": str(name_of_test), "run time": run_time, "fit val": current_population_fits, "seed": seed, "eval start": start, "eval stop": stop, "complexity type": inspect.getsourcelines(fitness_function)[0][0], "k": k, "number of genes": num_genes, "number of populations": num_populations, "migration frequency": migration_frequency, "migration probability": migration_probability, "crossover probability": crossover_probability, "mutation probability": mutation_probability, "elitism ratio":