[reportlab-users] Reusing flowables can cause problems
Robin Becker
robin at reportlab.com
Thu Sep 14 16:26:44 EDT 2006
Christoph Zwerschke wrote:
> I just noticed a very nasty pitfall in ReportLab's Platypus, namely the
> _postponed attribute of Flowables that may be set during the build
> process. The problem arises if you do something like this:
>
> spacer = Spacer(...)
>
> doc = SimpleDocTemplate(...)
> # build a document
> doc.build([spacer, ..., spacer, ...])
> ...
> doc = SimpleDocTemplate(...)
> # build another document
> doc.build([spacer, ..., spacer, ...])
>
> During the first build process, the _postponed attribute may have been
> sticked to the spacer, so that it will not behave correctly in the
> second build process.
>
> I noticed that the undocumented multiBuild method solves this problem by
> doing the following after each build process:
>
> #clean up so multi-build does not go wrong - the frame
> #packer might have tacked an attribute onto some flowables
> for elem in story:
> if hasattr(elem, '_postponed'):
> del elem._postponed
>
> Maybe this loop should be run automatically at the beginning of the
> build method instead?
>
> Otherwise, it should be better documented that Flowables should not be
> reused after they have gone through a build process.
>
.....
indeed you're right. You're supposed to chuck them away after use. The
alternative would be for the story processor to copy them and only use
the copy, but it was felt that would be too expensive. That way all
duplicates would be split on first entrance into the process and only
copies would undergo state changes. It shouldn't be too hard to make a
version of doctemplate that does that.
--
Robin Becker
More information about the reportlab-users
mailing list