<br><br><div class="gmail_quote">On Sat, Apr 14, 2012 at 18:56, Guido van Rossum <span dir="ltr">&lt;<a href="mailto:guido@python.org">guido@python.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="im">On Sat, Apr 14, 2012 at 3:50 PM, Brett Cannon &lt;<a href="mailto:brett@python.org">brett@python.org</a>&gt; wrote:<br>
&gt; On Sat, Apr 14, 2012 at 18:32, Guido van Rossum &lt;<a href="mailto:guido@python.org">guido@python.org</a>&gt; wrote:<br>
</div><div class="im">&gt;&gt; Funny, I was just thinking about having a simple standard API that<br>
&gt;&gt; will let you open files (and list directories) relative to a given<br>
&gt;&gt; module or package regardless of how the thing is loaded. If we<br>
&gt;&gt; guarantee that there&#39;s always a __loader__ that&#39;s a first step, though<br>
&gt;&gt; I think we may need to do a little more to get people who currently do<br>
&gt;&gt; things like open(os.path.join(os.path.basename(__file__),<br>
&gt;&gt; &#39;some_file_name&#39;) to switch. I was thinking of having a stdlib<br>
&gt;&gt; function that you give a module/package object, a relative filename,<br>
&gt;&gt; and optionally a mode (&#39;b&#39; or &#39;t&#39;) and returns a stream -- and sibling<br>
&gt;&gt; functions that return a string or bytes object (depending on what API<br>
&gt;&gt; the user is using either the stream or the data can be more useful).<br>
&gt;&gt; What would we call thos functions and where would the live?<br>
<br>
&gt; IOW go one level lower than get_data() and return the stream and then just<br>
&gt; have helper functions which I guess just exhaust the stream for you to<br>
&gt; return bytes or str? Or are you thinking that somehow providing a function<br>
&gt; that can get an explicit bytes or str object will be more optimized than<br>
&gt; doing something with the stream? Either way you will need new methods on<br>
&gt; loaders to make it work more efficiently since loaders only have get_data()<br>
&gt; which returns bytes and not a stream object. Plus there is currently no API<br>
&gt; for listing the contents of a directory.<br>
<br>
</div>Well, if it&#39;s a real file, and you need a stream, that&#39;s efficient,<br>
and if you need the data, you can read it. But if it comes from a<br>
loader, and you need a stream, you&#39;d have to wrap it in a StringIO<br>
instance. So having two APIs, one to get a stream, and one to get the<br>
data, allows the implementation to be more optimal -- it would be bad<br>
to wrap a StringIO instance around data only so you can read the data<br>
from the stream again...<br></blockquote><div><br></div><div>Right, so you would need to grow, which is fine and can be done in a backwards-compatible way using io.BytesIO and StringIO.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div class="im"><br>
&gt; As for what to call such functions, I really don&#39;t know since they are<br>
&gt; essentially abstract functions above the OS which work on whatever storage<br>
&gt; backend a module uses.<br>
&gt;<br>
&gt; For where they should live, it depends if you are viewing this as more of a<br>
&gt; file abstraction or something that ties into modules. For the former it<br>
&gt; seems like shutil or something that dealt with higher order file<br>
&gt; manipulation. If it&#39;s the latter I would say importlib.util.<br>
<br>
</div>if pkg_resources is in the stdlib that would be a fine place to put it.<br></blockquote><div><br></div><div>It&#39;s not.</div><div><br></div><div>-Brett</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div class="HOEnZb"><div class="h5"><br>
--<br>
--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)<br>
</div></div></blockquote></div><br>

AltStyle によって変換されたページ (->オリジナル) /