Skip to content

Bug: tojson() tries to read stdin twice, returns empty results #668

@yaniv-aknin

Description

@yaniv-aknin

What happened?

When using the petl executable, tojson() from stdin returns empty results -

$ petl 'dummytable().head(3).tocsv()' | petl 'fromcsv().tojson()'
[]
$

This is also true for a trivial program, without the executable (see code for repro.py below) -

$ ./repro.py < repro.csv
[]
$

What is the expected behavior?

I'd expect some data.

For example, tocsv() doesn't exhibit this problem -

$ petl 'dummytable().head(3).tocsv()' | petl 'fromcsv().tocsv()'          
foo,bar,baz
82,bananas,0.7873787427711181
3,oranges,0.13771232086689877
13,pears,0.24287642641761387
$

Reproducible test case

This is repro.py. Passing CSV data on stdin will emit an empty JSON array.

#!/usr/bin/env python3

import petl

petl.fromcsv().tojson()

What version of petl are you have found the bug?

v.1.7.15

Version

python 3.12

What OS are you seeing the problem on?

MacOS

What OS version are you using?

No response

What package manager you used to install?

Other

What's the current installed packages?

No response

Relevant log output

No response

Additional Notes

I wasn't sure how to fix it, but I'm pretty sure the bug is that sys.stdin is read twice (this line in tojson() invokes CSVView.__iter__ twice).

The first read depletes the lines from stdin and incorrectly discards the results. I'll try to investigate this further and report here if I do, but I also wanted other folks to be aware of the bug.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIt must work in all situations, but this failed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions