pytroll / aggdraw

Python package wrapping AGG2 drawing functionality
https://aggdraw.readthedocs.io/en/latest/
Other
98 stars 48 forks source link

Segmentation fault debugging aggdraw.Pen #58

Open eduardomezencio opened 5 years ago

eduardomezencio commented 5 years ago

Using aggdraw 1.3.10 in arch linux with cpython 3.7, I get a segmentation fault every time my debugger tries to inspect an aggdraw.Pen object. I tried it debugging with pudb3, with vscode python extension and with pycharm. Here's a minimal code:

from PIL import Image
import aggdraw

def main():
    image = create_image()
    image.show()

def create_image():
    image = Image.new('RGBA', (1024, 1024))
    draw = aggdraw.Draw(image)
    pen = aggdraw.Pen('red', 20)
    draw.line((0, 0, 100, 100), pen)
    draw.flush()
    return image

if __name__ == '__main__':
    main()

Put a breakpoint in the line draw.line((0, 0, 100, 100), pen) and try to inspect pen in your debugger to see if you can reproduce it.

I checked some issues here, and found #22 that also mentions segmentation faults related to Pen, but it seems to be a different issue, and already solved.

And less important, but just to mention it, I don't really want to inspect this object. It's just that debuggers usually show all local variables when you break, so every time I put a breakpoint with a local Pen object, my session crashes and this is the real problem

djhoese commented 5 years ago

How did you install aggdraw? PyPI via pip or from conda-forge via conda? I'm guessing it was a wheel? Can you try installing directly from the source code and see if this re-building of the C/C++ code fixes things?

eduardomezencio commented 5 years ago

Yes, it was installed from PyPI via pip. I have just uninstalled it, cloned this repository and compiled then installed it, but the same segmentation fault still happens.

djhoese commented 5 years ago

To clarify, you don't get a segmentation fault with normal operations? You only get it when using the debugger?

If that is true, my guess is that your debugger is trying to use the __repr__ or __str__ or some other dunder-method that isn't implemented on the aggdraw objects. Given how low-level they are, they may not implement every interface that your debugger wants to use and your debugger isn't smart enough to be ok with that. If this is the problem, this won't be fixed any time soon until we migrate to using cython where it would be easier for us to define these objects as full python objects.

eduardomezencio commented 5 years ago

Yes, there is no problem in normal program execution. I tried, during normal execution, printing str, repr, hash and dir of the object to see if anything fails, but they all work with no problems.

djhoese commented 5 years ago

So good news (?) is that I can reproduce this in pycharm. If I run it with the debugger enabled but without the breakpoint then it succeeds. If I use pdb import pdb; pdb.set_trace() the line before the specified breakpoint and print out pen it still works fine. I'm not sure there is much I can do about this. I would normally run a command with strace to get some kind of idea what is causing the segfault but I'm not sure how I can do that with a visual debugger.

A fix for this may have to wait until a large rewrite of aggdraw happens where these objects are more fully realized python objects.