Skip to content

Commit 17724ff

Browse files
authored
Merge pull request #26 from defeo/autogen_doctests
Converted autogen doctests to Python syntax
2 parents da192db + a4ec210 commit 17724ff

File tree

6 files changed

+92
-86
lines changed

6 files changed

+92
-86
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ install:
2828
- pip install --verbose .
2929
# command to run tests
3030
script:
31-
- (cd tests && python rundoctest.py)
31+
- python tests/rundoctest.py
3232
- (cd docs && make html)

autogen/doc.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ def sub_loop(regex, repl, text):
6060
6161
Ensure there a space between any 2 letters ``x``::
6262
63-
sage: from sage_setup.autogen.pari.doc import sub_loop
64-
sage: import re
65-
sage: sub_loop(re.compile("xx"), "x x", "xxx_xx")
66-
'x x x_x x'
63+
>>> from autogen.doc import sub_loop
64+
>>> import re
65+
>>> print(sub_loop(re.compile("xx"), "x x", "xxx_xx"))
66+
x x x_x x
6767
"""
6868
while True:
6969
text, n = regex.subn(repl, text)
@@ -72,7 +72,7 @@ def sub_loop(regex, repl, text):
7272

7373

7474
def raw_to_rest(doc):
75-
"""
75+
r"""
7676
Convert raw PARI documentation (with ``@``-codes) to reST syntax.
7777
7878
INPUT:
@@ -83,20 +83,21 @@ def raw_to_rest(doc):
8383
8484
EXAMPLES::
8585
86-
sage: from sage_setup.autogen.pari.doc import raw_to_rest
87-
sage: print(raw_to_rest("@[startbold]hello world@[endbold]"))
86+
>>> from autogen.doc import raw_to_rest
87+
>>> print(raw_to_rest(b"@[startbold]hello world@[endbold]"))
8888
:strong:`hello world`
8989
9090
TESTS::
9191
92-
sage: raw_to_rest("@[invalid]")
92+
>>> raw_to_rest(b"@[invalid]")
9393
Traceback (most recent call last):
9494
...
9595
SyntaxError: @ found: @[invalid]
9696
97-
sage: s = '@3@[startbold]*@[endbold] snip @[dollar]0@[dollar]\ndividing @[dollar]#E@[dollar].'
98-
sage: raw_to_rest(s)
99-
u'- snip :math:`0`\n dividing :math:`\\#E`.'
97+
>>> s = b'@3@[startbold]*@[endbold] snip @[dollar]0@[dollar]\ndividing @[dollar]#E@[dollar].'
98+
>>> print(raw_to_rest(s))
99+
- snip :math:`0`
100+
dividing :math:`\#E`.
100101
"""
101102
doc = doc.decode("utf-8")
102103

@@ -251,10 +252,19 @@ def get_raw_doc(function):
251252
252253
EXAMPLES::
253254
254-
sage: from sage_setup.autogen.pari.doc import get_raw_doc
255-
sage: get_raw_doc("cos")
256-
'@[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'
257-
sage: get_raw_doc("abcde")
255+
>>> from autogen.doc import get_raw_doc
256+
>>> print(get_raw_doc("cos").decode())
257+
@[startbold]cos@[dollar](x)@[dollar]:@[endbold]
258+
<BLANKLINE>
259+
@[label se:cos]
260+
Cosine of @[dollar]x@[dollar].
261+
<BLANKLINE>
262+
<BLANKLINE>
263+
The library syntax is @[startcode]GEN @[startbold]gcos@[endbold](GEN x, long prec)@[endcode].
264+
<BLANKLINE>
265+
<BLANKLINE>
266+
<BLANKLINE>
267+
>>> get_raw_doc("abcde")
258268
Traceback (most recent call last):
259269
...
260270
RuntimeError: no help found for 'abcde'
@@ -276,14 +286,14 @@ def get_rest_doc(function):
276286
277287
EXAMPLES::
278288
279-
sage: from sage_setup.autogen.pari.doc import get_rest_doc
280-
sage: print(get_rest_doc("teichmuller"))
289+
>>> from autogen.doc import get_rest_doc
290+
>>> print(get_rest_doc("teichmuller"))
281291
Teichmüller character of the :math:`p`-adic number :math:`x`, i.e. the unique
282292
:math:`(p-1)`-th root of unity congruent to :math:`x / p^{v_p(x)}` modulo :math:`p`...
283293
284294
::
285295
286-
sage: print(get_rest_doc("weber"))
296+
>>> print(get_rest_doc("weber"))
287297
One of Weber's three :math:`f` functions.
288298
If :math:`flag = 0`, returns
289299
<BLANKLINE>
@@ -312,7 +322,7 @@ def get_rest_doc(function):
312322
313323
::
314324
315-
sage: print(get_rest_doc("ellap"))
325+
>>> print(get_rest_doc("ellap"))
316326
Let :math:`E` be an :literal:`ell` structure as output by :literal:`ellinit`, defined over
317327
a number field or a finite field :math:`\mathbb{F}_q`. The argument :math:`p` is best left
318328
omitted if the curve is defined over a finite field, and must be a prime
@@ -401,9 +411,9 @@ def get_rest_doc(function):
401411
402412
::
403413
404-
sage: print(get_rest_doc("bitor"))
414+
>>> print(get_rest_doc("bitor"))
405415
bitwise (inclusive)
406-
:literal:`or` of two integers :math:`x` and :math:`y`, that is the integer
416+
:literal:`or` of two integers :math:`x` and :math:`y`, that is the integer
407417
<BLANKLINE>
408418
.. MATH::
409419
<BLANKLINE>

autogen/generator.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
"""
22
Auto-generate methods for PARI functions.
3-
4-
Run tests from the ``SAGE_SRC`` directory::
5-
6-
sage: from sage.env import SAGE_SRC
7-
sage: os.chdir(SAGE_SRC)
83
"""
94

105
#*****************************************************************************
@@ -74,15 +69,15 @@ def can_handle_function(self, function, cname="", **kwds):
7469
7570
EXAMPLES::
7671
77-
sage: from sage_setup.autogen.pari.generator import PariFunctionGenerator
78-
sage: G = PariFunctionGenerator()
79-
sage: G.can_handle_function("bnfinit", "bnfinit0", **{"class":"basic"})
72+
>>> from autogen.generator import PariFunctionGenerator
73+
>>> G = PariFunctionGenerator()
74+
>>> G.can_handle_function("bnfinit", "bnfinit0", **{"class":"basic"})
8075
True
81-
sage: G.can_handle_function("_bnfinit", "bnfinit0", **{"class":"basic"})
76+
>>> G.can_handle_function("_bnfinit", "bnfinit0", **{"class":"basic"})
8277
False
83-
sage: G.can_handle_function("bnfinit", "BNFINIT0", **{"class":"basic"})
78+
>>> G.can_handle_function("bnfinit", "BNFINIT0", **{"class":"basic"})
8479
False
85-
sage: G.can_handle_function("bnfinit", "bnfinit0", **{"class":"hard"})
80+
>>> G.can_handle_function("bnfinit", "bnfinit0", **{"class":"hard"})
8681
False
8782
"""
8883
if function in function_blacklist:
@@ -114,15 +109,15 @@ def handle_pari_function(self, function, cname="", prototype="", help="", obsole
114109
115110
EXAMPLES::
116111
117-
sage: from sage_setup.autogen.pari.parser import read_pari_desc
118-
sage: from sage_setup.autogen.pari.generator import PariFunctionGenerator
119-
sage: G = PariFunctionGenerator()
120-
sage: G.gen_file = sys.stdout
121-
sage: G.instance_file = sys.stdout
122-
sage: G.handle_pari_function("bnfinit",
123-
....: cname="bnfinit0", prototype="GD0,L,DGp",
124-
....: help=r"bnfinit(P,{flag=0},{tech=[]}): compute...",
125-
....: **{"class":"basic", "section":"number_fields"})
112+
>>> from autogen.parser import read_pari_desc
113+
>>> from autogen.generator import PariFunctionGenerator
114+
>>> G = PariFunctionGenerator()
115+
>>> G.gen_file = sys.stdout
116+
>>> G.instance_file = sys.stdout
117+
>>> G.handle_pari_function("bnfinit",
118+
... cname="bnfinit0", prototype="GD0,L,DGp",
119+
... help=r"bnfinit(P,{flag=0},{tech=[]}): compute...",
120+
... **{"class":"basic", "section":"number_fields"})
126121
def bnfinit(P, long flag=0, tech=None, long precision=0):
127122
...
128123
cdef GEN _P = P.g
@@ -135,10 +130,11 @@ def bnfinit(P, long flag=0, tech=None, long precision=0):
135130
cdef GEN _ret = bnfinit0(_P, flag, _tech, precision)
136131
return new_gen(_ret)
137132
<BLANKLINE>
138-
sage: G.handle_pari_function("ellmodulareqn",
139-
....: cname="ellmodulareqn", prototype="LDnDn",
140-
....: help=r"ellmodulareqn(N,{x},{y}): return...",
141-
....: **{"class":"basic", "section":"elliptic_curves"})
133+
...
134+
>>> G.handle_pari_function("ellmodulareqn",
135+
... cname="ellmodulareqn", prototype="LDnDn",
136+
... help=r"ellmodulareqn(N,{x},{y}): return...",
137+
... **{"class":"basic", "section":"elliptic_curves"})
142138
def ellmodulareqn(self, long N, x=None, y=None):
143139
...
144140
cdef long _x = -1
@@ -151,11 +147,11 @@ def ellmodulareqn(self, long N, x=None, y=None):
151147
cdef GEN _ret = ellmodulareqn(N, _x, _y)
152148
return new_gen(_ret)
153149
<BLANKLINE>
154-
sage: G.handle_pari_function("setrand",
155-
....: cname="setrand", prototype="vG",
156-
....: help=r"setrand(n): reset the seed...",
157-
....: doc=r"reseeds the random number generator...",
158-
....: **{"class":"basic", "section":"programming/specific"})
150+
>>> G.handle_pari_function("setrand",
151+
... cname="setrand", prototype="vG",
152+
... help=r"setrand(n): reset the seed...",
153+
... doc=r"reseeds the random number generator...",
154+
... **{"class":"basic", "section":"programming/specific"})
159155
def setrand(n):
160156
r'''
161157
Reseeds the random number generator using the seed :math:`n`. No value is
@@ -169,11 +165,12 @@ def setrand(n):
169165
setrand(_n)
170166
clear_stack()
171167
<BLANKLINE>
172-
sage: G.handle_pari_function("bernvec",
173-
....: cname="bernvec", prototype="L",
174-
....: help="bernvec(x): this routine is obsolete, use bernfrac repeatedly.",
175-
....: obsolete="2007-03-30",
176-
....: **{"class":"basic", "section":"transcendental"})
168+
...
169+
>>> G.handle_pari_function("bernvec",
170+
... cname="bernvec", prototype="L",
171+
... help="bernvec(x): this routine is obsolete, use bernfrac repeatedly.",
172+
... obsolete="2007-03-30",
173+
... **{"class":"basic", "section":"transcendental"})
177174
def bernvec(self, long x):
178175
r'''
179176
This routine is obsolete, kept for backward compatibility only.

autogen/parser.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
"""
22
Read and parse the file pari.desc
3-
4-
Run tests from the ``SAGE_SRC`` directory::
5-
6-
sage: from sage.env import SAGE_SRC
7-
sage: os.chdir(SAGE_SRC)
83
"""
94

105
#*****************************************************************************
@@ -39,16 +34,16 @@ def read_pari_desc():
3934
4035
EXAMPLES::
4136
42-
sage: from sage_setup.autogen.pari.parser import read_pari_desc
43-
sage: D = read_pari_desc()
44-
sage: D["cos"]
45-
{'class': 'basic',
46-
'cname': 'gcos',
47-
'doc': 'cosine of $x$.',
48-
'function': 'cos',
49-
'help': 'cos(x): cosine of x.',
50-
'prototype': 'Gp',
51-
'section': 'transcendental'}
37+
>>> from autogen.parser import read_pari_desc
38+
>>> D = read_pari_desc()
39+
>>> D["cos"] == { 'class': 'basic',
40+
... 'cname': 'gcos',
41+
... 'doc': 'cosine of $x$.',
42+
... 'function': 'cos',
43+
... 'help': 'cos(x): cosine of x.',
44+
... 'prototype': 'Gp',
45+
... 'section': 'transcendental'}
46+
True
5247
"""
5348
with open(os.path.join(pari_share(), b'pari.desc')) as f:
5449
lines = f.readlines()
@@ -89,9 +84,9 @@ def read_decl():
8984
9085
EXAMPLES::
9186
92-
sage: from sage_setup.autogen.pari.parser import read_decl
93-
sage: read_decl()
94-
{'ABC_to_bnr', ..., 'zx_to_zv'}
87+
>>> from autogen.parser import read_decl
88+
>>> sorted(read_decl())
89+
['ABC_to_bnr', ..., 'zx_to_zv']
9590
"""
9691
s = set()
9792
with open(os.path.join("cypari2", "paridecl.pxd")) as f:
@@ -126,12 +121,12 @@ def parse_prototype(proto, help, initial_args=[]):
126121
127122
EXAMPLES::
128123
129-
sage: from sage_setup.autogen.pari.parser import parse_prototype
130-
sage: proto = 'GD0,L,DGDGDG'
131-
sage: help = 'qfbred(x,{flag=0},{d},{isd},{sd})'
132-
sage: parse_prototype(proto, help)
124+
>>> from autogen.parser import parse_prototype
125+
>>> proto = 'GD0,L,DGDGDG'
126+
>>> help = 'qfbred(x,{flag=0},{d},{isd},{sd})'
127+
>>> parse_prototype(proto, help)
133128
([GEN x, long flag=0, GEN d=NULL, GEN isd=NULL, GEN sd=NULL], GEN)
134-
sage: parse_prototype("lp", "foo()", ["TEST"])
129+
>>> parse_prototype("lp", "foo()", ["TEST"])
135130
(['TEST', prec precision=0], long)
136131
"""
137132
# Use the help string just for the argument names.

autogen/paths.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def pari_share():
3535
3636
EXAMPLES::
3737
38-
sage: from sage_setup.autogen.pari.parser import pari_share
39-
sage: pari_share()
40-
'.../share/pari'
38+
>>> from autogen.parser import pari_share
39+
>>> pari_share().endswith(b'/share/pari')
40+
True
4141
"""
4242
from subprocess import Popen, PIPE
4343
if not gppath:

tests/rundoctest.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
import cypari2
66
import doctest
77

8-
path = os.path.dirname(__file__)
9-
if path:
10-
os.chdir(path)
8+
# Autogen tests must be run in the root dir, and with the proper module path
9+
path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
10+
os.chdir(path)
11+
sys.path.append(path)
12+
import autogen
1113

1214
failed = 0
1315
attempted = 0
1416
for mod in [cypari2.closure, cypari2.convert, cypari2.gen,
1517
cypari2.handle_error, cypari2.pari_instance, cypari2.stack,
16-
cypari2.string_utils]:
18+
cypari2.string_utils,
19+
autogen.doc, autogen.generator, autogen.parser,
20+
autogen.paths]:
1721

1822
print("="*80)
1923
print("Testing {}".format(mod.__name__))

0 commit comments

Comments
 (0)