2
2
Auto-generate methods for PARI functions.
3
3
"""
4
4
5
- #* ****************************************************************************
5
+ # ****************************************************************************
6
6
# Copyright (C) 2015 Jeroen Demeyer <[email protected] >
7
7
# 2017 Vincent Delecroix <[email protected] >
8
8
#
9
9
# This program is free software: you can redistribute it and/or modify
10
10
# it under the terms of the GNU General Public License as published by
11
11
# the Free Software Foundation, either version 2 of the License, or
12
12
# (at your option) any later version.
13
- # http ://www.gnu.org/licenses/
14
- #* ****************************************************************************
13
+ # https ://www.gnu.org/licenses/
14
+ # ****************************************************************************
15
15
16
16
from __future__ import absolute_import , print_function , unicode_literals
17
- import os , re , sys , io
17
+ import io
18
+ import os
19
+ import re
20
+ import sys
21
+
18
22
19
23
from .args import PariArgumentGEN , PariInstanceArgument
20
24
from .parser import read_pari_desc , parse_prototype
58
62
59
63
60
64
function_re = re .compile (r"^[A-Za-z][A-Za-z0-9_]*$" )
61
- function_blacklist = {"O" , # O(p^e) needs special parser support
62
- "alias" , # Not needed and difficult documentation
63
- "listcreate" , # "redundant and obsolete" according to PARI
64
- "print" , # Conflicts with Python builtin
65
- "allocatemem" , # Dangerous; overridden in PariInstance
66
- "global" , # Invalid in Python (and obsolete)
67
- "inline" , # Total confusion
68
- "uninline" , # idem
69
- "local" , # idem
70
- "my" , # idem
71
- }
65
+ function_blacklist = {"O" , # O(p^e) needs special parser support
66
+ "alias" , # Not needed and difficult documentation
67
+ "listcreate" , # "redundant and obsolete" according to PARI
68
+ "print" , # Conflicts with Python builtin
69
+ "allocatemem" , # Dangerous; overridden in PariInstance
70
+ "global" , # Invalid in Python (and obsolete)
71
+ "inline" , # Total confusion
72
+ "uninline" , # idem
73
+ "local" , # idem
74
+ "my" , # idem
75
+ }
72
76
73
77
74
78
class PariFunctionGenerator (object ):
@@ -119,7 +123,8 @@ def can_handle_function(self, function, cname="", **kwds):
119
123
return False
120
124
return True
121
125
122
- def handle_pari_function (self , function , cname , prototype = "" , help = "" , obsolete = None , ** kwds ):
126
+ def handle_pari_function (self , function , cname , prototype = "" , help = "" ,
127
+ obsolete = None , ** kwds ):
123
128
r"""
124
129
Handle one PARI function: decide whether or not to add the
125
130
function, in which file (as method of :class:`Gen` or
@@ -239,13 +244,13 @@ def polredord(self, x):
239
244
# If the first argument is a GEN, write a method of the
240
245
# Gen class.
241
246
self .write_method (function , cname , args , ret , args ,
242
- self .gen_file , doc , obsolete )
247
+ self .gen_file , doc , obsolete )
243
248
244
249
# In any case, write a method of the Pari class.
245
250
# Parse again with an extra "self" argument.
246
251
args , ret = parse_prototype (prototype , help , [PariInstanceArgument ()])
247
252
self .write_method (function , cname , args , ret , args [1 :],
248
- self .instance_file , doc , obsolete )
253
+ self .instance_file , doc , obsolete )
249
254
250
255
def write_declaration (self , cname , args , ret , file ):
251
256
"""
@@ -261,10 +266,12 @@ def write_declaration(self, cname, args, ret, file):
261
266
written to
262
267
"""
263
268
args = ", " .join (a .ctype () for a in args )
264
- s = ' {ret} {function}({args})' .format (ret = ret .ctype (), function = cname , args = args )
269
+ s = ' {ret} {function}({args})' .format (ret = ret .ctype (),
270
+ function = cname , args = args )
265
271
print (s , file = file )
266
272
267
- def write_method (self , function , cname , args , ret , cargs , file , doc , obsolete ):
273
+ def write_method (self , function , cname , args , ret , cargs ,
274
+ file , doc , obsolete ):
268
275
"""
269
276
Write Cython code with a method to call one PARI function.
270
277
@@ -286,7 +293,9 @@ def write_method(self, function, cname, args, ret, cargs, file, doc, obsolete):
286
293
- ``obsolete`` -- if ``True``, a deprecation warning will be
287
294
given whenever this method is called
288
295
"""
289
- doc = doc .replace ("\n " , "\n " ) # Indent doc
296
+ doc = "\n " .join (" " + line .strip () if line else line
297
+ for line in doc .splitlines ())
298
+ # Indent doc, no trailing whitespaces
290
299
291
300
protoargs = ", " .join (a .prototype_code () for a in args )
292
301
callargs = ", " .join (a .call_code () for a in cargs )
@@ -311,7 +320,8 @@ def write_method(self, function, cname, args, ret, cargs, file, doc, obsolete):
311
320
s += ret .assign_code ("{cname}({callargs})" )
312
321
s += ret .return_code ()
313
322
314
- s = s .format (function = function , protoargs = protoargs , cname = cname , callargs = callargs , doc = doc , obsolete = obsolete )
323
+ s = s .format (function = function , protoargs = protoargs , cname = cname ,
324
+ callargs = callargs , doc = doc , obsolete = obsolete )
315
325
print (s , file = file )
316
326
317
327
def __call__ (self ):
@@ -322,11 +332,14 @@ def __call__(self):
322
332
D = sorted (D .values (), key = lambda d : d ['function' ])
323
333
sys .stdout .write ("Generating PARI functions:" )
324
334
325
- self .gen_file = io .open (self .gen_filename + '.tmp' , 'w' , encoding = 'utf-8' )
335
+ self .gen_file = io .open (self .gen_filename + '.tmp' ,
336
+ 'w' , encoding = 'utf-8' )
326
337
self .gen_file .write (gen_banner )
327
- self .instance_file = io .open (self .instance_filename + '.tmp' , 'w' , encoding = 'utf-8' )
338
+ self .instance_file = io .open (self .instance_filename + '.tmp' ,
339
+ 'w' , encoding = 'utf-8' )
328
340
self .instance_file .write (instance_banner )
329
- self .decl_file = io .open (self .decl_filename + '.tmp' , 'w' , encoding = 'utf-8' )
341
+ self .decl_file = io .open (self .decl_filename + '.tmp' ,
342
+ 'w' , encoding = 'utf-8' )
330
343
self .decl_file .write (decl_banner )
331
344
332
345
# Check for availability of hi-res SVG plotting. This requires
@@ -345,7 +358,7 @@ def __call__(self):
345
358
sys .stdout .write (" (%s)" % func )
346
359
sys .stdout .write ("\n " )
347
360
348
- self .instance_file .write ("DEF HAVE_PLOT_SVG = {}" . format ( have_plot_svg ) )
361
+ self .instance_file .write (f "DEF HAVE_PLOT_SVG = { have_plot_svg } " )
349
362
350
363
self .gen_file .close ()
351
364
self .instance_file .close ()
0 commit comments