@@ -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 ):
@@ -346,15 +345,7 @@ def _survives_exclude(self, matchstr, match_type):
346345 L = len (self .package_name )
347346 if matchstr [:L ] == self .package_name :
348347 matchstr = matchstr [L :]
349- for pat in patterns :
350- try :
351- pat .search
352- except AttributeError :
353- pat = re .compile (pat )
354- if pat .search (matchstr ):
355- return False
356-
357- return True
348+ return not any (re .search (pat , matchstr ) for pat in patterns )
358349
359350 def discover_modules (self ):
360351 r"""Return module sequence discovered from ``self.package_name``
@@ -434,11 +425,8 @@ def write_modules_api(self, modules, outdir):
434425 document_body .append (body )
435426
436427 out_module = ulm + self .rst_extension
437- outfile = os .path .join (outdir , out_module )
438- fileobj = open (outfile , 'w' )
439-
440- fileobj .writelines (document_head + document_body )
441- fileobj .close ()
428+ with open (os .path .join (outdir , out_module ), 'w' ) as fileobj :
429+ fileobj .writelines (document_head + document_body )
442430 written_modules .append (out_module )
443431
444432 self .written_modules = written_modules
@@ -493,14 +481,13 @@ def write_index(self, outdir, froot='gen', relative_to=None):
493481 relpath = (outdir + os .path .sep ).replace (relative_to + os .path .sep , '' )
494482 else :
495483 relpath = outdir
496- idx = open (path , 'w' )
497- w = idx .write
498- w ('.. AUTO-GENERATED FILE -- DO NOT EDIT!\n \n ' )
499-
500- title = 'API Reference'
501- w (title + '\n ' )
502- w ('=' * len (title ) + '\n \n ' )
503- w ('.. toctree::\n \n ' )
504- for f in self .written_modules :
505- w (f' { os .path .join (relpath , f )} \n ' )
506- 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