This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: bug in core python variable binding
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, vavasis
Priority: normal Keywords:

Created on 2011-09-19 04:32 by vavasis, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pybugreport.zip vavasis, 2011-09-19 04:32 zip file containing pickled data and .py source code
Messages (2)
msg144261 - (view) Author: Stephen Vavasis (vavasis) Date: 2011-09-19 04:32
There seems to be a serious bug in how python 2.7.2 binds variables to values.  In the attached function buildfunclist, you see that there is a variable called 'funclist' that is initialized to [], and then is modified only with 'append' calls.  This means that once append is called 46 times, one expects that funclist[45] is defined and will not change?  And yet funclist[45] changes several times as more data items are appended.  The same bug is present in 3.2.2.  My operating system is Windows 7 64-bit on a Lenovo Thinkpad T410.  I'm guessing that there is a problem with python's lazy copying-- it is a bit too lazy and failing to make copies when lists are changed.

To exhibit this bug, proceed as follows:

import pickle
h = open('combined_oplists_pickle','r')
combined_oplists = pickle.Unpickler(h).load()
import pybugreport
funclist,funcdist = pybugreport.buildfunclist(combined_oplists)

and then you will see funclist[45] printed out on two successive iterations.  It has changed as a result of an append operation, which should not happen.  (It's 6th entry is longer.)

Here is the output:

funclist[45] = [0, 22973, '$FUNC', 'splitBoxInterior', [['InArg', [[['', 'ActiveBoxVectorI', '::', 'iterator', ''], ['thisboxdata_p', '']], [['', 'FaceIndex', ''], ['faceind', '']]]], ['InOutArg', [[['', 'ActiveBoxVectorI', ''], ['interiorOrbitNextLev', '']]]], ['RefGlobal', [[['', 'MIndex', ''], ['guiActiveBoxCount', '']]]], ['Workspace', [[['', 'QMGVector', '<', '', 'BoxCreationData', '', '> ', ''], ['boxCreationVec', '']]]]], [], [[0, 23017], [0, 23048], [0, 23068], [0, 23069]], [[0, 23001]]]
funclist[45] = [0, 22973, '$FUNC', 'splitBoxInterior', [['InArg', [[['', 'ActiveBoxVectorI', '::', 'iterator', ''], ['thisboxdata_p', '']], [['', 'FaceIndex', ''], ['faceind', '']]]], ['InOutArg', [[['', 'ActiveBoxVectorI', ''], ['interiorOrbitNextLev', '']]]], ['RefGlobal', [[['', 'MIndex', ''], ['guiActiveBoxCount', '']]]], ['Workspace', [[['', 'QMGVector', '<', '', 'BoxCreationData', '', '> ', ''], ['boxCreationVec', '']]]]], [[0, 23115], [0, 23116], [0, 23117], [0, 23118], [0, 23119], [0, 23120], [0, 23121], [0, 23122], [0, 23123], [0, 23124], [0, 23125], [0, 23126], [0, 23127], [0, 23128], [0, 23129], [0, 23130], [0, 23131], [0, 23132], [0, 23133], [0, 23134], [0, 23135], [0, 23136], [0, 23139], [0, 23140], [0, 23141], [0, 23142], [0, 23143], [0, 23144], [0, 23145], [0, 23146]], [[0, 23017], [0, 23048], [0, 23068], [0, 23069], [0, 23137], [0, 23138], [0, 23147], [0, 23148], [0, 23149], [0, 23161]], [[0, 23001]]]
msg144262 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-09-19 06:24
This is a bug in the script; the code is similar to the following::

  >>> funclist = []
  >>> global_list = []
  >>> funclist.append(global_list)
  >>> global_list.append(1)
  >>> funclist.append(global_list)
  >>> print funclist
  [[1], [1]]

i.e the same object is added twice to "funclist", any modification to the first item is a modification to the second. See also http://www.python.org/doc//current/faq/programming.html#how-do-i-create-a-multidimensional-list
In your script, it's certainly happens because there are multiple nested blocks ('{' '}') in the same function ('$FUNC'), so global_list is still the *same* list.
History
Date User Action Args
2022-04-11 14:57:21adminsetgithub: 57215
2011-09-19 06:24:21amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg144262

resolution: not a bug
2011-09-19 04:32:46vavasiscreate