YA-Newbie: Progress (but still baffled by deepcopy/pickle issues...:)

Fred Pacquier fredp at multimania.com.nospam
Wed Sep 29 04:56:37 EDT 1999


Following-up on myself:

Thanks to hints by Tim Peters (ly-yrs) and Jeremy Hylton, I have now 
identified the specific point of failure in my code, and a workaround, so I 
can now resume development -- thanks !

The uncomfortable part of the sitiation is that while I've found what 
doesn't work and what works as a replacement, I still don't understand why 
one solution fails and not the other. This bothers me, so I'll try to 
explain here what's going on in case someone gets interested and can shed 
some light on the matter...

As I said it's a card game, with a simple object structure. The problem 
happens with this combination:

First I have 3 module-global variables that hold wxWindows bitmap handles:

Faces1 = wxBitmap(...) # card faces held in 2 separate bitmaps
Faces2 = wxBitmap(...)
CardBack = wxBitmap(...)

Then I have the Card class :

class Card :
    	def __init__(self, index) :
    	    	self.color = ...
    	    	self.rank = ...
    	    	if ... : self.src = Faces1
    	    	else : self.src = Faces2
    	    	self.Xoffset = ...
    	    	self.Yoffset = ...
    	    	...

The point in this pseudocode is that each Card instance holds a reference 
to the Bitmap the card image is to be fetched from (and its offsets in it).

Next is the Stack class. It is essentially a list of Card instances, with a 
few simple data attributes (integers) and methods for displaying, moving 
around, etc, as in :

class Stack :
    	def showrow(self, row) :
            srcDC.SelectObject(self.Cards[row].src)
            dstDC.Blit(destx, desty, self.Cw, self.Ch, 
            srcDC,self.Cards[row].Xoffset,self.Cards[row].Yoffset,xCOPY)
            srcDC.SelectObject(wxNullBitmap)

The game state is almost entirely contained in a list of a dozen Stack 
instances (Cols), so saving and restoring it should be as easy as:

   def OnGameSave(self, event):
        SaveGame.Cols = deepcopy(self.Cols)

   def OnGameRestore(self, event):
        self.Cols = deepcopy(SaveGame.Cols)
        self.Refresh(1,None)

Unfortunately, as I mentioned initially, it's only that easy ONCE. If the 
state is saved once, and restored once, all is well. If it is restored 
twice, or saved twice, all is wrong. I could not be more precise in my 
first message but now I know exactly _what_ is wrong : the Faces1 and 
Faces2 bitmaps are "gone" (but, strangely, not CardBack...). All the object 
instances and references (addresses, like Card.src) are correct, but there 
is nothing to display except card backs, and it's time to bail out or 
wxPython will crash...

The difference between Faces1/Faces2 and CardBack being that the latter is 
not referenced by Card instances, on a hunch I tried the following changes:

# new global
CardFaces = (Faces1, Faces2)

# in Card __init__:
    	    	if ... : self.src = 0  # store an index instead of bitmap ref.
    	    	else : self.src = 1

# in Stack showrow():
            srcDC.SelectObject(CardFaces[self.Cards[row].src])

This seems to work flawlessly, although I don't understand why. Does anyone 
? If anybody is interested in debugging, I have a trimmed-down module of 
about 300 lines that exhibits both behaviours -- just mail me, but BEWARE 
the .nospam suffix in my return address (sorry Jeremy :)

Thanks for your attention,
fp

-- 
YAFAP : http://www.multimania.com/fredp/




More information about the Python-list mailing list