<s> from __future__ import with_statement <EOL> from os . path import join as path_join , basename , realpath <EOL> from glob import glob <EOL> from sys import version_info <EOL> import contextlib <EOL> from sandbox . test import SkipTest <EOL> import os <EOL> READ_FILENAME = realpath ( __file__ ) <EOL> with open ( READ_FILENAME , '<STR_LIT:rb>' ) as f : <EOL> FIRST_LINE = f . readline ( ) <EOL> if version_info >= ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> def bytes_literal ( text ) : <EOL> return text . encode ( '<STR_LIT:ascii>' ) <EOL> else : <EOL> def bytes_literal ( text ) : <EOL> return text <EOL> def _getTests ( module_dict , all_tests , keyword ) : <EOL> _locals = module_dict . items ( ) <EOL> for name , value in _locals : <EOL> if not name . startswith ( "<STR_LIT:test>" ) : <EOL> continue <EOL> if keyword and ( keyword not in name [ <NUM_LIT:4> : ] ) : <EOL> continue <EOL> all_tests . append ( value ) <EOL> def getTests ( main_dict , keyword = None ) : <EOL> all_tests = [ ] <EOL> _getTests ( main_dict , all_tests , keyword ) <EOL> for filename in glob ( path_join ( "<STR_LIT>" , "<STR_LIT:test>" , "<STR_LIT>" ) ) : <EOL> module_name = basename ( filename ) [ : - <NUM_LIT:3> ] <EOL> full_module_name = "<STR_LIT>" % module_name <EOL> try : <EOL> parent_module = __import__ ( full_module_name ) <EOL> except SkipTest , skip : <EOL> print ( "<STR_LIT>" % ( module_name , skip ) ) <EOL> continue <EOL> module = getattr ( parent_module . test , module_name ) <EOL> _getTests ( module . __dict__ , all_tests , keyword ) <EOL> all_tests . sort ( key = lambda func : func . __name__ ) <EOL> return all_tests <EOL> def read_first_line ( open ) : <EOL> with open ( READ_FILENAME , '<STR_LIT:rb>' ) as fp : <EOL> line = fp . readline ( ) <EOL> assert line == FIRST_LINE <EOL> @ contextlib . contextmanager <EOL> def capture_stdout ( ) : <EOL> import sys <EOL> import tempfile <EOL> stdout_fd = sys . stdout . fileno ( ) <EOL> with tempfile . TemporaryFile ( mode = '<STR_LIT>' ) as tmp : <EOL> stdout_copy = os . dup ( stdout_fd ) <EOL> try : <EOL> sys . stdout . flush ( ) <EOL> os . dup2 ( tmp . fileno ( ) , stdout_fd ) <EOL> yield tmp <EOL> finally : <EOL> sys . stdout . flush ( ) <EOL> os . dup2 ( stdout_copy , stdout_fd ) <EOL> os . close ( stdout_copy ) </s>
