ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* (no subject)
@ 2002-08-03 13:01 Florian Frank
  2002-08-03 23:45 ` reply for your message Yukihiro Matsumoto
  0 siblings, 1 reply; 5+ messages in thread
From: Florian Frank @ 2002-08-03 13:01 UTC (permalink / raw
  To: ruby-core

[-- Attachment #1: Type: text/plain, Size: 591 bytes --]

Hi all,

some methods of File::Stat return 0 if the underlying feature isn't
supported on the target platform. Wouldn't it be nice if they returned
nil? With nil it would be possible to use the idiomatic
	size = stat.blksize || 4096
to give a default value without first checking if blksize is 0.

This would avert the danger to use the 0 values and overlook the fact
that they don't make any sense on those platforms as well. I have
attached a patch to be applied if you consider this changes ok.

-- 
WAR IS PEACE FREEDOM IS SLAVERY IGNORANCE IS STRENGTH.
  -- George Orwell, "1984", 1948

[-- Attachment #2: file.diff --]
[-- Type: text/plain, Size: 1129 bytes --]

Index: file.c
===================================================================
RCS file: /src/ruby/file.c,v
retrieving revision 1.102
diff -u -p -u -r1.102 file.c
--- file.c	2002/06/11 01:27:46	1.102
+++ file.c	2002/08/03 00:31:13
@@ -214,7 +214,7 @@ rb_stat_rdev(self)
 #ifdef HAVE_ST_RDEV
     return ULONG2NUM(get_stat(self)->st_rdev);
 #else
-    return INT2FIX(0);
+    return Qnil;
 #endif
 }
 
@@ -226,7 +226,7 @@ rb_stat_rdev_major(self)
     long rdev = get_stat(self)->st_rdev;
     return ULONG2NUM(major(rdev));
 #else
-    return INT2FIX(0);
+    return Qnil;
 #endif
 }
 
@@ -238,7 +238,7 @@ rb_stat_rdev_minor(self)
     long rdev = get_stat(self)->st_rdev;
     return ULONG2NUM(minor(rdev));
 #else
-    return INT2FIX(0);
+    return Qnil;
 #endif
 }
 
@@ -256,7 +256,7 @@ rb_stat_blksize(self)
 #ifdef HAVE_ST_BLKSIZE
     return ULONG2NUM(get_stat(self)->st_blksize);
 #else
-    return INT2FIX(0);
+    return Qnil;
 #endif
 }
 
@@ -267,7 +267,7 @@ rb_stat_blocks(self)
 #ifdef HAVE_ST_BLOCKS
     return ULONG2NUM(get_stat(self)->st_blocks);
 #else
-    return INT2FIX(0);
+    return Qnil;
 #endif
 }
 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* reply for your message
  2002-08-03 13:01 Florian Frank
@ 2002-08-03 23:45 ` Yukihiro Matsumoto
  2002-08-07 10:08   ` File::Stat#blksize etc. should return nil instead of 0 Florian Frank
  2002-08-07 12:40   ` reply for your message Rich Kilmer
  0 siblings, 2 replies; 5+ messages in thread
From: Yukihiro Matsumoto @ 2002-08-03 23:45 UTC (permalink / raw
  To: ruby-core

Hi,

In message "your message"
    on 02/08/03, Florian Frank <flori@nixe.ping.de> writes:

|some methods of File::Stat return 0 if the underlying feature isn't
|supported on the target platform. Wouldn't it be nice if they returned
|nil? With nil it would be possible to use the idiomatic
|	size = stat.blksize || 4096
|to give a default value without first checking if blksize is 0.

Hmm, 

	size = stat.blksize.nonzero? || 4096

works well without any modify.  I'd like to hear from others,
especially those who working on the platforms without these members in
struct stat.

							matz.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* File::Stat#blksize etc. should return nil instead of 0
  2002-08-03 23:45 ` reply for your message Yukihiro Matsumoto
@ 2002-08-07 10:08   ` Florian Frank
  2002-08-07 10:26     ` Yukihiro Matsumoto
  2002-08-07 12:40   ` reply for your message Rich Kilmer
  1 sibling, 1 reply; 5+ messages in thread
From: Florian Frank @ 2002-08-07 10:08 UTC (permalink / raw
  To: ruby-core

On Sun, 2002-08-04 at 01:45, Yukihiro Matsumoto wrote:
> I'd like to hear from others,
> especially those who working on the platforms without these members in
> struct stat.

Ok, nobody seems to use those platforms (MS Windows, I guess) anyway. ;)

But I can try to explain more clearly where I see a problem. If somebody
writes some code like this

File.open(File.join("anyfile") do |f|
	until f.eof?
		s = f.read(f.stat.blksize)
		# do something
	end
end

it will work ok, if the platform supports blksize. If it doesn't this
will be an infinite loop because many reads of 0 bytes won't reach EOF.
With nil it would be ok by accident.

If f.read(8 * f.stat.blksize) is used, a type error will occur. But such
an error is probably better than a semantic error which causes the
program to get stuck in a loop.

-- 
Give me a man or a woman who has read a thousand books and you give me
an 
interesting companion. Give me a man or a woman who has read perhaps
three and 
you give me a dangerous enemy indeed.
  -- Anne Rice, "The witching hour"

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: File::Stat#blksize etc. should return nil instead of 0
  2002-08-07 10:08   ` File::Stat#blksize etc. should return nil instead of 0 Florian Frank
@ 2002-08-07 10:26     ` Yukihiro Matsumoto
  0 siblings, 0 replies; 5+ messages in thread
From: Yukihiro Matsumoto @ 2002-08-07 10:26 UTC (permalink / raw
  To: ruby-core

Hi,

In message "File::Stat#blksize etc. should return nil instead of 0"
    on 02/08/07, Florian Frank <flori@nixe.ping.de> writes:

|On Sun, 2002-08-04 at 01:45, Yukihiro Matsumoto wrote:
|> I'd like to hear from others,
|> especially those who working on the platforms without these members in
|> struct stat.
|
|Ok, nobody seems to use those platforms (MS Windows, I guess) anyway. ;)
|
|But I can try to explain more clearly where I see a problem. If somebody
|writes some code like this
|
|File.open(File.join("anyfile") do |f|
|	until f.eof?
|		s = f.read(f.stat.blksize)
|		# do something
|	end
|end
|
|it will work ok, if the platform supports blksize. If it doesn't this
|will be an infinite loop because many reads of 0 bytes won't reach EOF.
|With nil it would be ok by accident.
|
|If f.read(8 * f.stat.blksize) is used, a type error will occur. But such
|an error is probably better than a semantic error which causes the
|program to get stuck in a loop.

Sounds reasonable.  If no one stands against the idea, I will apply
your patch in the near future.

							matz.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: reply for your message
  2002-08-03 23:45 ` reply for your message Yukihiro Matsumoto
  2002-08-07 10:08   ` File::Stat#blksize etc. should return nil instead of 0 Florian Frank
@ 2002-08-07 12:40   ` Rich Kilmer
  1 sibling, 0 replies; 5+ messages in thread
From: Rich Kilmer @ 2002-08-07 12:40 UTC (permalink / raw
  To: ruby-core



> -----Original Message-----
> From: Yukihiro Matsumoto [mailto:matz@ruby-lang.org]
> Sent: Saturday, August 03, 2002 7:45 PM
> To: ruby-core@ruby-lang.org
> Subject: reply for your message
> 
> Hi,
> 
> In message "your message"
>     on 02/08/03, Florian Frank <flori@nixe.ping.de> writes:
> 
> |some methods of File::Stat return 0 if the underlying feature isn't
> |supported on the target platform. Wouldn't it be nice if they
returned
> |nil? With nil it would be possible to use the idiomatic
> |	size = stat.blksize || 4096
> |to give a default value without first checking if blksize is 0.
> 
> Hmm,
> 
> 	size = stat.blksize.nonzero? || 4096
> 
> works well without any modify.  I'd like to hear from others,
> especially those who working on the platforms without these members in
> struct stat.
> 

I would find this very useful.  I often write scripts that need to 
execute on Linux and Win32, and its this exact type of inconsistency 
that can be hard keep track of.

-Rich

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-08-07 12:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-08-03 13:01 Florian Frank
2002-08-03 23:45 ` reply for your message Yukihiro Matsumoto
2002-08-07 10:08   ` File::Stat#blksize etc. should return nil instead of 0 Florian Frank
2002-08-07 10:26     ` Yukihiro Matsumoto
2002-08-07 12:40   ` reply for your message Rich Kilmer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).