adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.01k stars 1.19k forks source link

FileIO.name property #9195

Open FoamyGuy opened 4 months ago

FoamyGuy commented 4 months ago

I'm wondering how hard it would be to add support for the name property to FileIO (or wherever is appropriate for use with the built-in open())

Currently In CPython:

Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("something.txt", "wb")
>>> f.name
'something.txt'

Currently In CircuitPython:

Adafruit CircuitPython 9.1.0-beta.1-2-g87a400aa1d on 2024-04-21; Adafruit Feather ESP32-S3 TFT with ESP32S3
>>> f = open("boot_out.txt", "rb")
>>> f.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'TextIOWrapper' object has no attribute 'name'

I noticed this difference between CPython and CircuitPython while working on a files argument for requests.post() In CPython that argument supports a type of value that would rely on retrieving the name of an already opened file. I was unable to find a way to do that with circuitpython, but if some way does already exist that could be used for my purpose and it wouldn't be strictly necessary to add support for name, but perhaps still nice to match cpython more closely.

If there is interest in this functionality, and if adding support for this wouldn't be super challenging I'd be willing to give it a try. But I would probably need a point in the right direction within the core code for where to start looking to add it.

tannewt commented 4 months ago

I think you'd need to add a property for it here: https://github.com/adafruit/circuitpython/blob/5836f1145f61e411840e2d53e817b4aaa705f3ee/extmod/vfs_fat_file.c#L146

The property getter would read internal state for it.