[Tutor] shelve error

Daniel Ehrenberg littledanehren at yahoo.com
Wed Jan 21 17:31:05 EST 2004


Ron A wrote:
> I have Python 2.2 and when I try this program I get
> a popup with this
> message. Can you see what I'm doing wrong? I just
> want to take some info
> I have in a file and (shelve it?). 
> PYTHONW caused an invalid page fault in 
> module MSVCRT.DLL at 0167:78012473. 
> Registers: 
> EAX=00000002 CS=0167 EIP=78012473 EFLGS=00010246 
> EBX=00750880 SS=016f ESP=0062cdc4 EBP=0062cdcc 
> ECX=ffff01f0 DS=016f ESI=00aef68e FS=0f77 
> EDX=00000000 ES=016f EDI=00aef70e GS=0000 
> Bytes at CS:EIP: 
> 8a 06 88 07 8a 46 01 c1 e9 02 88 47 01 83 c6 02 
> Stack dump: 
> 0000ff8e 1e03b1d0 0062ce00 00f1812c 00aef70e
> 00aef68e ffff01f2 00ada1c0
> 00aef68e 00add470 00aef70e 00adf700 00000008
> 00000080 1e030200 0062ce48 
> This program has performed an illegal operation and
> will be shut down. 
> ### Here's the program ######### 
> import cPickle, shelve 
> hold = [] 
> data = open('inout.txt', 'r') 
> while 1: 
>     temp = data.readline() 
>     if temp == '': 
>         break 
>     temp = temp[:-1] 
>     temp = temp.split('\t') 
>     hold.append(temp) 
> data.close() 
> data = shelve.open('inventory') 
> for num in hold: 
>     a = num[0] 
>     b = num[1] 
>     c = num[2] 
>     d = num[3] 
>     e = num[4] 
>     f = num[5] 
>     g = num[6] 
>     h = num[7] 
>     i = num[8] 
>     j = num[9] 
>     k = num[10] 
>     l = num[11] 
>     m = num[12] 
>     n = num[13] 
>     data[b] = [a, c, d, e, f, g, h, i, j, k, l, m,
> n] 
>     data.sync() 
> data.close

First of all, you should be using Python 2.3. 2.3
resolved many of 2.2's bugs and lets you use
generators.

Second of all, you're algorithm's somewhat
inefficient. Here's how I would implement it:

import shelve #you never used cPickle; why import it?
hold = [] 
input = open('inout.txt') #r is default
for line in input: #loops through lines in file
    temp = line.split('\t') 
    hold.append(temp) 
input.close() 
database = shelve.open('inventory') #don't reuse
variable names
for num in hold: 
    index = num.pop(1) #removes num[1] and returns it
    database[index] = num
    #sync not needed; done automatically when closed
database.close()

I don't see any real bugs in either version, though.
When you get one of those Microsoft errors like that,
it's Windows's fault, not Python's.

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus



More information about the Tutor mailing list