shailesh / pyglet

Automatically exported from code.google.com/p/pyglet
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

py3 compatibility problem while taking fullscreen snapshots #707

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Using pyglet tip from default repo ( rc43bc57c954f tip , dated 2014 01 03 )

For the py3 runs, installed pyglet with
    cd working_copy 
    py -3.3 setup.py install
so the py2to3 adjusts are performed

Test_script: the stock noisy.py modified by
   - going fullscreen after window instantiation
   - schedule a function to take a snapshot

Attached as noisy_snapshot.py

It runs ok with py2.6.6
It tracebacks with py3.3.1:

D:\hg_externals\pyglet_dev\examples\noisy>py -3.3 noisy_snapshot.py
Traceback (most recent call last):
  File "noisy_snapshot.py", line 121, in <module>
    pyglet.app.run()
  File "C:\Python33\lib\site-packages\pyglet\app\__init__.py", line 143, in run
    event_loop.run()
  File "C:\Python33\lib\site-packages\pyglet\app\base.py", line 136, in run
    self._run_estimated()
  File "C:\Python33\lib\site-packages\pyglet\app\base.py", line 165, in _run_est
imated
    timeout = self.idle()
  File "C:\Python33\lib\site-packages\pyglet\app\base.py", line 274, in idle
    redraw_all = self.clock.call_scheduled_functions(dt)
  File "C:\Python33\lib\site-packages\pyglet\clock.py", line 309, in call_schedu
led_functions
    item.func(ts - item.last_ts, *item.args, **item.kwargs)
  File "noisy_snapshot.py", line 89, in take_snapshot
    pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png')
  File "C:\Python33\lib\site-packages\pyglet\image\__init__.py", line 453, in sa
ve
    encoder.encode(self, file, filename)
  File "C:\Python33\lib\site-packages\pyglet\image\codecs\png.py", line 104, in
encode
    writer.write_array(file, data)
  File "C:\Python33\lib\site-packages\pyglet\image\codecs\pypng.py", line 328, i
n write_array
    self.write(outfile, self.array_scanlines(pixels))
  File "C:\Python33\lib\site-packages\pyglet\image\codecs\pypng.py", line 316, i
n write
    self.write_chunk(outfile, 'IDAT', compressed + flushed)
TypeError: Can't convert 'bytes' object to str implicitly

When commenting out the fullscreen part the script runs ok, the same as letting 
in the fullscreen but commenting out the take_snapshot.

So this is specific to the codepath snapshot + fullscreen.

Additional info:
windows xp 32bits
PIL (1.1.7) on py2.6.6 , Pillow (2.3.0) on py3.3.1

I will try to look at this, but currently I'm pushing the cocos compatibility 
with py3, so it can be a while.

Original issue reported on code.google.com by ccanepacc@gmail.com on 25 Feb 2014 at 8:12

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for your report!

Original comment by useboxnet on 26 Feb 2014 at 7:29

GoogleCodeExporter commented 9 years ago
Something is not OK in your installation, running 3.3.2 here (Linux) with 
Pillow 2.0.0 works as expected (see attachment).

Also I've noticed in your traceback that your code is using pypng instead of 
PIL (Pyllow) and that happens only if you're using that encoder explicitly or 
PIL is not found.

I've forced the pypng encoder with:

def take_snapshot(dt):
    from pyglet.image.codecs.png import PNGImageEncoder
    pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png', encoder=PNGImageEncoder())

But it works, I could get the screenshot, so it might be related to python 
3.3.1. I'll investigate further to see if I can reproduce it but I'd appreciate 
if you can confirm that using PIL works for you too. Thanks!

Original comment by useboxnet on 6 Mar 2014 at 9:14

Attachments:

GoogleCodeExporter commented 9 years ago
I inspected the file site-packages/pyglet/image/codecs/pil.py in the pyglet 
installation under py3.

Found this suspicious snippet near the start of file:

try:
    import Image
except ImportError:
    from .PIL import Image

That .PIL looks wrong.
Removing the '.' fixes the problem for me.

As that '.' does not show in the pyglet checkout, it must have been introduced 
by the 2to3 performed in the py -3.3 setup.py install

As to why you can't reproduce, it may be a difference in pillow version: mine 
is the latest, the 2.3.0, yours is older, a 2.0.0.

And I remember for some previous version a warning in the pypi page along the 
lines:
'import Image' is not supported from version ???, please use 
'from PIL import Image'
(I checked pypi now and the current pillow page don't shows this warning)

So, if your version still supports 'import Image', I can see why you don't hit 
the faulty line.

Another possibility is that the 2to3 in my python 3.3.1 win32bits added the 
'.', and your version not. 
Can you look your py3 pyglet installation to see if you also have the offending 
'.' ?
And maybe put a print to clarify if the flow goes trough the except branch ?

Original comment by ccanepacc@gmail.com on 7 Mar 2014 at 7:26

GoogleCodeExporter commented 9 years ago
That's interesting. Thanks for the follow up, I'll try latest version of Pillow 
and I'll try to investigate why is that dot added by 2to3.

Good catch!

Original comment by useboxnet on 7 Mar 2014 at 8:08

GoogleCodeExporter commented 9 years ago
The '.' is an artifact produced by 2to3 in windows.
I will fill a bug in python bugtracker, it is still present in cpython default 
branch.

The core of the 2to3 issue is the heuristic "if file A has a non doted import, 
and there exists a file B in the same directory which matchs the import target 
(plus .py, .pyc,...) then add a '.' in the import "
The implementation checks if os.path.exists(name) , but Windows filenames are 
case-insensitive, and pyglet has a module pil , lowercase. Hence the problem.

Later I will follow up with the python issue #

Original comment by ccanepacc@gmail.com on 7 Mar 2014 at 5:32

GoogleCodeExporter commented 9 years ago
I was suspecting something like that. There's a pil.py in that directory, but 
it is lower case!

Well, Windows... upper/lower case, who cares! ;)

Original comment by useboxnet on 7 Mar 2014 at 5:34

GoogleCodeExporter commented 9 years ago

Original comment by useboxnet on 7 Mar 2014 at 5:35

GoogleCodeExporter commented 9 years ago
2to3 bug reported at python bugtraker, under
http://bugs.python.org/issue20867

Original comment by ccanepacc@gmail.com on 7 Mar 2014 at 9:18

GoogleCodeExporter commented 9 years ago
Claudio, I've thinking about this issue... can you try the following patch to 
see if it works as a workaround?

Thanks!

Original comment by useboxnet on 29 Mar 2014 at 11:56

Attachments:

GoogleCodeExporter commented 9 years ago
I found something simpler: add a

from __future__ import absolute_import

to pil.py , before any other import

Applying this change to a pyglet checkout and then installing to 3 by
    cd working_copy
    py -3.3 setup.py install

1. prevents the unwanted '.'
2. the imports sections in the installed pil.py looks same as ( installed 
unpatched pyglet + manually removing the '.' )
3. The failing script runs ok

I think this is a clearer fix to the 'failure to use pillow'.

From your comment 2 I suspected the pil fallback (pypng) was also acting up, 
and doing some testing confirms that. I will do a new issue for that. 

Original comment by ccanepacc@gmail.com on 29 Mar 2014 at 1:48

GoogleCodeExporter commented 9 years ago
Excellent, I like your idea better. I'm going to test it and commit it if I 
can't find anything weird.

So the issue was pypng being used instead of PIL because of the import but in 
2to3. It would be great if you could open a new issue so we can close this one.

Thanks for the follow up!

Original comment by useboxnet on 29 Mar 2014 at 1:52

GoogleCodeExporter commented 9 years ago
done,
 issue #717:    pypng tracebacks when trying to save (bytes vs str problem)

Original comment by ccanepacc@gmail.com on 29 Mar 2014 at 2:03

GoogleCodeExporter commented 9 years ago
This issue was closed by revision 489162ee33f1.

Original comment by useboxnet on 29 Mar 2014 at 2:05