[Python-checkins] python/dist/src/Lib/test tf_inherit_check.py,NONE,1.1.2.1 test_tempfile.py,1.6,1.6.2.1
gvanrossum@users.sourceforge.net
gvanrossum@users.sourceforge.net
Sat, 17 Aug 2002 04:31:05 -0700
Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv14163
Modified Files:
Tag: tempfile_branch
test_tempfile.py
Added Files:
Tag: tempfile_branch
tf_inherit_check.py
Log Message:
Patch by Zack W to make test_noinherit() more robust.
--- NEW FILE: tf_inherit_check.py ---
# Helper script for test_tempfile.py. argv[2] is the number of a file
# descriptor which should _not_ be open. Check this by attempting to
# write to it -- if we succeed, something is wrong.
import sys
import os
verbose = (sys.argv[1] == 'v')
try:
fd = int(sys.argv[2])
try:
os.write(fd, "blat")
except os.error:
# Success -- could not write to fd.
sys.exit(0)
else:
if verbose:
sys.stderr.write("fd %d is open in child" % fd)
sys.exit(1)
except StandardError:
if verbose:
raise
sys.exit(1)
Index: test_tempfile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_tempfile.py,v
retrieving revision 1.6
retrieving revision 1.6.2.1
diff -C2 -d -r1.6 -r1.6.2.1
*** test_tempfile.py 16 Aug 2002 19:28:59 -0000 1.6
--- test_tempfile.py 17 Aug 2002 11:31:02 -0000 1.6.2.1
***************
*** 18,21 ****
--- 18,22 ----
has_textmode = (tempfile._text_openflags != tempfile._bin_openflags)
+ has_spawnl = hasattr(os, 'spawnl')
# TEST_FILES may need to be tweaked for systems depending on the maximum
***************
*** 324,360 ****
def test_noinherit(self):
"""_mkstemp_inner file handles are not inherited by child processes"""
! # FIXME: Find a way to test this on Windows.
! if os.name != 'posix':
return # ugh, can't use TestSkipped.
! file = self.do_create()
! # We have to exec something, so that FD_CLOEXEC will take
! # effect. The sanest thing to try is /bin/sh; we can easily
! # instruct it to attempt to write to the fd and report success
! # or failure. Unfortunately, sh syntax does not permit use of
! # fds numerically larger than 9; abandon this test if so.
! if file.fd > 9:
! raise test_support.TestSkipped, 'cannot test with fd %d' % file.fd
! pid = os.fork()
! if pid:
! status = os.wait()[1]
! self.failUnless(os.WIFEXITED(status),
! "child process did not exit (status %d)" % status)
! # We want the child to have exited _un_successfully, indicating
! # failure to write to the closed fd.
! self.failUnless(os.WEXITSTATUS(status) != 0,
! "child process exited successfully")
! else:
! try:
! # Throw away stderr.
! nul = os.open('/dev/null', os.O_RDWR)
! os.dup2(nul, 2)
! os.execv('/bin/sh', ['sh', '-c', 'echo blat >&%d' % file.fd])
! except:
! os._exit(0)
def test_textmode(self):
--- 325,355 ----
def test_noinherit(self):
"""_mkstemp_inner file handles are not inherited by child processes"""
! if not has_spawnl:
return # ugh, can't use TestSkipped.
! if test_support.verbose:
! v="v"
! else:
! v="q"
! file = self.do_create()
! fd = "%d" % file.fd
! try:
! me = __file__
! except NameError:
! me = sys.argv[0]
! # We have to exec something, so that FD_CLOEXEC will take
! # effect. The core of this test is therefore in
! # tf_inherit_check.py, which see.
! tester = os.path.join(os.path.dirname(os.path.abspath(me)),
! "tf_inherit_check.py")
! retval = os.spawnl(os.P_WAIT, sys.executable,
! sys.executable, tester, v, fd)
! self.failIf(retval < 0,
! "child process caught fatal signal %d" % -retval)
! self.failIf(retval > 0, "child process reports failure")
def test_textmode(self):