Skip to content

File IO issues #547

@silmeth

Description

@silmeth

There a few differences between the way CPython handles file IO and the way RustPython does.

  1. RustPython does not currently offer any close() methods, opened files get leaked and can be closed only by invoking os.close(file.raw.fileno) (or os.close(file.buffer.raw.fileno) for files in text mode) after importing the os module.
  2. RustPython currently doesn’t offer text writing mode (which is the default for writing in CPython), so in CPython:
    >>> file = open('/tmp/tst', 'w')
    >>> file
    <_io.TextIOWrapper name='/tmp/tst' mode='w' encoding='UTF-8'>
    one can try to open a file for text writing in RustPython with wt mode, but then writing fails:
    >>>>> 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'
    and files for writing are by default opened in binary mode.
  3. RustPython’s fileno is a property, while in CPython it is a method returning the underlying file descriptor.
  4. CPython doesn’t rely on the fileno() value for file interactions, instead it uses internal file handle while fileno() just returns it as a Python int, so one can modify it, and the file object will still be referring to the original file:
    >>> 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
    >>> 
    while in RustPython, changing the fileno property 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
    >>>>> 
  5. 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions