def _get_closest_end(end_after, begin_after):
 end_iter = iter(end_after)
 begin_iter = iter(begin_after)
 while True:
  try:
   e = next(end_iter)
  except:
   raise NoEnvError('No closing environment detected')
  try:
   b = next(begin_iter)
  except:
   break
  if (not (e.begin() > b.begin())):
   break
 return e

def _get_closest_end ( end_after , begin_after ) : end_iter = iter ( end_after ) begin_iter = iter ( begin_after ) while True : try : e = next ( end_iter ) except : raise NoEnvError ( 'No closing environment detected' ) try : b = next ( begin_iter ) except : break if ( not ( e . begin ( ) > b . begin ( ) ) ) : break return e
returns the closest end .