Skip to content

io module gets initialized multiple times when opening files #557

@silmeth

Description

@silmeth

When experimenting with file IO, I found out that when opening a file, one always initializes the io module from scratch (and then, when importing it, one initializes it exactly once, no matter how many times it’s been already initialized), making checking if objects are instances of given classes from that module impossible, as those classes are represented by different memory objects, eg.:

>>>>> file = open('/tmp/tst')
>>>>> file.buffer
<BufferedReader object at 0x55658e5c30e0>
>>>>> type(file.buffer)
<class 'BufferedReader'>
>>>>> import io
>>>>> io.BufferedReader
<class 'BufferedReader'>
>>>>> type(file.buffer) is io.BufferedReader
False
>>>>> type(file.buffer) == io.BufferedReader
False
>>>>> from io import BufferedReader  # subsequent imports from the module do not cause that
>>>>> BufferedReader is io.BufferedReader
True
>>>>> id(type(file.buffer))
93894668449408
>>>>> id(BufferedReader)
93894668474000

It seems this is the only module affected by this behaviour (all the other mk_module(&ctx) functions are afaik used only in the module_inits hash map used for import initialization).

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