[issue13424] Add examples for open’s new opener argument

New submission from Éric Araujo <merwok@netwok.org>: The new opener argument to open and TextIOWrapper closed two bugs on this tracker: using O_CLOEXEC and replacing the unofficial 'c' mode (O_CREATE). I think it’d be nice to have these as examples (maybe not in the docs of TextIOWrapper which are already huge, but for example in the os docs after the O_* constants). ---------- assignee: docs@python components: Documentation messages: 147840 nosy: docs@python, eric.araujo priority: normal severity: normal status: open title: Add examples for open’s new opener argument versions: Python 3.3 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Éric Araujo <merwok@netwok.org> added the comment: s/TextIOWrapper/FileIO/ ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Changes by Ross Lagerwall <rosslagerwall@gmail.com>: ---------- nosy: +rosslagerwall _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- keywords: +easy stage: -> needs patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Guillaume P added the comment: Here is a proposed patch to the documentation. ---------- keywords: +patch nosy: +guillaumep Added file: http://bugs.python.org/file27866/13424.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Roundup Robot added the comment: New changeset 17b094c08600 by Éric Araujo in branch '3.3': Add examples for opener argument of open (#13424). http://hg.python.org/cpython/rev/17b094c08600 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Éric Araujo added the comment: Fixed, thanks! ---------- resolution: -> fixed stage: needs patch -> committed/rejected status: open -> closed versions: +Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Serhiy Storchaka added the comment: See my comments in Rietveld. ---------- nosy: +serhiy.storchaka type: -> enhancement _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Changes by Éric Araujo <merwok@netwok.org>: ---------- resolution: fixed -> stage: committed/rejected -> commit review status: closed -> open _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Éric Araujo added the comment: fd leak fixed in 95ea024f0569. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Serhiy Storchaka added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Ezio Melotti added the comment: There's also a Sphinx warning that should be fixed: 3.3/Doc/library/functions.rst:955: WARNING: undefined label: dir_fd (if the link has no caption the label must precede a section header) ---------- nosy: +ezio.melotti _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Roundup Robot added the comment: New changeset bf1bf3bf3fe2 by Éric Araujo in branch '3.3': Address reviews for open’s opener argument doc patch (#13424). http://hg.python.org/cpython/rev/bf1bf3bf3fe2 New changeset 8ca6f4953c4b by Éric Araujo in branch 'default': Merge #13424 followup from 3.3 http://hg.python.org/cpython/rev/8ca6f4953c4b ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________

Éric Araujo added the comment: Thanks for all comments. If you think of a better example to show the usage of the argument, feel free to change it. ---------- assignee: docs@python -> eric.araujo resolution: -> fixed stage: commit review -> committed/rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13424> _______________________________________
participants (6)
-
Ezio Melotti
-
Guillaume P
-
Ross Lagerwall
-
Roundup Robot
-
Serhiy Storchaka
-
Éric Araujo