Message174779
Isn't it be clearer?
>>> import os
>>> dir_fd = os.open('somedir', os.O_RDONLY)
>>> def opener(path, flags):
... return os.open(path, flags, dir_fd=dir_fd)
...
>>> with open('spamspam.txt', 'w', opener=opener) as f:
... print('This will be written to somedir/spamspam.txt', file=f)
...
>>> os.close(dir_fd) # don't leak a file descriptor
Or if you want stronger example:
>>> import os, contextlib, functools
>>> @contextlib.contextmanager
... def open_relative(dirname):
... dir_fd = os.open(dirname, os.O_RDONLY)
... def opener(path, flags):
... return os.open(path, flags, dir_fd=dir_fd)
... try:
... yield functools.partial(open, opener=opener)
... finally:
... os.close(dir_fd)
...
>>> with open_relative('somedir') as open:
... with open('spamspam.txt', 'w') as f:
... print('This will be written to somedir/spamspam.txt', file=f)
...
Frankly speaking, all of these examples looks unconvincing to me. Even the second example could be implemented without an "opener" argument. |
|
Date |
User |
Action |
Args |
2012-11-04 11:09:55 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, eric.araujo, docs@python, rosslagerwall, python-dev, guillaumep |
2012-11-04 11:09:54 | serhiy.storchaka | set | messageid: <1352027394.99.0.396110604586.issue13424@psf.upfronthosting.co.za> |
2012-11-04 11:09:54 | serhiy.storchaka | link | issue13424 messages |
2012-11-04 11:09:54 | serhiy.storchaka | create | |
|