Beispiel #1
0
 def check_close_std_fds(self, fds):
     # Issue #9905: test that subprocess pipes still work properly with
     # some standard fds closed
     stdin = 0
     newfds = []
     for a in fds:
         b = os.dup(a)
         newfds.append(b)
         if a == 0:
             stdin = b
     try:
         for fd in fds:
             os.close(fd)
         out, err = subprocess.Popen([sys.executable, "-c",
                           'import sys;'
                           'sys.stdout.write("apple");'
                           'sys.stdout.flush();'
                           'sys.stderr.write("orange")'],
                    stdin=stdin,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE).communicate()
         err = test_support.strip_python_stderr(err)
         self.assertEqual((out, err), (b'apple', b'orange'))
     finally:
         for b, a in zip(newfds, fds):
             os.dup2(b, a)
         for b in newfds:
             os.close(b)
Beispiel #2
0
def _assert_python(expected_success, *args, **env_vars):
    cmd_line = [sys.executable]
    if not env_vars:
        cmd_line.append('-E')
    cmd_line.extend(args)
    # Need to preserve the original environment, for in-place testing of
    # shared library builds.
    env = os.environ.copy()
    env.update(env_vars)
    p = subprocess.Popen(cmd_line,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         env=env)
    try:
        out, err = p.communicate()
    finally:
        subprocess._cleanup()
        p.stdout.close()
        p.stderr.close()
    rc = p.returncode
    err = strip_python_stderr(err)
    if (rc and expected_success) or (not rc and not expected_success):
        raise AssertionError("Process return code is %d, "
                             "stderr follows:\n%s" %
                             (rc, err.decode('ascii', 'ignore')))
    return rc, out, err
Beispiel #3
0
 def check_close_std_fds(self, fds):
     # Issue #9905: test that subprocess pipes still work properly with
     # some standard fds closed
     stdin = 0
     newfds = []
     for a in fds:
         b = os.dup(a)
         newfds.append(b)
         if a == 0:
             stdin = b
     try:
         for fd in fds:
             os.close(fd)
         out, err = subprocess.Popen([
             sys.executable, "-c", 'import sys;'
             'sys.stdout.write("apple");'
             'sys.stdout.flush();'
             'sys.stderr.write("orange")'
         ],
                                     stdin=stdin,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE).communicate()
         err = test_support.strip_python_stderr(err)
         self.assertEqual((out, err), (b'apple', b'orange'))
     finally:
         for b, a in zip(newfds, fds):
             os.dup2(b, a)
         for b in newfds:
             os.close(b)
Beispiel #4
0
def _assert_python(expected_success, *args, **env_vars):
    cmd_line = [sys.executable]
    if not env_vars:
        cmd_line.append('-E')
    cmd_line.extend(args)
    # Need to preserve the original environment, for in-place testing of
    # shared library builds.
    env = os.environ.copy()
    env.update(env_vars)
    p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                         env=env)
    try:
        out, err = p.communicate()
    finally:
        subprocess._cleanup()
        p.stdout.close()
        p.stderr.close()
    rc = p.returncode
    err =  strip_python_stderr(err)
    if (rc and expected_success) or (not rc and not expected_success):
        raise AssertionError(
            "Process return code is %d, "
            "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
    return rc, out, err
Beispiel #5
0
 def get_hash(self, repr_, seed=None):
     env = os.environ.copy()
     if seed is not None:
         env['PYTHONHASHSEED'] = str(seed)
     else:
         env.pop('PYTHONHASHSEED', None)
     cmd_line = [sys.executable, '-c', self.get_hash_command(repr_)]
     p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                          env=env)
     out, err = p.communicate()
     out = test_support.strip_python_stderr(out)
     return int(out.strip())
Beispiel #6
0
 def get_hash(self, repr_, seed=None):
     env = os.environ.copy()
     if seed is not None:
         env['PYTHONHASHSEED'] = str(seed)
     else:
         env.pop('PYTHONHASHSEED', None)
     cmd_line = [sys.executable, '-c', self.get_hash_command(repr_)]
     p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                          env=env)
     out, err = p.communicate()
     out = test_support.strip_python_stderr(out)
     return int(out.strip())
Beispiel #7
0
    def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
        # open up some temporary files
        temps = [mkstemp() for i in range(3)]
        temp_fds = [fd for fd, fname in temps]
        try:
            # unlink the files -- we won't need to reopen them
            for fd, fname in temps:
                os.unlink(fname)

            # save a copy of the standard file descriptors
            saved_fds = [os.dup(fd) for fd in range(3)]
            try:
                # duplicate the temp files over the standard fd's 0, 1, 2
                for fd, temp_fd in enumerate(temp_fds):
                    os.dup2(temp_fd, fd)

                # write some data to what will become stdin, and rewind
                os.write(stdin_no, b"STDIN")
                os.lseek(stdin_no, 0, 0)

                # now use those files in the given order, so that subprocess
                # has to rearrange them in the child
                p = subprocess.Popen(
                    [
                        sys.executable,
                        "-c",
                        "import sys; got = sys.stdin.read();" 'sys.stdout.write("got %s"%got); sys.stderr.write("err")',
                    ],
                    stdin=stdin_no,
                    stdout=stdout_no,
                    stderr=stderr_no,
                )
                p.wait()

                for fd in temp_fds:
                    os.lseek(fd, 0, 0)

                out = os.read(stdout_no, 1024)
                err = test_support.strip_python_stderr(os.read(stderr_no, 1024))
            finally:
                for std, saved in enumerate(saved_fds):
                    os.dup2(saved, std)
                    os.close(saved)

            self.assertEqual(out, b"got STDIN")
            self.assertEqual(err, b"err")

        finally:
            for fd in temp_fds:
                os.close(fd)
Beispiel #8
0
    def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
        # open up some temporary files
        temps = [mkstemp() for i in range(3)]
        temp_fds = [fd for fd, fname in temps]
        try:
            # unlink the files -- we won't need to reopen them
            for fd, fname in temps:
                os.unlink(fname)

            # save a copy of the standard file descriptors
            saved_fds = [os.dup(fd) for fd in range(3)]
            try:
                # duplicate the temp files over the standard fd's 0, 1, 2
                for fd, temp_fd in enumerate(temp_fds):
                    os.dup2(temp_fd, fd)

                # write some data to what will become stdin, and rewind
                os.write(stdin_no, b"STDIN")
                os.lseek(stdin_no, 0, 0)

                # now use those files in the given order, so that subprocess
                # has to rearrange them in the child
                p = subprocess.Popen([
                    sys.executable, "-c", 'import sys; got = sys.stdin.read();'
                    'sys.stdout.write("got %s"%got); sys.stderr.write("err")'
                ],
                                     stdin=stdin_no,
                                     stdout=stdout_no,
                                     stderr=stderr_no)
                p.wait()

                for fd in temp_fds:
                    os.lseek(fd, 0, 0)

                out = os.read(stdout_no, 1024)
                err = test_support.strip_python_stderr(os.read(
                    stderr_no, 1024))
            finally:
                for std, saved in enumerate(saved_fds):
                    os.dup2(saved, std)
                    os.close(saved)

            self.assertEqual(out, b"got STDIN")
            self.assertEqual(err, b"err")

        finally:
            for fd in temp_fds:
                os.close(fd)
Beispiel #9
0
+        self.assertEqual(len(os.urandom(10)), 10)
+        self.assertEqual(len(os.urandom(100)), 100)
+        self.assertEqual(len(os.urandom(1000)), 1000)
+
+    def test_urandom_value(self):
+        data1 = os.urandom(16)
+        data2 = os.urandom(16)
+        self.assertNotEqual(data1, data2)
+
+    def get_urandom_subprocess(self, count):
+        code = '\n'.join((
+            'import os, sys',
+            'data = os.urandom(%s)' % count,
+            'sys.stdout.write(data)',
+            'sys.stdout.flush()'))
+        cmd_line = [sys.executable, '-c', code]
+        p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
+                             stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+        out, err = p.communicate()
+        out = test_support.strip_python_stderr(out)
+        self.assertEqual(len(out), count)
+        return out
+
+    def test_urandom_subprocess(self):
+        data1 = self.get_urandom_subprocess(16)
+        data2 = self.get_urandom_subprocess(16)
+        self.assertNotEqual(data1, data2)
 
 class Win32ErrorTests(unittest.TestCase):
     def test_rename(self):