[Zope-dev] Question about import code

Shane Hathaway shane@digicool.com
Thu, 10 May 2001 10:59:14 -0400


Fred Wilson Horch wrote:
> The problem that happens on import is a KeyError on line 194 of this bit
> of code:
> 
> lines 192-197 of lib/python/ZODB/ExportImport.py
> 
>                 ooid=h[:8]
>                 if oids:
>                     oid=oids[ooid]
>                     if type(oid) is TupleType: oid=oid[0]
>                 else:
>                     oids[ooid]=return_oid=oid=new_oid()
> 
> If I change this code to
> 
>                 ooid=h[:8]
>                 if oids and oids.has_key(ooid):
>                     oid=oids[ooid]
>                     if type(oid) is TupleType: oid=oid[0]
>                 else:
>                     oids[ooid]=return_oid=oid=new_oid()
> 
> then the import appears to succeed, but only the final object in the
> file is actually imported.

Importing multiple objects simultaneously could result in a fair amount
of confusion...

But if you really think it's a good idea, if you used a list called
return_oids instead, and appended to this list in the code above, you'd
probably get what you're looking for.

                ooid=h[:8]
                if oids and oids.has_key(ooid):
                    oid=oids[ooid]
                    if type(oid) is TupleType: oid=oid[0]
                else:
                    oids[ooid] = oid = new_oid()
                    return_oids.append(oid)

Then at the end of the function you'll want to dereference each of the
OIDs and return a list of objects.  Everything that uses import will
have to be modified, unless you change the name of the method and
provide a wrapper method with the existing signature.

Note that for Zope 2.4 this code has changed a bit.  Import is now done
as a subtransaction rather than a transaction "on the side".

Shane