[reportlab-users] Problem with reportlab.lib.utils.ImageReader and deepcopy

Robin Becker robin at reportlab.com
Thu Jul 1 11:03:05 EDT 2010


On 01/07/2010 15:47, Roberto Alsina wrote:

> For complicated reasons (which means, I am not sure I am doing something

> smart), I need to deepcopy some reportlab flowables.

>

> I run into some problems when copying paragraphs that contain inline JPG

> images in them, like<img src="blah.jpg">.

>

> I get this:

>

> * File "/usr/lib/python2.6/copy.py", line 162, in deepcopy

> * y = copier(x, memo)

> * File "/usr/lib/python2.6/copy.py", line 255, in _deepcopy_dict

> * y[deepcopy(key, memo)] = deepcopy(value, memo)

> * File "/usr/lib/python2.6/copy.py", line 189, in deepcopy

> * y = _reconstruct(x, rv, 1, memo)

> * File "/usr/lib/python2.6/copy.py", line 323, in _reconstruct

> * y = callable(*args)

> * File "/usr/lib/python2.6/copy_reg.py", line 93, in __newobj__

> * return cls.__new__(cls, *args)

> * TypeError: instancemethod expected at least 2 arguments, got 0

>

>

> The object that is not being copied right is this one:

>

> <class 'reportlab.lib.utils.ImageReader'>

> {'fp':<open file

> '/home/ralsina/Desktop/proyectos/rst/trunk/rst2pdf/tests/input/images/biohazard.jpg',

> mode 'rb' at 0x9ded3e8>, 'jpeg_fh':<bound method ImageReader._jpeg_fh of

> <reportlab.lib.utils.ImageReader object at 0x9bcf26c>>, '_height': 16,

> '_width': 16, '_transparent': None, '_image':

> <PIL.JpegImagePlugin.JpegImageFile image mode=L size=16x16 at 0x9BCF40C>,

> '_data': None, 'fileName':

> '/home/ralsina/Desktop/proyectos/rst/trunk/rst2pdf/tests/input/images/biohazard.jpg'}

>

>

> As a very ugly workaround, adding this seems to cause no side effects on

> rst2pdf's test suite:

>

> reportlab.lib.utils.ImageReader.__deepcopy__ = lambda self,*x: copy(self)

........
you might want to consider what's going on here a bit more deeply :)

The problem is that the Image reader is keeping an open file lying about. If you
make a copy in the above way it means that two objects now have the same open
file inside them. That will eventually lead to some trouble ie if a read happens
on one then the file ptr will be at the wrong place etc etc. There are some
options to make the ImageReader close its open files so that we will always
re-open etc etc, perhaps that would be easier.


--
Robin Becker


More information about the reportlab-users mailing list