Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3861a32

Browse files
committed
Merge #14492: fix some bugs in Tools/scripts/pdeps.py.
Initial patch by Popa Claudiu.
2 parents e0029ba + d3af634 commit 3861a32

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

Lib/test/test_tools.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
import os
88
import sys
9+
import imp
910
import unittest
1011
import sysconfig
12+
import tempfile
1113
from test import support
1214
from test.script_helper import assert_python_ok
1315

@@ -72,6 +74,31 @@ def test_analyze_dxp_import(self):
7274
import analyze_dxp
7375

7476

77+
class PdepsTests(unittest.TestCase):
78+
79+
@classmethod
80+
def setUpClass(self):
81+
path = os.path.join(scriptsdir, 'pdeps.py')
82+
self.pdeps = imp.load_source('pdeps', path)
83+
84+
@classmethod
85+
def tearDownClass(self):
86+
if 'pdeps' in sys.modules:
87+
del sys.modules['pdeps']
88+
89+
def test_process_errors(self):
90+
# Issue #14492: m_import.match(line) can be None.
91+
with tempfile.TemporaryDirectory() as tmpdir:
92+
fn = os.path.join(tmpdir, 'foo')
93+
with open(fn, 'w') as stream:
94+
stream.write("#!/this/will/fail")
95+
self.pdeps.process(fn, {})
96+
97+
def test_inverse_attribute_error(self):
98+
# Issue #14492: this used to fail with an AttributeError.
99+
self.pdeps.inverse({'a': []})
100+
101+
75102
def test_main():
76103
support.run_unittest(*[obj for obj in globals().values()
77104
if isinstance(obj, type)])

Tools/scripts/pdeps.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ def process(filename, table):
7676
nextline = fp.readline()
7777
if not nextline: break
7878
line = line[:-1] + nextline
79-
if m_import.match(line) >= 0:
80-
(a, b), (a1, b1) = m_import.regs[:2]
81-
elif m_from.match(line) >= 0:
82-
(a, b), (a1, b1) = m_from.regs[:2]
79+
m_found = m_import.match(line) or m_from.match(line)
80+
if m_found:
81+
(a, b), (a1, b1) = m_found.regs[:2]
8382
else: continue
8483
words = line[a1:b1].split(',')
8584
# print '#', line, words
8685
for word in words:
8786
word = word.strip()
8887
if word not in list:
8988
list.append(word)
89+
fp.close()
9090

9191

9292
# Compute closure (this is in fact totally general)
@@ -123,7 +123,7 @@ def closure(table):
123123
def inverse(table):
124124
inv = {}
125125
for key in table.keys():
126-
if not inv.has_key(key):
126+
if key not in inv:
127127
inv[key] = []
128128
for item in table[key]:
129129
store(inv, item, key)

0 commit comments

Comments
 (0)