~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/test/test_coding.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import test.support, unittest
 
3
from test.support import TESTFN, unlink
 
4
import os, sys
 
5
 
 
6
class CodingTest(unittest.TestCase):
 
7
    def test_bad_coding(self):
 
8
        module_name = 'bad_coding'
 
9
        self.verify_bad_module(module_name)
 
10
 
 
11
    def test_bad_coding2(self):
 
12
        module_name = 'bad_coding2'
 
13
        self.verify_bad_module(module_name)
 
14
 
 
15
    def verify_bad_module(self, module_name):
 
16
        self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
 
17
 
 
18
        path = os.path.dirname(__file__)
 
19
        filename = os.path.join(path, module_name + '.py')
 
20
        fp = open(filename, "rb")
 
21
        bytes = fp.read()
 
22
        fp.close()
 
23
        self.assertRaises(SyntaxError, compile, bytes, filename, 'exec')
 
24
 
 
25
    def test_exec_valid_coding(self):
 
26
        d = {}
 
27
        exec('# coding: cp949\na = 5\n', d)
 
28
        self.assertEqual(d['a'], 5)
 
29
 
 
30
    def test_file_parse(self):
 
31
        # issue1134: all encodings outside latin-1 and utf-8 fail on
 
32
        # multiline strings and long lines (>512 columns)
 
33
        if TESTFN in sys.modules:
 
34
            del sys.modules[TESTFN]
 
35
        sys.path.insert(0, ".")
 
36
        filename = TESTFN + ".py"
 
37
        f = open(filename, "w")
 
38
        try:
 
39
            f.write("# -*- coding: cp1252 -*-\n")
 
40
            f.write("'''A short string\n")
 
41
            f.write("'''\n")
 
42
            f.write("'A very long string %s'\n" % ("X" * 1000))
 
43
            f.close()
 
44
 
 
45
            __import__(TESTFN)
 
46
        finally:
 
47
            f.close()
 
48
            unlink(TESTFN+".py")
 
49
            unlink(TESTFN+".pyc")
 
50
            sys.path.pop(0)
 
51
 
 
52
def test_main():
 
53
    test.support.run_unittest(CodingTest)
 
54
 
 
55
if __name__ == "__main__":
 
56
    test_main()