From: Billy G. A. <bil...@de...> - 2007-10-19 07:23:31
|
Billy G. Allie wrote: > SourceForge.net wrote: >> Bugs item #1755777, was opened at 2007-07-17 18:56 >> Message generated for change (Tracker Item Submitted) made by Item >> Submitter >> You can respond by visiting: >> https://sourceforge.net/tracker/?func=detail&atid=116528&aid=1755777&group_id=16528 >> >> >> Please note that this message will contain a full copy of the comment >> thread, >> including the initial issue submission, for this request, >> not just the latest update. >> Category: PgNumeric >> Group: None >> Status: Open >> Resolution: None >> Priority: 5 >> Private: No >> Submitted By: Ken Lalonde (kenlalonde) >> Assigned to: Nobody/Anonymous (nobody) >> Summary: PgNumeric += is strange >> >> Initial Comment: >> The augmented arithmetic operations on PgNumeric instances will >> update the object directly, >> instead of creating a new one. >> This leads to unexpected results such as: >> >> $ cat t.py >> from pyPgSQL.PgSQL import PgNumeric >> a = PgNumeric(1.0) >> b = a >> b += 1 >> print a, b >> $ python t.py >> 2.0 2.0 >> >> Here we see b is incremented, but both a and b >> are changed, because the __iadd__ method updates >> b in place. >> >> Call me naive, but I would expect "b += 1" >> to behave exactly like "b = b+1". >> >> Perhaps PgNumeric should allocate a new object >> if the old one has multiple references? >> I'm not sure, but the current behavior >> is not intuitive, and leads to hard-to-find bugs. > It's in the nature of the in-place operators when they apply to > objects. The statement "b +=1" adds one to the existing object > referenced by > "b". Since a is also a reference to the same object, it also displays > the new result. The statement "b = b + 1" creates a new object with > the value of "b + 1" and puts a reference to it into b. > > Ok, that explains the problem. The question is how to fix it. The > solution as you pointed out would be to create a new PgNumeric object > and place a reference to it into "b", but I don't know if it is > possible to do that. Any thoughts on the problem would be appreciated. > > -- > Bill Allie > > It turned out to be easier to fix then I thought. Attached is a patch to fix the problem. -- Bill Allie. |