import os
import subprocess

## small_file must be less than 20481
small_file='/home/wtorres/tmp/small_file'
## large file must be greater than 20480
large_file='/home/wtorres/tmp/large_file'

def test_popen_bug( file_name ):
    # this is a temp file
    some_file = '/tmp/some_file'

    cmd_line = 'cat %s' % file_name

    ## first, show the work-around ( since this is what will work )
    # make sure we remove this tmp file
    if os.path.isfile( some_file ):
        os.remove( some_file )

    # create a file handle
    print "using my own file handle ....",
    my_fh = open( some_file, 'w'  )
    my_popen = subprocess.Popen( cmd_line, shell = True, stdout = my_fh.fileno(), stderr = subprocess.STDOUT )
    rc = my_popen.wait()
    my_fh.close()
    print "ok"

    ## now show it broken
    print "using PIPE ...",
    my_popen = subprocess.Popen( cmd_line, shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
    rc = my_popen.wait()
    print "ok"

## end test_python_bug()

if __name__ == '__main__':
    # this should work fine
    print "testing with a small file - these should both work"
    test_popen_bug( small_file )

    # this should hang
    print "testing with a large file - the second one here should fail"
    test_popen_bug( large_file )

## done!
