IronLanguages / IronLanguages/ironpython3

OSError raised on opening a file with mismatched mode

Open
#1,843 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

CPython compatibility module-io
Dominant language
C#
Stars
2.8k
Forks
316
Avg merge
1d 9h
Merged PRs (30d)
1

Description

One way of opening a file in Python is by using os.open to open a file descriptor, and then passing that descriptor to io.open (or the open builtin). Both calls require a file mode to be specified, but in a different way: os.open uses OS flags, and io.open a mode string. Both modes, though different in form, should be equivalent, though need not to.

For instance, flag os.O_WRONLY should be matched with 'w' or wb, os.O_RDONLY with 'r' or 'rb', and os.O_RDWR with 'w+' or 'wb+'.

In CPython, when the mode used with io.open does not match the opening flags used with os.open, the opening of the file succeeds, and io.UnsupportedOperation is raised only when the user code attempts to perform read/write operation that was not enabled when the file descriptor was opened.

In IronPython, OSError is raised immediately when the file object is being created, with a cryptic message "raw" argument must be readable. or "raw" argument must be writable.

This difference leads to some situations when the code works in CPython, but not in IronPython. Example:

import os, io

temp_file = "temp_file"

fd = os.open(temp_file, os.O_CREAT | os.O_WRONLY)
f = open(fd, 'w+') # OSError in IPY
f.write("hello\n")
f.close()

fd = os.open(temp_file, os.O_RDONLY)
f = io.open(fd, 'w+') # OSError in IPY
print(f.read())
f.close()

I consider it a minor incompatibility (because, basically, the user code is not quite consistent), but if changing IronPython is too complicated and not worth the effort for the payoff, at least the error message could be changed to something more informative, and the behavior documented in "Differences with CPython".

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the os.open and io.open/open entry points involved in the two examples, then compare IronPython's file-object creation behavior with the described CPython behavior. Done means either compatible delayed errors or a more informative error plus documented behavior in Differences with CPython; the issue names no source file or test.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, python
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.