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

Skip to content

Commit 2bee8fe

Browse files
committed
Pdb.lineinfo(): Don't use os.popen('egrep ...') to find the line in
the file that a function is defined on. Non-portable to Windows and JPython. Instead, new find_function() uses re module on a similar (simple-minded) pattern.
1 parent a2e4855 commit 2bee8fe

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

Lib/pdb.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@
1111
import bdb
1212
import repr
1313
import os
14+
import re
15+
16+
def find_function(funcname, filename):
17+
cre = re.compile(r'def\s+%s\s*[(]' % funcname)
18+
try:
19+
fp = open(filename)
20+
except IOError:
21+
return None
22+
# consumer of this info expects the first line to be 1
23+
lineno = 1
24+
answer = None
25+
while 1:
26+
line = fp.readline()
27+
if line == '':
28+
break
29+
if cre.match(line):
30+
answer = funcname, filename, lineno
31+
break
32+
lineno = lineno + 1
33+
fp.close()
34+
return answer
1435

1536

1637
# Interaction prompt line will separate file and call info from code
@@ -26,7 +47,6 @@ def __init__(self):
2647
bdb.Bdb.__init__(self)
2748
cmd.Cmd.__init__(self)
2849
self.prompt = '(Pdb) '
29-
self.lineinfoCmd = 'egrep -n "def *%s *[(:]" %s /dev/null'
3050
self.aliases = {}
3151
# Try to load readline if it exists
3252
try:
@@ -283,13 +303,8 @@ def lineinfo(self, identifier):
283303
if f:
284304
fname = f
285305
item = parts[1]
286-
grepstring = self.lineinfoCmd % (item, fname)
287-
answer = os.popen(grepstring, 'r').readline()
288-
if answer:
289-
f, line, junk = string.split(answer, ':', 2)
290-
return(item, f,line)
291-
else:
292-
return failed
306+
answer = find_function(item, fname)
307+
return answer or failed
293308

294309
def checkline(self, filename, lineno):
295310
"""Return line number of first line at or after input

0 commit comments

Comments
 (0)