]> granicus.if.org Git - python/commitdiff
#14492: fix some bugs in Tools/scripts/pdeps.py.
authorR David Murray <rdmurray@bitdance.com>
Fri, 6 Apr 2012 02:59:13 +0000 (22:59 -0400)
committerR David Murray <rdmurray@bitdance.com>
Fri, 6 Apr 2012 02:59:13 +0000 (22:59 -0400)
Initial patch by Popa Claudiu.

Lib/test/test_tools.py
Tools/scripts/pdeps.py

index 83a1e0df57ab1f6d20f7a2082c530817e86b3d31..cfe13acc2df9bc008c36cf8eae6badea2bbe065a 100644 (file)
@@ -6,8 +6,10 @@ Tools directory of a Python checkout or tarball, such as reindent.py.
 
 import os
 import sys
+import imp
 import unittest
 import sysconfig
+import tempfile
 from test import support
 from test.script_helper import assert_python_ok
 
@@ -72,6 +74,31 @@ class TestSundryScripts(unittest.TestCase):
                 import analyze_dxp
 
 
+class PdepsTests(unittest.TestCase):
+
+    @classmethod
+    def setUpClass(self):
+        path = os.path.join(scriptsdir, 'pdeps.py')
+        self.pdeps = imp.load_source('pdeps', path)
+
+    @classmethod
+    def tearDownClass(self):
+        if 'pdeps' in sys.modules:
+            del sys.modules['pdeps']
+
+    def test_process_errors(self):
+        # Issue #14492: m_import.match(line) can be None.
+        with tempfile.TemporaryDirectory() as tmpdir:
+            fn = os.path.join(tmpdir, 'foo')
+            with open(fn, 'w') as stream:
+                stream.write("#!/this/will/fail")
+            self.pdeps.process(fn, {})
+
+    def test_inverse_attribute_error(self):
+        # Issue #14492: this used to fail with an AttributeError.
+        self.pdeps.inverse({'a': []})
+
+
 def test_main():
     support.run_unittest(*[obj for obj in globals().values()
                                if isinstance(obj, type)])
index 938f31c16439b4f9bc2248835db10fe76a5dc22f..f8218ac5243d1e511e3790874dfddcd9b7b46a56 100755 (executable)
@@ -76,10 +76,9 @@ def process(filename, table):
             nextline = fp.readline()
             if not nextline: break
             line = line[:-1] + nextline
-        if m_import.match(line) >= 0:
-            (a, b), (a1, b1) = m_import.regs[:2]
-        elif m_from.match(line) >= 0:
-            (a, b), (a1, b1) = m_from.regs[:2]
+        m_found = m_import.match(line) or m_from.match(line)
+        if m_found:
+            (a, b), (a1, b1) = m_found.regs[:2]
         else: continue
         words = line[a1:b1].split(',')
         # print '#', line, words
@@ -87,6 +86,7 @@ def process(filename, table):
             word = word.strip()
             if word not in list:
                 list.append(word)
+    fp.close()
 
 
 # Compute closure (this is in fact totally general)
@@ -123,7 +123,7 @@ def closure(table):
 def inverse(table):
     inv = {}
     for key in table.keys():
-        if not inv.has_key(key):
+        if key not in inv:
             inv[key] = []
         for item in table[key]:
             store(inv, item, key)