272
272
self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError')
273
273
self.assertEqual(b'', out)
275
def test_stdout_flush_at_shutdown(self):
276
# Issue #5319: if stdout.flush() fails at shutdown, an error should
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')
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)
294
# Issue #7111: Python should work without standard streams
296
@unittest.skipIf(os.name != 'posix', "test needs POSIX semantics")
297
def _test_no_stdio(self, streams):
300
for i, s in enumerate({streams}):
301
if getattr(sys, s) is not None:
303
os._exit(42)""".format(streams=streams)
305
if 'stdin' in streams:
307
if 'stdout' in streams:
309
if 'stderr' in streams:
311
p = subprocess.Popen(
312
[sys.executable, "-E", "-c", code],
313
stdin=subprocess.PIPE,
314
stdout=subprocess.PIPE,
315
stderr=subprocess.PIPE,
317
out, err = p.communicate()
318
self.assertEqual(test.support.strip_python_stderr(err), b'')
319
self.assertEqual(p.returncode, 42)
321
def test_no_stdin(self):
322
self._test_no_stdio(['stdin'])
324
def test_no_stdout(self):
325
self._test_no_stdio(['stdout'])
327
def test_no_stderr(self):
328
self._test_no_stdio(['stderr'])
330
def test_no_std_streams(self):
331
self._test_no_stdio(['stdin', 'stdout', 'stderr'])
333
def test_hash_randomization(self):
334
# Verify that -R enables hash randomization:
335
self.verify_valid_flag('-R')
338
code = 'print(hash("spam"))'
339
rc, out, err = assert_python_ok('-R', '-c', code)
340
self.assertEqual(rc, 0)
342
self.assertNotEqual(hashes[0], hashes[1])
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)
277
351
test.support.run_unittest(CmdLineTest)