~ubuntu-branches/ubuntu/precise/python3.2/precise-proposed

« back to all changes in this revision

Viewing changes to Lib/test/test_cmd_line.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-03-09 18:40:39 UTC
  • mfrom: (30.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120309184039-j3yk2emxr1plyo21
Tags: 3.2.3~rc1-1
* Python 3.2.3 release candidate 1.
* Update to 20120309 from the 3.2 branch.
* Fix libpython.a symlink. Closes: #660146.
* Build-depend on xauth.
* Run the gdb tests for the debug build only.

Show diffs side-by-side

added added

removed removed

Lines of Context:
272
272
        self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError')
273
273
        self.assertEqual(b'', out)
274
274
 
 
275
    def test_stdout_flush_at_shutdown(self):
 
276
        # Issue #5319: if stdout.flush() fails at shutdown, an error should
 
277
        # be printed out.
 
278
        code = """if 1:
 
279
            import os, sys
 
280
            sys.stdout.write('x')
 
281
            os.close(sys.stdout.fileno())"""
 
282
        rc, out, err = assert_python_ok('-c', code)
 
283
        self.assertEqual(b'', out)
 
284
        self.assertRegex(err.decode('ascii', 'ignore'),
 
285
                         'Exception IOError: .* ignored')
 
286
 
 
287
    def test_closed_stdout(self):
 
288
        # Issue #13444: if stdout has been explicitly closed, we should
 
289
        # not attempt to flush it at shutdown.
 
290
        code = "import sys; sys.stdout.close()"
 
291
        rc, out, err = assert_python_ok('-c', code)
 
292
        self.assertEqual(b'', err)
 
293
 
 
294
    # Issue #7111: Python should work without standard streams
 
295
 
 
296
    @unittest.skipIf(os.name != 'posix', "test needs POSIX semantics")
 
297
    def _test_no_stdio(self, streams):
 
298
        code = """if 1:
 
299
            import os, sys
 
300
            for i, s in enumerate({streams}):
 
301
                if getattr(sys, s) is not None:
 
302
                    os._exit(i + 1)
 
303
            os._exit(42)""".format(streams=streams)
 
304
        def preexec():
 
305
            if 'stdin' in streams:
 
306
                os.close(0)
 
307
            if 'stdout' in streams:
 
308
                os.close(1)
 
309
            if 'stderr' in streams:
 
310
                os.close(2)
 
311
        p = subprocess.Popen(
 
312
            [sys.executable, "-E", "-c", code],
 
313
            stdin=subprocess.PIPE,
 
314
            stdout=subprocess.PIPE,
 
315
            stderr=subprocess.PIPE,
 
316
            preexec_fn=preexec)
 
317
        out, err = p.communicate()
 
318
        self.assertEqual(test.support.strip_python_stderr(err), b'')
 
319
        self.assertEqual(p.returncode, 42)
 
320
 
 
321
    def test_no_stdin(self):
 
322
        self._test_no_stdio(['stdin'])
 
323
 
 
324
    def test_no_stdout(self):
 
325
        self._test_no_stdio(['stdout'])
 
326
 
 
327
    def test_no_stderr(self):
 
328
        self._test_no_stdio(['stderr'])
 
329
 
 
330
    def test_no_std_streams(self):
 
331
        self._test_no_stdio(['stdin', 'stdout', 'stderr'])
 
332
 
 
333
    def test_hash_randomization(self):
 
334
        # Verify that -R enables hash randomization:
 
335
        self.verify_valid_flag('-R')
 
336
        hashes = []
 
337
        for i in range(2):
 
338
            code = 'print(hash("spam"))'
 
339
            rc, out, err = assert_python_ok('-R', '-c', code)
 
340
            self.assertEqual(rc, 0)
 
341
            hashes.append(out)
 
342
        self.assertNotEqual(hashes[0], hashes[1])
 
343
 
 
344
        # Verify that sys.flags contains hash_randomization
 
345
        code = 'import sys; print("random is", sys.flags.hash_randomization)'
 
346
        rc, out, err = assert_python_ok('-R', '-c', code)
 
347
        self.assertEqual(rc, 0)
 
348
        self.assertIn(b'random is 1', out)
275
349
 
276
350
def test_main():
277
351
    test.support.run_unittest(CmdLineTest)