4040# recognize a function definition in rst
4141is_func = re .compile (r"\.\.( )+(c:)?function( )*::" )
4242# rename types to avoid python -- c name collisions
43- rename_types = [(re .compile (r"\bfmpz\b" ),"fmpz_struct" ),(re .compile (r"\bfmpq\b" ), "fmpq_struct" )]
43+ rename_types = [(re .compile (r"\bfmpz\b" ), "fmpz_struct" ), (re .compile (r"\bfmpq\b" ), "fmpq_struct" )]
4444# comment out functions which use these types
4545comment_types = re .compile (r"(\bFILE\b)|(\bmpz_t\b)|(\bmpq_t\b)" )
4646comment_set = set (["FILE" , "mpz_t" , "mpq_t" ])
4747c_types = set (["char" , "short" , "long" , "int" , "float" , "double" ])
4848type_modifers = re .compile (r"\*|(\bconst\b)|(\bunsigned\b)|(\bsigned\b)" )
4949import_dict = {}
5050
51+
5152def get_cython_struct_types (file ):
5253 """
5354 Extract cython types from a pxd file.
@@ -63,6 +64,7 @@ def get_cython_struct_types(file):
6364 ret .append (l .split ()[- 1 ])
6465 return ret
6566
67+
6668def fill_import_dict (pyflintlibdir ):
6769 """
6870 Get a map from cython structs to the pxd that defines them
@@ -74,6 +76,7 @@ def fill_import_dict(pyflintlibdir):
7476 for t in get_cython_struct_types (pxd ):
7577 import_dict [t ] = f .name .split ('.' )[0 ]
7678
79+
7780def undecorate (str ):
7881 """
7982 remove variable name, const, ``*``, etc. to just get types
@@ -83,16 +86,19 @@ def undecorate(str):
8386 ret = re .sub (type_modifers , '' , ret )
8487 return ret .strip ()
8588
89+
8690def get_parameter_types (str ):
8791 params = str [str .find ("(" ) + 1 : str .rfind (")" )].split ("," )
8892 return [undecorate (s ) for s in params ]
8993
94+
9095def clean_types (function ):
9196 ret = function .strip ()
9297 for old , new in rename_types :
9398 ret = re .sub (old , new , ret )
9499 return ret
95100
101+
96102def get_functions (file ):
97103 """
98104 Get a list of functions from an rst file
@@ -102,7 +108,7 @@ def get_functions(file):
102108 for line in file :
103109 m = is_func .match (line )
104110 if m :
105- ret .append ( clean_types (line [m .end ():]))
111+ ret .append (clean_types (line [m .end ():]))
106112 in_list = True
107113 else :
108114 if in_list :
@@ -112,13 +118,15 @@ def get_functions(file):
112118 ret .append (clean_types (line ))
113119 return ret
114120
121+
115122def get_all_types (function_list ):
116123 ret = set ()
117124 for f in function_list :
118125 for t in get_parameter_types (f ):
119126 ret .add (t )
120127 return ret
121128
129+
122130def gen_imports (function_list ):
123131 """
124132 Generate import statements for known functions.
@@ -132,26 +140,27 @@ def gen_imports(function_list):
132140 imports [import_dict [t ]].append (t )
133141 else :
134142 ret .add (t )
135- for k ,v in imports .items ():
143+ for k , v in imports .items ():
136144 types = ", " .join (v )
137145 print ("from flint.flintlib." + k + " cimport " + types )
138146 return ret
139147
148+
140149def generate_pxd_file (h_name , opts ):
141150 fill_import_dict (opts .flint_lib_dir )
142- l = []
151+ l = []
143152 docdir = opts .arb_doc_dir
144- name = h_name
153+ name = h_name
145154 if name [:6 ] == "flint/" :
146155 docdir = opts .flint_doc_dir
147156 name = name [6 :]
148157 with open (os .path .join (docdir , name + ".rst" )) as f :
149158 l = get_functions (f )
150159 s = gen_imports (l )
151160 print ()
152- print ("\n # unimported types " , s - comment_set )
161+ print ("\n # unimported types " , s - comment_set )
153162 print ()
154- print (r'cdef extern from "' + h_name + r'.h":' )
163+ print (r'cdef extern from "' + h_name + r'.h":' )
155164 for f in l :
156165 if comment_types .search (f ):
157166 print (" # " + f )
@@ -181,4 +190,3 @@ def main(*args):
181190
182191if __name__ == "__main__" :
183192 main (* sys .argv [1 :])
184-
0 commit comments