help with print function in extension

Eric Hagemann ehagemann at
Wed Sep 25 17:03:30 EDT 2002

Alex, Thanks -- missed that section in the manual.  I blindly assumed that I
could use the repr() as the print func......

ok so for the next trick question. I implemented a function that has the
int print_func(PyObject *obj, FILE *fp,int flags)

when I try to print to fp -- as in --> fprintf(fp,"<fmt>", , )... I crash
the program.

when the print function is used and output is sent to the screen is the fp
set equal to stdout ?

The example in the "Extending and Embedding the Python Interpreter" show a
fprintf to fp as might be expected ?!?


"Alex Martelli" <aleax at> wrote in message
news:4jfk9.163306$pX1.5842487 at
> Eric Hagemann wrote:
> > I'm handcrafting an extension in C
> >
> > If have defined both a str() and a repr() function and when I execute
> > either repr(obj), or str(obj) I get what I expect
> >
> > however if I execute 'print obj' I get the following error
> >
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > SystemError: error return without exception set
> >
> > although 'print str(obj)' works like I think it should
> >
> > Am I mistaken in thinking that  'print str(obj)' should be the same as
> > 'print obj'
> *ALMOST!*  In the PyTypeObject struct, there's a tp_print slot
> that is used by preference, if non-null, when you print something
> and stdout is a real file.  What do you have in tp_print for
> your type?  It's smack in-between tp_dealloc and tp_getattr.
> Alex

More information about the Python-list mailing list