blob: 2f1c410b1bfa255dd6200d30a88a4a6fb3d58b8e [file] [log] [blame]
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +00001# Test various flavors of legal and illegal future statements
2
Neal Norwitz328f3382003-12-13 22:43:34 +00003import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Serhiy Storchaka8b583392016-12-11 14:39:01 +02005import os
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +00006import re
7
R David Murray44b548d2016-09-08 13:59:53 -04008rx = re.compile(r'\((\S+).py, line (\d+)')
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +00009
Neal Norwitz328f3382003-12-13 22:43:34 +000010def get_error_location(msg):
11 mo = rx.search(str(msg))
12 return mo.group(1, 2)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000013
Neal Norwitz328f3382003-12-13 22:43:34 +000014class FutureTest(unittest.TestCase):
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000015
Serhiy Storchaka8b583392016-12-11 14:39:01 +020016 def check_syntax_error(self, err, basename, lineno, offset=0):
17 self.assertIn('%s.py, line %d' % (basename, lineno), str(err))
18 self.assertEqual(os.path.basename(err.filename), basename + '.py')
19 self.assertEqual(err.lineno, lineno)
20 self.assertEqual(err.offset, offset)
21
Neal Norwitz328f3382003-12-13 22:43:34 +000022 def test_future1(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020023 with support.CleanImport('future_test1'):
24 from test import future_test1
25 self.assertEqual(future_test1.result, 6)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000026
Neal Norwitz328f3382003-12-13 22:43:34 +000027 def test_future2(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020028 with support.CleanImport('future_test2'):
29 from test import future_test2
30 self.assertEqual(future_test2.result, 6)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000031
Neal Norwitz328f3382003-12-13 22:43:34 +000032 def test_future3(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020033 with support.CleanImport('test_future3'):
34 from test import test_future3
Jeremy Hylton8471a352001-08-20 20:33:42 +000035
Neal Norwitz328f3382003-12-13 22:43:34 +000036 def test_badfuture3(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020037 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000038 from test import badsyntax_future3
Serhiy Storchaka8b583392016-12-11 14:39:01 +020039 self.check_syntax_error(cm.exception, "badsyntax_future3", 3)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000040
Neal Norwitz328f3382003-12-13 22:43:34 +000041 def test_badfuture4(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020042 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000043 from test import badsyntax_future4
Serhiy Storchaka8b583392016-12-11 14:39:01 +020044 self.check_syntax_error(cm.exception, "badsyntax_future4", 3)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000045
Neal Norwitz328f3382003-12-13 22:43:34 +000046 def test_badfuture5(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020047 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000048 from test import badsyntax_future5
Serhiy Storchaka8b583392016-12-11 14:39:01 +020049 self.check_syntax_error(cm.exception, "badsyntax_future5", 4)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000050
Neal Norwitz328f3382003-12-13 22:43:34 +000051 def test_badfuture6(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020052 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000053 from test import badsyntax_future6
Serhiy Storchaka8b583392016-12-11 14:39:01 +020054 self.check_syntax_error(cm.exception, "badsyntax_future6", 3)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000055
Neal Norwitz328f3382003-12-13 22:43:34 +000056 def test_badfuture7(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020057 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000058 from test import badsyntax_future7
Serhiy Storchaka8b583392016-12-11 14:39:01 +020059 self.check_syntax_error(cm.exception, "badsyntax_future7", 3, 53)
Neal Norwitz328f3382003-12-13 22:43:34 +000060
61 def test_badfuture8(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020062 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000063 from test import badsyntax_future8
Serhiy Storchaka8b583392016-12-11 14:39:01 +020064 self.check_syntax_error(cm.exception, "badsyntax_future8", 3)
Neal Norwitz328f3382003-12-13 22:43:34 +000065
66 def test_badfuture9(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020067 with self.assertRaises(SyntaxError) as cm:
Neal Norwitz328f3382003-12-13 22:43:34 +000068 from test import badsyntax_future9
Serhiy Storchaka8b583392016-12-11 14:39:01 +020069 self.check_syntax_error(cm.exception, "badsyntax_future9", 3, 0)
Neal Norwitz328f3382003-12-13 22:43:34 +000070
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070071 def test_badfuture10(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +020072 with self.assertRaises(SyntaxError) as cm:
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070073 from test import badsyntax_future10
Serhiy Storchaka8b583392016-12-11 14:39:01 +020074 self.check_syntax_error(cm.exception, "badsyntax_future10", 3, 0)
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070075
Thomas Wouters89f507f2006-12-13 04:49:30 +000076 def test_parserhack(self):
77 # test that the parser.c::future_hack function works as expected
78 # Note: although this test must pass, it's not testing the original
79 # bug as of 2.6 since the with statement is not optional and
80 # the parser hack disabled. If a new keyword is introduced in
81 # 2.6, change this to refer to the new future import.
82 try:
Benjamin Peterson9aebc612008-10-26 20:58:53 +000083 exec("from __future__ import print_function; print 0")
Thomas Wouters89f507f2006-12-13 04:49:30 +000084 except SyntaxError:
85 pass
86 else:
87 self.fail("syntax error didn't occur")
88
89 try:
Benjamin Peterson9aebc612008-10-26 20:58:53 +000090 exec("from __future__ import (print_function); print 0")
Thomas Wouters89f507f2006-12-13 04:49:30 +000091 except SyntaxError:
92 pass
93 else:
94 self.fail("syntax error didn't occur")
95
Benjamin Peterson9aebc612008-10-26 20:58:53 +000096 def test_multiple_features(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020097 with support.CleanImport("test.test_future5"):
98 from test import test_future5
Benjamin Peterson9aebc612008-10-26 20:58:53 +000099
Benjamin Petersonf216c942008-10-31 02:28:05 +0000100 def test_unicode_literals_exec(self):
101 scope = {}
102 exec("from __future__ import unicode_literals; x = ''", {}, scope)
Ezio Melottie9615932010-01-24 19:26:24 +0000103 self.assertIsInstance(scope["x"], str)
Benjamin Petersonf216c942008-10-31 02:28:05 +0000104
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105
Neal Norwitz328f3382003-12-13 22:43:34 +0000106
107if __name__ == "__main__":
Ezio Melotti1ed6be32013-02-27 10:00:03 +0200108 unittest.main()