[Python-Dev] Require loaders set __package__ and __loader__

Brett Cannon brett at python.org
Sun Apr 15 00:58:00 CEST 2012


On Sat, Apr 14, 2012 at 18:41, Christian Heimes <lists at cheimes.de> wrote:

> Am 15.04.2012 00:32, schrieb Guido van Rossum:
> > Funny, I was just thinking about having a simple standard API that
> > will let you open files (and list directories) relative to a given
> > module or package regardless of how the thing is loaded. If we
> > guarantee that there's always a __loader__ that's a first step, though
> > I think we may need to do a little more to get people who currently do
> > things like open(os.path.join(os.path.basename(__file__),
> > 'some_file_name') to switch. I was thinking of having a stdlib
> > function that you give a module/package object, a relative filename,
> > and optionally a mode ('b' or 't') and returns a stream -- and sibling
> > functions that return a string or bytes object (depending on what API
> > the user is using either the stream or the data can be more useful).
> > What would we call thos functions and where would the live?
>
> pkg_resources has a similar API [1] that supports dotted names.
> pkg_resources also does some caching for files that aren't stored on a
> local file system (database, ZIP file, you name it). It should be
> trivial to support both dotted names and module instances.
>
>
But that begs the question of whether this API should conflate module
hierarchies with file directories. Are we trying to support reading files
from within packages w/o caring about storage details but still
fundamentally working with files, or are we trying to abstract away the
concept of files and deal more with stored bytes inside packages? For the
former you would essentially want the root package and then simply specify
some file path. But for the latter you would want the module or package
that is next to or containing the data and grab it from there.

And I just realized that we would have to be quite clear that for namespace
packages it is what is in __file__ that people care about, else people
might expect some search to be performed on their behalf. Namespace
packages also dictate that you would want the module closest to the data in
the hierarchy to make sure you went down the right directory (e.g. if you
had the namespace package monty with modules spam and bacon but from
different directories, you really want to make sure you grab the right
module). I would argue that you can only go next to/within
modules/packages; going up would just cause confusion on where you were
grabbing from and going down could be done but makes things a little
messier.

-Brett


> Christian
>
> [1]
>
> http://packages.python.org/distribute/pkg_resources.html#resourcemanager-api
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/brett%40python.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120414/ad097a87/attachment.html>


More information about the Python-Dev mailing list