Skip to content

jsonstreams.Stream should not close file descriptors when leaving context if we instantiated jsonstreams.Stream with one #23

@JonoYang

Description

@JonoYang

If I instantiate jsonstreams.Stream with a file descriptor that I made or am using from somewhere else, i.e.

fd = open('somefile.json', 'w')
with jsonstreams.Stream(jsonstreams.Type.object, fd=output_file) as s:
...

the file descriptor fd is closed when I leave the jsonstreams.Stream context (https://github.com/dcbaker/jsonstreams/blob/master/jsonstreams/__init__.py#L568). I'd expect the file descriptor fd to be open when I leave the context, so I can manage fd myself.

I ran into this problem using a file descriptor managed by click (https://click.palletsprojects.com/en/7.x/api/?highlight=file#click.File), where jsonstreams.Stream closed the descriptor before click was finished with it.

I had to do a hack, where I finish the jsonstreams.Stream object by calling _Stream__inst.Close() and leaving the file descriptor open so click can deal with it (https://github.com/nexB/scancode-toolkit/blob/2303-stream-json-output/src/formattedcode/output_json.py#L178)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions