path.startswith(dir)?

Magnus Lie Hetland mlh at vier.idi.ntnu.no
Sat May 11 14:56:09 EDT 2002


In article <mailman.1021130503.14411.python-list at python.org>, Sean
'Shaleh' Perry wrote:
>
[snip]
>
>Sorry for not noticing the hidden os.path import.

No -- my mistake. I should have been more explicit :)

>Could you show us say 10 lines or so of this code being used?  From the above
>code it looks ok, but may have hidden snags.  Than again, maybe this is simple
>and I am not seeing it.

Here is some:

def inside(dir, name):
    return isfile(name) and name.startswith(join(dir, ''))

[...]

    def _handle(self, query):
        dir = self.dirname
        name = abspath(join(dir, query))
        if not inside(dir, name): raise AccessDenied
        try: return open(name).read()
        except IOError: raise UnhandledQuery

Here self.dirname has been abspath'ed before.

Of course, slapping on two abspaths in the inside() function would
probably be a good idea. It's just that I need to call abspath on them
elsewhere, so I thought I might as well drop it there... (Makes the
inside function less reusable, I guess...)

Hm. I guess I could just use abspath inside the inside function
(excuse the pun) and dop it elsewhere; after all, open(name) ought to
give me the same result as open(abspath(name)), right?

--
Magnus Lie Hetland                                  The Anygui Project
http://hetland.org                                  http://anygui.org



More information about the Python-list mailing list