Created
October 3, 2011 02:09
-
-
Save luislavena/1258289 to your computer and use it in GitHub Desktop.
Ruby: playing with require and load
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/file.c b/file.c | |
index 3107895..8bb0839 100644 | |
--- a/file.c | |
+++ b/file.c | |
@@ -5117,7 +5117,7 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level) | |
if (!ext[0]) return 0; | |
if (f[0] == '~') { | |
- fname = file_expand_path_1(fname); | |
+ fname = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, fname); | |
if (safe_level >= 1 && OBJ_TAINTED(fname)) { | |
rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); | |
} | |
@@ -5130,7 +5130,7 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level) | |
if (safe_level >= 1 && !fpath_check(fname)) { | |
rb_raise(rb_eSecurityError, "loading from unsafe path %s", f); | |
} | |
- if (!expanded) fname = file_expand_path_1(fname); | |
+ if (!expanded) fname = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, fname); | |
fnlen = RSTRING_LEN(fname); | |
for (i=0; ext[i]; i++) { | |
rb_str_cat2(fname, ext[i]); | |
@@ -5151,7 +5151,6 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level) | |
if (!load_path) return 0; | |
fname = rb_str_dup(*filep); | |
- RBASIC(fname)->klass = 0; | |
fnlen = RSTRING_LEN(fname); | |
tmp = rb_str_tmp_new(MAXPATHLEN + 2); | |
for (j=0; ext[j]; j++) { | |
@@ -5161,14 +5160,14 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level) | |
RB_GC_GUARD(str) = rb_get_path_check(str, safe_level); | |
if (RSTRING_LEN(str) == 0) continue; | |
- file_expand_path(fname, str, 0, tmp); | |
+ tmp = rb_funcall(rb_cFile, rb_intern("expand_path"), 2, fname, str); | |
if (file_load_ok(RSTRING_PTR(tmp))) { | |
*filep = copy_path_class(tmp, *filep); | |
return (int)(j+1); | |
} | |
FL_UNSET(tmp, FL_TAINT | FL_UNTRUSTED); | |
} | |
- rb_str_set_len(fname, fnlen); | |
+ rb_str_resize(fname, fnlen); | |
} | |
RB_GC_GUARD(load_path); | |
return 0; | |
@@ -5188,7 +5187,7 @@ rb_find_file_safe(VALUE path, int safe_level) | |
int expanded = 0; | |
if (f[0] == '~') { | |
- tmp = file_expand_path_1(path); | |
+ tmp = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, path); | |
if (safe_level >= 1 && OBJ_TAINTED(tmp)) { | |
rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); | |
} | |
@@ -5203,7 +5202,7 @@ rb_find_file_safe(VALUE path, int safe_level) | |
} | |
if (!file_load_ok(f)) return 0; | |
if (!expanded) | |
- path = copy_path_class(file_expand_path_1(path), path); | |
+ path = copy_path_class(rb_funcall(rb_cFile, rb_intern("expand_path"), 1, path), path); | |
return path; | |
} | |
@@ -5220,7 +5219,7 @@ rb_find_file_safe(VALUE path, int safe_level) | |
VALUE str = RARRAY_PTR(load_path)[i]; | |
RB_GC_GUARD(str) = rb_get_path_check(str, safe_level); | |
if (RSTRING_LEN(str) > 0) { | |
- file_expand_path(path, str, 0, tmp); | |
+ tmp = rb_funcall(rb_cFile, rb_intern("expand_path"), 2, path, str); | |
f = RSTRING_PTR(tmp); | |
if (file_load_ok(f)) goto found; | |
} | |
diff --git a/load.c b/load.c | |
index 9ed386d..3bd0037 100644 | |
--- a/load.c | |
+++ b/load.c | |
@@ -43,7 +43,7 @@ rb_get_expanded_load_path(void) | |
ary = rb_ary_new2(RARRAY_LEN(load_path)); | |
for (i = 0; i < RARRAY_LEN(load_path); ++i) { | |
- VALUE path = rb_file_expand_path(RARRAY_PTR(load_path)[i], Qnil); | |
+ VALUE path = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, RARRAY_PTR(load_path)[i]); | |
rb_str_freeze(path); | |
rb_ary_push(ary, path); | |
} | |
@@ -233,7 +233,7 @@ rb_feature_provided(const char *feature, const char **loading) | |
if (*feature == '.' && | |
(feature[1] == '/' || strncmp(feature+1, "./", 2) == 0)) { | |
- fullpath = rb_file_expand_path(rb_str_new2(feature), Qnil); | |
+ fullpath = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, rb_str_new2(feature)); | |
feature = RSTRING_PTR(fullpath); | |
} | |
if (ext && !strchr(ext, '/')) { | |
@@ -305,7 +305,7 @@ rb_load_internal(VALUE fname, int wrap) | |
th->mild_compile_error++; | |
node = (NODE *)rb_load_file(RSTRING_PTR(fname)); | |
loaded = TRUE; | |
- iseq = rb_iseq_new_top(node, rb_str_new2("<top (required)>"), fname, rb_realpath_internal(Qnil, fname, 1), Qfalse); | |
+ iseq = rb_iseq_new_top(node, rb_str_new2("<top (required)>"), fname, rb_funcall(rb_cFile, rb_intern("realpath"), 1, fname), Qfalse); | |
th->mild_compile_error--; | |
rb_iseq_eval(iseq); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
V:\a\b\c\d\e>set RUBYOPT | |
RUBYOPT=-rV:/ruby-perf/replace | |
V:\a\b\c\d\e>dir /B | |
foo.rb | |
V:\a\b\c\d\e>ruby --disable-gems -I. -e "require 'foo'; File.results" | |
$LOAD_PATH: 9 | |
$LOADED_FEATURES: 9 | |
expanded: 82 | |
absolute: 0 | |
realpath: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$expanded = [] | |
$absolute = [] | |
$realpath = [] | |
class File < IO | |
class << self | |
alias_method :orig_expand_path, :expand_path | |
def expand_path(file_name, dir_string = nil) | |
$expanded << "expand_path: #{file_name.inspect}, #{dir_string.inspect}" | |
orig_expand_path file_name, dir_string | |
end | |
alias_method :orig_absolute_path, :absolute_path | |
def absolute_path(file_name, dir_string = nil) | |
$absolute << "absolute_path: #{file_name.inspect}, #{dir_string.inspect}" | |
orig_absolute_path(file_name, dir_string) | |
end | |
alias_method :orig_realpath, :realpath | |
def realpath(pathname, dir_string = nil) | |
$realpath << "realpath: #{pathname.inspect}, #{dir_string.inspect}" | |
orig_realpath pathname, dir_string | |
end | |
def results | |
puts "$LOAD_PATH: #{$LOAD_PATH.size}" | |
puts "$LOADED_FEATURES: #{$LOADED_FEATURES.size}" | |
puts "expanded: #{$expanded.size}" | |
puts "absolute: #{$absolute.size}" | |
puts "realpath: #{$realpath.size}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another interesting detail:
Usage of
-I
in the command line results in a expanded path before it is added to$LOAD_PATH
, not so when usingunshift
(btw, you shouldn't)