Patch to fix loading images of the same name

72 views
Skip to first unread message

Nathan

unread,
Oct 4, 2012, 12:58:40 AM10/4/12
to pyglet...@googlegroups.com
Currently, if you set pyglet.resource.path to ["foo"] and load "abc.png" (foo/abc.png), and then change pyglet.resource.path to ["bar"] and try to load "abc.png", you still get foo/abc.png, since the caching looks at the name of the image file only.

This simple patch makes it so that the images are cached per specific setting of pyglet.resource.path.  I included the patch as an attachment and inline below since it's so tiny.

# HG changeset patch
# User Nathan Stocks <nathan...@gmail.com>
# Date 1349326606 21600
# Node ID f8102f9ee5b19c43c6129dac070bf393f82e3ef7
# Parent  bf7f6c05275664ed76aab926944fe3a6a74ff022
Made it so that once you load an image, you can still load more images of the same name by changing pyglet.resource.path.

diff -r bf7f6c052756 -r f8102f9ee5b1 pyglet/resource.py
--- a/pyglet/resource.py Sat Sep 15 16:48:08 2012 -0500
+++ b/pyglet/resource.py Wed Oct 03 22:56:46 2012 -0600
@@ -504,9 +504,9 @@
         '''
         self._require_index()
         if name in self._cached_images:
-            identity = self._cached_images[name]
+            identity = self._cached_images[str(self.path)+name]
         else:
-            identity = self._cached_images[name] = self._alloc_image(name,
+            identity = self._cached_images[str(self.path)+name] = self._alloc_image(name,
                 atlas=atlas)
 
         if not rotate and not flip_x and not flip_y:


Works great for me!

Thoughts?

~ Nathan
resource_cache_fix.diff

Andre D

unread,
Oct 4, 2012, 7:35:40 AM10/4/12
to pyglet...@googlegroups.com
Please use os.path.join to combine paths
> --
> You received this message because you are subscribed to the Google Groups
> "pyglet-users" group.
> To post to this group, send email to pyglet...@googlegroups.com.
> To unsubscribe from this group, send email to
> pyglet-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pyglet-users?hl=en.

Nathan

unread,
Oct 4, 2012, 11:36:10 AM10/4/12
to pyglet...@googlegroups.com
Andre, I'm not combining paths.  I'm using the string representation of the current state of the list of paths to add a unique prefix to the key used to cache the image.

For example, if pyglet.resource.path is ['my_sad_theme', 'my_happy_theme'],

...then the key will end up being the string  "['my_sad_theme', 'my_happy_theme']imagename"

If you change the path in any way, then the string representation of the entire path list changes, which results in a different unique key, and a new image searched for and cached using the new string representation of the list as a prefix to the key.

Not quite as elegant as making a hash of the path state and using that as a shorter prefix, but it definitely works.

~ Nathan

Nathan

unread,
Oct 8, 2012, 11:03:22 PM10/8/12
to pyglet...@googlegroups.com
Sooo, would anyone be willing to commit this?  Or...?

~ Nathan

Adam Bark

unread,
Oct 9, 2012, 5:00:05 AM10/9/12
to pyglet...@googlegroups.com

I should have some time to check it over tonight. Is there an issue for this in the tracker?

Andreas Schiefer

unread,
Oct 9, 2012, 6:13:36 AM10/9/12
to pyglet...@googlegroups.com
I just noticed a small bug in this patch that causes it to never
actually use the cached images. The condition for testing if a cached
version is available still looks for "name" in the dict.

So the line
if name in self._cached_images:
should of course read
if str(self.path)+name in self._cached_images:

With that change, it might be desirable to put the value of
"str(self.path)+name" into a variable before the "if", instead of
computing it twice (for checking and then getting/setting the cached
image).

Additionally, for consistency reasons, I think we should add similar
changes to the texture() and animation() functions too, which also use
a cache.

Nathan

unread,
Oct 9, 2012, 10:54:54 AM10/9/12
to pyglet...@googlegroups.com
I will check.  Would you like me to create one if I don't find one?

~ Nathan

Nathan

unread,
Oct 9, 2012, 10:55:44 AM10/9/12
to pyglet...@googlegroups.com
Good catch, and good ideas.

Adam Bark

unread,
Oct 9, 2012, 3:03:21 PM10/9/12
to pyglet...@googlegroups.com

It might be good to document it if you can be bothered.

On Oct 9, 2012 3:54 PM, "Nathan" <nathan...@gmail.com> wrote:

Nathan

unread,
Oct 10, 2012, 12:42:10 AM10/10/12
to pyglet...@googlegroups.com
Absolutely.  I reviewed all open issues, and found no existing relevant ones, so I added this as issue 612.


~ Nathan

On Tue, Oct 9, 2012 at 1:03 PM, Adam Bark <adam....@gmail.com> wrote:

It might be good to document it if you can be bothered.

On Oct 9, 2012 3:54 PM, "Nathan" <nathan...@gmail.com> wrote:

Adam Bark

unread,
Oct 10, 2012, 6:09:23 PM10/10/12
to pyglet...@googlegroups.com
On 10/10/12 05:42, Nathan wrote:
Absolutely.  I reviewed all open issues, and found no existing relevant ones, so I added this as issue 612.


~ Nathan

On Tue, Oct 9, 2012 at 1:03 PM, Adam Bark <adam....@gmail.com> wrote:

It might be good to document it if you can be bothered.

Thanks. I've posted a couple of things including a work around that I would like opinions on.
Cheers,
Adam.

Nathan

unread,
Dec 3, 2012, 1:05:47 AM12/3/12
to pyglet...@googlegroups.com, Richard Jones
Richard, while you're in a committing mood, can you commit Adam's patch for issue 612?


I tested it, and it works great for me.  It's also a much more elegant (and functional) solution than my quick-n-dirty attempt.

~ Nathan


--

Adam Bark

unread,
Dec 3, 2012, 3:00:57 AM12/3/12
to pyglet...@googlegroups.com
On 03/12/12 06:05, Nathan wrote:
> Richard, while you're in a committing mood, can you commit Adam's
> patch for issue 612?
>
> https://code.google.com/p/pyglet/issues/detail?id=612#c2
>
> I tested it, and it works great for me. It's also a much more elegant
> (and functional) solution than my quick-n-dirty attempt.
>
> ~ Nathan
>
I can commit it later, I was just waiting for confirmation that it works.
Thanks,
Adam.

Nathan

unread,
Dec 3, 2012, 12:50:18 PM12/3/12
to pyglet...@googlegroups.com
Oh, great.  Didn't realize you were a committer.

~ Nathan



Adam.

--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To post to this group, send email to pyglet...@googlegroups.com.
To unsubscribe from this group, send email to pyglet-users+unsubscribe@googlegroups.com.

Adam Bark

unread,
Dec 4, 2012, 3:52:30 PM12/4/12
to pyglet...@googlegroups.com
On 03/12/12 17:50, Nathan wrote:
Oh, great.  Didn't realize you were a committer.

~ Nathan


On Mon, Dec 3, 2012 at 1:00 AM, Adam Bark <adam....@gmail.com> wrote:
On 03/12/12 06:05, Nathan wrote:
Richard, while you're in a committing mood, can you commit Adam's patch for issue 612?

https://code.google.com/p/pyglet/issues/detail?id=612#c2

I tested it, and it works great for me.  It's also a much more elegant (and functional) solution than my quick-n-dirty attempt.

~ Nathan

I can commit it later, I was just waiting for confirmation that it works.
Thanks,

Adam.

Ok issue 612 is now fixed as of changeset 9e151b2a47e4.
Adam.
Reply all
Reply to author
Forward
0 new messages