Skip to content

Commit 4fa0cde

Browse files
author
matz
committed
* numeric.c (num_step): better iteration condition for float
values; suggested by Masahiro TANAKA <masa@ir.isas.ac.jp>. * range.c (range_step): step (for Range#step method) <= 0 makes no sence, thus ArgError will be raised. * range.c (range_each): Range#each method is special case for Range#step(1) * file.c (rb_find_file): load must be done from an abolute path if $SAFE >= 4. * enum.c (enum_partition): new method. [new] * re.c (rb_reg_s_quote): quote whitespaces for /x cases. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2420 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent ca55fe4 commit 4fa0cde

File tree

7 files changed

+155
-104
lines changed

7 files changed

+155
-104
lines changed

ChangeLog

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1+
Tue Apr 30 09:23:05 2002 Yukihiro Matsumoto <matz@ruby-lang.org>
2+
3+
* numeric.c (num_step): better iteration condition for float
4+
values; suggested by Masahiro TANAKA <masa@ir.isas.ac.jp>.
5+
6+
Tue Apr 30 05:59:42 2002 Michal Rokos <m.rokos@sh.cvut.cz>
7+
8+
* range.c (range_step): step (for Range#step method) <= 0 makes no
9+
sence, thus ArgError will be raised.
10+
11+
* range.c (range_each): Range#each method is special case for
12+
Range#step(1)
13+
14+
Mon Apr 29 18:46:42 2002 Yukihiro Matsumoto <matz@ruby-lang.org>
15+
16+
* file.c (rb_find_file): load must be done from an abolute path if
17+
$SAFE >= 4.
18+
119
Sun Apr 28 17:01:56 2002 WATANABE Hirofumi <eban@ruby-lang.org>
220

321
* win32/win32.c (insert): fix prototype for ANSI C.
422

23+
Fri Apr 26 13:47:15 2002 Yukihiro Matsumoto <matz@ruby-lang.org>
24+
25+
* enum.c (enum_partition): new method. [new]
26+
27+
Fri Apr 26 13:41:00 2002 Yukihiro Matsumoto <matz@ruby-lang.org>
28+
29+
* re.c (rb_reg_s_quote): quote whitespaces for /x cases.
30+
531
Fri Apr 26 06:48:23 2002 Takaaki Tateishi <ttate@kt.jaist.ac.jp>
632

733
* ext/dl/ptr.c (cary2ary): missing break in switch statements.

enum.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,32 @@ enum_inject(argc, argv, obj)
216216
return n;
217217
}
218218

219+
static VALUE
220+
partition_i(i, ary)
221+
VALUE i, *ary;
222+
{
223+
if (RTEST(rb_yield(i))) {
224+
rb_ary_push(ary[0], i);
225+
}
226+
else {
227+
rb_ary_push(ary[1], i);
228+
}
229+
return Qnil;
230+
}
231+
232+
static VALUE
233+
enum_partition(obj)
234+
VALUE obj;
235+
{
236+
VALUE ary[2];
237+
238+
ary[0] = rb_ary_new();
239+
ary[1] = rb_ary_new();
240+
rb_iterate(rb_each, obj, partition_i, (VALUE)ary);
241+
242+
return rb_assoc_new(ary[0], ary[1]);
243+
}
244+
219245
static VALUE
220246
enum_sort(obj)
221247
VALUE obj;
@@ -475,6 +501,7 @@ Init_Enumerable()
475501
rb_define_method(rb_mEnumerable,"collect", enum_collect, 0);
476502
rb_define_method(rb_mEnumerable,"map", enum_collect, 0);
477503
rb_define_method(rb_mEnumerable,"inject", enum_inject, -1);
504+
rb_define_method(rb_mEnumerable,"partition", enum_partition, 0);
478505
rb_define_method(rb_mEnumerable,"all?", enum_all, 0);
479506
rb_define_method(rb_mEnumerable,"any?", enum_any, 0);
480507
rb_define_method(rb_mEnumerable,"min", enum_min, 0);

eval.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ rb_add_method(klass, mid, node, noex)
227227
NODE *body;
228228

229229
if (NIL_P(klass)) klass = rb_cObject;
230-
if (rb_safe_level() >= 4 && (klass == rb_cObject || !OBJ_TAINTED(klass))) {
230+
if (ruby_safe_level >= 4 && (klass == rb_cObject || !OBJ_TAINTED(klass))) {
231231
rb_raise(rb_eSecurityError, "Insecure: can't define method");
232232
}
233233
if (OBJ_FROZEN(klass)) rb_error_frozen("class/module");
@@ -310,7 +310,7 @@ remove_method(klass, mid)
310310
if (klass == rb_cObject) {
311311
rb_secure(4);
312312
}
313-
if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
313+
if (ruby_safe_level >= 4 && !OBJ_TAINTED(klass)) {
314314
rb_raise(rb_eSecurityError, "Insecure: can't remove method");
315315
}
316316
if (OBJ_FROZEN(klass)) rb_error_frozen("class/module");
@@ -1588,7 +1588,7 @@ rb_undef(klass, id)
15881588
if (ruby_class == rb_cObject) {
15891589
rb_secure(4);
15901590
}
1591-
if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
1591+
if (ruby_safe_level >= 4 && !OBJ_TAINTED(klass)) {
15921592
rb_raise(rb_eSecurityError, "Insecure: can't undef");
15931593
if (id == __id__ || id == __send__ || id == init) {
15941594
rb_name_error(id, "undefining `%s' prohibited", rb_id2name(id));
@@ -3166,7 +3166,7 @@ rb_eval(self, n)
31663166
VALUE klass;
31673167
NODE *body = 0, *defn;
31683168

3169-
if (rb_safe_level() >= 4 && !OBJ_TAINTED(recv)) {
3169+
if (ruby_safe_level >= 4 && !OBJ_TAINTED(recv)) {
31703170
rb_raise(rb_eSecurityError, "Insecure: can't define singleton method");
31713171
}
31723172
if (FIXNUM_P(recv) || SYMBOL_P(recv)) {
@@ -3179,7 +3179,7 @@ rb_eval(self, n)
31793179
if (OBJ_FROZEN(recv)) rb_error_frozen("object");
31803180
klass = rb_singleton_class(recv);
31813181
if (st_lookup(RCLASS(klass)->m_tbl, node->nd_mid, &body)) {
3182-
if (rb_safe_level() >= 4) {
3182+
if (ruby_safe_level >= 4) {
31833183
rb_raise(rb_eSecurityError, "redefining method prohibited");
31843184
}
31853185
if (RTEST(ruby_verbose)) {
@@ -3248,7 +3248,7 @@ rb_eval(self, n)
32483248
goto override_class;
32493249
}
32503250
}
3251-
if (rb_safe_level() >= 4) {
3251+
if (ruby_safe_level >= 4) {
32523252
rb_raise(rb_eSecurityError, "extending class prohibited");
32533253
}
32543254
}
@@ -3288,7 +3288,7 @@ rb_eval(self, n)
32883288
rb_raise(rb_eTypeError, "%s is not a module",
32893289
rb_id2name(node->nd_cname));
32903290
}
3291-
if (rb_safe_level() >= 4) {
3291+
if (ruby_safe_level >= 4) {
32923292
rb_raise(rb_eSecurityError, "extending module prohibited");
32933293
}
32943294
}
@@ -3315,7 +3315,7 @@ rb_eval(self, n)
33153315
rb_raise(rb_eTypeError, "no virtual class for %s",
33163316
rb_class2name(CLASS_OF(result)));
33173317
}
3318-
if (rb_safe_level() >= 4 && !OBJ_TAINTED(result))
3318+
if (ruby_safe_level >= 4 && !OBJ_TAINTED(result))
33193319
rb_raise(rb_eSecurityError, "Insecure: can't extend object");
33203320
klass = rb_singleton_class(result);
33213321

@@ -5603,7 +5603,7 @@ static void
56035603
secure_visibility(self)
56045604
VALUE self;
56055605
{
5606-
if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
5606+
if (ruby_safe_level >= 4 && !OBJ_TAINTED(self)) {
56075607
rb_raise(rb_eSecurityError, "Insecure: can't change method visibility");
56085608
}
56095609
}
@@ -6315,15 +6315,15 @@ proc_save_safe_level(data)
63156315
VALUE data;
63166316
{
63176317
if (OBJ_TAINTED(data)) {
6318-
switch (rb_safe_level()) {
6318+
switch (ruby_safe_level) {
63196319
case 3:
63206320
FL_SET(data, PROC_T3);
63216321
break;
63226322
case 4:
63236323
FL_SET(data, PROC_T4);
63246324
break;
63256325
default:
6326-
if (rb_safe_level() > 4) {
6326+
if (ruby_safe_level > 4) {
63276327
FL_SET(data, PROC_TMAX);
63286328
}
63296329
break;
@@ -6621,8 +6621,8 @@ block_pass(self, node)
66216621
block = b;
66226622
}
66236623

6624-
if (rb_safe_level() >= 1 && OBJ_TAINTED(block)) {
6625-
if (rb_safe_level() > proc_get_safe_level(block)) {
6624+
if (ruby_safe_level >= 1 && OBJ_TAINTED(block)) {
6625+
if (ruby_safe_level > proc_get_safe_level(block)) {
66266626
rb_raise(rb_eSecurityError, "Insecure: tainted block value");
66276627
}
66286628
}
@@ -8316,7 +8316,7 @@ rb_thread_safe_level(thread)
83168316

83178317
th = rb_thread_check(thread);
83188318
if (th == curr_thread) {
8319-
return INT2NUM(rb_safe_level());
8319+
return INT2NUM(ruby_safe_level);
83208320
}
83218321
return INT2NUM(th->safe);
83228322
}
@@ -8859,7 +8859,7 @@ rb_thread_local_aref(thread, id)
88598859
VALUE val;
88608860

88618861
th = rb_thread_check(thread);
8862-
if (rb_safe_level() >= 4 && th != curr_thread) {
8862+
if (ruby_safe_level >= 4 && th != curr_thread) {
88638863
rb_raise(rb_eSecurityError, "Insecure: thread locals");
88648864
}
88658865
if (!th->locals) return Qnil;
@@ -8884,7 +8884,7 @@ rb_thread_local_aset(thread, id, val)
88848884
{
88858885
rb_thread_t th = rb_thread_check(thread);
88868886

8887-
if (rb_safe_level() >= 4 && th != curr_thread) {
8887+
if (ruby_safe_level >= 4 && th != curr_thread) {
88888888
rb_raise(rb_eSecurityError, "Insecure: can't modify thread locals");
88898889
}
88908890
if (OBJ_FROZEN(thread)) rb_error_frozen("thread locals");

file.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,6 +2482,10 @@ rb_find_file(path)
24822482
if (file_load_ok(f)) return path;
24832483
}
24842484

2485+
if (rb_safe_level() >= 4) {
2486+
rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f);
2487+
}
2488+
24852489
if (rb_load_path) {
24862490
int i;
24872491

numeric.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ num_step(argc, argv, from)
788788
while (i <= end) {
789789
rb_yield(INT2FIX(i));
790790
i += diff;
791+
printf("<<%g>>\n", i - end);
791792
}
792793
}
793794
else {
@@ -798,21 +799,16 @@ num_step(argc, argv, from)
798799
}
799800
}
800801
else if (TYPE(from) == T_FLOAT || TYPE(to) == T_FLOAT || TYPE(step) == T_FLOAT) {
802+
const double epsilon = 2.2204460492503131E-16;
801803
double beg = NUM2DBL(from);
802804
double end = NUM2DBL(to);
803805
double unit = NUM2DBL(step);
804-
double n = beg;
805-
long i = 0;
806+
double n = (end - beg)/unit;
807+
long i;
806808

807-
if (unit > 0) {
808-
for (i=0; n<=end; i++, n=beg+unit*i) {
809-
rb_yield(rb_float_new(n));
810-
}
811-
}
812-
else {
813-
for (i=0; n>=end; i++, n=beg+unit*i) {
814-
rb_yield(rb_float_new(n));
815-
}
809+
n = floor(n + n*epsilon) + 1;
810+
for (i=0; i<n; i++) {
811+
rb_yield(rb_float_new(i*unit+beg));
816812
}
817813
}
818814
else {

0 commit comments

Comments
 (0)