mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
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.
This commit is contained in:
parent
a2e48552d2
commit
2bee8feac6
1 changed files with 23 additions and 8 deletions
31
Lib/pdb.py
31
Lib/pdb.py
|
@ -11,6 +11,27 @@ import cmd
|
||||||
import bdb
|
import bdb
|
||||||
import repr
|
import repr
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
def find_function(funcname, filename):
|
||||||
|
cre = re.compile(r'def\s+%s\s*[(]' % funcname)
|
||||||
|
try:
|
||||||
|
fp = open(filename)
|
||||||
|
except IOError:
|
||||||
|
return None
|
||||||
|
# consumer of this info expects the first line to be 1
|
||||||
|
lineno = 1
|
||||||
|
answer = None
|
||||||
|
while 1:
|
||||||
|
line = fp.readline()
|
||||||
|
if line == '':
|
||||||
|
break
|
||||||
|
if cre.match(line):
|
||||||
|
answer = funcname, filename, lineno
|
||||||
|
break
|
||||||
|
lineno = lineno + 1
|
||||||
|
fp.close()
|
||||||
|
return answer
|
||||||
|
|
||||||
|
|
||||||
# Interaction prompt line will separate file and call info from code
|
# Interaction prompt line will separate file and call info from code
|
||||||
|
@ -26,7 +47,6 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
||||||
bdb.Bdb.__init__(self)
|
bdb.Bdb.__init__(self)
|
||||||
cmd.Cmd.__init__(self)
|
cmd.Cmd.__init__(self)
|
||||||
self.prompt = '(Pdb) '
|
self.prompt = '(Pdb) '
|
||||||
self.lineinfoCmd = 'egrep -n "def *%s *[(:]" %s /dev/null'
|
|
||||||
self.aliases = {}
|
self.aliases = {}
|
||||||
# Try to load readline if it exists
|
# Try to load readline if it exists
|
||||||
try:
|
try:
|
||||||
|
@ -283,13 +303,8 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
||||||
if f:
|
if f:
|
||||||
fname = f
|
fname = f
|
||||||
item = parts[1]
|
item = parts[1]
|
||||||
grepstring = self.lineinfoCmd % (item, fname)
|
answer = find_function(item, fname)
|
||||||
answer = os.popen(grepstring, 'r').readline()
|
return answer or failed
|
||||||
if answer:
|
|
||||||
f, line, junk = string.split(answer, ':', 2)
|
|
||||||
return(item, f,line)
|
|
||||||
else:
|
|
||||||
return failed
|
|
||||||
|
|
||||||
def checkline(self, filename, lineno):
|
def checkline(self, filename, lineno):
|
||||||
"""Return line number of first line at or after input
|
"""Return line number of first line at or after input
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue