File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 \r RECORDSTREAM\n " )
685+
686+
649687if __name__ == "__main__" :
650688 __import__ ("standalone_test" ).main (globals ())
You can’t perform that action at this time.
0 commit comments