Project

General

Profile

Actions

Backport #8662

closed

__dir__ not working in eval with binding and filename

Added by srawlins (Sam Rawlins) over 11 years ago. Updated over 11 years ago.


Description

It seems that #8436 is not yet fixed, even in 2.0.0-p247. When a filename is passed into eval as the third argument, dir still shows up as nil:

$ cat t.rb
puts "without file => #{eval("__dir__", TOPLEVEL_BINDING).inspect}"
puts "with file    => #{eval("__dir__", TOPLEVEL_BINDING, "test/builder/line.ru").inspect}"
$ ruby t.rb                                                                                            
without file => "/usr/local/google/home/srawlins/code/rack__rack"
with file    => nil

In the second case, dir should still be the directory name of FILE, not nil. I've attached a file, simple.patch, that is a pretty simple fix.

However, simple.patch makes the eval_string_with_cref() function 5 if-statements deep though (it was previously 4). So I have another patch that is cleaner: cleaner.patch. It adds some refactoring.

Both patch files add a test. Both patch files are written against 2.0.0-p247.

A note: there was a lot of discussion about dir and absolute paths on #3346, and this patch allows dir to return something that is not an absolute path, because it just returns the dirname of the filename passed in. This is what people will expect. Here is ruby 2.0.0-p247:

$ ruby -e 'eval("puts __FILE__", binding, "not-a-file")'
not-a-file
$ ruby -e 'eval("puts File.dirname(__FILE__)", binding, "not-a-file")'
.
$ ruby -e 'eval("puts File.dirname(__FILE__)", binding, "foo/not-a-file")'
foo

Files

simple.patch (1.35 KB) simple.patch simple patch, but goes 5 if-statements deep srawlins (Sam Rawlins), 07/21/2013 03:08 PM
cleaner.patch (1.66 KB) cleaner.patch cleaner patch, with a touch of refactoring srawlins (Sam Rawlins), 07/21/2013 03:08 PM
cleaner-v2.patch (1.63 KB) cleaner-v2.patch an even cleaner cleaner.patch srawlins (Sam Rawlins), 07/22/2013 05:53 AM

Updated by nobu (Nobuyoshi Nakada) over 11 years ago

In cleaner.patch, the second strcmp() is unnecessary, as it returns 0 always.
And splitting the condition is applicable in simple.patch too.

It might be better to check by Check_TypedData(), not rb_obj_is_kind_of().

Updated by srawlins (Sam Rawlins) over 11 years ago

nobu (Nobuyoshi Nakada) wrote:

In cleaner.patch, the second strcmp() is unnecessary, as it returns 0 always.
And splitting the condition is applicable in simple.patch too.

It might be better to check by Check_TypedData(), not rb_obj_is_kind_of().

I don't see a Check_TypedData(), do you mean rb_check_typeddata()?

The line would be rb_check_typeddata(scope, &binding_data_type), but binding_data_type is defined in proc.c, so I don't think this is possible, without more refactoring :(

So, I've fixed cleaner.patch in cleaner-v2.patch, just removing the second strcmp().

Actions #3

Updated by nobu (Nobuyoshi Nakada) over 11 years ago

  • Tracker changed from Backport to Bug
  • Project changed from Backport200 to Ruby master
Actions #4

Updated by nobu (Nobuyoshi Nakada) over 11 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r42103.
Sam, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


vm_eval.c: use file argument

  • vm_eval.c (eval_string_with_cref): use the given file name unless
    eval even if scope is given. additional fix for [Bug #8436].
    based on the patch by srawlins at [ruby-core:56099] [Bug #8662].
Actions #5

Updated by nagachika (Tomoyuki Chikanaga) over 11 years ago

  • Tracker changed from Bug to Backport
  • Project changed from Ruby master to Backport200
  • Status changed from Closed to Assigned
  • Assignee set to nagachika (Tomoyuki Chikanaga)
Actions #6

Updated by nagachika (Tomoyuki Chikanaga) over 11 years ago

  • Status changed from Assigned to Closed

This issue was solved with changeset r42116.
Sam, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


merge revision(s) 42103: [Backport #8662]

* vm_eval.c (eval_string_with_cref): use the given file name unless
  eval even if scope is given.  additional fix for [Bug #8436].
  based on the patch by srawlins at [ruby-core:56099] [Bug #8662].
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0