1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/dbm/dbm.c (fdbm_select): 1.7 behavior.

* ext/gdbm/gdbm.c (fgdbm_select): ditto.

* ext/sdbm/sdbm.c (fsdbm_select): ditto.

* ext/dbm/dbm.c (fdbm_delete): adopt Hash#delete behavior.

* ext/sdbm/sdbm.c (fsdbm_delete): ditto.

* ext/gdbm/gdbm.c: need not to dup key to the block.

* ext/sdbm/sdbm.c : replace RuntimeError with SDBMError.

* eval.c (rb_f_missing): NoMethod error messages for true, false,
  nil must respond visibility like for other objects.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2145 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2002-02-27 04:52:21 +00:00
parent 2fcd221fec
commit 3fc04d9361
9 changed files with 398 additions and 194 deletions

View file

@ -15,10 +15,31 @@ Wed Feb 27 07:05:17 2002 Akinori MUSHA <knu@iDaemons.org>
KAME PR: 393
FreeBSD PR: kern/34242
Wed Feb 27 03:36:47 2002 Koji Arai <jca02266@nifty.ne.jp>
* ext/dbm/dbm.c (fdbm_select): 1.7 behavior.
* ext/gdbm/gdbm.c (fgdbm_select): ditto.
* ext/sdbm/sdbm.c (fsdbm_select): ditto.
* ext/dbm/dbm.c (fdbm_delete): adopt Hash#delete behavior.
* ext/sdbm/sdbm.c (fsdbm_delete): ditto.
* ext/gdbm/gdbm.c: need not to dup key to the block.
* ext/sdbm/sdbm.c : replace RuntimeError with SDBMError.
Tue Feb 26 21:34:07 2002 Usaku Nakamura <usa@ruby-lang.org>
* bignum.c (rb_big_2comp): void function cannot return any value.
Tue Feb 26 16:52:12 2002 Yukihiro Matsumoto <matz@ruby-lang.org>
* eval.c (rb_f_missing): NoMethod error messages for true, false,
nil must respond visibility like for other objects.
Tue Feb 26 15:41:30 2002 Yukihiro Matsumoto <matz@ruby-lang.org>
* eval.c (rb_eval): call trace_func for if/while conditions.

47
eval.c
View file

@ -4232,13 +4232,13 @@ rb_f_missing(argc, argv, obj)
switch (TYPE(obj)) {
case T_NIL:
format = "undefined method `%s' for nil";
desc = "nil";
break;
case T_TRUE:
format = "undefined method `%s' for true";
desc = "true";
break;
case T_FALSE:
format = "undefined method `%s' for false";
desc = "false";
break;
case T_OBJECT:
d = rb_any_to_s(obj);
@ -4248,40 +4248,41 @@ rb_f_missing(argc, argv, obj)
break;
}
if (d) {
if (last_call_status & CSTAT_PRIV) {
format = "private method `%s' called for %s%s%s";
}
if (last_call_status & CSTAT_PROT) {
format = "protected method `%s' called for %s%s%s";
}
else if (last_call_status & CSTAT_VCALL) {
const char *mname = rb_id2name(id);
if (('a' <= mname[0] && mname[0] <= 'z') || mname[0] == '_') {
format = "undefined local variable or method `%s' for %s%s%s";
exc = rb_eNameError;
}
}
if (!format) {
format = "undefined method `%s' for %s%s%s";
}
if (RSTRING(d)->len > 65) {
d = rb_any_to_s(obj);
}
desc = RSTRING(d)->ptr;
}
if (last_call_status & CSTAT_PRIV) {
format = "private method `%s' called for %s%s%s";
}
if (last_call_status & CSTAT_PROT) {
format = "protected method `%s' called for %s%s%s";
}
else if (last_call_status & CSTAT_VCALL) {
const char *mname = rb_id2name(id);
if (('a' <= mname[0] && mname[0] <= 'z') || mname[0] == '_') {
format = "undefined local variable or method `%s' for %s%s%s";
exc = rb_eNameError;
}
}
if (!format) {
format = "undefined method `%s' for %s%s%s";
}
ruby_sourcefile = file;
ruby_sourceline = line;
PUSH_FRAME(); /* fake frame */
*ruby_frame = *_frame.prev->prev;
{
char buf[BUFSIZ];
int noclass = (!d || desc[0]=='#');
snprintf(buf, BUFSIZ, format, rb_id2name(id),
desc, desc[0]=='#' ? "" : ":",
desc[0]=='#' ? "" : rb_class2name(CLASS_OF(obj)));
desc, noclass ? "" : ":",
noclass ? "" : rb_class2name(CLASS_OF(obj)));
exc = rb_exc_new2(exc, buf);
rb_iv_set(exc, "name", argv[0]);
rb_iv_set(exc, "args", rb_ary_new4(argc-1, argv+1));

View file

@ -32,7 +32,7 @@ struct dbmdata {
static void
closed_dbm()
{
rb_raise(rb_eRuntimeError, "closed DBM file");
rb_raise(rb_eDBMError, "closed DBM file");
}
#define GetDBM(obj, dbmp) {\
@ -51,7 +51,18 @@ free_dbm(dbmp)
}
}
static VALUE fdbm_close _((VALUE));
static VALUE
fdbm_close(obj)
VALUE obj;
{
struct dbmdata *dbmp;
GetDBM(obj, dbmp);
dbm_close(dbmp->di_dbm);
dbmp->di_dbm = 0;
return Qnil;
}
static VALUE
fdbm_s_new(argc, argv, klass)
@ -129,19 +140,6 @@ fdbm_s_open(argc, argv, klass)
return obj;
}
static VALUE
fdbm_close(obj)
VALUE obj;
{
struct dbmdata *dbmp;
GetDBM(obj, dbmp);
dbm_close(dbmp->di_dbm);
dbmp->di_dbm = 0;
return Qnil;
}
static VALUE
fdbm_fetch(obj, keystr, ifnone)
VALUE obj, keystr, ifnone;
@ -229,6 +227,45 @@ fdbm_indexes(argc, argv, obj)
return new;
}
static VALUE
fdbm_select(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE new = rb_ary_new2(argc);
int i;
if (rb_block_given_p()) {
datum key, val;
DBM *dbm;
struct dbmdata *dbmp;
VALUE keystr, valstr;
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
}
GetDBM(obj, dbmp);
dbm = dbmp->di_dbm;
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
VALUE assoc;
val = dbm_fetch(dbm, key);
assoc = rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
rb_tainted_str_new(val.dptr, val.dsize));
if (RTEST(rb_yield(assoc)))
rb_ary_push(new, assoc);
}
}
else {
for (i=0; i<argc; i++) {
rb_ary_push(new, fdbm_fetch(obj, argv[i]));
}
}
return new;
}
static VALUE
fdbm_delete(obj, keystr)
VALUE obj, keystr;
@ -236,6 +273,7 @@ fdbm_delete(obj, keystr)
datum key, value;
struct dbmdata *dbmp;
DBM *dbm;
VALUE valstr;
rb_secure(4);
StringValue(keystr);
@ -247,10 +285,13 @@ fdbm_delete(obj, keystr)
value = dbm_fetch(dbm, key);
if (value.dptr == 0) {
if (rb_block_given_p()) rb_yield(keystr);
if (rb_block_given_p()) return rb_yield(keystr);
return Qnil;
}
/* need to save value before dbm_delete() */
valstr = rb_tainted_str_new(value.dptr, value.dsize);
if (dbm_delete(dbm, key)) {
dbmp->di_size = -1;
rb_raise(rb_eDBMError, "dbm_delete failed");
@ -258,7 +299,7 @@ fdbm_delete(obj, keystr)
else if (dbmp->di_size >= 0) {
dbmp->di_size--;
}
return obj;
return valstr;
}
static VALUE
@ -691,6 +732,7 @@ Init_dbm()
rb_define_method(rb_cDBM, "index", fdbm_index, 1);
rb_define_method(rb_cDBM, "indexes", fdbm_indexes, -1);
rb_define_method(rb_cDBM, "indices", fdbm_indexes, -1);
rb_define_method(rb_cDBM, "select", fdbm_select, -1);
rb_define_method(rb_cDBM, "length", fdbm_length, 0);
rb_define_method(rb_cDBM, "size", fdbm_length, 0);
rb_define_method(rb_cDBM, "empty?", fdbm_empty_p, 0);

View file

@ -165,7 +165,7 @@ class TestDBM < RUNIT::TestCase
assert_nil(dbm.close)
# closed DBM file
assert_exception(RuntimeError) { dbm.close }
assert_exception(DBMError) { dbm.close }
end
def test_aref
@ -245,6 +245,25 @@ class TestDBM < RUNIT::TestCase
assert_equals(values.reverse, @dbm.indexes(*keys.reverse))
end
def test_select
keys = %w(foo bar baz)
values = %w(FOO BAR BAZ)
@dbm[keys[0]], @dbm[keys[1]], @dbm[keys[2]] = values
assert_equals(values.reverse, @dbm.select(*keys.reverse))
end
def test_select_with_block
keys = %w(foo bar baz)
values = %w(FOO BAR BAZ)
@dbm[keys[0]], @dbm[keys[1]], @dbm[keys[2]] = values
ret = @dbm.select {|k,v|
assert_equals(k.upcase, v)
k != "bar"
}
assert_equals([['baz', 'BAZ'], ['foo', 'FOO']],
ret.sort)
end
def test_length
num = 10
assert_equals(0, @dbm.size)
@ -377,7 +396,7 @@ class TestDBM < RUNIT::TestCase
@dbm[keys[0]], @dbm[keys[1]], @dbm[keys[2]] = values
assert_equals(@dbm, @dbm.delete(key))
assert_equals('BAR', @dbm.delete(key))
assert_nil(@dbm[key])
assert_equals(2, @dbm.size)
@ -394,12 +413,13 @@ class TestDBM < RUNIT::TestCase
def test_delete_with_block
key = 'no called block'
@dbm[key] = 'foo'
assert_equals(@dbm, @dbm.delete(key) {|k| k.replace 'called block'})
assert_equals('foo', @dbm.delete(key) {|k| k.replace 'called block'})
assert_equals('no called block', key)
assert_equals(0, @dbm.size)
key = 'no called block'
assert_nil(@dbm.delete(key) {|k| k.replace 'called block'})
assert_equals(:blockval,
@dbm.delete(key) {|k| k.replace 'called block'; :blockval})
assert_equals('called block', key)
assert_equals(0, @dbm.size)
end

View file

@ -14,11 +14,11 @@
#include <fcntl.h>
#include <errno.h>
static VALUE cGDBM, rb_eGDBMError, rb_eGDBMFatalError;
static VALUE rb_cGDBM, rb_eGDBMError, rb_eGDBMFatalError;
#define MY_BLOCK_SIZE (2048)
#define MY_FATAL_FUNC rb_gdbm_fatal
void
static void
rb_gdbm_fatal(msg)
char *msg;
{
@ -52,7 +52,18 @@ free_dbm(dbmp)
}
}
static VALUE fgdbm_close _((VALUE));
static VALUE
fgdbm_close(obj)
VALUE obj;
{
struct dbmdata *dbmp;
GetDBM(obj, dbmp);
gdbm_close(dbmp->di_dbm);
dbmp->di_dbm = 0;
return Qnil;
}
static VALUE
fgdbm_s_new(argc, argv, klass)
@ -140,19 +151,6 @@ fgdbm_s_open(argc, argv, klass)
return obj;
}
static VALUE
fgdbm_close(obj)
VALUE obj;
{
struct dbmdata *dbmp;
GetDBM(obj, dbmp);
gdbm_close(dbmp->di_dbm);
dbmp->di_dbm = 0;
return Qnil;
}
static VALUE
rb_gdbm_fetch(dbm, key)
GDBM_FILE dbm;
@ -219,7 +217,7 @@ rb_gdbm_firstkey(dbm)
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
OBJ_TAINT(str);
return (VALUE)str;
return str;
}
static VALUE
@ -243,7 +241,7 @@ rb_gdbm_nextkey(dbm, keystr)
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
OBJ_TAINT(str);
return (VALUE)str;
return str;
}
static VALUE
@ -326,6 +324,43 @@ fgdbm_indexes(argc, argv, obj)
return new;
}
static VALUE
fgdbm_select(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE new = rb_ary_new2(argc);
int i;
if (rb_block_given_p()) {
GDBM_FILE dbm;
struct dbmdata *dbmp;
VALUE keystr;
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
}
GetDBM(obj, dbmp);
dbm = dbmp->di_dbm;
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
VALUE assoc = rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr));
if (RTEST(rb_yield(assoc)))
rb_ary_push(new, assoc);
}
}
else {
for (i=0; i<argc; i++) {
rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i]));
}
}
return new;
}
static VALUE
rb_gdbm_delete(obj, keystr)
VALUE obj, keystr;
@ -407,7 +442,7 @@ fgdbm_delete_if(obj)
keystr = rb_gdbm_nextkey(dbm, keystr)) {
valstr = rb_gdbm_fetch2(dbm, keystr);
ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
ret = rb_protect(rb_yield, rb_assoc_new(keystr, valstr), &status);
if (status != 0) break;
if (RTEST(ret)) rb_ary_push(ary, keystr);
}
@ -625,7 +660,7 @@ fgdbm_each_key(obj)
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_yield(rb_str_dup(keystr));
rb_yield(keystr);
}
return obj;
}
@ -644,8 +679,7 @@ fgdbm_each_pair(obj)
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_yield(rb_assoc_new(rb_str_dup(keystr),
rb_gdbm_fetch2(dbm, keystr)));
rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
}
return obj;
@ -755,8 +789,7 @@ fgdbm_to_a(obj)
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_ary_push(ary, rb_assoc_new(rb_str_dup(keystr),
rb_gdbm_fetch2(dbm, keystr)));
rb_ary_push(ary, rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
}
return ary;
@ -886,75 +919,76 @@ fgdbm_reject(obj)
void
Init_gdbm()
{
cGDBM = rb_define_class("GDBM", rb_cObject);
rb_cGDBM = rb_define_class("GDBM", rb_cObject);
rb_eGDBMError = rb_define_class("GDBMError", rb_eStandardError);
rb_eGDBMFatalError = rb_define_class("GDBMFatalError", rb_eException);
rb_include_module(cGDBM, rb_mEnumerable);
rb_include_module(rb_cGDBM, rb_mEnumerable);
rb_define_singleton_method(cGDBM, "new", fgdbm_s_new, -1);
rb_define_singleton_method(cGDBM, "open", fgdbm_s_open, -1);
rb_define_singleton_method(rb_cGDBM, "new", fgdbm_s_new, -1);
rb_define_singleton_method(rb_cGDBM, "open", fgdbm_s_open, -1);
rb_define_method(cGDBM, "initialize", fgdbm_initialize, -1);
rb_define_method(cGDBM, "close", fgdbm_close, 0);
rb_define_method(cGDBM, "[]", fgdbm_aref, 1);
rb_define_method(cGDBM, "fetch", fgdbm_fetch_m, -1);
rb_define_method(cGDBM, "[]=", fgdbm_store, 2);
rb_define_method(cGDBM, "store", fgdbm_store, 2);
rb_define_method(cGDBM, "index", fgdbm_index, 1);
rb_define_method(cGDBM, "indexes", fgdbm_indexes, -1);
rb_define_method(cGDBM, "indices", fgdbm_indexes, -1);
rb_define_method(cGDBM, "length", fgdbm_length, 0);
rb_define_alias(cGDBM, "size", "length");
rb_define_method(cGDBM, "empty?", fgdbm_empty_p, 0);
rb_define_method(cGDBM, "each", fgdbm_each_pair, 0);
rb_define_method(cGDBM, "each_value", fgdbm_each_value, 0);
rb_define_method(cGDBM, "each_key", fgdbm_each_key, 0);
rb_define_method(cGDBM, "each_pair", fgdbm_each_pair, 0);
rb_define_method(cGDBM, "keys", fgdbm_keys, 0);
rb_define_method(cGDBM, "values", fgdbm_values, 0);
rb_define_method(cGDBM, "shift", fgdbm_shift, 0);
rb_define_method(cGDBM, "delete", fgdbm_delete, 1);
rb_define_method(cGDBM, "delete_if", fgdbm_delete_if, 0);
rb_define_method(cGDBM, "reject!", fgdbm_delete_if, 0);
rb_define_method(cGDBM, "reject", fgdbm_reject, 0);
rb_define_method(cGDBM, "clear", fgdbm_clear, 0);
rb_define_method(cGDBM,"invert", fgdbm_invert, 0);
rb_define_method(cGDBM,"update", fgdbm_update, 1);
rb_define_method(cGDBM,"replace", fgdbm_replace, 1);
rb_define_method(cGDBM,"reorganize", fgdbm_reorganize, 0);
rb_define_method(cGDBM,"sync", fgdbm_sync, 0);
/* rb_define_method(cGDBM,"setopt", fgdbm_setopt, 2); */
rb_define_method(cGDBM,"cachesize=", fgdbm_set_cachesize, 1);
rb_define_method(cGDBM,"fastmode=", fgdbm_set_fastmode, 1);
rb_define_method(cGDBM,"syncmode=", fgdbm_set_syncmode, 1);
rb_define_method(rb_cGDBM, "initialize", fgdbm_initialize, -1);
rb_define_method(rb_cGDBM, "close", fgdbm_close, 0);
rb_define_method(rb_cGDBM, "[]", fgdbm_aref, 1);
rb_define_method(rb_cGDBM, "fetch", fgdbm_fetch_m, -1);
rb_define_method(rb_cGDBM, "[]=", fgdbm_store, 2);
rb_define_method(rb_cGDBM, "store", fgdbm_store, 2);
rb_define_method(rb_cGDBM, "index", fgdbm_index, 1);
rb_define_method(rb_cGDBM, "indexes", fgdbm_indexes, -1);
rb_define_method(rb_cGDBM, "indices", fgdbm_indexes, -1);
rb_define_method(rb_cGDBM, "select", fgdbm_select, -1);
rb_define_method(rb_cGDBM, "length", fgdbm_length, 0);
rb_define_method(rb_cGDBM, "size", fgdbm_length, 0);
rb_define_method(rb_cGDBM, "empty?", fgdbm_empty_p, 0);
rb_define_method(rb_cGDBM, "each", fgdbm_each_pair, 0);
rb_define_method(rb_cGDBM, "each_value", fgdbm_each_value, 0);
rb_define_method(rb_cGDBM, "each_key", fgdbm_each_key, 0);
rb_define_method(rb_cGDBM, "each_pair", fgdbm_each_pair, 0);
rb_define_method(rb_cGDBM, "keys", fgdbm_keys, 0);
rb_define_method(rb_cGDBM, "values", fgdbm_values, 0);
rb_define_method(rb_cGDBM, "shift", fgdbm_shift, 0);
rb_define_method(rb_cGDBM, "delete", fgdbm_delete, 1);
rb_define_method(rb_cGDBM, "delete_if", fgdbm_delete_if, 0);
rb_define_method(rb_cGDBM, "reject!", fgdbm_delete_if, 0);
rb_define_method(rb_cGDBM, "reject", fgdbm_reject, 0);
rb_define_method(rb_cGDBM, "clear", fgdbm_clear, 0);
rb_define_method(rb_cGDBM,"invert", fgdbm_invert, 0);
rb_define_method(rb_cGDBM,"update", fgdbm_update, 1);
rb_define_method(rb_cGDBM,"replace", fgdbm_replace, 1);
rb_define_method(rb_cGDBM,"reorganize", fgdbm_reorganize, 0);
rb_define_method(rb_cGDBM,"sync", fgdbm_sync, 0);
/* rb_define_method(rb_cGDBM,"setopt", fgdbm_setopt, 2); */
rb_define_method(rb_cGDBM,"cachesize=", fgdbm_set_cachesize, 1);
rb_define_method(rb_cGDBM,"fastmode=", fgdbm_set_fastmode, 1);
rb_define_method(rb_cGDBM,"syncmode=", fgdbm_set_syncmode, 1);
rb_define_method(cGDBM, "include?", fgdbm_has_key, 1);
rb_define_method(cGDBM, "has_key?", fgdbm_has_key, 1);
rb_define_method(cGDBM, "member?", fgdbm_has_key, 1);
rb_define_method(cGDBM, "has_value?", fgdbm_has_value, 1);
rb_define_method(cGDBM, "key?", fgdbm_has_key, 1);
rb_define_method(cGDBM, "value?", fgdbm_has_value, 1);
rb_define_method(rb_cGDBM, "include?", fgdbm_has_key, 1);
rb_define_method(rb_cGDBM, "has_key?", fgdbm_has_key, 1);
rb_define_method(rb_cGDBM, "member?", fgdbm_has_key, 1);
rb_define_method(rb_cGDBM, "has_value?", fgdbm_has_value, 1);
rb_define_method(rb_cGDBM, "key?", fgdbm_has_key, 1);
rb_define_method(rb_cGDBM, "value?", fgdbm_has_value, 1);
rb_define_method(cGDBM, "to_a", fgdbm_to_a, 0);
rb_define_method(cGDBM, "to_hash", fgdbm_to_hash, 0);
rb_define_method(rb_cGDBM, "to_a", fgdbm_to_a, 0);
rb_define_method(rb_cGDBM, "to_hash", fgdbm_to_hash, 0);
/* flags for gdbm_opn() */
/*
rb_define_const(cGDBM, "READER", INT2FIX(GDBM_READER));
rb_define_const(cGDBM, "WRITER", INT2FIX(GDBM_WRITER));
rb_define_const(cGDBM, "WRCREAT", INT2FIX(GDBM_WRCREAT));
rb_define_const(cGDBM, "NEWDB", INT2FIX(GDBM_NEWDB));
rb_define_const(rb_cGDBM, "READER", INT2FIX(GDBM_READER));
rb_define_const(rb_cGDBM, "WRITER", INT2FIX(GDBM_WRITER));
rb_define_const(rb_cGDBM, "WRCREAT", INT2FIX(GDBM_WRCREAT));
rb_define_const(rb_cGDBM, "NEWDB", INT2FIX(GDBM_NEWDB));
*/
rb_define_const(cGDBM, "FAST", INT2FIX(GDBM_FAST));
rb_define_const(rb_cGDBM, "FAST", INT2FIX(GDBM_FAST));
/* this flag is obsolete in gdbm 1.8.
On gdbm 1.8, fast mode is default behavior. */
/* gdbm version 1.8 specific */
#if defined(GDBM_SYNC)
rb_define_const(cGDBM, "SYNC", INT2FIX(GDBM_SYNC));
rb_define_const(rb_cGDBM, "SYNC", INT2FIX(GDBM_SYNC));
#endif
#if defined(GDBM_NOLOCK)
rb_define_const(cGDBM, "NOLOCK", INT2FIX(GDBM_NOLOCK));
rb_define_const(rb_cGDBM, "NOLOCK", INT2FIX(GDBM_NOLOCK));
#endif
rb_define_const(cGDBM, "VERSION", rb_str_new2(gdbm_version));
rb_define_const(rb_cGDBM, "VERSION", rb_str_new2(gdbm_version));
}

View file

@ -279,6 +279,25 @@ class TestGDBM < RUNIT::TestCase
assert_equals(values.reverse, @gdbm.indexes(*keys.reverse))
end
def test_select
keys = %w(foo bar baz)
values = %w(FOO BAR BAZ)
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
assert_equals(values.reverse, @gdbm.select(*keys.reverse))
end
def test_select_with_block
keys = %w(foo bar baz)
values = %w(FOO BAR BAZ)
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
ret = @gdbm.select {|k,v|
assert_equals(k.upcase, v)
k != "bar"
}
assert_equals([['baz', 'BAZ'], ['foo', 'FOO']],
ret.sort)
end
def test_length
num = 10
assert_equals(0, @gdbm.size)
@ -411,7 +430,7 @@ class TestGDBM < RUNIT::TestCase
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
assert_equals(@gdbm, @gdbm.delete(key))
assert_equals('BAR', @gdbm.delete(key))
assert_nil(@gdbm[key])
assert_equals(2, @gdbm.size)
@ -428,12 +447,13 @@ class TestGDBM < RUNIT::TestCase
def test_delete_with_block
key = 'no called block'
@gdbm[key] = 'foo'
assert_equals(@gdbm, @gdbm.delete(key) {|k| k.replace 'called block'})
assert_equals('foo', @gdbm.delete(key) {|k| k.replace 'called block'})
assert_equals('no called block', key)
assert_equals(0, @gdbm.size)
key = 'no called block'
assert_nil(@gdbm.delete(key) {|k| k.replace 'called block'})
assert_equals(:blockval,
@gdbm.delete(key) {|k| k.replace 'called block'; :blockval})
assert_equals('called block', key)
assert_equals(0, @gdbm.size)
end

View file

@ -16,7 +16,7 @@
#include <fcntl.h>
#include <errno.h>
static VALUE cSDBM;
static VALUE rb_cDBM, rb_eDBMError;
struct dbmdata {
int di_size;
@ -26,7 +26,7 @@ struct dbmdata {
static void
closed_sdbm()
{
rb_raise(rb_eRuntimeError, "closed SDBM file");
rb_raise(rb_eDBMError, "closed SDBM file");
}
#define GetDBM(obj, dbmp) {\
@ -56,6 +56,17 @@ fsdbm_close(obj)
return Qnil;
}
static VALUE
fsdbm_s_new(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
VALUE obj = Data_Wrap_Struct(klass, 0, free_sdbm, 0);
rb_obj_call_init(obj, argc, argv);
return obj;
}
static VALUE
fsdbm_initialize(argc, argv, obj)
int argc;
@ -99,17 +110,6 @@ fsdbm_initialize(argc, argv, obj)
return obj;
}
static VALUE
fsdbm_s_new(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
VALUE obj = Data_Wrap_Struct(klass, 0, free_sdbm, 0);
rb_obj_call_init(obj, argc, argv);
return obj;
}
static VALUE
fsdbm_s_open(argc, argv, klass)
int argc;
@ -215,6 +215,45 @@ fsdbm_indexes(argc, argv, obj)
return new;
}
static VALUE
fsdbm_select(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE new = rb_ary_new2(argc);
int i;
if (rb_block_given_p()) {
datum key, val;
DBM *dbm;
struct dbmdata *dbmp;
VALUE keystr, valstr;
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
}
GetDBM(obj, dbmp);
dbm = dbmp->di_dbm;
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
VALUE assoc;
val = sdbm_fetch(dbm, key);
assoc = rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
rb_tainted_str_new(val.dptr, val.dsize));
if (RTEST(rb_yield(assoc)))
rb_ary_push(new, assoc);
}
}
else {
for (i=0; i<argc; i++) {
rb_ary_push(new, fsdbm_fetch(obj, argv[i]));
}
}
return new;
}
static VALUE
fsdbm_delete(obj, keystr)
VALUE obj, keystr;
@ -222,6 +261,7 @@ fsdbm_delete(obj, keystr)
datum key, value;
struct dbmdata *dbmp;
DBM *dbm;
VALUE valstr;
rb_secure(4);
StringValue(keystr);
@ -234,18 +274,21 @@ fsdbm_delete(obj, keystr)
value = sdbm_fetch(dbm, key);
if (value.dptr == 0) {
if (rb_block_given_p()) rb_yield(keystr);
if (rb_block_given_p()) return rb_yield(keystr);
return Qnil;
}
/* need to save value before sdbm_delete() */
valstr = rb_tainted_str_new(value.dptr, value.dsize);
if (sdbm_delete(dbm, key)) {
dbmp->di_size = -1;
rb_raise(rb_eRuntimeError, "dbm_delete failed");
rb_raise(rb_eDBMError, "dbm_delete failed");
}
else if (dbmp->di_size >= 0) {
dbmp->di_size--;
}
return obj;
return valstr;
}
static VALUE
@ -304,7 +347,7 @@ fsdbm_delete_if(obj)
key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len;
if (sdbm_delete(dbm, key)) {
rb_raise(rb_eRuntimeError, "sdbm_delete failed");
rb_raise(rb_eDBMError, "sdbm_delete failed");
}
}
if (status) rb_jump_tag(status);
@ -327,7 +370,7 @@ fsdbm_clear(obj)
dbmp->di_size = -1;
while (key = sdbm_firstkey(dbm), key.dptr) {
if (sdbm_delete(dbm, key)) {
rb_raise(rb_eRuntimeError, "sdbm_delete failed");
rb_raise(rb_eDBMError, "sdbm_delete failed");
}
}
dbmp->di_size = 0;
@ -381,7 +424,7 @@ static VALUE
fsdbm_update(obj, other)
VALUE obj, other;
{
rb_iterate((VALUE(*)_((VALUE)))each_pair, other, update_i, obj);
rb_iterate(each_pair, other, update_i, obj);
return obj;
}
@ -390,7 +433,7 @@ fsdbm_replace(obj, other)
VALUE obj, other;
{
fsdbm_clear(obj);
rb_iterate((VALUE(*)_((VALUE)))each_pair, other, update_i, obj);
rb_iterate(each_pair, other, update_i, obj);
return obj;
}
@ -427,7 +470,7 @@ fsdbm_store(obj, keystr, valstr)
sdbm_clearerr(dbm);
#endif
if (errno == EPERM) rb_sys_fail(0);
rb_raise(rb_eRuntimeError, "sdbm_store failed");
rb_raise(rb_eDBMError, "sdbm_store failed");
}
return valstr;
@ -670,46 +713,49 @@ fsdbm_reject(obj)
void
Init_sdbm()
{
cSDBM = rb_define_class("SDBM", rb_cObject);
rb_include_module(cSDBM, rb_mEnumerable);
rb_cDBM = rb_define_class("SDBM", rb_cObject);
rb_eDBMError = rb_define_class("SDBMError", rb_eStandardError);
rb_include_module(rb_cDBM, rb_mEnumerable);
rb_define_singleton_method(cSDBM, "open", fsdbm_s_open, -1);
rb_define_singleton_method(cSDBM, "new", fsdbm_s_new, -1);
rb_define_method(cSDBM, "initialize", fsdbm_initialize, -1);
rb_define_method(cSDBM, "close", fsdbm_close, 0);
rb_define_method(cSDBM, "[]", fsdbm_aref, 1);
rb_define_method(cSDBM, "fetch", fsdbm_fetch_m, -1);
rb_define_method(cSDBM, "[]=", fsdbm_store, 2);
rb_define_method(cSDBM, "store", fsdbm_store, 2);
rb_define_method(cSDBM, "index", fsdbm_index, 1);
rb_define_method(cSDBM, "indexes", fsdbm_indexes, -1);
rb_define_method(cSDBM, "indices", fsdbm_indexes, -1);
rb_define_method(cSDBM, "length", fsdbm_length, 0);
rb_define_alias(cSDBM, "size", "length");
rb_define_method(cSDBM, "empty?", fsdbm_empty_p, 0);
rb_define_method(cSDBM, "each", fsdbm_each_pair, 0);
rb_define_method(cSDBM, "each_value", fsdbm_each_value, 0);
rb_define_method(cSDBM, "each_key", fsdbm_each_key, 0);
rb_define_method(cSDBM, "each_pair", fsdbm_each_pair, 0);
rb_define_method(cSDBM, "keys", fsdbm_keys, 0);
rb_define_method(cSDBM, "values", fsdbm_values, 0);
rb_define_method(cSDBM, "shift", fsdbm_shift, 0);
rb_define_method(cSDBM, "delete", fsdbm_delete, 1);
rb_define_method(cSDBM, "delete_if", fsdbm_delete_if, 0);
rb_define_method(cSDBM, "reject!", fsdbm_delete_if, 0);
rb_define_method(cSDBM, "reject", fsdbm_reject, 0);
rb_define_method(cSDBM, "clear", fsdbm_clear, 0);
rb_define_method(cSDBM,"invert", fsdbm_invert, 0);
rb_define_method(cSDBM,"update", fsdbm_update, 1);
rb_define_method(cSDBM,"replace", fsdbm_replace, 1);
rb_define_singleton_method(rb_cDBM, "new", fsdbm_s_new, -1);
rb_define_singleton_method(rb_cDBM, "open", fsdbm_s_open, -1);
rb_define_method(cSDBM, "include?", fsdbm_has_key, 1);
rb_define_method(cSDBM, "has_key?", fsdbm_has_key, 1);
rb_define_method(cSDBM, "member?", fsdbm_has_key, 1);
rb_define_method(cSDBM, "has_value?", fsdbm_has_value, 1);
rb_define_method(cSDBM, "key?", fsdbm_has_key, 1);
rb_define_method(cSDBM, "value?", fsdbm_has_value, 1);
rb_define_method(rb_cDBM, "initialize", fsdbm_initialize, -1);
rb_define_method(rb_cDBM, "close", fsdbm_close, 0);
rb_define_method(rb_cDBM, "[]", fsdbm_aref, 1);
rb_define_method(rb_cDBM, "fetch", fsdbm_fetch_m, -1);
rb_define_method(rb_cDBM, "[]=", fsdbm_store, 2);
rb_define_method(rb_cDBM, "store", fsdbm_store, 2);
rb_define_method(rb_cDBM, "index", fsdbm_index, 1);
rb_define_method(rb_cDBM, "indexes", fsdbm_indexes, -1);
rb_define_method(rb_cDBM, "indices", fsdbm_indexes, -1);
rb_define_method(rb_cDBM, "select", fsdbm_select, -1);
rb_define_method(rb_cDBM, "length", fsdbm_length, 0);
rb_define_method(rb_cDBM, "size", fsdbm_length, 0);
rb_define_method(rb_cDBM, "empty?", fsdbm_empty_p, 0);
rb_define_method(rb_cDBM, "each", fsdbm_each_pair, 0);
rb_define_method(rb_cDBM, "each_value", fsdbm_each_value, 0);
rb_define_method(rb_cDBM, "each_key", fsdbm_each_key, 0);
rb_define_method(rb_cDBM, "each_pair", fsdbm_each_pair, 0);
rb_define_method(rb_cDBM, "keys", fsdbm_keys, 0);
rb_define_method(rb_cDBM, "values", fsdbm_values, 0);
rb_define_method(rb_cDBM, "shift", fsdbm_shift, 0);
rb_define_method(rb_cDBM, "delete", fsdbm_delete, 1);
rb_define_method(rb_cDBM, "delete_if", fsdbm_delete_if, 0);
rb_define_method(rb_cDBM, "reject!", fsdbm_delete_if, 0);
rb_define_method(rb_cDBM, "reject", fsdbm_reject, 0);
rb_define_method(rb_cDBM, "clear", fsdbm_clear, 0);
rb_define_method(rb_cDBM,"invert", fsdbm_invert, 0);
rb_define_method(rb_cDBM,"update", fsdbm_update, 1);
rb_define_method(rb_cDBM,"replace", fsdbm_replace, 1);
rb_define_method(cSDBM, "to_a", fsdbm_to_a, 0);
rb_define_method(cSDBM, "to_hash", fsdbm_to_hash, 0);
rb_define_method(rb_cDBM, "include?", fsdbm_has_key, 1);
rb_define_method(rb_cDBM, "has_key?", fsdbm_has_key, 1);
rb_define_method(rb_cDBM, "member?", fsdbm_has_key, 1);
rb_define_method(rb_cDBM, "has_value?", fsdbm_has_value, 1);
rb_define_method(rb_cDBM, "key?", fsdbm_has_key, 1);
rb_define_method(rb_cDBM, "value?", fsdbm_has_value, 1);
rb_define_method(rb_cDBM, "to_a", fsdbm_to_a, 0);
rb_define_method(rb_cDBM, "to_hash", fsdbm_to_hash, 0);
}

View file

@ -139,7 +139,7 @@ class TestSDBM < RUNIT::TestCase
assert_nil(sdbm.close)
# closed SDBM file
assert_exception(RuntimeError) { sdbm.close }
assert_exception(SDBMError) { sdbm.close }
end
def test_aref
@ -219,6 +219,25 @@ class TestSDBM < RUNIT::TestCase
assert_equals(values.reverse, @sdbm.indexes(*keys.reverse))
end
def test_select
keys = %w(foo bar baz)
values = %w(FOO BAR BAZ)
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
assert_equals(values.reverse, @sdbm.select(*keys.reverse))
end
def test_select_with_block
keys = %w(foo bar baz)
values = %w(FOO BAR BAZ)
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
ret = @sdbm.select {|k,v|
assert_equals(k.upcase, v)
k != "bar"
}
assert_equals([['baz', 'BAZ'], ['foo', 'FOO']],
ret.sort)
end
def test_length
num = 10
assert_equals(0, @sdbm.size)
@ -351,7 +370,7 @@ class TestSDBM < RUNIT::TestCase
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
assert_equals(@sdbm, @sdbm.delete(key))
assert_equals('BAR', @sdbm.delete(key))
assert_nil(@sdbm[key])
assert_equals(2, @sdbm.size)
@ -360,12 +379,13 @@ class TestSDBM < RUNIT::TestCase
def test_delete_with_block
key = 'no called block'
@sdbm[key] = 'foo'
assert_equals(@sdbm, @sdbm.delete(key) {|k| k.replace 'called block'})
assert_equals('foo', @sdbm.delete(key) {|k| k.replace 'called block'})
assert_equals('no called block', key)
assert_equals(0, @sdbm.size)
key = 'no called block'
assert_nil(@sdbm.delete(key) {|k| k.replace 'called block'})
assert_equals(:blockval,
@sdbm.delete(key) {|k| k.replace 'called block'; :blockval})
assert_equals('called block', key)
assert_equals(0, @sdbm.size)
end

View file

@ -901,11 +901,10 @@ r_object(arg)
len--;
#endif
}
big = RBIGNUM(rb_big_norm((VALUE)big));
if (TYPE(big) == T_BIGNUM) {
r_regist((VALUE)big, arg);
v = rb_big_norm((VALUE)big);
if (TYPE(v) == T_BIGNUM) {
r_regist(v, arg);
}
v = (VALUE)big;
}
break;
@ -1009,10 +1008,10 @@ r_object(arg)
klass = rb_path2class(r_unique(arg));
v = rb_obj_alloc(klass);
r_regist(v, arg);
if (TYPE(v) != T_OBJECT) {
rb_raise(rb_eArgError, "dump format error");
}
r_regist(v, arg);
r_ivar(v, arg);
}
break;
@ -1051,9 +1050,10 @@ r_object(arg)
case TYPE_SYMBOL:
v = ID2SYM(r_symreal(arg));
break;
case TYPE_SYMLINK:
v = ID2SYM(r_symlink(arg));
return ID2SYM(r_symlink(arg));
default:
rb_raise(rb_eArgError, "dump format error(0x%x)", type);