Project

General

Profile

Backport #8662

__dir__ not working in eval with binding and filename

Added by srawlins (Sam Rawlins) almost 6 years ago. Updated almost 6 years ago.

Status:
Closed
Priority:
Normal
[ruby-core:56099]

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

Associated revisions

Revision c7a56bd6
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

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].

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42103 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 42103
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

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].

Revision 42103
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

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].

Revision 42103
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

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].

Revision 42103
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

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].

Revision 42103
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

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].

Revision 42103
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

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].

Revision 65dc7d00
Added by nagachika (Tomoyuki Chikanaga) almost 6 years 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 [ruby-core:56099] [Bug #8662].

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@42116 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 42116
Added by nagachika (Tomoyuki Chikanaga) almost 6 years 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 [ruby-core:56099] [Bug #8662].

Revision 55a9667d
Added by sorah (Sorah Fukumori) about 5 years ago

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45360 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 45360
Added by sorah (Sorah Fukumori) about 5 years ago

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

Revision 45360
Added by sorah (Sorah Fukumori) about 5 years ago

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

Revision 45360
Added by sorah (Sorah Fukumori) about 5 years ago

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

Revision 45360
Added by sorah (Sorah Fukumori) about 5 years ago

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

Revision 45360
Added by sorah (Sorah Fukumori) about 5 years ago

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

Revision 45360
Added by sorah (Sorah Fukumori) about 5 years ago

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

Revision d0be5c2d
Added by nagachika (Tomoyuki Chikanaga) about 5 years 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 [ruby-core:56099] [Bug #8662] and r42103.

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@45474 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 45474
Added by nagachika (Tomoyuki Chikanaga) about 5 years 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 [ruby-core:56099] [Bug #8662] and r42103.

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

Revision 3cbfd634
Added by nagachika (Tomoyuki Chikanaga) about 5 years 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 [ruby-core:56099] [Bug #8662] and r42103.

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@45927 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 45927
Added by nagachika (Tomoyuki Chikanaga) about 5 years 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 [ruby-core:56099] [Bug #8662] and r42103.

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

History

Updated by nobu (Nobuyoshi Nakada) almost 6 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) almost 6 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().

#3

Updated by nobu (Nobuyoshi Nakada) almost 6 years ago

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

Updated by nobu (Nobuyoshi Nakada) almost 6 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].
#5

Updated by nagachika (Tomoyuki Chikanaga) almost 6 years ago

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

Updated by nagachika (Tomoyuki Chikanaga) almost 6 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].

Also available in: Atom PDF