Thank you for your donation!   Download the book!   Join the group!   Public source code repository
Showing posts with label Python 3. Show all posts
Showing posts with label Python 3. Show all posts

Finally

I have finished conversion of images rendering code from Python 2.7 + pygtk to Python 3.8 + pycairo. I didn't retain compatibility with Python 2.7, since this code is used just for my book.

Even though I didn't plan to do any refactoring, it happened that this conversion was also nudging me to do some code clean-up. For instance, it wasn't really necessary, but I enforced that each board always have size according to variant it represents. Then, I added view, which describes which portion of a board is actually visible. Sure, there was board descriptor class already with the same purpose, but it was more clumsy in use, felt like an afterthought, which it was.

While I initially disliked context-sensitive nature of cairo, it actually works very well. Also, performance. It seems to me that new code is visibly faster than the old one. I'd say that cairo code might be twice as fast as old gtk code. And, on top of that, cairo is much more flexible.

Up next, versioning clean-up, updating and displaying version info on a GitHub page.

After that, dunno. At the moment, I have no clear plan of actions.

Pillow fight gone awry

As you might noticed, I started conversion to Python 3 + Pillow a few days ago. Since I checked Pillow features, API, and found them fit, I was expecting a smooth ride, which it was until I stumbled upon an issue. As soon as I added a few images, some with texts, Pillow started to crash, either with:
Segmentation fault (core dumped)
or with:
munmap_chunk(): invalid pointer
Aborted (core dumped)
depending on how it was run (console, argument to python3 or through MS Visual Code).

I found several bug reports similar to this, most was closed, or led to nowhere. I tried a short debugging session, just to see if I can quickly find a workaround: 
$ gdb python3
then in gdb console:
(gdb) r draw_mark.py
and after a crash:
(gdb) where
gives location of a call to calloc(), which actually crashes:
#1  0x00007fffe86261e2 in hb_object_create<hb_font_t> () at hb-object.hh:225
Couldn't find anything suspicious about it, except maybe usage of pure-C allocations in a C++ templatized code might not be by-the-book, i.e. new should be preferred. Anyway, that code is a part of HarfBuzz, which is called by _imagingft, which is used by Raqm, which is used by Pillow. Good luck submitting bug report or fix, with a proper minimal example, then waiting for a patch to arrive in my Linux distro repository.

This is where I decided to look around, and after a short search I stumbled upon cairo. Tried it via pycairo, with hundreds of filled, outlined polygons, hundreds of outlined texts; tried generating hundred images, it works. Well, except show_text() doesn't do anything if called after set_source_rgba(), not sure if bug, or I'm using the API wrongly.

The thing is, I don't like cairo API, it's context sensitive. So, it's entirely possible to insert a few graphical elements in otherwise working code, and then suddenly it spits out garbage.

Another thing is, I'm still debating with myself, should I ditch Python, and go full Rust (via Gtk-rs)? This way I'd remove at least one dependency from the whole project. This would be opportunity to redo rendering code, while knowing in advance all of requirements. Also, this would make me finally sit down and learn Rust properly. 

Or, should I do the most opportune thing, and just translate code to Python 3 + pycairo?