Skip to content

Commit

Permalink
* pychecker/pcmodules.py:
Browse files Browse the repository at this point in the history
	  Add an assertion to make sure that the alias being imported
	  was not already imported before.
	* test/test_internal.py:
	  Add a test for star imports.
	  Also triggers the assert above.
  • Loading branch information
thomasvs committed Jan 16, 2011
1 parent b9142b0 commit a0a4ac2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
@@ -1,3 +1,12 @@
2011-01-16 Thomas Vander Stichele <thomas at apestaart dot org>

* pychecker/pcmodules.py:
Add an assertion to make sure that the alias being imported
was not already imported before.
* test/test_internal.py:
Add a test for star imports.
Also triggers the assert above.

2011-01-13 Thomas Vander Stichele <thomas at apestaart dot org>

* pychecker/pcmodules.py:
Expand Down
4 changes: 3 additions & 1 deletion pychecker/pcmodules.py
Expand Up @@ -478,7 +478,9 @@ def addImported(self, name, line, pcmodule):
@param pcmodule: the module this name is being imported from
@type pcmodule: L{PyCheckerModule}
"""
assert name not in self.imported, "name %s already imported"
assert name not in self.imported, \
"name %s from module %s already imported in %s" % (
name, pcmodule.moduleName, self.moduleName)
self.imported[name] = (line, pcmodule)

def filename(self) :
Expand Down
48 changes: 46 additions & 2 deletions test/test_internal.py
Expand Up @@ -36,7 +36,7 @@ def check(self, paths):
return warnings

def formatWarnings(self, warnings):
return [w.format() for w in warnings]
return "\n".join([w.format() for w in warnings])

def assertWarnings(self, warnings, paths):
# check that all warnings are for files in the given paths
Expand Down Expand Up @@ -91,10 +91,11 @@ def test_unused_import(self):
('xml', 'dom'): ('input/unused_import.py', 10),
})

class NestedTestCase(InternalTestCase):
def test_nested(self):
warnings = self.check(['input/nested.py', ])

self.assertEquals(len(warnings), 1)
self.assertEquals(len(warnings), 1, self.formatWarnings(warnings))
self.assertWarnings(warnings, ['input/nested.py'])

# check the module and the code
Expand Down Expand Up @@ -125,5 +126,48 @@ def test_nested(self):
self.failIf(pcmodule.codes[2].stack)
self.failIf(pcmodule.codes[3].stack)

class StarImportTestCase(InternalTestCase):
todo = 'make functions keyed on alias'
def test_star_import(self):
warnings = self.check(['input/starimport.py', ])

self.assertEquals(len(warnings), 0, self.formatWarnings(warnings))

# check the module doing the star import
pcmodule = pcmodules.getPCModule("starimport", moduleDir="input")
self.assertEquals(pcmodule.moduleName, "starimport")
self.assertEquals(pcmodule.moduleDir, "input")

if utils.pythonVersion() >= utils.PYTHON_2_6:
self.assertEquals(pcmodule.variables.keys(), ["__package__"])
else:
self.assertEquals(pcmodule.variables.keys(), [])
self.assertEquals(pcmodule.classes.keys(), [])
self.assertEquals(pcmodule.functions.keys(), [])
self.assertEquals(pcmodule.modules.keys(), ["gettext", ])

# check the code
self.assertEquals(len(pcmodule.codes), 1)
self.assertEquals(pcmodule.codes[0].func.function.func_name, '__main__')

# FIXME: why do we have a non-empty stack here ?
# self.assertEquals(pcmodule.codes[0].stack, [])

# check the module from which we are starimporting
pcmodule = pcmodules.getPCModule("starimportfrom", moduleDir="input")
self.assertEquals(pcmodule.moduleName, "starimportfrom")
self.assertEquals(pcmodule.moduleDir, "input")

if utils.pythonVersion() >= utils.PYTHON_2_6:
self.assertEquals(pcmodule.variables.keys(), ["__package__"])
else:
self.assertEquals(pcmodule.variables.keys(), [])
self.assertEquals(pcmodule.classes.keys(), [])
self.assertEquals(pcmodule.functions.keys(), ["_", ])
self.assertEquals(pcmodule.modules.keys(), ["gettext", ])

# check the code
self.assertEquals(len(pcmodule.codes), 0)

if __name__ == '__main__':
unittest.main()

0 comments on commit a0a4ac2

Please sign in to comment.