[Tutor] Stacks and Stack underflow/Stack overflow

Steven D'Aprano steve at pearwood.info
Sun Nov 20 01:03:15 CET 2011


Joe Batt wrote:
> Hi All Could some kind soul please explain why you get a stack
> underflow and a stack overflow.


I don't, because I never make misteaks.

*grin*

Stack underflow and stack overflow are generic errors. You get a stack
underflow when you have (say) five items on a stack and try to retrieve 
six: if you try to retrieve more items than are actually there, the 
stack underflows.

Stack overflow occurs when you fill a stack. Many, but not all, 
applications set their stack to have a maximum size, to prevent it 
growing without bounds and using all your memory. So if the maximum size 
is (say) 1000 items, and you push 1001 items onto it, the stack will be
filled and you get an overflow.

*Specifically* related to pickle, you get an stack underflow when either
your pickle file is corrupt, or because you aren't using it correctly.
Or (very unlikely) pickle has a bug.

You can't manually change the stack. You shouldn't need to change the 
stack. An application could have dozens of stacks, for all you know.

(Sometimes people talk about "the stack", meaning the stack used by the 
operating system for executing code. You don't want to touch that, even 
if you could, which fortunately you can't.)

Without seeing how you created your pickle file, and what you did to it
in the meantime, it is impossible to say why it is corrupt or how it 
became corrupted. But my guess is that you may have opened the file in 
Notepad or something, and hit save, and Notepad "helpfully" changed the 
contents somehow. Result: a corrupt file that pickle can no longer read 
correctly.

Please run this code and confirm that pickle works correctly for you:

# Create some data.
a = {'bb': None, 'cc': [1, 2, 3], 'dd': 42}
b = [None, (23, 24, 25), "spam"]

# Save it to a pickle file.
import pickle
f = open('///Users/joebatt/Desktop/tmp12345', 'wb')
pickle.dump(a, f)
pickle.dump(b, f)
f.close()

# Read it back in.
f = open('///Users/joebatt/Desktop/tmp12345', 'rb')
c = pickle.load(f)
d = pickle.load(f)
f.close()

# Confirm the values are correct.
assert a == c
assert b == d



-- 
Steven


More information about the Tutor mailing list