def _instancedoc_(self): """ Provide documentation for the wrapped function. TESTS:: sage: from sage.misc.sageinspect import sage_getdoc sage: from sage.sets.set_from_iterator import Decorator sage: d = Decorator() sage: d.f = Integer.is_prime sage: print(sage_getdoc(d)) # indirect doctest Test whether "self" is prime. ... Calls the PARI "isprime" function. """ from sage.misc.sageinspect import sage_getsourcelines, sage_getfile, _extract_embedded_position f = self.f doc = f.__doc__ or '' if _extract_embedded_position(doc) is None: try: sourcelines = sage_getsourcelines(f) from sage.env import SAGE_LIB, SAGE_SRC filename = sage_getfile(f) # The following is a heuristics to get # the file name of the cached function # or method if filename.startswith(SAGE_SRC): filename = filename[len(SAGE_SRC):] elif filename.startswith(SAGE_LIB): filename = filename[len(SAGE_LIB):] file_info = "File: %s (starting at line %d)\n"%(filename,sourcelines[1]) doc = file_info+doc except IOError: pass return doc
def source_code(s, globs, system='sage'): r""" Format obj's source code for printing in Sage notebook. AUTHORS: - William Stein: partly taken from IPython for use in Sage - Nick Alexander: extensions """ if system not in ['sage', 'python']: s = system + '.' + s try: obj = eval(s, globs) except NameError: return "No object %s"%s try: try: return obj._sage_src_() except: pass filename = sageinspect.sage_getfile(obj) lines, lineno = sageinspect.sage_getsourcelines(obj, is_binary=False) src = ''.join(lines) src = sagedoc.format_src(src) if not lineno is None: src = "File: %s\nSource Code (starting at line %s):\n%s"%(filename, lineno, src) return src except (TypeError, IndexError), msg: print msg return "Source code for %s not available."%obj
def source_code(s, globs, system='sage'): r""" Format an object's source code to process and display in the Sage notebook. INPUT: - ``s`` - a string; a name of an object - ``globs`` - a string:object dictionary; a context in which to evaluate ``s`` - ``system`` - a string (default: 'sage'); the system to which to confine the search OUTPUT: - a string containing the object's file, starting line number, and source code AUTHORS: - William Stein: partly taken from IPython for use in Sage - Nick Alexander: extensions """ if system not in ['sage', 'python']: s = system + '.' + s try: obj = eval(s, globs) except NameError: return html_markup("No object %s"%s) try: try: return html_markup(obj._sage_src_()) except: pass newline = "\n\n" # blank line to start new paragraph indent = " " # indent source code to mark it as a code block filename = sageinspect.sage_getfile(obj) try: lines, lineno = sageinspect.sage_getsourcelines(obj) except IOError as msg: return html_markup(str(msg)) src = indent.join(lines) src = indent + format_src(src) if not lineno is None: output = "**File:** %s"%filename output += newline output += "**Source Code** (starting at line %s)::"%lineno output += newline output += src return html_markup(output) except (TypeError, IndexError): return html_markup("Source code for {} is not available.".format(s) + "\nUse {}? to see the documentation.".format(s))
def _sage_doc_(self): """ Provide documentation for the wrapped function. TESTS:: sage: from sage.misc.sageinspect import sage_getdoc sage: from sage.sets.set_from_iterator import Decorator sage: d = Decorator() sage: d.f = Integer.is_prime sage: print sage_getdoc(d) # indirect doctest Test whether "self" is prime. ... Calls the PARI "isprime" function. """ from sage.misc.sageinspect import sage_getsourcelines, sage_getfile, _extract_embedded_position f = self.f doc = f.__doc__ or '' if _extract_embedded_position(doc) is None: try: sourcelines = sage_getsourcelines(f) from sage.env import SAGE_LIB, SAGE_SRC filename = sage_getfile(f) # The following is a heuristics to get # the file name of the cached function # or method if filename.startswith(SAGE_SRC): filename = filename[len(SAGE_SRC):] elif filename.startswith(SAGE_LIB): filename = filename[len(SAGE_LIB):] file_info = "File: %s (starting at line %d)\n"%(filename,sourcelines[1]) doc = file_info+doc except IOError: pass return doc
def file_and_line(obj): r""" Look up source file and line number of obj. If the file lies in the Sage library, the path name of the corresponding file in the current branch (i.e., the file that gets copied into the Sage library upon running 'sage -br'). Note that the first line of a file is considered to be 1 rather than 0 because most editors think that this is the case. AUTHORS: - Nils Bruin (2007-10-03) - Simon King (2011-05): Use :mod:`~sage.misc.sageinspect` to get the file and the line. EXAMPLES:: sage: import sage.misc.edit_module as edit_module sage: edit_module.file_and_line(sage) ('...sage/__init__.py', 0) The following tests against a bug that was fixed in trac ticket #11298:: sage: edit_module.file_and_line(x) ('...sage/symbolic/expression.pyx', ...) """ # d = inspect.getdoc(obj) # ret = sage.misc.sageinspect._extract_embedded_position(d); # if ret is not None: # (_, filename, lineno) = ret # else: # filename = inspect.getsourcefile(obj) # _,lineno = inspect.findsource(obj) # # for sage files, the registered source file is the result of the preparsing # these files end in ".py" and have "*autogenerated*" on the second line # for those files, we replace the extension by ".sage" and we subtract # 3 from the line number to compensate for the 3 lines that were prefixed # in the preparsing process # from sage.misc.sageinspect import sage_getfile, sage_getsourcelines filename = sage_getfile(obj) lineno = sage_getsourcelines(obj)[1] - 1 if filename.endswith(".py"): infile = open(filename, "r") infile.readline() if infile.readline().find("*autogenerated*") >= 0: filename = filename[:-3] + ".sage" lineno = lineno - 3 sageroot = sage.misc.sageinspect.SAGE_ROOT + "/" runpathpattern = "^" + sageroot + "local/lib/python[^/]*/site-packages" develbranch = sageroot + "devel/sage" filename = re.sub(runpathpattern, develbranch, filename) return filename, lineno + 1
def file_and_line(obj): r""" Look up source file and line number of obj. If the file lies in the Sage library, the path name of the corresponding file in the current branch (i.e., the file that gets copied into the Sage library upon running 'sage -br'). Note that the first line of a file is considered to be 1 rather than 0 because most editors think that this is the case. AUTHORS: - Nils Bruin (2007-10-03) - Simon King (2011-05): Use :mod:`~sage.misc.sageinspect` to get the file and the line. EXAMPLES:: sage: import sage.misc.edit_module as edit_module sage: edit_module.file_and_line(sage) ('...sage/__init__.py', 0) The following tests against a bug that was fixed in trac ticket #11298:: sage: edit_module.file_and_line(x) ('...sage/symbolic/expression.pyx', ...) """ #d = inspect.getdoc(obj) #ret = sage.misc.sageinspect._extract_embedded_position(d); #if ret is not None: # (_, filename, lineno) = ret #else: # filename = inspect.getsourcefile(obj) # _,lineno = inspect.findsource(obj) # # for sage files, the registered source file is the result of the preparsing # these files end in ".py" and have "*autogenerated*" on the second line # for those files, we replace the extension by ".sage" and we subtract # 3 from the line number to compensate for the 3 lines that were prefixed # in the preparsing process # from sage.misc.sageinspect import sage_getfile, sage_getsourcelines filename = sage_getfile(obj) lineno = sage_getsourcelines(obj)[1] - 1 if filename.endswith('.py'): infile=open(filename,'r') infile.readline() if infile.readline().find("*autogenerated*") >= 0: filename=filename[:-3]+'.sage' lineno = lineno-3 sageroot = sage.misc.sageinspect.SAGE_ROOT+'/' runpathpattern = '^'+sageroot+'local/lib/python[^/]*/site-packages' develbranch = sageroot+'devel/sage' filename=re.sub(runpathpattern,develbranch,filename) return filename, lineno+1
def f(wrapper): update_wrapper(wrapper, wrapped, assigned=assigned, updated=updated) wrapper._sage_src_ = lambda: sage_getsource(wrapped) wrapper._sage_src_lines_ = lambda: sage_getsourcelines(wrapped) #Getting the signature right in documentation by Sphinx (Trac 9976) #The attribute _sage_argspec_() is read by Sphinx if present and used #as the argspec of the function instead of using reflection. wrapper._sage_argspec_ = lambda: sage_getargspec(wrapped) return wrapper
def f(wrapper, assigned=assigned, updated=updated): update_wrapper(wrapper, wrapped, assigned=assigned, updated=updated) # For backwards-compatibility with old versions of sage_wraps wrapper.f = wrapped # For forwards-compatibility with functools.wraps on Python 3 wrapper.__wrapped__ = wrapped wrapper._sage_src_ = lambda: sage_getsource(wrapped) wrapper._sage_src_lines_ = lambda: sage_getsourcelines(wrapped) #Getting the signature right in documentation by Sphinx (Trac 9976) #The attribute _sage_argspec_() is read by Sphinx if present and used #as the argspec of the function instead of using reflection. wrapper._sage_argspec_ = lambda: sage_getargspec(wrapped) return wrapper
def _sage_src_lines_(self): """ Returns the source code location for the wrapped function. EXAMPLES: sage: from sage.misc.sageinspect import sage_getsourcelines sage: g = lazy_attribute(banner) sage: (src, lines) = sage_getsourcelines(g) sage: src[0] 'def banner():\n' sage: lines 74 """ from sage.misc.sageinspect import sage_getsourcelines return sage_getsourcelines(self.f)
def _sage_src_lines_(self): """ Returns the source code location for the wrapped function. EXAMPLES:: sage: from sage.misc.sageinspect import sage_getsourcelines sage: g = abstract_method(banner) sage: (src, lines) = sage_getsourcelines(g) sage: src[0] 'def banner():\n' sage: lines 81 """ from sage.misc.sageinspect import sage_getsourcelines return sage_getsourcelines(self._f)
def _sage_src_lines_(self): """ Returns the source code location for the wrapped function. EXAMPLES:: sage: from sage.misc.sageinspect import sage_getsourcelines sage: g = abstract_method(banner) sage: (src, lines) = sage_getsourcelines(g) sage: src[0] 'def banner():\n' sage: lines 79 """ from sage.misc.sageinspect import sage_getsourcelines return sage_getsourcelines(self._f)
def _sage_src_lines_(self): """ Returns the source code location for the wrapped function. EXAMPLES:: sage: p = Permutation([1,3,2,4]) sage: cm = p.left_tableau; cm Combinatorial map: Robinson-Schensted insertion tableau sage: (src, lines) = cm._sage_src_lines_() sage: src[0] " @combinatorial_map(name='Robinson-Schensted insertion tableau')\n" sage: lines # random 2653 """ from sage.misc.sageinspect import sage_getsourcelines return sage_getsourcelines(self._f)
def _sage_src_lines_(self): r""" Returns the list of source lines and the first line number of the wrapped function. TESTS:: sage: from sage.misc.sageinspect import sage_getsourcelines sage: from sage.sets.set_from_iterator import Decorator sage: d = Decorator() sage: d.f = MathieuGroup.order sage: S = sage_getsourcelines(d) # indirect doctest sage: S[0][2] ' Return the number of elements of this group.\n' sage: S[0][25] ' return Integer(1)\n' """ from sage.misc.sageinspect import sage_getsourcelines return sage_getsourcelines(self.f)
def _sage_src_lines_(self): r""" Returns the list of source lines and the first line number of the wrapped function. TESTS:: sage: from sage.misc.sageinspect import sage_getsourcelines sage: from sage.sets.set_from_iterator import Decorator sage: d = Decorator() sage: d.f = MathieuGroup.order sage: S = sage_getsourcelines(d) # indirect doctest sage: S[0][2] ' Return the number of elements of this group.\n' sage: S[0][18] ' return Integer(1)\n' """ from sage.misc.sageinspect import sage_getsourcelines return sage_getsourcelines(self.f)
def _sage_doc_(self): """ Provide documentation for the wrapped function. TESTS:: sage: from sage.misc.sageinspect import sage_getdoc sage: from sage.sets.set_from_iterator import Decorator sage: d = Decorator() sage: d.f = Integer.is_prime sage: print sage_getdoc(d) # indirect doctest Returns "True" if "self" is prime. ... IMPLEMENTATION: Calls the PARI "isprime" function. <BLANKLINE> """ from sage.misc.sageinspect import sage_getsourcelines, sage_getfile f = self.f if hasattr(f, "func_doc"): try: sourcelines = sage_getsourcelines(f) except IOError: sourcelines = None if sourcelines is not None: from sage.env import SAGE_LIB, SAGE_SRC filename = sage_getfile(f) # The following is a heuristics to get # the file name of the cached function # or method if filename.startswith(SAGE_SRC): filename = filename[len(SAGE_SRC):] elif filename.startswith(SAGE_LIB): filename = filename[len(SAGE_LIB):] file_info = "File: %s (starting at line %d)\n" % ( filename, sourcelines[1]) doc = file_info + (f.func_doc or '') else: doc = f.func_doc else: doc = f.__doc__ return doc
def _sage_doc_(self): """ Provide documentation for the wrapped function. TESTS:: sage: from sage.misc.sageinspect import sage_getdoc sage: from sage.sets.set_from_iterator import Decorator sage: d = Decorator() sage: d.f = Integer.is_prime sage: print sage_getdoc(d) # indirect doctest Returns "True" if "self" is prime. ... IMPLEMENTATION: Calls the PARI "isprime" function. <BLANKLINE> """ from sage.misc.sageinspect import sage_getsourcelines, sage_getfile f = self.f if hasattr(f, "func_doc"): try: sourcelines = sage_getsourcelines(f) except IOError: sourcelines = None if sourcelines is not None: from sage.env import SAGE_LIB, SAGE_SRC filename = sage_getfile(f) # The following is a heuristics to get # the file name of the cached function # or method if filename.startswith(SAGE_SRC): filename = filename[len(SAGE_SRC):] elif filename.startswith(SAGE_LIB): filename = filename[len(SAGE_LIB):] file_info = "File: %s (starting at line %d)\n"%(filename,sourcelines[1]) doc = file_info+(f.func_doc or '') else: doc = f.func_doc else: doc = f.__doc__ return doc
def _sage_src_lines_(self): r""" Get the source lines of the dynamic class. This defers to the source lines of the ``_doccls`` attribute, which is set when the dynamic class is constructed. EXAMPLES:: sage: from sage.misc.sageinspect import sage_getsourcelines sage: from sage.structure.dynamic_class import dynamic_class sage: C = dynamic_class("SomeClass", [object], doccls=Integer) sage: sage_getsourcelines(C)[0][0] 'cdef class Integer(sage.structure.element.EuclideanDomainElement):\n' """ try: # HACK: _doccls is a 1-element tuple to avoid __classget__ # or trouble with binding behaviour... doccls = self._doccls[0] except AttributeError: raise NotImplementedError("no _doccls found") from sage.misc.sageinspect import sage_getsourcelines return sage_getsourcelines(doccls)
def _sage_src_lines(): return sage_getsourcelines(doccls)