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

confusing error for file("foo", "w++") #49079

Closed
eckhardt mannequin opened this issue Jan 4, 2009 · 5 comments
Closed

confusing error for file("foo", "w++") #49079

eckhardt mannequin opened this issue Jan 4, 2009 · 5 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@eckhardt
Copy link
Mannequin

eckhardt mannequin commented Jan 4, 2009

BPO 4829
Nosy @birkenfeld, @jszakmeister
Files
  • python-2.7-fopen-mode-parsing.0.patch: 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 = None
    closed_at = <Date 2010-10-21.13:45:58.574>
    created_at = <Date 2009-01-04.11:17:16.825>
    labels = ['interpreter-core', 'type-bug']
    title = 'confusing error for file("foo", "w++")'
    updated_at = <Date 2010-10-21.13:45:58.573>
    user = 'https://bugs.python.org/eckhardt'

    bugs.python.org fields:

    activity = <Date 2010-10-21.13:45:58.573>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-10-21.13:45:58.574>
    closer = 'georg.brandl'
    components = ['Interpreter Core']
    creation = <Date 2009-01-04.11:17:16.825>
    creator = 'eckhardt'
    dependencies = []
    files = ['12581']
    hgrepos = []
    issue_num = 4829
    keywords = ['patch']
    message_count = 5.0
    messages = ['79043', '88503', '88734', '89072', '119295']
    nosy_count = 3.0
    nosy_names = ['georg.brandl', 'jszakmeister', 'eckhardt']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4829'
    versions = ['Python 2.7']

    @eckhardt
    Copy link
    Mannequin Author

    eckhardt mannequin commented Jan 4, 2009

    Specifying the '+' more than once while opening a file results in the
    error "Must have exactly one of read/write/append mode". The attached
    patch extends that with ".. and at most one optional plus".

    Further, the patch checks these after the loop that parses the mode
    string, avoiding some unnecessary gotos and saving a few lines of code
    overall.

    @eckhardt eckhardt mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Jan 4, 2009
    @jszakmeister
    Copy link
    Mannequin

    jszakmeister mannequin commented May 29, 2009

    On trunk, it seems that it's perfectly happy if you specify more than
    one '+':

    Python 2.7a0 (trunk, May 29 2009, 05:57:26) 
    [GCC 4.0.1 (Apple Inc. build 5470) (Aspen 5470.3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> open('foo.txt', 'w++')
    <open file 'foo.txt', mode 'w++' at 0x39b2a0>

    Is this still really an issue then? The current trunk also allows
    multiple mode letters too, so it seems like a decision has been made.

    @eckhardt
    Copy link
    Mannequin Author

    eckhardt mannequin commented Jun 2, 2009

    Good catch, it just took me a while to actually figure out myself where
    the bug is. Try the following instead:

      import io
      io.FileIO('foo.text', 'w++')

    This will yield "ValueError: Must have exactly one of read/write/append
    mode" with 2.6 on win32. I haven't tested on the latest 2.x or 3.x
    branches, but looking at the 2.7 branch, I see the faulty code is still
    there.

    BTW:
    Using io.open('foo.text', 'w++') yields "ValueError: invalid mode:
    'w++'", I would have expected the same error as for io.FileIO() above.
    Using open('foo.text', 'w++') works.
    Using open('foo.text', 'ww++') yields "IOError: [Errno 22] invalid mode
    ('ww++') or filename: 'foo.text')".

    In other words, Python 2.6 is behaving a bit inconsistent here. The
    patch only fixes one of those issues, the others and the necessary unit
    tests remain.

    @jszakmeister
    Copy link
    Mannequin

    jszakmeister mannequin commented Jun 8, 2009

    The offending lines in io.py are:
    modes = set(mode)
    if modes - set("arwb+tU") or len(mode) > len(modes):
    raise ValueError("invalid mode: %r" % mode)

    In particular, the "or len(mode) > len(modes)" is picking off the fact
    that there is repeated mode characters. Leaving that off allows
    io.open() to behave exactly like the built-in open() call.

    OTOH, someone obviously wanted to make sure that repeat mode characters
    were not allowed. So I guess someone needs to rule on whether we want
    io.open() and io.FileIO() to behave like the built-in, or to keep things
    more strict.

    @birkenfeld
    Copy link
    Member

    Amended error message in r85773.

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant