~ubuntu-branches/ubuntu/natty/python3.1/natty-security

« back to all changes in this revision

Viewing changes to Lib/test/test_sys.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-07-06 16:52:42 UTC
  • mfrom: (1.2.1 upstream) (2.1.11 sid)
  • Revision ID: james.westby@ubuntu.com-20100706165242-2xv4i019r3et6c0j
Tags: 3.1.2+20100706-1ubuntu1
* Merge with Debian; remaining changes:
  - Regenerate the control file.
  - Add debian/patches/overwrite-semaphore-check for Lucid buildds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
    # Python/pythonrun.c::PyErr_PrintEx() is tricky.
80
80
 
81
81
    def test_exit(self):
 
82
        import subprocess
 
83
 
82
84
        self.assertRaises(TypeError, sys.exit, 42, 42)
83
85
 
84
86
        # call without argument
133
135
            self.fail("no exception")
134
136
 
135
137
        # test that the exit machinery handles SystemExits properly
136
 
        import subprocess
137
138
        rc = subprocess.call([sys.executable, "-c",
138
139
                              "raise SystemExit(47)"])
139
140
        self.assertEqual(rc, 47)
140
141
 
 
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)))
 
149
 
 
150
        # test that stderr buffer if flushed before the exit message is written
 
151
        # into stderr
 
152
        check_exit_message(
 
153
            r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
 
154
            b"unflushed,message")
 
155
 
 
156
        # test that the exit message is written with backslashreplace error
 
157
        # handler to stderr
 
158
        check_exit_message(
 
159
            r'import sys; sys.exit("surrogates:\uDCFF")',
 
160
            b"surrogates:\\udcff")
 
161
 
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
403
424
 
404
425
        self.assertRaises(TypeError, sys.intern, S("abc"))
405
426
 
 
427
    def test_main_invalid_unicode(self):
 
428
        import locale
 
429
        non_decodable = b"\xff"
 
430
        encoding = locale.getpreferredencoding()
 
431
        try:
 
432
            non_decodable.decode(encoding)
 
433
        except UnicodeDecodeError:
 
434
            pass
 
435
        else:
 
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)))
406
444
 
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'?')
440
478
 
 
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
 
483
        import subprocess
 
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")
 
493
        p.wait()
 
494
        self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))])
 
495
 
441
496
 
442
497
class SizeofTest(unittest.TestCase):
443
498
 
737
792
        # sys.flags
738
793
        check(sys.flags, size(vh) + self.P * len(sys.flags))
739
794
 
 
795
    def test_getfilesystemencoding(self):
 
796
        import codecs
 
797
 
 
798
        def check_fsencoding(fs_encoding):
 
799
            if sys.platform == 'darwin':
 
800
                self.assertEqual(fs_encoding, 'utf-8')
 
801
            elif fs_encoding is None:
 
802
                return
 
803
            codecs.lookup(fs_encoding)
 
804
 
 
805
        fs_encoding = sys.getfilesystemencoding()
 
806
        check_fsencoding(fs_encoding)
 
807
 
 
808
        # Even in C locale
 
809
        try:
 
810
            sys.executable.encode('ascii')
 
811
        except UnicodeEncodeError:
 
812
            # Python doesn't start with ASCII locale if its path is not ASCII,
 
813
            # see issue #8611
 
814
            pass
 
815
        else:
 
816
            env = os.environ.copy()
 
817
            env['LANG'] = 'C'
 
818
            output = subprocess.check_output(
 
819
                [sys.executable, "-c",
 
820
                 "import sys; print(sys.getfilesystemencoding())"],
 
821
                env=env)
 
822
            fs_encoding = output.rstrip().decode('ascii')
 
823
            check_fsencoding(fs_encoding)
 
824
 
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)
 
827
        try:
 
828
            sys.setfilesystemencoding("iso-8859-1")
 
829
            self.assertEqual(sys.getfilesystemencoding(), "iso-8859-1")
 
830
        finally:
 
831
            sys.setfilesystemencoding(old)
 
832
        try:
 
833
            self.assertRaises(LookupError, sys.setfilesystemencoding, "xxx")
 
834
        finally:
 
835
            sys.setfilesystemencoding(old)
745
836
 
746
837
def test_main():
747
838
    test.support.run_unittest(SysModuleTest, SizeofTest)