Forums » Ruby-core » SCRIPT_LINES__ issue when loading a file more than once

SCRIPT_LINES__ issue when loading a file more than once
Posted by Mauricio Fernandez (Guest)
on 19.05.2006 11:47
SCRIPT_LINES__ is an obscure feature very few people care about, but I 
happen
to use it in the rcov code coverage tool to obtain the source code of 
the
program in execution[1], and ran into the following issue.

When a file ("the same" or with changing contents) is loaded more than 
once,
its contents get appended to SCRIPT_LINES__[filename] repeatedly:

$ cat tst.rb
SCRIPT_LINES__ = {}
4.times{ load "foo.rb" }
p SCRIPT_LINES__["./foo.rb"]
$ cat foo.rb
a = 1
$ ruby19 tst.rb
["a = 1\n", "a = 1\n", "a = 1\n", "a = 1\n"]
$ ruby tst.rb
["a = 1\n", "a = 1\n", "a = 1\n", "a = 1\n"]

I think it would make more sense to make it work in such way that the 
above
example returns either

(a)
[["a = 1\n"], ["a = 1\n"], ["a = 1\n"], ["a = 1\n"]]

that is, turning SCRIPT_LINES__[fname] into an array to which ruby 
appends
arrays holding the contents of the files with that name as it parses 
them, or

(b)
["a = 1\n"]

i.e. discarding all the previous data and keeping the contents from the 
last
parse.

I believe (a) could be implemented in HEAD; the ruby_1_8 branch could 
get (a)
or, if it's deemed too radical a change, (b).

Here's the patch for (a):


diff -p -u -r1.435 parse.y
--- parse.y     13 May 2006 08:55:39 -0000      1.435
+++ parse.y     19 May 2006 09:25:00 -0000
@@ -4556,15 +4556,19 @@ yycompile(VALUE vparser, const char *f,
     Data_Get_Struct(vparser, struct parser_params, parser);
     if (!compile_for_eval && rb_safe_level() == 0 &&
        rb_const_defined(rb_cObject, rb_intern("SCRIPT_LINES__"))) {
-       VALUE hash, fname;
+       VALUE hash, fname, arr;

        hash = rb_const_get(rb_cObject, rb_intern("SCRIPT_LINES__"));
        if (TYPE(hash) == T_HASH) {
            fname = rb_str_new2(f);
-           ruby_debug_lines = rb_hash_aref(hash, fname);
-           if (NIL_P(ruby_debug_lines)) {
+           arr = rb_hash_aref(hash, fname);
+           if (NIL_P(arr)) {
+               arr = rb_ary_new();
+               rb_hash_aset(hash, fname, arr);
+           }
+           if(TYPE(arr) == T_ARRAY) {
                ruby_debug_lines = rb_ary_new();
-               rb_hash_aset(hash, fname, ruby_debug_lines);
+               rb_ary_push(arr, ruby_debug_lines);
            }
        }
        if (line > 1) {


After applying:

$ ruby19 -v tst.rb
ruby 1.9.0 (2006-05-18) [i686-linux]
[["a = 1\n"], ["a = 1\n"], ["a = 1\n"], ["a = 1\n"]]


The patch for (b) is attached to this message.

After applying:

$ ruby -v tst.rb
ruby 1.8.4 (2006-05-18) [i686-linux]
["a = 1\n"]


[1] I need that to generate meaningful reports; it's AFAIK the most 
robust
way to get the source code in pure Ruby.
Re: SCRIPT_LINES__ issue when loading a file more than once
Posted by Mauricio Fernandez (Guest)
on 05.06.2006 22:28
On Fri, May 19, 2006 at 06:46:05PM +0900, Mauricio Fernandez wrote:
> ["a = 1\n", "a = 1\n", "a = 1\n", "a = 1\n"]
> $ ruby tst.rb 
> ["a = 1\n", "a = 1\n", "a = 1\n", "a = 1\n"]

Does it work that way by design (WONTFIX) or is it just that nobody 
cares
about SCRIPT_LINES__?
Re: SCRIPT_LINES__ issue when loading a file more than once
Posted by Yukihiro Matsumoto (Guest)
on 06.06.2006 00:47
Hi,

In message "Re: [PATCH] SCRIPT_LINES__ issue when loading a file more 
than once"
    on Tue, 6 Jun 2006 05:26:52 +0900, Mauricio Fernandez <mfp@acm.org> 
writes:

|> When a file ("the same" or with changing contents) is loaded more than once,
|> its contents get appended to SCRIPT_LINES__[filename] repeatedly:

|Does it work that way by design (WONTFIX) or is it just that nobody cares
|about SCRIPT_LINES__?

I care.  But I couldn't have time to investigate the patch.  Could you
wait for a week or so?

							matz.
Re: SCRIPT_LINES__ issue when loading a file more than once
Posted by Mauricio Fernandez (Guest)
on 06.06.2006 01:24
On Tue, Jun 06, 2006 at 07:45:52AM +0900, Yukihiro Matsumoto wrote:
> wait for a week or so?
Of course, there's no urgency here and there are indeed more important 
things.
Sorry for the crude speech.


Thank you,
Re: SCRIPT_LINES__ issue when loading a file more than once
Posted by unknown (Guest)
on 06.06.2006 11:47
Hi,

At Fri, 19 May 2006 18:46:05 +0900,
Mauricio Fernandez wrote in [ruby-core:07909]:
> parse.
> 
> I believe (a) could be implemented in HEAD; the ruby_1_8 branch could get (a)
> or, if it's deemed too radical a change, (b).

I vote to (b) for HEAD too.
Re: SCRIPT_LINES__ issue when loading a file more than once
Posted by Yukihiro Matsumoto (Guest)
on 06.06.2006 11:53
Hi,

In message "Re: [PATCH] SCRIPT_LINES__ issue when loading a file more 
than once"
    on Tue, 6 Jun 2006 18:45:54 +0900, nobu@ruby-lang.org writes:

|> I believe (a) could be implemented in HEAD; the ruby_1_8 branch could get (a)
|> or, if it's deemed too radical a change, (b).
|
|I vote to (b) for HEAD too.

Do you?  But redundant lines in SCRIPT_LINES__ are not useful at all,
and I believe no one uses this (mis)feature even on 1.8.

							matz.
Re: SCRIPT_LINES__ issue when loading a file more than once
Posted by unknown (Guest)
on 06.06.2006 15:07
Hi,

At Tue, 6 Jun 2006 18:50:22 +0900,
Yukihiro Matsumoto wrote in [ruby-core:07953]:
> |> I believe (a) could be implemented in HEAD; the ruby_1_8 branch could get (a)
> |> or, if it's deemed too radical a change, (b).
> |
> |I vote to (b) for HEAD too.
> 
> Do you?  But redundant lines in SCRIPT_LINES__ are not useful at all,
> and I believe no one uses this (mis)feature even on 1.8.

I agree that they are unuseful, and (b) overwrites previous
data by the last code, doesn't it?
Re: SCRIPT_LINES__ issue when loading a file more than once
Posted by Yukihiro Matsumoto (Guest)
on 06.06.2006 15:35
Hi,

In message "Re: [PATCH] SCRIPT_LINES__ issue when loading a file more 
than once"
    on Tue, 6 Jun 2006 22:05:44 +0900, nobu@ruby-lang.org writes:

|> |I vote to (b) for HEAD too.
|> 
|> Do you?  But redundant lines in SCRIPT_LINES__ are not useful at all,
|> and I believe no one uses this (mis)feature even on 1.8.
|
|I agree that they are unuseful, and (b) overwrites previous
|data by the last code, doesn't it?

I guess so.  Can it be a problem?

							matz.
Re: SCRIPT_LINES__ issue when loading a file more than once
Posted by unknown (Guest)
on 06.06.2006 15:51
Hi,

At Tue, 6 Jun 2006 22:33:10 +0900,
Yukihiro Matsumoto wrote in [ruby-core:07957]:
> |> |I vote to (b) for HEAD too.
> |> 
> |> Do you?  But redundant lines in SCRIPT_LINES__ are not useful at all,
> |> and I believe no one uses this (mis)feature even on 1.8.
> |
> |I agree that they are unuseful, and (b) overwrites previous
> |data by the last code, doesn't it?
> 
> I guess so.  Can it be a problem?

I don't guess it would cause a problem.  By analogy with an
ordinary script file, it seems unclear that the previous data
are still there.