M
Marco
Does anyone have an example of utilisation?
Does anyone have an example of utilisation?
As of Python 3.2.3, there is no "opener" parameter in the open() function.
http://docs.python.org/py3k/library/functions.html
I don't know of any such parameter in earlier or later versions, but I
could be wrong there.
Am 03.09.2012 14:32, schrieb Marco:
The opener argument is a new 3.3 feature. For example you can use the
feature to implement exclusive creation of a file to avoid symlink
attacks.
import os
def opener(file, flags):
return os.open(file, flags | os.O_EXCL)
open("newfile", "w", opener=opener)
Because os.open() returns a low-level file descriptor, not a PythonWhy does the open builtin need this added complexity? Why not just call
os.open directly? Or for more complex openers, just call the opener
directly?
What is the rationale for complicating open instead of telling people to
just call their opener directly?
Because os.open() returns a low-level file descriptor, not a
Python file object?
To avoid the new syntax would mean coding the example as
f = os.fdopen(os.open("newfile", flags | os.O_EXCL), "w")
which does NOT look any cleaner to me...
Especially not if "opener" is to be used in more than one location.
Furthermore, using "opener" could
allow for a localized change to affect all open statements in the module
-- change file path, open for string I/O rather than file I/O, etc.
Why does the open builtin need this added complexity? Why not just call
os.open directly? Or for more complex openers, just call the opener
directly?
What is the rationale for complicating open instead of telling people to
just call their opener directly?
But it returns an OS file descriptor... It doesn't return a Pythonwhy not call that directly?
f = opener(file, flags)
It certainly is cleaner than either of the alternatives so far, and it
doesn't add a parameter to the builtin.
Not really -- but if they went one step further and suppliedI don't know of any real-life code which would be significantly improved
by that. Can you point us to some?
io.open depends on a function the returns an open file descriptor. opener
exposes that dependency so it can be replaced.
I skimmed the bug report comments but didn't find an answer to this:
Why not just monkey-patch?
Chris Angelico said:I skimmed the bug report comments but didn't find an answer to this:
Why not just monkey-patch? When a module function calls on a support
function and you want to change that support function's behaviour,
isn't monkey-patching the most usual?
Monkey-patching globals is not thread-safe: other threads will see your
modification, which is risky and fragile.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.