A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/attachments/20090403/5db48395/attachment.htm below:

<div class="gmail_quote">(cross-posting back to python-dev to finalize discussions)<br><br>2009/4/2 Guido van Rossum <span dir="ltr">&lt;<a href="mailto:guido@python.org">guido@python.org</a>&gt;</span><br>[...]<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div class="h5">&gt; The problem you report:<br>
&gt;&gt;<br>
&gt;&gt; Â try:<br>
&gt;&gt; Â  Â ...<br>
&gt;&gt; Â except OSWinError:<br>
&gt;&gt; Â  Â ...<br>
&gt;&gt; Â except OSLinError:<br>
&gt;&gt; Â  Â ...<br>
&gt;&gt;<br>
&gt;<br>
&gt; Would be solved if both OSWinError and OSLinError were always defined in<br>
&gt; both Linux and Windows Python.  Programs could be written to catch both<br>
&gt; OSWinError and OSLinError, except that on Linux OSWinError would never<br>
&gt; actually be raised, and on Windows OSLinError would never occur.  Problem<br>
&gt; solved.<br>
<br>
</div></div>Yeah, but now you&#39;d have to generate the list of exceptions (which<br>
would be enormously long) based on the union of all errno codes in the<br>
universe.<br>
<br>
Unless you only want to do it for some errno codes and not for others,<br>
which sounds like asking for trouble.<br>
<br>
Also you need a naming scheme that works for all errnos and doesn&#39;t<br>
require manual work. Frankly, the only scheme that I can think of that<br>
could be automated would be something like OSError_ENAME.<br>
<br>
And, while OSError is built-in, I think these exceptions (because<br>
there are so many) should not be built-in, and probably not even live<br>
in the &#39;os&#39; namespace -- the best place for them would be the errno<br>
module, so errno.OSError_ENAME.<br>
<div class="im"><br>
&gt; The downsides of this?  I can only see memory, at the moment, but I might be<br>
&gt; missing something.<br>
<br>
</div>It&#39;s an enormous amount of work to make it happen across all<br>
platforms. And it doesn&#39;t really solve an important problem.</blockquote><div><br>I partially agree.  It will be a lot of work.  I think the problem is valid, although not very important, I agree.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<div class="im"><br>
&gt; Now just one final word why I think this matters.  The currently correct way<br>
&gt; to remove a directory tree and only ignore the error &quot;it does not exist&quot; is:<br>
&gt;<br>
&gt; try:<br>
&gt; Â Â Â  shutil.rmtree(&quot;dirname&quot;)<br>
&gt; except OSError, e:<br>
&gt; Â Â Â  if errno.errorcode[e.errno] != &#39;ENOENT&#39;:<br>
&gt; Â Â Â Â Â Â  raise<br>
&gt;<br>
&gt; However, only very experienced programmers will know to write that correct<br>
&gt; code (apparently I am not experienced enought!).<br>
<br>
</div>That doesn&#39;t strike me as correct at all, since it doesn&#39;t distinguish<br>
between ENOENT being raised for some file deep down in the tree vs.<br>
the root not existing. (This could happen if after you did<br>
os.listdir() some other process deleted some file.)</blockquote><div><br>OK.  Maybe in a generic case this could happen, although I&#39;m sure this won&#39;t happen in my particular scenario.  This is about a build system, and I am assuming there are no two concurrent builds (or else a lot of other things would fail anyway).<br>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
A better way might be<br>
<br>
try:<br>
 Â shutil.rmtree(&lt;dir&gt;)<br>
except OSError:<br>
 Â if os.path.exists(&lt;dir&gt;):<br>
 Â  raise</blockquote><div><br>Sure, this works, but at the cost of an extra system call.  I think it&#39;s more elegant to check the errno (assuming the corner case you pointed out above is not an issue). <br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

<br>


Though I don&#39;t know what you wish to happen of &lt;dir&gt; were a dangling symlink.<br>
<div class="im"><br>
&gt; What I am proposing is that the simpler correct code would be something<br>
&gt; like:<br>
&gt;<br>
&gt; try:<br>
&gt; Â Â Â  shutil.rmtree(&quot;dirname&quot;)<br>
&gt; except OSNoEntryError:<br>
&gt; Â Â Â  pass<br>
&gt;<br>
&gt; Much simpler, no?<br>
<br>
</div>And wrong.<br>
<div class="im"><br>
&gt; Right now, developers are tempted to write code like:<br>
&gt;<br>
&gt; Â Â Â  shutil.rmtree(&quot;dirname&quot;, ignore_errors=True)<br>
&gt;<br>
&gt; Or:<br>
&gt;<br>
&gt; try:<br>
&gt; Â Â Â  shutil.rmtree(&quot;dirname&quot;)<br>
&gt; except OSError:<br>
&gt; Â Â Â  pass<br>
&gt;<br>
&gt; Both of which follow the error hiding anti-pattern [1].<br>
&gt;<br>
&gt; [1] <a href="http://en.wikipedia.org/wiki/Error_hiding" target="_blank">http://en.wikipedia.org/wiki/Error_hiding</a><br>
&gt;<br>
&gt; Thanks for reading this far.<br>
<br>
</div>Thanks for not wasting any more of my time.</blockquote></div><br>OK, I won&#39;t waste more time.  If this were an obvious improvement beyond doubt to most people, I would pursue it, but since it&#39;s not, I can live with it.<br>
<br>Thanks anyway,<br clear="all"><br>-- <br>Gustavo J. A. M. Carneiro<br>INESC Porto, Telecommunications and Multimedia Unit<br>&quot;The universe is always one step beyond logic.&quot; -- Frank Herbert<br>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4