This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients docs@python, eric.araujo, guillaumep, python-dev, rosslagerwall, serhiy.storchaka
Date 2012-11-04.11:09:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1352027394.99.0.396110604586.issue13424@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2012-11-04 11:09:55serhiy.storchakasetrecipients: + serhiy.storchaka, eric.araujo, docs@python, rosslagerwall, python-dev, guillaumep
2012-11-04 11:09:54serhiy.storchakasetmessageid: <1352027394.99.0.396110604586.issue13424@psf.upfronthosting.co.za>
2012-11-04 11:09:54serhiy.storchakalinkissue13424 messages
2012-11-04 11:09:54serhiy.storchakacreate