est / winpdb

Automatically exported from code.google.com/p/winpdb
0 stars 1 forks source link

winpdb cannot find source for relative path #33

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The test files:
(
C:\test_python>dir /a-d/s/b
C:\test_python\first.py
C:\test_python\rpdb2.py
C:\test_python\second\second.py
)

The content of the first file:
(
C:\test_python>type first.py
print "enter first.py"
execfile("./second/second.py")
)

The content of the second file:
(
C:\test_python>type second\second.py
import rpdb2
print "enter second.py"
print "waiting for debugger..."
rpdb2.start_embedded_debugger('password')
print "after waiting for debugger"
)

Then run this:
C:\test_python>python c:\test_python\first.py

Then try to use winpdb to attach the the script, the debugger will report:
(
Failed to load source file 'c:\test_python\second.py' from debuggee.
You may continue to debug, but you will not see 
source lines from this file.
)

The function to calculate the source is in calc_frame_path ( file rpdb2.py ), 
we can add some print in it to get some logs:

def calc_frame_path(frame):
    globals_filename = frame.f_globals.get('__file__', None)
    filename = frame.f_code.co_filename

    if filename.startswith('<'):
        if globals_filename == None:
            return filename
        else:
            filename = CalcScriptName(os.path.basename(globals_filename))

    if filename in g_frames_path:
        return g_frames_path[filename]

    if globals_filename != None:
        dirname = os.path.dirname(globals_filename)
        basename = os.path.basename(filename)
        path = my_os_path_join(dirname, basename)

        print "globals_filename is: " + globals_filename
        print "dirname is: " + dirname
        print "filename is: " + filename
        print "basename is: " + basename
        print "path is: " + path

        if os.path.isabs(path):    
            abspath = my_abspath(path)
            lowered = winlower(abspath)        
            g_frames_path[filename] = lowered
            print "path is absolute path, will return, lowered is: " + lowered
            return lowered

        try:
            abspath = FindFile(path, fModules = True)
            lowered = winlower(abspath)    
            g_frames_path[filename] = lowered
            return lowered

        except IOError:
            pass

    if os.path.isabs(filename):
        abspath = my_abspath(filename)
        lowered = winlower(abspath)
        g_frames_path[filename] = lowered
        return lowered

    try:
        abspath = FindFile(filename, fModules = True)
        lowered = winlower(abspath)    
        g_frames_path[filename] = lowered
        return lowered

    except IOError:
        lowered = winlower(filename)    
        return lowered

Here is some of the logs:
(
enter first.py
enter second.py
waiting for debugger...
globals_filename is: c:\test_python\first.py
dirname is: c:\test_python
filename is: ./second/second.py
basename is: second.py
path is: c:\test_python\second.py
path is absolute path, will return, lowered is: c:\test_python\second.py
)

So it seems that when python run a script with absolute path, and if the file 
in execfile is a relative path, the path of the souce file is not calculated 
correctly.

Do not know why calc_frame_path is so complicated ( perhaps it needs to handle 
many different situations )

But when I modify the code in this way then it can work:
...
    if globals_filename != None:
        dirname = os.path.dirname(globals_filename)
        basename = os.path.basename(filename)
        #path = my_os_path_join(dirname, basename)
        path = my_os_path_join(os.getcwd(),filename)
...

Some of the logs:
(
C:\test_python>python c:\test_python\first.py
enter first.py
enter second.py
waiting for debugger...
globals_filename is: c:\test_python\first.py
dirname is: c:\test_python
filename is: ./second/second.py
basename is: second.py
path is: C:\test_python\./second/second.py
path is absolute path, will return, lowered is: c:\test_python\second\second.py
)

So I think we can modify calc_frame_path function to handle this situation?

Original issue reported on code.google.com by mobilehu...@gmail.com on 2 May 2013 at 2:56