Permalink
Browse files

Stop calling to_path of String

* file.c (rb_get_path_check_to_string): stop calling to_path method of String.
  We remove unnecessary argument.

* file.c (rb_get_path_check): follow the above change.

* internal.h (rb_get_path_check_to_string): ditto.

* load.c (rb_construct_expanded_load_path): not check to have to_path method
  since to_path is not called for String.

* load.c (rb_get_expanded_load_path): fix comment.

* test/ruby/test_require.rb (TestRequire): remove invalid test.
  • Loading branch information...
1 parent 480fcd4 commit bc7652840d6ad03db4fb713c4142e1464a695e0c @shirosaki committed Nov 2, 2012
Showing with 13 additions and 37 deletions.
  1. +5 −6 file.c
  2. +1 −1 internal.h
  3. +7 −7 load.c
  4. +0 −23 test/ruby/test_require.rb
View
11 file.c
@@ -168,7 +168,7 @@ check_path_encoding(VALUE str)
}
VALUE
-rb_get_path_check_to_string(VALUE obj, int level, int *has_to_path)
+rb_get_path_check_to_string(VALUE obj, int level)
{
VALUE tmp;
ID to_path;
@@ -177,14 +177,14 @@ rb_get_path_check_to_string(VALUE obj, int level, int *has_to_path)
rb_insecure_operation();
}
+ if (RB_TYPE_P(obj, T_STRING)) {
+ return obj;
+ }
CONST_ID(to_path, "to_path");
tmp = rb_check_funcall(obj, to_path, 0, 0);
if (tmp == Qundef) {
tmp = obj;
}
- else {
- *has_to_path = 1;
- }
StringValue(tmp);
return tmp;
}
@@ -206,8 +206,7 @@ rb_get_path_check_convert(VALUE obj, VALUE tmp, int level)
static VALUE
rb_get_path_check(VALUE obj, int level)
{
- int has_to_path = 0;
- VALUE tmp = rb_get_path_check_to_string(obj, level, &has_to_path);
+ VALUE tmp = rb_get_path_check_to_string(obj, level);
return rb_get_path_check_convert(obj, tmp, level);
}
View
2 internal.h
@@ -108,7 +108,7 @@ void rb_file_const(const char*, VALUE);
int rb_file_load_ok(const char *);
VALUE rb_file_expand_path_fast(VALUE, VALUE);
VALUE rb_file_expand_path_internal(VALUE, VALUE, int, int, VALUE);
-VALUE rb_get_path_check_to_string(VALUE, int, int *);
+VALUE rb_get_path_check_to_string(VALUE, int);
VALUE rb_get_path_check_convert(VALUE, VALUE, int);
void Init_File(void);
View
14 load.c
@@ -42,8 +42,8 @@ enum expand_type {
/* Construct expanded load path and store it to cache.
We rebuild load path partially if the cache is invalid.
- We don't cache non string object and object which has to_path method and
- expand it every times. We ensure $LOAD_PATH string objects are frozen.
+ We don't cache non string object and expand it every times. We ensure that
+ string objects in $LOAD_PATH are frozen.
*/
static void
rb_construct_expanded_load_path(int type, int *has_relative, int *has_non_cache)
@@ -58,12 +58,12 @@ rb_construct_expanded_load_path(int type, int *has_relative, int *has_non_cache)
ary = rb_ary_new2(RARRAY_LEN(load_path));
for (i = 0; i < RARRAY_LEN(load_path); ++i) {
VALUE path, as_str, expanded_path;
- int is_string, non_cache, has_to_path = 0;
+ int is_string, non_cache;
char *as_cstr;
as_str = path = RARRAY_PTR(load_path)[i];
- is_string = (TYPE(path) == T_STRING) ? 1 : 0;
- as_str = rb_get_path_check_to_string(path, level, &has_to_path);
- non_cache = (!is_string || has_to_path) ? 1 : 0;
+ is_string = RB_TYPE_P(path, T_STRING) ? 1 : 0;
+ non_cache = !is_string ? 1 : 0;
+ as_str = rb_get_path_check_to_string(path, level);
as_cstr = RSTRING_PTR(as_str);
if (!non_cache) {
@@ -117,7 +117,7 @@ rb_get_expanded_load_path(void)
vm->load_path_check_cache = load_path_getcwd();
}
else if (has_non_cache) {
- /* Non string object and object which has to_path method. */
+ /* Non string object. */
vm->load_path_check_cache = non_cache;
}
else {
View
23 test/ruby/test_require.rb
@@ -517,29 +517,6 @@ def a.to_path
}
end
- def test_require_string_to_path_in_load_path
- Dir.mktmpdir {|tmp|
- Dir.chdir(tmp) {
- open("foo.rb", "w") {}
- assert_in_out_err([], <<-INPUT, %w(:ok), [])
- a = "foo"
- ENV['FOO'] = "bar"
- def a.to_path
- ENV['FOO']
- end
- $: << a
- begin
- require "foo"
- p :ng
- rescue LoadError
- end
- ENV['FOO'] = "#{tmp}"
- p :ok if require "foo"
- INPUT
- }
- }
- end
-
def test_require_to_str_redefined_in_load_path
Dir.mktmpdir {|tmp|
Dir.chdir(tmp) {

0 comments on commit bc76528

Please sign in to comment.