Re: [Zope] Python script: problem with session
The copy method of a dictionary returns a "shallow" copy of the dictionary. If you are changing elements of the dictionary that are themselves mutable, you will still have problems. Here are the rules (from the Python docs): The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances): a.. A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. b.. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. Providing you with more information would require that we know all the items in the 'foo' dictionary. If any of them is mutable, and you're changing it, you would see the behavior you describe. ----- Original Message ----- From: "p.t." <p.training@tin.it> To: "Chris McDonough" <chrism@zope.com> Sent: Wednesday, June 12, 2002 12:17 PM Subject: Re: [Zope] Python script: problem with session
Sorry, Chris, you are right. "not working" means that the assignment is Ok, but the chamges are still reflected on the referenced item. p.t.
At 10:38 12/06/2002 -0400, you wrote:
When you say "not working" what does that mean? What do you go on to do that doesn't work? ----- Original Message ----- From: "p.t." <p.training@tin.it> To: "Chris McDonough" <chrism@ZOPE.COM> Cc: <zope@zope.org> Sent: Wednesday, June 12, 2002 10:19 AM Subject: Re: [Zope] Python script: problem with session
Fine, Chris. I'll try your suggestion exactley in the way you wraite it down. I've already tryied this:
context.REQUEST['foo'] = context.REQUEST.SESSION['foo'].copy()
but (suprisingly) not working (and no error issue). Things work fine "renaming" objects, like:
Initialise: context.REQUEST.SESSION['FOO'] = foo
Assign: context.REQUEST['foo'] = context.REQUEST.SESSION['FOO']
but this is quite impratical, because I'm often using indirect naming via the Zope function getitem(). Thanks, p.t.
At 10:01 12/06/2002 -0400, Chris McDonough wrote:
I see... when you hold a reference to a dictionary in two places you are holding a reference to the same object, therefore changes made via one reference are reflected when you take a look at the object via the other reference.
One way to avoid this in your case in a Python Script:
d = context.REQUEST.SESSION['foo'] REQUEST['foo'] = d.copy()
In other words, don't assign the value of SESSION['foo'] to REQUEST['foo'], rather assign a copy.
----- Original Message ----- From: "p.t." <p.training@tin.it> To: "Chris McDonough" <chrism@zope.com> Cc: <zope@zope.org> Sent: Wednesday, June 12, 2002 9:41 AM Subject: Re: [Zope] Python script: problem with session
Chris, it is a mapped object (a dictionary). A closer view is:
First (in a calling procedure) I put in SESSION[processID]['status']['dataTable'] the name of a table. Then (in the called procedure) I retrieve this name to the current status: REQUEST['status'] = SESSION[processID]['status']
REQUEST['status'] can change some values before calling back the caller. TIA, p.t.
At 09:10 12/06/2002 -0400, Chris McDonough wrote:
What kind of object does REQUEST['foo'] return?
On Wed, 2002-06-12 at 08:43, p.t. wrote: > I did already post this with a different thread: probably the formulation > was to complex. Or may be there is some very basic knowledge I'm missing. > I'll try again. > > If I assign to variable in a Python script (say context.REQUEST['foo']) a > value stored in SESSION (say context.SESSION['foo']), and then I change the > retrieved value (the one in REQUEST, not the original one), also the > variable in SESSION is changed. In this way I have lost my original > information stored in SESSION! > > I've tryied to avoid this, but I can't manage to find how. > Any suggestion? > > TIA, > p.t. > > > > _______________________________________________ > Zope maillist - Zope@zope.org > http://lists.zope.org/mailman/listinfo/zope > ** No cross posts or HTML encoding! ** > (Related lists - > http://lists.zope.org/mailman/listinfo/zope-announce > http://lists.zope.org/mailman/listinfo/zope-dev )
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
First of all, thanks Chris and David for the explanations: now things are really more understandable to me and I can devise a suitable way to work around the problem. Next, where is the Python doc referenced? Library reference? Language reference? Something else? Finally: does a "deepCopy()" method exist? This would make things easier when storing objects in SESSION: I beleave that (genarally speaking) assigning values to SESSION should be given an option if it is a reference or a deep copy, because SESSION is a temporary repository to be used to "remember" values relevant to object attributes which often are used in a LIFO stack fashion with the same name (polimorphic approach), but independetly managed by different procedure. p.t. At 01:44 PM 6/12/2002 -0400, Chris McDonough wrote:
The copy method of a dictionary returns a "shallow" copy of the dictionary. If you are changing elements of the dictionary that are themselves mutable, you will still have problems. Here are the rules (from the Python docs):
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
a.. A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
b.. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. Providing you with more information would require that we know all the items in the 'foo' dictionary. If any of them is mutable, and you're changing it, you would see the behavior you describe.
Just stumbled on this again this morning looking for emancipation... http://www.python.org/doc/current/lib/lib.html Cheers, Paz -----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org] On Behalf Of p.t. Sent: Thursday, June 13, 2002 11:38 AM To: Chris McDonough Cc: Zope Users Subject: Re: [Zope] Python script: problem with session First of all, thanks Chris and David for the explanations: now things are really more understandable to me and I can devise a suitable way to work around the problem. Next, where is the Python doc referenced? Library reference? Language reference? Something else? Finally: does a "deepCopy()" method exist? This would make things easier when storing objects in SESSION: I beleave that (genarally speaking) assigning values to SESSION should be given an option if it is a reference or a deep copy, because SESSION is a temporary repository to be used to "remember" values relevant to object attributes which often are used in a LIFO stack fashion with the same name (polimorphic approach), but independetly managed by different procedure. p.t. At 01:44 PM 6/12/2002 -0400, Chris McDonough wrote:
The copy method of a dictionary returns a "shallow" copy of the dictionary. If you are changing elements of the dictionary that are themselves mutable, you will still have problems. Here are the rules (from the Python docs):
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
a.. A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
b.. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. Providing you with more information would require that we know all the items in the 'foo' dictionary. If any of them is mutable, and you're changing it, you would see the behavior you describe.
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
participants (3)
-
Chris McDonough -
p.t. -
Paul Zwarts