-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Description
There a few differences between the way CPython handles file IO and the way RustPython does.
- RustPython does not currently offer any
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 theosmodule. - RustPython currently doesn’t offer text writing mode (which is the default for writing in CPython), so in CPython:
one can try to open a file for text writing in RustPython with
>>> file = open('/tmp/tst', 'w') >>> file <_io.TextIOWrapper name='/tmp/tst' mode='w' encoding='UTF-8'>
wtmode, but then writing fails:and files for writing are by default opened in binary mode.>>>>> file = open('/tmp/tst', 'wt') >>>>> file.write('\ntest string') Traceback (most recent call last): File <stdin>, line 0, in <module> AttributeError: 'TextIOWrapper' object has no attribute 'write'
- RustPython’s
filenois a property, while in CPython it is a method returning the underlying file descriptor. - CPython doesn’t rely on the
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:while in RustPython, changing the>>> file = open('/tmp/test', 'wb') >>> file.write(b'writing to the original file\n') 29 >>> file.raw.fileno = lambda: 1 # stdout descriptor >>> file.write(b'again writing to the original file\n') 35 >>>
filenoproperty changes the underlying file:>>>>> file = open('/tmp/test', 'wb') >>>>> file.write(b'writing to the original file\n') 29 >>>>> file.raw.fileno = 1 # stdout descriptor >>>>> file.write(b'this will appear in the stdout instead\n') this will appear in the stdout instead 39 >>>>>
- Some file attributes in CPython are protected from modifications, eg.:
>>> file = open('/tmp/test', 'wb') >>> file.raw.mode = 'rb' Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: attribute 'mode' of '_io.FileIO' objects is not writable
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).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels