-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Make _is_writable_dir more flexible to obscure failure modes #4165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Actually, while we are at it... I have run into rare issues where the home On Wed, Feb 25, 2015 at 12:48 PM, Erik Bray notifications@github.com
|
Probably the same issue. I'm a little surprised that case wouldn't result in an |
Seems fine to me. @tacaswell |
In the case where it is not an OSError, maybe it would be worth printing out the exception and then proceeding. |
In fact it does seem like this issue goes a bit deeper: matplotlib/lib/matplotlib/__init__.py Line 606 in 1fe5b9c
This gets called every time matplotlib is imported, even if the config dir already exists and the matplotlibrc already exists. It seems to me it shouldn't be doing this |
This is fine with me as is, but 👍 on printing exception in strange cases. I am ambivalent on minor changes in the logic in |
@tacaswell, Agreed--init.py is due for a close look. Related to that, it would be good to have a routine startup time benchmark test. For some applications startup time really matters. Part of it is out of our control (mainly numpy), but much of it isn't. A few years ago we were paying attention to this--@mdboom, I'm sure you remember. |
FWIW I was just testing this myself and found the time for |
…nged imports a little bit.
…nged imports a little bit.
MNT : Ensure the gc module is available during interpreter exit
@embray, would you add a comment to the code, so that a later reader will know why a universal |
@@ -225,7 +225,7 @@ def _is_writable_dir(p): | |||
t.write(b'1') | |||
finally: | |||
t.close() | |||
except OSError: | |||
except: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't forget a "# noqa" flag here. I would also think it would be better to do catch Exception as a bare except catches everything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would it catch besides a subclass of Exception
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the other hand there is still code out there that raises strange things (like strings), catching everything here seems like the correct thing to do 'Can we write to this dir yes or no?' I don't think we really care why we can't write there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@embray, there is a subtle difference between a bare exception and catching Exception. A bare exception is basically equivalent to "except BaseException:", which isn't the same thing as "except Exception:". But, @tacaswell has a good point, we want to be able to catch any sort of failure, for whatever reason and just move on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't there was a separate BaseException
under Exception
, but all the more reason not to explicitly catch just Exception
.
I don't see how could could "raise strings"--that would result in a TypeError.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought the restriction that you could only raise Exception
objects was new-ish and we support back to 2.6 (but I couldn't figure out quickly when that got changed).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought the restriction that you could only raise Exception
objects was new-ish and we support back to 2.6 (but I couldn't figure out quickly when that got changed).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we are clear on that. Just tried on py2.6.
[centos6] [broot@rd22 gust_checks]$ /usr/bin/python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> raise "SomeString"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: exceptions must be old-style classes or derived from
BaseException, not str
>>>
On Mon, Mar 2, 2015 at 4:43 PM, Thomas A Caswell notifications@github.com
wrote:
In lib/matplotlib/init.py
#4165 (comment):@@ -225,7 +225,7 @@ def _is_writable_dir(p):
t.write(b'1')
finally:
t.close()
- except OSError:
- except:
I thought the restriction that you could only raise Exception objects was
new-ish and we support back to 2.6 (but I couldn't figure out quickly when
that got changed).—
Reply to this email directly or view it on GitHub
https://github.com/matplotlib/matplotlib/pull/4165/files#r25641395.
@efiring I might find it worth adding a message if I understood why this was being done at all. I guess it has something to do with writing a default matplotlibrc, but how frequently has anyone been worried about that? |
Make _is_writable_dir more flexible to obscure failure modes
Travis is happy; let's move on. |
This is pretty obscure, but is something I've observed in testing out some installation issues.
When setuptools installs a package downloaded from a package index using
easy_install
--in particular if that package is listed in thesetup_requires
list of build-time requirements for another package, it uses a function calledrun_setup
to run thesetup.py
script for the downloaded package. It runs thesetup.py
in the context of aDirectorySandbox
which prevents thesetup.py
from writing files outside of the temporary directory set up for building the package.If, in this case, the
setup_requires
package happens to import matplotlib and it goes down the right code path that his function is called, it will result in aSandboxViolation
because matplotlib tried to write a file outside the sandbox. Unfortunately this is not anOSError
, so the exception is raised rather than simply returningFalse
is it should (indeed, the directory is not writable, so no attempt should be made to write a matplotlibrc file).I think that if matplotlib is going to attempt to write files to the filesystem during import time this function should be prepared for any kind of obscure error condition that could result :)