-
Notifications
You must be signed in to change notification settings - Fork 1.3k
File IO issues #547
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
Comments
As to 2. – it seems that What’s interesting, >>> class PseudoFile:
... def write(self, buf):
... print(f'printing buffer: {buf.decode("utf-8")}')
... return 4
... def read(self):
... return b'a'
... def flush(self):
... print('flushing')
... def readable(self):
... return True
... def writable(self):
... res = self._num == 0
... self._num += 1
... return res
... def seekable(self):
... return False
... def __init__(self):
... self.closed = False
... self._num = 0
...
>>>
>>> pseudoWrapper = io.TextIOWrapper(PseudoFile())
>>> pseudoWrapper.buffer.writable()
False
>>> pseudoWrapper.write('noël')
5
>>> pseudoWrapper.write('noël')
5
>>> pseudoWrapper.flush()
printing buffer: noëlnoël
flushing Also, the |
@silmeth is this resolved now? There have been a fair amount of |
It seems it is not. There is some progress (eg. files are opened by default in text, not binary, mode, as before), but still:
(and I believe the other two are also still true but didn’t test them, they are lower priority anyway, and the last one is not IO-specific) Run on master, commit f34f16b2cc0422116f9044df1f4818bcc6941a4c: % RUSTPYTHONPATH=~/Projekty/RustPython/Lib cargo run --release
Finished release [optimized] target(s) in 0.18s
Running `target/release/rustpython`
Welcome to the magnificent Rust Python 0.0.1 interpreter 😱 🖖
>>>>> # Correct text mode (with TextIOWrapper):
>>>>> file = open('/tmp/tst', 'w')
>>>>> file
<TextIOWrapper object at 0x560ef58f43e0>
>>>>>
>>>>> # No writing for text files:
>>>>> file.write('\ntest string')
Traceback (most recent call last):
File "<stdin>", line 0, in <module>
AttributeError: "'TextIOWrapper' object has no attribute 'write'"
>>>>>
>>>>> # No closing:
>>>>> file.close()
Traceback (most recent call last):
File "<stdin>", line 0, in <module>
AttributeError: "'TextIOWrapper' object has no attribute 'close'"
>>>>> file.buffer.close()
Traceback (most recent call last):
File "<stdin>", line 0, in <module>
AttributeError: "'BufferedWriter' object has no attribute 'close'"
>>>>> file.buffer.raw.close()
Traceback (most recent call last):
File "<stdin>", line 0, in <module>
AttributeError: "'FileIO' object has no attribute 'close'"
>>>>>
>>>>> # fileno is still a property, not a method:
>>>>> file.buffer.raw.fileno
3 cf. with CPython 3.7: % python3
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> file = open('/tmp/tst', 'w')
>>> file
<_io.TextIOWrapper name='/tmp/tst' mode='w' encoding='UTF-8'>
>>> file.write('\ntest string')
12
>>> file.buffer.close()
>>> file.close()
>>> file.buffer.raw.fileno
<built-in method fileno of _io.FileIO object at 0x7f4eb195fc60> |
it seems |
Step 2 works:
Writing w/o text mode works
Closing buffers etc. works
fileno is a method now rather than a property
The behavior
The Python documentation says:
Given all of this, I think the issue can be closed. |
Thank you for confirming! |
Uh oh!
There was an error while loading. Please reload this page.
There a few differences between the way CPython handles file IO and the way RustPython does.
close()
methods, opened files get leaked and can be closed only by invokingos.close(file.raw.fileno)
(oros.close(file.buffer.raw.fileno)
for files in text mode) after importing theos
module.wt
mode, but then writing fails:fileno
is a property, while in CPython it is a method returning the underlying file descriptor.fileno()
value for file interactions, instead it uses internal file handle whilefileno()
just returns it as a Pythonint
, so one can modify it, and the file object will still be referring to the original file:fileno
property changes the underlying file:And then there are some attributes missing (eg. RustPython files do not have a
mode
attribute at all).The first three issues seem easy to fix, but the 4. and 5. need changes in the way RustPython handles objects (they mostly behave like regular Python objects, but need custom payload and properties protected from being overwritten).
The text was updated successfully, but these errors were encountered: