Skip to content

Commit 3ba6a21

Browse files
committed
autogenerate paridecl.pxd
1 parent 83ea7d2 commit 3ba6a21

File tree

3 files changed

+57
-34
lines changed

3 files changed

+57
-34
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ cypari2/auto_gen.pxi.tmp
44
cypari2/auto_gen.pxi
55
cypari2/auto_instance.pxi.tmp
66
cypari2/auto_instance.pxi
7-
cypari2/auto_paridecl.pxd.tmp
8-
cypari2/auto_paridecl.pxd
7+
cypari2/paridecl.pxd.tmp
8+
cypari2/paridecl.pxd
99
cypari2/closure.c
1010
cypari2/convert.c
1111
cypari2/gen.c

autogen/generator.py

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@
4444
"""
4545
'''
4646

47-
decl_banner = autogen_top + '''
48-
from .types cimport *
49-
50-
cdef extern from *:
51-
'''
52-
5347

5448
function_re = re.compile(r"^[A-Za-z][A-Za-z0-9_]*$")
5549
function_blacklist = {"O", # O(p^e) needs special parser support
@@ -75,9 +69,29 @@ class PariFunctionGenerator(object):
7569
def __init__(self):
7670
self.gen_filename = os.path.join('cypari2', 'auto_gen.pxi')
7771
self.instance_filename = os.path.join('cypari2', 'auto_instance.pxi')
78-
self.decl_filename = os.path.join('cypari2', 'auto_paridecl.pxd')
72+
self.decl_filename = os.path.join('cypari2', 'paridecl.pxd')
73+
74+
def write_paridecl_no_desc(self, D):
75+
r"""
76+
Write the template ``template_paridecl.pxd`` into the declaration file
77+
after removing all functions from ``D`` (that are obtained from
78+
``pari.desc``).
79+
"""
80+
fnc = re.compile(" (int|long|void|GEN) +([A-Za-z][0-9A-Za-z_]*)\(.*\)")
81+
82+
functions = set(f.get('cname') for f in D)
83+
84+
with open('autogen/template_paridecl.pxd') as template:
85+
for line in template.read().split('\n'):
86+
match = fnc.match(line)
87+
if match:
88+
out_type, fname = match.groups()
89+
if fname in functions:
90+
continue
91+
self.decl_file.write(line)
92+
self.decl_file.write("\n")
7993

80-
def can_handle_function(self, function, cname="", **kwds):
94+
def can_handle_function(self, function, cname="", prototype="", help="", **kwds):
8195
"""
8296
Can we actually handle this function?
8397
@@ -107,6 +121,10 @@ def can_handle_function(self, function, cname="", **kwds):
107121
if sec == "programming/control":
108122
# Skip if, return, break, ...
109123
return False
124+
try:
125+
args, ret = parse_prototype(prototype, help)
126+
except NotImplementedError:
127+
return # Skip unsupported prototype codes
110128
return True
111129

112130
def handle_pari_function(self, function, cname, prototype="", help="", obsolete=None, **kwds):
@@ -201,12 +219,8 @@ def bernvec(self, long x):
201219
return new_gen(_ret)
202220
<BLANKLINE>
203221
"""
204-
try:
205-
args, ret = parse_prototype(prototype, help)
206-
except NotImplementedError:
207-
return # Skip unsupported prototype codes
208-
209222
doc = get_rest_doc(function)
223+
args, ret = parse_prototype(prototype, help)
210224

211225
self.write_declaration(cname, args, ret, self.decl_file)
212226

@@ -293,31 +307,40 @@ def __call__(self):
293307
"""
294308
D = read_pari_desc()
295309
D = sorted(D.values(), key=lambda d: d['function'])
296-
sys.stdout.write("Generating PARI functions:")
297310

298311
self.gen_file = io.open(self.gen_filename + '.tmp', 'w', encoding='utf-8')
299312
self.gen_file.write(gen_banner)
300313
self.instance_file = io.open(self.instance_filename + '.tmp', 'w', encoding='utf-8')
301314
self.instance_file.write(instance_banner)
302315
self.decl_file = io.open(self.decl_filename + '.tmp', 'w', encoding='utf-8')
303-
self.decl_file.write(decl_banner)
304-
305-
# Check for availability of hi-res SVG plotting. This requires
306-
# PARI-2.10 or later.
307-
have_plot_svg = False
308316

317+
sys.stdout.write("Non handled PARI functions:")
318+
DD = []
309319
for v in D:
310320
func = v["function"]
311321
if self.can_handle_function(**v):
312-
sys.stdout.write(" %s" % func)
313-
sys.stdout.flush()
314-
self.handle_pari_function(**v)
315-
if func == "plothraw":
316-
have_plot_svg = True
322+
DD.append(v)
317323
else:
318324
sys.stdout.write(" (%s)" % func)
319325
sys.stdout.write("\n")
320326

327+
self.write_paridecl_no_desc(DD)
328+
329+
330+
sys.stdout.write("Generating PARI functions:")
331+
332+
# Check for availability of hi-res SVG plotting. This requires
333+
# PARI-2.10 or later.
334+
have_plot_svg = False
335+
for v in DD:
336+
func = v["function"]
337+
sys.stdout.write(" %s" % func)
338+
sys.stdout.flush()
339+
self.handle_pari_function(**v)
340+
if func == "plothraw":
341+
have_plot_svg = True
342+
sys.stdout.write("\n")
343+
321344
self.instance_file.write("DEF HAVE_PLOT_SVG = {}".format(have_plot_svg))
322345

323346
self.gen_file.close()

autogen/template_paridecl.pxd

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# distutils: libraries = gmp pari
22
"""
3-
Declarations for non-inline functions from PARI.
3+
Declarations of PARI functions.
44
55
This file contains all declarations from headers/paridecl.h from
6-
the PARI distribution, except the inline functions which are in
7-
declinl.pxi (that file is automatically included by this file).
8-
6+
the PARI distribution.
97
108
AUTHORS:
119
@@ -20,6 +18,9 @@ AUTHORS:
2018
2119
- Jeroen Demeyer (2014-09-19): upgrade to PARI 2.8 (:trac:`16997`)
2220
21+
- Vincent Delecroix (2017-2018): auto-generate part of the declarations
22+
from ``pari.desc``
23+
2324
"""
2425

2526
#*****************************************************************************
@@ -5168,7 +5169,6 @@ cdef extern from *: # PARI headers already included by types.pxd
51685169
void pari_err_VAR(const char *f, GEN x, GEN y)
51695170
void pari_err_ROOTS0(const char *f)
51705171

5171-
# Auto-generated declarations. There are taken from the PARI version
5172-
# on the system, so they more up-to-date than the above. In case of
5173-
# conflicting declarations, auto_paridecl should have priority.
5174-
from .auto_paridecl cimport *
5172+
#########################################################
5173+
# All functions below are auto-generated from pari.desc #
5174+
#########################################################

0 commit comments

Comments
 (0)