Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for open’s new opener argument #57633

Closed
merwok opened this issue Nov 18, 2011 · 11 comments
Closed

Add examples for open’s new opener argument #57633

merwok opened this issue Nov 18, 2011 · 11 comments
Assignees
Labels
docs Documentation in the Doc dir easy type-feature A feature request or enhancement

Comments

@merwok
Copy link
Member

merwok commented Nov 18, 2011

BPO 13424
Nosy @ezio-melotti, @merwok, @serhiy-storchaka
Files
  • 13424.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/merwok'
    closed_at = <Date 2012-11-22.05:17:30.107>
    created_at = <Date 2011-11-18.09:52:13.281>
    labels = ['easy', 'type-feature', 'docs']
    title = 'Add examples for open\xe2\x80\x99s new opener argument'
    updated_at = <Date 2012-11-22.05:17:30.106>
    user = 'https://github.com/merwok'

    bugs.python.org fields:

    activity = <Date 2012-11-22.05:17:30.106>
    actor = 'eric.araujo'
    assignee = 'eric.araujo'
    closed = True
    closed_date = <Date 2012-11-22.05:17:30.107>
    closer = 'eric.araujo'
    components = ['Documentation']
    creation = <Date 2011-11-18.09:52:13.281>
    creator = 'eric.araujo'
    dependencies = []
    files = ['27866']
    hgrepos = []
    issue_num = 13424
    keywords = ['patch', 'easy']
    message_count = 11.0
    messages = ['147840', '147842', '174687', '174695', '174697', '174698', '174701', '174779', '175788', '176096', '176097']
    nosy_count = 7.0
    nosy_names = ['ezio.melotti', 'eric.araujo', 'docs@python', 'rosslagerwall', 'python-dev', 'serhiy.storchaka', 'guillaumep']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue13424'
    versions = ['Python 3.3', 'Python 3.4']

    @merwok
    Copy link
    Member Author

    merwok commented Nov 18, 2011

    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).

    @merwok merwok added the docs Documentation in the Doc dir label Nov 18, 2011
    @merwok
    Copy link
    Member Author

    merwok commented Nov 18, 2011

    s/TextIOWrapper/FileIO/

    @guillaumep
    Copy link
    Mannequin

    guillaumep mannequin commented Nov 3, 2012

    Here is a proposed patch to the documentation.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Nov 3, 2012

    New changeset 17b094c08600 by Éric Araujo in branch '3.3':
    Add examples for opener argument of open (bpo-13424).
    http://hg.python.org/cpython/rev/17b094c08600

    @merwok
    Copy link
    Member Author

    merwok commented Nov 3, 2012

    Fixed, thanks!

    @merwok merwok closed this as completed Nov 3, 2012
    @serhiy-storchaka
    Copy link
    Member

    See my comments in Rietveld.

    @serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Nov 3, 2012
    @merwok merwok reopened this Nov 3, 2012
    @merwok
    Copy link
    Member Author

    merwok commented Nov 3, 2012

    fd leak fixed in 95ea024f0569.

    @serhiy-storchaka
    Copy link
    Member

    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.

    @ezio-melotti
    Copy link
    Member

    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)

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Nov 22, 2012

    New changeset bf1bf3bf3fe2 by Éric Araujo in branch '3.3':
    Address reviews for open’s opener argument doc patch (bpo-13424).
    http://hg.python.org/cpython/rev/bf1bf3bf3fe2

    New changeset 8ca6f4953c4b by Éric Araujo in branch 'default':
    Merge bpo-13424 followup from 3.3
    http://hg.python.org/cpython/rev/8ca6f4953c4b

    @merwok
    Copy link
    Member Author

    merwok commented Nov 22, 2012

    Thanks for all comments. If you think of a better example to show the usage of the argument, feel free to change it.

    @merwok merwok closed this as completed Nov 22, 2012
    @merwok merwok assigned merwok and unassigned docspython Nov 22, 2012
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir easy type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants