Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted autogen doctests to Python syntax #26

Merged
merged 5 commits into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions autogen/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def sub_loop(regex, repl, text):

Ensure there a space between any 2 letters ``x``::

sage: from sage_setup.autogen.pari.doc import sub_loop
sage: import re
sage: sub_loop(re.compile("xx"), "x x", "xxx_xx")
'x x x_x x'
>>> from autogen.doc import sub_loop
>>> import re
>>> print(sub_loop(re.compile("xx"), "x x", "xxx_xx"))
x x x_x x
"""
while True:
text, n = regex.subn(repl, text)
Expand All @@ -72,7 +72,7 @@ def sub_loop(regex, repl, text):


def raw_to_rest(doc):
"""
r"""
Convert raw PARI documentation (with ``@``-codes) to reST syntax.

INPUT:
Expand All @@ -83,20 +83,21 @@ def raw_to_rest(doc):

EXAMPLES::

sage: from sage_setup.autogen.pari.doc import raw_to_rest
sage: print(raw_to_rest("@[startbold]hello world@[endbold]"))
>>> from autogen.doc import raw_to_rest
>>> print(raw_to_rest(b"@[startbold]hello world@[endbold]"))
:strong:`hello world`

TESTS::

sage: raw_to_rest("@[invalid]")
>>> raw_to_rest(b"@[invalid]")
Traceback (most recent call last):
...
SyntaxError: @ found: @[invalid]

sage: s = '@3@[startbold]*@[endbold] snip @[dollar]0@[dollar]\ndividing @[dollar]#E@[dollar].'
sage: raw_to_rest(s)
u'- snip :math:`0`\n dividing :math:`\\#E`.'
>>> s = b'@3@[startbold]*@[endbold] snip @[dollar]0@[dollar]\ndividing @[dollar]#E@[dollar].'
>>> print(raw_to_rest(s))
- snip :math:`0`
dividing :math:`\#E`.
"""
doc = doc.decode("utf-8")

Expand Down Expand Up @@ -251,10 +252,19 @@ def get_raw_doc(function):

EXAMPLES::

sage: from sage_setup.autogen.pari.doc import get_raw_doc
sage: get_raw_doc("cos")
'@[startbold]cos@[dollar](x)@[dollar]:@[endbold]\n\n@[label se:cos]\nCosine of @[dollar]x@[dollar].\n\n\nThe library syntax is @[startcode]GEN @[startbold]gcos@[endbold](GEN x, long prec)@[endcode].\n\n\n'
sage: get_raw_doc("abcde")
>>> from autogen.doc import get_raw_doc
>>> print(get_raw_doc("cos").decode())
@[startbold]cos@[dollar](x)@[dollar]:@[endbold]
<BLANKLINE>
@[label se:cos]
Cosine of @[dollar]x@[dollar].
<BLANKLINE>
<BLANKLINE>
The library syntax is @[startcode]GEN @[startbold]gcos@[endbold](GEN x, long prec)@[endcode].
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
>>> get_raw_doc("abcde")
Traceback (most recent call last):
...
RuntimeError: no help found for 'abcde'
Expand All @@ -276,14 +286,14 @@ def get_rest_doc(function):

EXAMPLES::

sage: from sage_setup.autogen.pari.doc import get_rest_doc
sage: print(get_rest_doc("teichmuller"))
>>> from autogen.doc import get_rest_doc
>>> print(get_rest_doc("teichmuller"))
Teichmüller character of the :math:`p`-adic number :math:`x`, i.e. the unique
:math:`(p-1)`-th root of unity congruent to :math:`x / p^{v_p(x)}` modulo :math:`p`...

::

sage: print(get_rest_doc("weber"))
>>> print(get_rest_doc("weber"))
One of Weber's three :math:`f` functions.
If :math:`flag = 0`, returns
<BLANKLINE>
Expand Down Expand Up @@ -312,7 +322,7 @@ def get_rest_doc(function):

::

sage: print(get_rest_doc("ellap"))
>>> print(get_rest_doc("ellap"))
Let :math:`E` be an :literal:`ell` structure as output by :literal:`ellinit`, defined over
a number field or a finite field :math:`\mathbb{F}_q`. The argument :math:`p` is best left
omitted if the curve is defined over a finite field, and must be a prime
Expand Down Expand Up @@ -401,9 +411,9 @@ def get_rest_doc(function):

::

sage: print(get_rest_doc("bitor"))
>>> print(get_rest_doc("bitor"))
bitwise (inclusive)
:literal:`or` of two integers :math:`x` and :math:`y`, that is the integer
:literal:`or` of two integers :math:`x` and :math:`y`, that is the integer
<BLANKLINE>
.. MATH::
<BLANKLINE>
Expand Down
65 changes: 31 additions & 34 deletions autogen/generator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
"""
Auto-generate methods for PARI functions.

Run tests from the ``SAGE_SRC`` directory::

sage: from sage.env import SAGE_SRC
sage: os.chdir(SAGE_SRC)
"""

#*****************************************************************************
Expand Down Expand Up @@ -74,15 +69,15 @@ def can_handle_function(self, function, cname="", **kwds):

EXAMPLES::

sage: from sage_setup.autogen.pari.generator import PariFunctionGenerator
sage: G = PariFunctionGenerator()
sage: G.can_handle_function("bnfinit", "bnfinit0", **{"class":"basic"})
>>> from autogen.generator import PariFunctionGenerator
>>> G = PariFunctionGenerator()
>>> G.can_handle_function("bnfinit", "bnfinit0", **{"class":"basic"})
True
sage: G.can_handle_function("_bnfinit", "bnfinit0", **{"class":"basic"})
>>> G.can_handle_function("_bnfinit", "bnfinit0", **{"class":"basic"})
False
sage: G.can_handle_function("bnfinit", "BNFINIT0", **{"class":"basic"})
>>> G.can_handle_function("bnfinit", "BNFINIT0", **{"class":"basic"})
False
sage: G.can_handle_function("bnfinit", "bnfinit0", **{"class":"hard"})
>>> G.can_handle_function("bnfinit", "bnfinit0", **{"class":"hard"})
False
"""
if function in function_blacklist:
Expand Down Expand Up @@ -114,15 +109,15 @@ def handle_pari_function(self, function, cname="", prototype="", help="", obsole

EXAMPLES::

sage: from sage_setup.autogen.pari.parser import read_pari_desc
sage: from sage_setup.autogen.pari.generator import PariFunctionGenerator
sage: G = PariFunctionGenerator()
sage: G.gen_file = sys.stdout
sage: G.instance_file = sys.stdout
sage: G.handle_pari_function("bnfinit",
....: cname="bnfinit0", prototype="GD0,L,DGp",
....: help=r"bnfinit(P,{flag=0},{tech=[]}): compute...",
....: **{"class":"basic", "section":"number_fields"})
>>> from autogen.parser import read_pari_desc
>>> from autogen.generator import PariFunctionGenerator
>>> G = PariFunctionGenerator()
>>> G.gen_file = sys.stdout
>>> G.instance_file = sys.stdout
>>> G.handle_pari_function("bnfinit",
... cname="bnfinit0", prototype="GD0,L,DGp",
... help=r"bnfinit(P,{flag=0},{tech=[]}): compute...",
... **{"class":"basic", "section":"number_fields"})
def bnfinit(P, long flag=0, tech=None, long precision=0):
...
cdef GEN _P = P.g
Expand All @@ -135,10 +130,11 @@ def bnfinit(P, long flag=0, tech=None, long precision=0):
cdef GEN _ret = bnfinit0(_P, flag, _tech, precision)
return new_gen(_ret)
<BLANKLINE>
sage: G.handle_pari_function("ellmodulareqn",
....: cname="ellmodulareqn", prototype="LDnDn",
....: help=r"ellmodulareqn(N,{x},{y}): return...",
....: **{"class":"basic", "section":"elliptic_curves"})
...
>>> G.handle_pari_function("ellmodulareqn",
... cname="ellmodulareqn", prototype="LDnDn",
... help=r"ellmodulareqn(N,{x},{y}): return...",
... **{"class":"basic", "section":"elliptic_curves"})
def ellmodulareqn(self, long N, x=None, y=None):
...
cdef long _x = -1
Expand All @@ -151,11 +147,11 @@ def ellmodulareqn(self, long N, x=None, y=None):
cdef GEN _ret = ellmodulareqn(N, _x, _y)
return new_gen(_ret)
<BLANKLINE>
sage: G.handle_pari_function("setrand",
....: cname="setrand", prototype="vG",
....: help=r"setrand(n): reset the seed...",
....: doc=r"reseeds the random number generator...",
....: **{"class":"basic", "section":"programming/specific"})
>>> G.handle_pari_function("setrand",
... cname="setrand", prototype="vG",
... help=r"setrand(n): reset the seed...",
... doc=r"reseeds the random number generator...",
... **{"class":"basic", "section":"programming/specific"})
def setrand(n):
r'''
Reseeds the random number generator using the seed :math:`n`. No value is
Expand All @@ -169,11 +165,12 @@ def setrand(n):
setrand(_n)
clear_stack()
<BLANKLINE>
sage: G.handle_pari_function("bernvec",
....: cname="bernvec", prototype="L",
....: help="bernvec(x): this routine is obsolete, use bernfrac repeatedly.",
....: obsolete="2007-03-30",
....: **{"class":"basic", "section":"transcendental"})
...
>>> G.handle_pari_function("bernvec",
... cname="bernvec", prototype="L",
... help="bernvec(x): this routine is obsolete, use bernfrac repeatedly.",
... obsolete="2007-03-30",
... **{"class":"basic", "section":"transcendental"})
def bernvec(self, long x):
r'''
This routine is obsolete, kept for backward compatibility only.
Expand Down
41 changes: 18 additions & 23 deletions autogen/parser.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
"""
Read and parse the file pari.desc

Run tests from the ``SAGE_SRC`` directory::

sage: from sage.env import SAGE_SRC
sage: os.chdir(SAGE_SRC)
"""

#*****************************************************************************
Expand Down Expand Up @@ -39,16 +34,16 @@ def read_pari_desc():

EXAMPLES::

sage: from sage_setup.autogen.pari.parser import read_pari_desc
sage: D = read_pari_desc()
sage: D["cos"]
{'class': 'basic',
'cname': 'gcos',
'doc': 'cosine of $x$.',
'function': 'cos',
'help': 'cos(x): cosine of x.',
'prototype': 'Gp',
'section': 'transcendental'}
>>> from autogen.parser import read_pari_desc
>>> D = read_pari_desc()
>>> D["cos"] == { 'class': 'basic',
... 'cname': 'gcos',
... 'doc': 'cosine of $x$.',
... 'function': 'cos',
... 'help': 'cos(x): cosine of x.',
... 'prototype': 'Gp',
... 'section': 'transcendental'}
True
"""
with open(os.path.join(pari_share(), b'pari.desc')) as f:
lines = f.readlines()
Expand Down Expand Up @@ -89,9 +84,9 @@ def read_decl():

EXAMPLES::

sage: from sage_setup.autogen.pari.parser import read_decl
sage: read_decl()
{'ABC_to_bnr', ..., 'zx_to_zv'}
>>> from autogen.parser import read_decl
>>> sorted(read_decl())
['ABC_to_bnr', ..., 'zx_to_zv']
"""
s = set()
with open(os.path.join("cypari2", "paridecl.pxd")) as f:
Expand Down Expand Up @@ -126,12 +121,12 @@ def parse_prototype(proto, help, initial_args=[]):

EXAMPLES::

sage: from sage_setup.autogen.pari.parser import parse_prototype
sage: proto = 'GD0,L,DGDGDG'
sage: help = 'qfbred(x,{flag=0},{d},{isd},{sd})'
sage: parse_prototype(proto, help)
>>> from autogen.parser import parse_prototype
>>> proto = 'GD0,L,DGDGDG'
>>> help = 'qfbred(x,{flag=0},{d},{isd},{sd})'
>>> parse_prototype(proto, help)
([GEN x, long flag=0, GEN d=NULL, GEN isd=NULL, GEN sd=NULL], GEN)
sage: parse_prototype("lp", "foo()", ["TEST"])
>>> parse_prototype("lp", "foo()", ["TEST"])
(['TEST', prec precision=0], long)
"""
# Use the help string just for the argument names.
Expand Down
6 changes: 3 additions & 3 deletions autogen/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def pari_share():

EXAMPLES::

sage: from sage_setup.autogen.pari.parser import pari_share
sage: pari_share()
'.../share/pari'
>>> from autogen.parser import pari_share
>>> pari_share().endswith(b'/share/pari')
True
"""
from subprocess import Popen, PIPE
if not gppath:
Expand Down
6 changes: 5 additions & 1 deletion tests/rundoctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import os
import sys
import cypari2
import autogen
import doctest

path = os.path.dirname(__file__)
if path:
os.chdir(path)
os.chdir('..')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one particularly bugs me. Why were we chdir'ing before?


failed = 0
attempted = 0
for mod in [cypari2.closure, cypari2.convert, cypari2.gen,
cypari2.handle_error, cypari2.pari_instance, cypari2.stack,
cypari2.string_utils]:
cypari2.string_utils,
autogen.doc, autogen.generator, autogen.parser,
autogen.paths]:

print("="*80)
print("Testing {}".format(mod.__name__))
Expand Down