doctest + shelve question

Sebastian Bartos seth.kriticos at
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 ( 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):
    ... = name
    >>> tinst = tdata(u'foo')

and then run it with my module M:

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

which calls:

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

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/", line 1228, in __run
        compileflags, 1) in test.globs
      File "/usr/lib/python2.5/", line 123, in __setitem__
    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

ps. Sys: Linux, Python: 2.5.2


Sebastian Bartos, <seth.kriticos at>
-------------- 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: <>

More information about the Python-list mailing list