Backport #8662

__dir__ not working in eval with binding and filename

Added by Sam Rawlins 9 months ago. Updated 9 months ago.

[ruby-core:56099]
Status:Closed
Priority:Normal
Assignee:Tomoyuki Chikanaga

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 evalstringwith_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

simple.patch Magnifier - simple patch, but goes 5 if-statements deep (1.35 KB) Sam Rawlins, 07/21/2013 03:08 PM

cleaner.patch Magnifier - cleaner patch, with a touch of refactoring (1.66 KB) Sam Rawlins, 07/21/2013 03:08 PM

cleaner-v2.patch Magnifier - an even cleaner cleaner.patch (1.63 KB) Sam Rawlins, 07/22/2013 05:53 AM

Associated revisions

Revision 42116
Added by Tomoyuki Chikanaga 9 months ago

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  [Bug #8662].

Revision 45360
Added by Shota Fukumori about 1 month ago

  • vmeval.c (evalstringwithcref): Use file path even if scope is given. Related to [Bug #8662] and r42103.

Revision 45474
Added by Tomoyuki Chikanaga 21 days ago

merge revision(s) r45360,r45361: [Backport #9651]

* vm_eval.c (eval_string_with_cref): Use file path even if scope is
  given. Related to  [Bug #8662] and r42103.

* vm_eval.c (eval_string_with_cref): Unify to use NIL_P.

History

#1 Updated by Nobuyoshi Nakada 9 months 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 CheckTypedData(), not rbobjiskind_of().

#2 Updated by Sam Rawlins 9 months 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 CheckTypedData(), not rbobjiskind_of().

I don't see a CheckTypedData(), do you mean rbcheck_typeddata()?

The line would be rbchecktypeddata(scope, &bindingdatatype), but bindingdatatype 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().

#3 Updated by Nobuyoshi Nakada 9 months ago

  • Tracker changed from Backport to Bug
  • Project changed from Backport200 to ruby-trunk

#4 Updated by Nobuyoshi Nakada 9 months 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

  • vmeval.c (evalstringwithcref): use the given file name unless eval even if scope is given. additional fix for [Bug #8436]. based on the patch by srawlins at [Bug #8662].

#5 Updated by Tomoyuki Chikanaga 9 months ago

  • Tracker changed from Bug to Backport
  • Project changed from ruby-trunk to Backport200
  • Status changed from Closed to Assigned
  • Assignee set to Tomoyuki Chikanaga

#6 Updated by Tomoyuki Chikanaga 9 months 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  [Bug #8662].

Also available in: Atom PDF