Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
"""
|
|
|
|
Test the API of the symtable module.
|
|
|
|
"""
|
2005-11-25 11:15:49 +08:00
|
|
|
import symtable
|
2006-01-23 15:49:36 +08:00
|
|
|
import unittest
|
2001-02-03 02:24:26 +08:00
|
|
|
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
from test import support
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CODE = """
|
|
|
|
import sys
|
|
|
|
|
|
|
|
glob = 42
|
|
|
|
|
|
|
|
class Mine:
|
|
|
|
instance_var = 24
|
|
|
|
def a_method(p1, p2):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def spam(a, b, *var, **kw):
|
|
|
|
global bar
|
|
|
|
bar = 47
|
|
|
|
x = 23
|
|
|
|
glob
|
|
|
|
def internal():
|
|
|
|
return x
|
|
|
|
return internal
|
2001-02-10 06:57:10 +08:00
|
|
|
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
def foo():
|
|
|
|
pass
|
2003-07-16 04:24:27 +08:00
|
|
|
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
def namespace_test(): pass
|
|
|
|
def namespace_test(): pass
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def find_block(block, name):
|
|
|
|
for ch in block.get_children():
|
|
|
|
if ch.get_name() == name:
|
|
|
|
return ch
|
2005-10-20 22:27:21 +08:00
|
|
|
|
2006-01-23 15:49:36 +08:00
|
|
|
|
|
|
|
class SymtableTest(unittest.TestCase):
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
|
|
|
|
top = symtable.symtable(TEST_CODE, "?", "exec")
|
|
|
|
# These correspond to scopes in TEST_CODE
|
|
|
|
Mine = find_block(top, "Mine")
|
|
|
|
a_method = find_block(Mine, "a_method")
|
|
|
|
spam = find_block(top, "spam")
|
|
|
|
internal = find_block(spam, "internal")
|
|
|
|
foo = find_block(top, "foo")
|
|
|
|
|
|
|
|
def test_type(self):
|
|
|
|
self.assertEqual(self.top.get_type(), "module")
|
|
|
|
self.assertEqual(self.Mine.get_type(), "class")
|
|
|
|
self.assertEqual(self.a_method.get_type(), "function")
|
|
|
|
self.assertEqual(self.spam.get_type(), "function")
|
|
|
|
self.assertEqual(self.internal.get_type(), "function")
|
|
|
|
|
|
|
|
def test_optimized(self):
|
|
|
|
self.assertFalse(self.top.is_optimized())
|
|
|
|
self.assertFalse(self.top.has_exec())
|
|
|
|
|
|
|
|
self.assertTrue(self.spam.is_optimized())
|
|
|
|
|
|
|
|
def test_nested(self):
|
|
|
|
self.assertFalse(self.top.is_nested())
|
|
|
|
self.assertFalse(self.Mine.is_nested())
|
|
|
|
self.assertFalse(self.spam.is_nested())
|
|
|
|
self.assertTrue(self.internal.is_nested())
|
|
|
|
|
|
|
|
def test_children(self):
|
|
|
|
self.assertTrue(self.top.has_children())
|
|
|
|
self.assertTrue(self.Mine.has_children())
|
|
|
|
self.assertFalse(self.foo.has_children())
|
|
|
|
|
|
|
|
def test_lineno(self):
|
|
|
|
self.assertEqual(self.top.get_lineno(), 0)
|
|
|
|
self.assertEqual(self.spam.get_lineno(), 11)
|
|
|
|
|
|
|
|
def test_function_info(self):
|
|
|
|
func = self.spam
|
2008-08-20 10:06:00 +08:00
|
|
|
self.assertEqual(func.get_parameters(), {"a", "b", "kw", "var"})
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
self.assertEqual(func.get_locals(),
|
2008-08-20 10:06:00 +08:00
|
|
|
{"a", "b", "bar", "glob", "internal", "kw", "var", "x"})
|
|
|
|
self.assertEqual(func.get_globals(), {"bar", "glob"})
|
|
|
|
self.assertEqual(self.internal.get_frees(), {"x"})
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
|
|
|
|
def test_globals(self):
|
|
|
|
self.assertTrue(self.spam.lookup("glob").is_global())
|
|
|
|
self.assertTrue(self.spam.lookup("bar").is_global())
|
|
|
|
self.assertFalse(self.internal.lookup("x").is_global())
|
|
|
|
self.assertFalse(self.Mine.lookup("instance_var").is_global())
|
|
|
|
|
|
|
|
def test_local(self):
|
|
|
|
self.assertTrue(self.spam.lookup("x").is_local())
|
|
|
|
self.assertFalse(self.internal.lookup("x").is_local())
|
|
|
|
|
|
|
|
def test_referenced(self):
|
|
|
|
self.assertTrue(self.internal.lookup("x").is_referenced())
|
|
|
|
self.assertTrue(self.spam.lookup("internal").is_referenced())
|
|
|
|
self.assertFalse(self.spam.lookup("x").is_referenced())
|
|
|
|
|
|
|
|
def test_parameters(self):
|
|
|
|
for sym in ("a", "var", "kw"):
|
|
|
|
self.assertTrue(self.spam.lookup(sym).is_parameter())
|
|
|
|
self.assertFalse(self.spam.lookup("x").is_parameter())
|
|
|
|
|
|
|
|
def test_symbol_lookup(self):
|
|
|
|
self.assertEqual(len(self.top.get_identifiers()),
|
|
|
|
len(self.top.get_symbols()))
|
|
|
|
|
|
|
|
self.assertRaises(KeyError, self.top.lookup, "not_here")
|
|
|
|
|
|
|
|
def test_namespaces(self):
|
|
|
|
self.assertTrue(self.top.lookup("Mine").is_namespace())
|
|
|
|
self.assertTrue(self.Mine.lookup("a_method").is_namespace())
|
|
|
|
self.assertTrue(self.top.lookup("spam").is_namespace())
|
|
|
|
self.assertTrue(self.spam.lookup("internal").is_namespace())
|
|
|
|
self.assertTrue(self.top.lookup("namespace_test").is_namespace())
|
|
|
|
self.assertFalse(self.spam.lookup("x").is_namespace())
|
|
|
|
|
|
|
|
self.assert_(self.top.lookup("spam").get_namespace() is self.spam)
|
|
|
|
ns_test = self.top.lookup("namespace_test")
|
|
|
|
self.assertEqual(len(ns_test.get_namespaces()), 2)
|
|
|
|
self.assertRaises(ValueError, ns_test.get_namespace)
|
|
|
|
|
|
|
|
def test_assigned(self):
|
|
|
|
self.assertTrue(self.spam.lookup("x").is_assigned())
|
|
|
|
self.assertTrue(self.spam.lookup("bar").is_assigned())
|
|
|
|
self.assertTrue(self.top.lookup("spam").is_assigned())
|
|
|
|
self.assertTrue(self.Mine.lookup("a_method").is_assigned())
|
|
|
|
self.assertFalse(self.internal.lookup("x").is_assigned())
|
|
|
|
|
|
|
|
def test_imported(self):
|
|
|
|
self.assertTrue(self.top.lookup("sys").is_imported())
|
|
|
|
|
|
|
|
def test_name(self):
|
|
|
|
self.assertEqual(self.top.get_name(), "top")
|
|
|
|
self.assertEqual(self.spam.get_name(), "spam")
|
|
|
|
self.assertEqual(self.spam.lookup("x").get_name(), "x")
|
|
|
|
self.assertEqual(self.Mine.get_name(), "Mine")
|
|
|
|
|
|
|
|
def test_class_info(self):
|
2008-08-20 10:06:00 +08:00
|
|
|
self.assertEqual(self.Mine.get_methods(), {'a_method'})
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
|
|
|
|
def test_filename_correct(self):
|
|
|
|
### Bug tickler: SyntaxError file name correct whether error raised
|
|
|
|
### while parsing or building symbol table.
|
|
|
|
def checkfilename(brokencode):
|
|
|
|
try:
|
|
|
|
symtable.symtable(brokencode, "spam", "exec")
|
|
|
|
except SyntaxError as e:
|
|
|
|
self.assertEqual(e.filename, "spam")
|
|
|
|
else:
|
|
|
|
self.fail("no SyntaxError for %r" % (brokencode,))
|
|
|
|
checkfilename("def f(x): foo)(") # parse-time
|
|
|
|
checkfilename("def f(x): global x") # symtable-build-time
|
2006-01-23 15:49:36 +08:00
|
|
|
|
|
|
|
def test_eval(self):
|
|
|
|
symbols = symtable.symtable("42", "?", "eval")
|
|
|
|
|
|
|
|
def test_single(self):
|
|
|
|
symbols = symtable.symtable("42", "?", "single")
|
|
|
|
|
|
|
|
def test_exec(self):
|
|
|
|
symbols = symtable.symtable("def f(x): return x", "?", "exec")
|
|
|
|
|
|
|
|
|
|
|
|
def test_main():
|
2008-05-21 05:35:26 +08:00
|
|
|
support.run_unittest(SymtableTest)
|
2006-01-23 15:49:36 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|