22Auto-generate methods for PARI functions.
33"""
44
5- #* ****************************************************************************
5+ # ****************************************************************************
66# Copyright (C) 2015 Jeroen Demeyer <[email protected] > 77# 2017 Vincent Delecroix <[email protected] > 88#
99# This program is free software: you can redistribute it and/or modify
1010# it under the terms of the GNU General Public License as published by
1111# the Free Software Foundation, either version 2 of the License, or
1212# (at your option) any later version.
13- # http ://www.gnu.org/licenses/
14- #* ****************************************************************************
13+ # https ://www.gnu.org/licenses/
14+ # ****************************************************************************
1515
1616from __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+
1822
1923from .args import PariArgumentGEN , PariInstanceArgument
2024from .parser import read_pari_desc , parse_prototype
5862
5963
6064function_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+ }
7276
7377
7478class PariFunctionGenerator (object ):
@@ -119,7 +123,8 @@ def can_handle_function(self, function, cname="", **kwds):
119123 return False
120124 return True
121125
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 ):
123128 r"""
124129 Handle one PARI function: decide whether or not to add the
125130 function, in which file (as method of :class:`Gen` or
@@ -239,13 +244,13 @@ def polredord(self, x):
239244 # If the first argument is a GEN, write a method of the
240245 # Gen class.
241246 self .write_method (function , cname , args , ret , args ,
242- self .gen_file , doc , obsolete )
247+ self .gen_file , doc , obsolete )
243248
244249 # In any case, write a method of the Pari class.
245250 # Parse again with an extra "self" argument.
246251 args , ret = parse_prototype (prototype , help , [PariInstanceArgument ()])
247252 self .write_method (function , cname , args , ret , args [1 :],
248- self .instance_file , doc , obsolete )
253+ self .instance_file , doc , obsolete )
249254
250255 def write_declaration (self , cname , args , ret , file ):
251256 """
@@ -261,10 +266,12 @@ def write_declaration(self, cname, args, ret, file):
261266 written to
262267 """
263268 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 )
265271 print (s , file = file )
266272
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 ):
268275 """
269276 Write Cython code with a method to call one PARI function.
270277
@@ -286,7 +293,9 @@ def write_method(self, function, cname, args, ret, cargs, file, doc, obsolete):
286293 - ``obsolete`` -- if ``True``, a deprecation warning will be
287294 given whenever this method is called
288295 """
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
290299
291300 protoargs = ", " .join (a .prototype_code () for a in args )
292301 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):
311320 s += ret .assign_code ("{cname}({callargs})" )
312321 s += ret .return_code ()
313322
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 )
315325 print (s , file = file )
316326
317327 def __call__ (self ):
@@ -322,11 +332,14 @@ def __call__(self):
322332 D = sorted (D .values (), key = lambda d : d ['function' ])
323333 sys .stdout .write ("Generating PARI functions:" )
324334
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' )
326337 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' )
328340 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' )
330343 self .decl_file .write (decl_banner )
331344
332345 # Check for availability of hi-res SVG plotting. This requires
@@ -345,7 +358,7 @@ def __call__(self):
345358 sys .stdout .write (" (%s)" % func )
346359 sys .stdout .write ("\n " )
347360
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 } " )
349362
350363 self .gen_file .close ()
351364 self .instance_file .close ()
0 commit comments