[Tutor] String Encoding problem

Matt HellZFury+Python at gmail.com
Mon Apr 20 16:46:47 CEST 2009

Hey everyone,

I'm hoping someone here can help me solve an odd problem (bug?). I'm having
trouble with string encoding, object deletion, and the xml.etree library. If
this isn't the right list to be posting this question, please let me know.
I'm new to Python and don't know of any other "help me" Python mailing
lists. I have tried debugging this ad-infinitem. Anyway, at the bottom of
this e-mail you will find the code of a python file. This is a gross
over-simplification of my code, with little exception handling so that the
errors are obvious.

Running this interactively, if you finish off with 'del db', it exits fine
and creates a skeleton xml file called 'db.xml' with text '<root />'.
However, if you instead CTRL-D, it throws at exception while quitting and
then leaves an empty 'db.xml' which won't work. Can anyone here help me
figure out why this is?

Stuff I've done:
I've traced this down to the self.commit() call in __del__. The stacktrace
and a few print statements injected into xml.etree leads me to the call
'root'.encode('us-ascii') throwing a LookupError on line 751 of
xml.etree.ElementTree. This makes no sense to me, since it works fine

Thank you very much. Any and all help or pointers are appreciated.


#### db.py ###
from xml.etree import ElementTree as ET
import os

class Database(object):
    def __init__(self, path):
        self.__dbpath = path    ## Path to the database
    def __del__(self):
        ## FIXME: Known bug:
        ##  del db at command line works properly
        ##  Ctrl-D, when there is no db file present, results in a
        ##    and empty xml file
        from StringIO import StringIO
        from traceback import print_exc
        trace = StringIO()
            print 5
            print 7
        except Exception:
            print_exc(100, trace)
            print trace.getvalue()
    def load(self):
        if os.path.exists(self.__dbpath):
            self.root = ET.parse(self.__dbpath).getroot()
            self.root = ET.Element("root")
    def commit(self):
db = Database('db.xml')
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090420/c56df459/attachment.htm>

More information about the Tutor mailing list