[Python-Dev] PEP 3147, __pycache__ directories and umask

Cameron Simpson cs at zip.com.au
Tue Mar 23 01:40:02 CET 2010

On 22Mar2010 09:56, Barry Warsaw <barry at python.org> wrote:
| I have a pretty good start on PEP 3147 implementation [1], but I've
| encountered a situation that I'd like to get some feedback on.  Here's the
| test case illustrating the problem.  From test_import.py:
|     def test_writable_directory(self):
|         # The umask is not conducive to creating a writable __pycache__
|         # directory.
|         with umask(0o222):
| The __pycache__ directory does not exist before the import, and the import
| machinery creates the directory, but the umask leaves the directory unwritable
| by anybody.  So of course when the import machinery goes to write the .pyc
| file inside __pycache__, it fails.  This does not cause an ImportError though,
| just like if today the package directory were unwritable.
| This might be different than today's situation though because once the
| unwritable __pycache__ directory is created, nothing is going to change that
| without explicit user interaction, and that might be difficult after the
| fact.

Like any bad/suboptimal permission.

| I'm not sure what the right answer is.  Some possible choices:
| * Tough luck

I'd go with this one myself.

| * Force the umask so that the directory is writable, but then the question is,
|   by whom?  ugo+w or something less?

Racy and dangerous. The umask is a UNIX process global, and other threads may
get bad results if they're active during this window.

| * Copy the permissions from the parent directory and ignore umask

Maybe. But consider that you may not be the owner of the parent: then
the new child will have different ownership than the parent but the same
permission mask. Potentially a bad mix. This approach is very hard to
get right.

| * Raise an exception or refuse to create __pycache__ if it's not writable
|   (again, by whom?)

Bleah. My python program won't run because an obscure (to the user)
directory had unusual permissions?

Tough ove, it's the only way:-)
Cameron Simpson <cs at zip.com.au> DoD#743

When asked what would I most want to try before doing it, I said Death.
        - Michael Burton, michaelb at compnews.co.uk

More information about the Python-Dev mailing list