openerp-india team mailing list archive
-
openerp-india team
-
Mailing list archive
-
Message #08531
[Bug 525808] Re: Default values should be immutable
I stumbled on another small nasty thing I'd like to draw your attention to: if I'm not mistaken, when a caller passes a context to a function, this function receives the context's "pointer", not a distinct copy of the data.
As a consequence, functions that modify or added keys to the context must do so on a copy of the context, otherwise they may change the behavior of the caller.
That step is still missing in a good number of places I think: I suggest we do this at the same time as testing for None contexts in case it may be modified, in a test like this:
if context is None:
context = {}
else:
context=context.copy()
Lionel Sausin.
--
You received this bug notification because you are a member of OpenERP
Indian Team, which is subscribed to OpenERP Server.
https://bugs.launchpad.net/bugs/525808
Title:
Default values should be immutable
Status in OpenERP Addons (modules):
Opinion
Status in OpenERP Addons extra-trunk series:
Opinion
Status in OpenERP Addons trunk series:
Opinion
Status in OpenERP GTK Client:
Opinion
Status in OpenERP Server:
Fix Released
Status in OpenERP Server trunk series:
Fix Released
Bug description:
Often in the code base we have:
def foo(...., context={},....):
....
As explained in comment #1, this should be changed to
def foo(...., context=None,....):
# you should add this test only if the context is actually used
if context is None:
context={}
....
(This bug originally proposed the other way round hence the "nope" in
comment #1.)
(xmo) Complements of information:
* The test should be `context is None` rather than `not context`: the caller could provide an empty dict argument {} and expect to have data in it after the call, if a test is done using `not context` an empty dict will match and the function will be creating a brand new context.
* The test should *only ever* performed when the current function fetches data from the context. If you don't do anything with the context but forward it to other functions and methods (e.g. read() or create()), no need to test for anything.
To manage notifications about this bug go to:
https://bugs.launchpad.net/openobject-addons/+bug/525808/+subscriptions