MCPcopy
hub / github.com/amoffat/sh / test_close_fds

Method test_close_fds

tests/sh_test.py:664–688  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

662 t.close()
663
664 def test_close_fds(self):
665 # guarantee some extra fds in our parent process that don't close on exec.
666 # we have to explicitly do this because at some point (I believe python 3.4),
667 # python started being more stringent with closing fds to prevent security
668 # vulnerabilities. python 2.7, for example, doesn't set CLOEXEC on
669 # tempfile.TemporaryFile()s
670 #
671 # https://www.python.org/dev/peps/pep-0446/
672 tmp = [tempfile.TemporaryFile() for i in range(10)]
673 for t in tmp:
674 flags = fcntl.fcntl(t.fileno(), fcntl.F_GETFD)
675 flags &= ~fcntl.FD_CLOEXEC
676 fcntl.fcntl(t.fileno(), fcntl.F_SETFD, flags)
677
678 py = create_tmp_test(
679 """
680import os
681print(os.listdir("/dev/fd"))
682"""
683 )
684 out = python(py.name).strip()
685 self.assertEqual(out, "['0', '1', '2', '3']")
686
687 for t in tmp:
688 t.close()
689
690 def test_pass_fds(self):
691 # guarantee some extra fds in our parent process that don't close on exec.

Callers

nothing calls this directly

Calls 3

create_tmp_testFunction · 0.85
filenoMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected