@@ -185,9 +185,8 @@ def _parse_module(self, uri):
185185 # nothing that we could handle here.
186186 return ([], [])
187187
188- f = open (filename )
189- functions , classes = self ._parse_lines (f )
190- f .close ()
188+ with open (filename ) as f :
189+ functions , classes = self ._parse_lines (f )
191190 return functions , classes
192191
193192 def _parse_module_with_import (self , uri ):
@@ -220,9 +219,7 @@ def _parse_module_with_import(self, uri):
220219 if not self .other_defines and getmodule (obj ) != mod :
221220 continue
222221 # figure out if obj is a function or class
223- if (
224- hasattr (obj , 'func_name' ) or isinstance (obj , (BuiltinFunctionType , FunctionType ))
225- ):
222+ if hasattr (obj , 'func_name' ) or isinstance (obj , (BuiltinFunctionType , FunctionType )):
226223 functions .append (obj_str )
227224 else :
228225 try :
@@ -348,15 +345,7 @@ def _survives_exclude(self, matchstr, match_type):
348345 L = len (self .package_name )
349346 if matchstr [:L ] == self .package_name :
350347 matchstr = matchstr [L :]
351- for pat in patterns :
352- try :
353- pat .search
354- except AttributeError :
355- pat = re .compile (pat )
356- if pat .search (matchstr ):
357- return False
358-
359- return True
348+ return not any (re .search (pat , matchstr ) for pat in patterns )
360349
361350 def discover_modules (self ):
362351 r"""Return module sequence discovered from ``self.package_name``
@@ -436,11 +425,8 @@ def write_modules_api(self, modules, outdir):
436425 document_body .append (body )
437426
438427 out_module = ulm + self .rst_extension
439- outfile = os .path .join (outdir , out_module )
440- fileobj = open (outfile , 'w' )
441-
442- fileobj .writelines (document_head + document_body )
443- fileobj .close ()
428+ with open (os .path .join (outdir , out_module ), 'w' ) as fileobj :
429+ fileobj .writelines (document_head + document_body )
444430 written_modules .append (out_module )
445431
446432 self .written_modules = written_modules
@@ -495,14 +481,13 @@ def write_index(self, outdir, froot='gen', relative_to=None):
495481 relpath = (outdir + os .path .sep ).replace (relative_to + os .path .sep , '' )
496482 else :
497483 relpath = outdir
498- idx = open (path , 'w' )
499- w = idx .write
500- w ('.. AUTO-GENERATED FILE -- DO NOT EDIT!\n \n ' )
501-
502- title = 'API Reference'
503- w (title + '\n ' )
504- w ('=' * len (title ) + '\n \n ' )
505- w ('.. toctree::\n \n ' )
506- for f in self .written_modules :
507- w (f' { os .path .join (relpath , f )} \n ' )
508- idx .close ()
484+ with open (path , 'w' ) as idx :
485+ w = idx .write
486+ w ('.. AUTO-GENERATED FILE -- DO NOT EDIT!\n \n ' )
487+
488+ title = 'API Reference'
489+ w (title + '\n ' )
490+ w ('=' * len (title ) + '\n \n ' )
491+ w ('.. toctree::\n \n ' )
492+ for f in self .written_modules :
493+ w (f' { os .path .join (relpath , f )} \n ' )
0 commit comments