133
135
self.fail("no exception")
135
137
# test that the exit machinery handles SystemExits properly
137
138
rc = subprocess.call([sys.executable, "-c",
138
139
"raise SystemExit(47)"])
139
140
self.assertEqual(rc, 47)
142
def check_exit_message(code, expected):
143
process = subprocess.Popen([sys.executable, "-c", code],
144
stderr=subprocess.PIPE)
145
stdout, stderr = process.communicate()
146
self.assertEqual(process.returncode, 1)
147
self.assertTrue(stderr.startswith(expected),
148
"%s doesn't start with %s" % (ascii(stderr), ascii(expected)))
150
# test that stderr buffer if flushed before the exit message is written
153
r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
154
b"unflushed,message")
156
# test that the exit message is written with backslashreplace error
159
r'import sys; sys.exit("surrogates:\uDCFF")',
160
b"surrogates:\\udcff")
141
162
def test_getdefaultencoding(self):
142
163
self.assertRaises(TypeError, sys.getdefaultencoding, 42)
143
164
# can't check more than the type, as the user might have changed it
404
425
self.assertRaises(TypeError, sys.intern, S("abc"))
427
def test_main_invalid_unicode(self):
429
non_decodable = b"\xff"
430
encoding = locale.getpreferredencoding()
432
non_decodable.decode(encoding)
433
except UnicodeDecodeError:
436
self.skipTest('%r is decodable with encoding %s'
437
% (non_decodable, encoding))
438
code = b'print("' + non_decodable + b'")'
439
p = subprocess.Popen([sys.executable, "-c", code], stderr=subprocess.PIPE)
440
stdout, stderr = p.communicate()
441
self.assertEqual(p.returncode, 1)
442
self.assert_(b"UnicodeEncodeError:" in stderr,
443
"%r not in %s" % (b"UniodeEncodeError:", ascii(stderr)))
407
445
def test_sys_flags(self):
408
446
self.assertTrue(sys.flags)
438
476
out = p.stdout.read().strip()
439
477
self.assertEqual(out, b'?')
479
def test_executable(self):
480
# Issue #7774: Ensure that sys.executable is an empty string if argv[0]
481
# has been set to an non existent program name and Python is unable to
482
# retrieve the real program name
484
# For a normal installation, it should work without 'cwd'
485
# argument. For test runs in the build directory, see #7774.
486
python_dir = os.path.dirname(os.path.realpath(sys.executable))
487
p = subprocess.Popen(
488
["nonexistent", "-c",
489
'import sys; print(sys.executable.encode("ascii", "backslashreplace"))'],
490
executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir)
491
stdout = p.communicate()[0]
492
executable = stdout.strip().decode("ASCII")
494
self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))])
442
497
class SizeofTest(unittest.TestCase):
738
793
check(sys.flags, size(vh) + self.P * len(sys.flags))
795
def test_getfilesystemencoding(self):
798
def check_fsencoding(fs_encoding):
799
if sys.platform == 'darwin':
800
self.assertEqual(fs_encoding, 'utf-8')
801
elif fs_encoding is None:
803
codecs.lookup(fs_encoding)
805
fs_encoding = sys.getfilesystemencoding()
806
check_fsencoding(fs_encoding)
810
sys.executable.encode('ascii')
811
except UnicodeEncodeError:
812
# Python doesn't start with ASCII locale if its path is not ASCII,
816
env = os.environ.copy()
818
output = subprocess.check_output(
819
[sys.executable, "-c",
820
"import sys; print(sys.getfilesystemencoding())"],
822
fs_encoding = output.rstrip().decode('ascii')
823
check_fsencoding(fs_encoding)
740
825
def test_setfilesystemencoding(self):
741
826
old = sys.getfilesystemencoding()
742
sys.setfilesystemencoding("iso-8859-1")
743
self.assertEqual(sys.getfilesystemencoding(), "iso-8859-1")
744
sys.setfilesystemencoding(old)
828
sys.setfilesystemencoding("iso-8859-1")
829
self.assertEqual(sys.getfilesystemencoding(), "iso-8859-1")
831
sys.setfilesystemencoding(old)
833
self.assertRaises(LookupError, sys.setfilesystemencoding, "xxx")
835
sys.setfilesystemencoding(old)
747
838
test.support.run_unittest(SysModuleTest, SizeofTest)