@@ -745,7 +745,7 @@ make_writeconv(rb_io_t *fptr)
745
745
/* writing functions */
746
746
747
747
static long
748
- io_binwrite (VALUE str , rb_io_t * fptr )
748
+ io_binwrite (VALUE str , rb_io_t * fptr , int nosync )
749
749
{
750
750
long len , n , r , l , offset = 0 ;
751
751
@@ -757,7 +757,7 @@ io_binwrite(VALUE str, rb_io_t *fptr)
757
757
fptr -> wbuf_capa = 8192 ;
758
758
fptr -> wbuf = ALLOC_N (char , fptr -> wbuf_capa );
759
759
}
760
- if ((fptr -> mode & (FMODE_SYNC |FMODE_TTY )) ||
760
+ if ((! nosync && ( fptr -> mode & (FMODE_SYNC |FMODE_TTY ) )) ||
761
761
(fptr -> wbuf && fptr -> wbuf_capa <= fptr -> wbuf_len + len )) {
762
762
/* xxx: use writev to avoid double write if available */
763
763
if (fptr -> wbuf_len && fptr -> wbuf_len + len <= fptr -> wbuf_capa ) {
@@ -811,8 +811,8 @@ io_binwrite(VALUE str, rb_io_t *fptr)
811
811
return len ;
812
812
}
813
813
814
- static long
815
- io_fwrite (VALUE str , rb_io_t * fptr )
814
+ static VALUE
815
+ do_writeconv (VALUE str , rb_io_t * fptr )
816
816
{
817
817
if (NEED_WRITECONV (fptr )) {
818
818
VALUE common_encoding = Qnil ;
@@ -842,8 +842,14 @@ io_fwrite(VALUE str, rb_io_t *fptr)
842
842
str = rb_econv_str_convert (fptr -> writeconv , str , ECONV_PARTIAL_INPUT );
843
843
}
844
844
}
845
+ return str ;
846
+ }
845
847
846
- return io_binwrite (str , fptr );
848
+ static long
849
+ io_fwrite (VALUE str , rb_io_t * fptr , int nosync )
850
+ {
851
+ str = do_writeconv (str , fptr );
852
+ return io_binwrite (str , fptr , nosync );
847
853
}
848
854
849
855
long
@@ -855,29 +861,11 @@ rb_io_fwrite(const char *ptr, long len, FILE *f)
855
861
of .stdio_file = f ;
856
862
of .mode = FMODE_WRITABLE ;
857
863
of .pathv = Qnil ;
858
- return io_fwrite (rb_str_new (ptr , len ), & of );
864
+ return io_fwrite (rb_str_new (ptr , len ), & of , 0 );
859
865
}
860
866
861
- /*
862
- * call-seq:
863
- * ios.write(string) => integer
864
- *
865
- * Writes the given string to <em>ios</em>. The stream must be opened
866
- * for writing. If the argument is not a string, it will be converted
867
- * to a string using <code>to_s</code>. Returns the number of bytes
868
- * written.
869
- *
870
- * count = $stdout.write( "This is a test\n" )
871
- * puts "That was #{count} bytes of data"
872
- *
873
- * <em>produces:</em>
874
- *
875
- * This is a test
876
- * That was 15 bytes of data
877
- */
878
-
879
867
static VALUE
880
- io_write (VALUE io , VALUE str )
868
+ io_write (VALUE io , VALUE str , int nosync )
881
869
{
882
870
rb_io_t * fptr ;
883
871
long n ;
@@ -897,12 +885,36 @@ io_write(VALUE io, VALUE str)
897
885
GetOpenFile (io , fptr );
898
886
rb_io_check_writable (fptr );
899
887
900
- n = io_fwrite (str , fptr );
888
+ n = io_fwrite (str , fptr , nosync );
901
889
if (n == -1L ) rb_sys_fail_path (fptr -> pathv );
902
890
903
891
return LONG2FIX (n );
904
892
}
905
893
894
+ /*
895
+ * call-seq:
896
+ * ios.write(string) => integer
897
+ *
898
+ * Writes the given string to <em>ios</em>. The stream must be opened
899
+ * for writing. If the argument is not a string, it will be converted
900
+ * to a string using <code>to_s</code>. Returns the number of bytes
901
+ * written.
902
+ *
903
+ * count = $stdout.write( "This is a test\n" )
904
+ * puts "That was #{count} bytes of data"
905
+ *
906
+ * <em>produces:</em>
907
+ *
908
+ * This is a test
909
+ * That was 15 bytes of data
910
+ */
911
+
912
+ static VALUE
913
+ io_write_m (VALUE io , VALUE str )
914
+ {
915
+ return io_write (io , str , 0 );
916
+ }
917
+
906
918
VALUE
907
919
rb_io_write (VALUE io , VALUE str )
908
920
{
@@ -5417,8 +5429,15 @@ void
5417
5429
rb_p (VALUE obj ) /* for debug print within C code */
5418
5430
{
5419
5431
VALUE str = rb_obj_as_string (rb_inspect (obj ));
5420
- rb_str_buf_append (str , rb_default_rs );
5421
- rb_io_write (rb_stdout , str );
5432
+ if (TYPE (rb_stdout ) == T_FILE &&
5433
+ rb_method_basic_definition_p (CLASS_OF (rb_stdout ), id_write )) {
5434
+ io_write (rb_stdout , str , 1 );
5435
+ io_write (rb_stdout , rb_default_rs , 0 );
5436
+ }
5437
+ else {
5438
+ rb_io_write (rb_stdout , str );
5439
+ rb_io_write (rb_stdout , rb_default_rs );
5440
+ }
5422
5441
}
5423
5442
5424
5443
/*
@@ -7445,7 +7464,7 @@ copy_stream_body(VALUE arg)
7445
7464
rb_str_resize (str ,len );
7446
7465
read_buffered_data (RSTRING_PTR (str ), len , src_fptr );
7447
7466
if (dst_fptr ) /* IO or filename */
7448
- io_fwrite (str , dst_fptr );
7467
+ io_fwrite (str , dst_fptr , 0 );
7449
7468
else /* others such as StringIO */
7450
7469
rb_io_write (stp -> dst , str );
7451
7470
stp -> total += len ;
@@ -8273,7 +8292,7 @@ Init_IO(void)
8273
8292
rb_define_method (rb_cIO , "write_nonblock" , rb_io_write_nonblock , 1 );
8274
8293
rb_define_method (rb_cIO , "readpartial" , io_readpartial , -1 );
8275
8294
rb_define_method (rb_cIO , "read" , io_read , -1 );
8276
- rb_define_method (rb_cIO , "write" , io_write , 1 );
8295
+ rb_define_method (rb_cIO , "write" , io_write_m , 1 );
8277
8296
rb_define_method (rb_cIO , "gets" , rb_io_gets_m , -1 );
8278
8297
rb_define_method (rb_cIO , "readline" , rb_io_readline , -1 );
8279
8298
rb_define_method (rb_cIO , "getc" , rb_io_getc , 0 );
0 commit comments