weird slash/backslash usage in resource path on windows

30 views
Skip to first unread message

josch

unread,
Nov 21, 2008, 5:10:04 AM11/21/08
to pyglet-users
put this file together with current pyglet code (i used trunk) in a
folder and execute it:

import pyglet

print pyglet.resource._default_loader._index.keys()

try:
pyglet.resource.file("pyglet\window\carbon\__init__.py")
except pyglet.resource.ResourceNotFoundException:
print "backslashes do not work"
else:
print "backslashes do work"

try:
pyglet.resource.file("pyglet/window/carbon/__init__.py")
except pyglet.resource.ResourceNotFoundException:
print "slashes do not work"
else:
print "slashes do work"

try:
pyglet.resource.file("pyglet\\window/carbon/__init__.py")
except pyglet.resource.ResourceNotFoundException:
print "slashes and backslashes do not work"
else:
print "slashes and backslashes do work"

it first prints all found paths as a list (which look already messes
up with slashes and backslashes) and then for me:

backslashes do not work
slashes do not work
slashses and backslashes do work

so the only way i can access a resource is throuh a combination of
backslashes and slashes like this:
pyglet\\window/carbon/__init__.py

i will try to fix that on my own in resource.py and will come back if
i found the solution but as i think that alex will be faster i'm
posting this issue here anyways

Kao Cardoso Felix

unread,
Nov 21, 2008, 7:10:16 PM11/21/08
to pyglet...@googlegroups.com
On Fri, Nov 21, 2008 at 8:10 AM, josch <j.sc...@web.de> wrote:
> so the only way i can access a resource is throuh a combination of
> backslashes and slashes like this:
> pyglet\\window/carbon/__init__.py

What about using os.path.join('pyglet', 'window', 'carbon',
'__init__.py')? Wouldn't it be "the right way to do it"?

--
Kao Cardoso Félix

Página pessoal: http://www.inf.ufrgs.br/~kcfelix
Blog: http://kaofelix.blogspot.com

josch

unread,
Nov 22, 2008, 3:05:09 AM11/22/08
to pyglet-users
On 22 Nov., 01:10, "Kao Cardoso Felix" <kcfe...@gmail.com> wrote:
> What about using os.path.join('pyglet', 'window', 'carbon',
> '__init__.py')? Wouldn't it be "the right way to do it"?

no, os.path.join() creates a path either separated by slashes (on
unix) or backslashes (on windows) and not a mix of both as requiered
here.

josch

unread,
Nov 22, 2008, 3:48:11 AM11/22/08
to pyglet-users
i got the fix!

resource.py in trunk, line 333 and following:

# Force forward slashes for index
if dirpath:
parts = filter(None, os.path.split(dirpath))
dirpath = '/'.join(parts)

has to be:

# Force forward slashes for index
if dirpath:
parts = filter(None, dirpath.split(os.sep))
dirpath = '/'.join(parts)

os.path.split ist not the same as string.split. from the docs:

"Split the pathname path into a pair, (head, tail) where tail is the
last pathname component and head is everything leading up to that."

but as i understand it we want to have a path that is only separated
by slashes and not a mix of them.
also it might be faster to use string.replace() instead of splitting
and joining lists?

sorry for not posting a diff but i could only test this at a friend's
computer as i do not have windows and it lacks the gnu tools :(

Alex Holkner

unread,
Nov 22, 2008, 10:16:35 PM11/22/08
to pyglet...@googlegroups.com

Thanks very much; fixe in r2384.

(Also, what a terribly confusing implementation/name for
os.path.split, given that it's not the analogue of str.split).

Alex.

Reply all
Reply to author
Forward
0 new messages