doctest + shelve question

Sebastian Bartos seth.kriticos at googlemail.com
Fri Mar 6 22:56:07 CET 2009


Hello list,

I have a question. I'm writing a simple object serialization module
using shelve to write arbitrary objects to a file (M.py). Now I have the
problem, that if I create a simple object in the doctest documentation
file M.txt like this:

    >>> class tdata(object):
    ...     def __init__(self, name):
    ...         self.name = name
    >>> tinst = tdata(u'foo')

and then run it with my module M:

    >>> import M
    >>> foo = M.serialize('/tmp/foo', tinst.name, tinst)

which calls:

class serialize(object):
	def __init__(self, path, key, data):
		db = shelve.open(path)
		db[key.encode('utf-8')] = data
		db.close()

then I get the following problem:

Failed example:
    foo = M.serialize('/tmp/foo', tinst)
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python2.5/doctest.py", line 1228, in __run
        compileflags, 1) in test.globs
...
      File "/usr/lib/python2.5/shelve.py", line 123, in __setitem__
        p.dump(value)
    PicklingError: Can't pickle <class 'tdata'>: attribute lookup
__builtin__.tdata failed


If I do the same in the interactive interpreter, then it works fine.

Now, as I want to test an arbitrary data class, the doctest file is the
place to put the simple tdata class, but as far as I traced the problem,
it uses it's own namespace or something, and so does not work.

I'd like to keep a consistent doctest documentation here and not spawn
my project with additional test modules or similar.

Any idea on how I can make it work like as if it would be called from
the interactive interpreter? The documentation and Google were not too
helpful.

ps. Sys: Linux, Python: 2.5.2

thanks

-- 
Sebastian Bartos, <seth.kriticos at googlemail.com>
keyserevr: pgp.mit.edu
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 204 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-list/attachments/20090306/914e5fee/attachment.pgp>


More information about the Python-list mailing list