Skip to content

Commit 9cad89f

Browse files
authored
Fix RecordReader not reading from stdin by default (#94)
Calling `RecordReader()` without arguments should always default to stdin
1 parent 6144cf4 commit 9cad89f

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

flow/record/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ def RecordAdapter(
801801
if sub_adapter:
802802
cls_url = sub_adapter + "://" + cls_url
803803
if out is False:
804-
if url in ("-", ""):
804+
if url in ("-", "", None) and fileobj is None:
805805
# For reading stdin, we cannot rely on an extension to know what sort of stream is incoming. Thus, we will
806806
# treat it as a 'fileobj', where we can peek into the stream and try to select the appropriate adapter.
807807
fileobj = getattr(sys.stdin, "buffer", sys.stdin)

tests/test_regression.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,5 +646,43 @@ def test_fieldtype_typedlist_net_ipaddress():
646646
assert issubclass(fieldtype("net.ipaddress[]"), fieldtypes.FieldType)
647647

648648

649+
def test_record_reader_default_stdin(tmp_path):
650+
"""RecordWriter should default to stdin if no path is given"""
651+
TestRecord = RecordDescriptor(
652+
"test/record",
653+
[
654+
("string", "text"),
655+
],
656+
)
657+
658+
# write some records
659+
records_path = tmp_path / "test.records"
660+
with RecordWriter(records_path) as writer:
661+
writer.write(TestRecord("foo"))
662+
663+
# Test stdin
664+
with patch("sys.stdin", BytesIO(records_path.read_bytes())):
665+
with RecordReader() as reader:
666+
for record in reader:
667+
assert record.text == "foo"
668+
669+
670+
def test_record_writer_default_stdout(capsysbinary):
671+
"""RecordWriter should default to stdout if no path is given"""
672+
TestRecord = RecordDescriptor(
673+
"test/record",
674+
[
675+
("string", "text"),
676+
],
677+
)
678+
679+
# write a record to stdout
680+
with RecordWriter() as writer:
681+
writer.write(TestRecord("foo"))
682+
683+
stdout = capsysbinary.readouterr().out
684+
assert stdout.startswith(b"\x00\x00\x00\x0f\xc4\rRECORDSTREAM\n")
685+
686+
649687
if __name__ == "__main__":
650688
__import__("standalone_test").main(globals())

0 commit comments

Comments
 (0)