* sprintf.c (rb_f_sprintf): preserve original val for

format_integer. [ruby-talk:92975]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5756 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2004-02-17 15:26:03 +00:00
parent dd23ad7e84
commit 4b41d18b6c
4 changed files with 63 additions and 41 deletions

View File

@ -5,6 +5,7 @@
*~ *~
.ccmalloc .ccmalloc
.ppack .ppack
.ext
COPYING.LIB COPYING.LIB
ChangeLog.pre-alpha ChangeLog.pre-alpha
ChangeLog.pre1_1 ChangeLog.pre1_1
@ -22,7 +23,6 @@ config.h.in
config.log config.log
config.status config.status
configure configure
foo.rb
libruby.so.* libruby.so.*
miniruby miniruby
miniruby.elhash miniruby.elhash
@ -30,7 +30,6 @@ miniruby.elhash2
miniruby.orig2 miniruby.orig2
miniruby.plhash miniruby.plhash
miniruby.plhash2 miniruby.plhash2
modex.rb
newdate.rb newdate.rb
newver.rb newver.rb
parse.c parse.c

View File

@ -1,3 +1,8 @@
Tue Feb 17 23:40:34 2004 Guy Decoux <ts@moulon.inra.fr>
* sprintf.c (rb_f_sprintf): preserve original val for
format_integer. [ruby-talk:92975]
Tue Feb 17 23:28:45 2004 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp> Tue Feb 17 23:28:45 2004 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>
* test/soap/marshal/test_marshal.rb, test/ruby/test_marshal.rb: do $: * test/soap/marshal/test_marshal.rb, test/ruby/test_marshal.rb: do $:

View File

@ -35,7 +35,11 @@ class PStore
def in_transaction def in_transaction
raise PStore::Error, "not in transaction" unless @transaction raise PStore::Error, "not in transaction" unless @transaction
end end
private :in_transaction def in_transaction_wr()
in_transaction()
raise PStore::Error, "in read-only transaction" if @rdonly
end
private :in_transaction, :in_transaction_wr
def [](name) def [](name)
in_transaction in_transaction
@ -52,11 +56,11 @@ class PStore
self[name] self[name]
end end
def []=(name, value) def []=(name, value)
in_transaction in_transaction_wr()
@table[name] = value @table[name] = value
end end
def delete(name) def delete(name)
in_transaction in_transaction_wr()
@table.delete name @table.delete name
end end
@ -86,27 +90,38 @@ class PStore
def transaction(read_only=false) def transaction(read_only=false)
raise PStore::Error, "nested transaction" if @transaction raise PStore::Error, "nested transaction" if @transaction
begin begin
@rdonly = read_only
@abort = false
@transaction = true @transaction = true
value = nil value = nil
backup = @filename+"~" new_file = @filename + ".new"
begin
file = File::open(@filename, read_only ? "rb" : "rb+") content = nil
orig = true file = File.open(@filename, File::RDWR | File::CREAT)
rescue Errno::ENOENT if !read_only
raise if read_only file.flock(File::LOCK_EX)
file = File::open(@filename, "wb+") commit_new() if FileTest.exist?(new_file)
content = file.read()
else
file.flock(File::LOCK_SH)
if FileTest.exist?(new_file)
File.open(new_file) {|fp| content = fp.read()}
else
content = file.read()
end
end end
file.flock(read_only ? File::LOCK_SH : File::LOCK_EX)
if read_only if content != ""
@table = Marshal::load(file)
elsif orig and (content = file.read) != ""
@table = Marshal::load(content) @table = Marshal::load(content)
size = content.size if !read_only
md5 = Digest::MD5.digest(content) size = content.size
content = nil # unreference huge data md5 = Digest::MD5.digest(content)
end
else else
@table = {} @table = {}
end end
content = nil # unreference huge data
begin begin
catch(:pstore_abort_transaction) do catch(:pstore_abort_transaction) do
value = yield(self) value = yield(self)
@ -116,24 +131,17 @@ class PStore
raise raise
ensure ensure
if !read_only and !@abort if !read_only and !@abort
file.rewind tmp_file = @filename + ".tmp"
content = Marshal::dump(@table) content = Marshal::dump(@table)
if !md5 || size != content.size || md5 != Digest::MD5.digest(content) if !md5 || size != content.size || md5 != Digest::MD5.digest(content)
File::copy @filename, backup File.open(tmp_file, "w") {|t|
begin t.write(content)
file.write(content) }
file.truncate(file.pos) File.rename(tmp_file, new_file)
content = nil # unreference huge data commit_new()
rescue end
File::rename backup, @filename if File::exist?(backup) content = nil # unreference huge data
raise
end
end
end end
if @abort and !orig
File.unlink(@filename)
end
@abort = false
end end
ensure ensure
@table = nil @table = nil
@ -142,6 +150,15 @@ class PStore
end end
value value
end end
private
def commit_new()
new_file = @filename + ".new"
if !File.copy(new_file, @filename)
raise IOError
end
File.unlink(new_file)
end
end end
if __FILE__ == $0 if __FILE__ == $0

View File

@ -444,6 +444,7 @@ rb_f_sprintf(argc, argv)
long v = 0; long v = 0;
int base, bignum = 0; int base, bignum = 0;
int len, pos; int len, pos;
VALUE tmp;
switch (*p) { switch (*p) {
case 'd': case 'd':
@ -571,8 +572,8 @@ rb_f_sprintf(argc, argv)
} }
if (sign) { if (sign) {
val = rb_big2str(val, base); tmp = rb_big2str(val, base);
s = RSTRING(val)->ptr; s = RSTRING(tmp)->ptr;
if (s[0] == '-') { if (s[0] == '-') {
s++; s++;
sc = '-'; sc = '-';
@ -592,8 +593,8 @@ rb_f_sprintf(argc, argv)
val = rb_big_clone(val); val = rb_big_clone(val);
rb_big_2comp(val); rb_big_2comp(val);
} }
val = rb_big2str(val, base); tmp = rb_big2str(val, base);
s = RSTRING(val)->ptr; s = RSTRING(tmp)->ptr;
if (*s == '-') { if (*s == '-') {
if (base == 10) { if (base == 10) {
rb_warning("negative number for %%u specifier"); rb_warning("negative number for %%u specifier");
@ -601,8 +602,8 @@ rb_f_sprintf(argc, argv)
} }
else { else {
remove_sign_bits(++s, base); remove_sign_bits(++s, base);
val = rb_str_new(0, 3+strlen(s)); tmp = rb_str_new(0, 3+strlen(s));
t = RSTRING(val)->ptr; t = RSTRING(tmp)->ptr;
if (!(flags&FPREC)) { if (!(flags&FPREC)) {
strcpy(t, ".."); strcpy(t, "..");
t += 2; t += 2;
@ -619,7 +620,7 @@ rb_f_sprintf(argc, argv)
bignum = 2; bignum = 2;
} }
} }
s = RSTRING(val)->ptr; s = RSTRING(tmp)->ptr;
format_integer: format_integer:
pos = -1; pos = -1;