Problem embedding in small Win32 App

2007-07-27 Thread Brad Johnson
I received no responses yesterday, this is a repost. I'm still stuck on
this one ladies and gentlemen, and I'm sure it's one of those simple
things (isn't it always?)

I am creating a small test application in Windows to test the embedding
of the interpreter in order to execute arbitrary Python statements and
print their results, all via a dialog application.

Strings are sent to the interpreter via:

PyRun_String(CString(*pbstrInput), Py_file_input, _d, _d);

Where _d is my global dictionary.

I would like to capture the output, so I created a small class to extend
Python:

  PyRun_String(class OutputCatcher:\n
\tdef __init__(self):\n
\t\tself.data=''\n
\tdef write(self, stuff):\n
\t\tself.data = stuff\n\n
import sys\n
_outcatcher = OutputCatcher()\n
sys.stdout = _outcatcher\n,
Py_file_input, _d, _d);

Using this class, I thought I could access _outcatcher.data to get the
last thing written to sys.stdout, and this does seem to work using the
following code:

  PyObject* o = PyDict_GetItemString(_d, _outcatcher);
  PyObject* a = PyObject_GetAttrString(o, data);
  ::MessageBox(NULL, PyString_AsString(a), _T(), NULL);

However, it only works twice. On the third try, I get an access
violation trying to access the stdout object (for a print statement).
The pointer appears to be valid, but all of the data has overwritten
with 0xDBDBDBDB, with obvious consequences. 

Thanks for your help in advance.

~Brad Johnson
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem embedding in small Win32 App

2007-07-27 Thread Gabriel Genellina
En Fri, 27 Jul 2007 16:06:32 -0300, Brad Johnson  
[EMAIL PROTECTED] escribió:

   PyObject* o = PyDict_GetItemString(_d, _outcatcher);
   PyObject* a = PyObject_GetAttrString(o, data);
   ::MessageBox(NULL, PyString_AsString(a), _T(), NULL);

 However, it only works twice. On the third try, I get an access
 violation trying to access the stdout object (for a print statement).
 The pointer appears to be valid, but all of the data has overwritten
 with 0xDBDBDBDB, with obvious consequences.

By far, the most common problem extending/embedding Python is to do wrong  
reference counts.
Read http://docs.python.org/ext/refcounts.html again (I hope you already  
did!) and make sure you don't hold a pointer to an object without  
incrementing its reference count, by example.
If you can shrink your application to the minimum code that shows the  
problem, try posting it here.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem embedding in small Win32 App

2007-07-27 Thread Brad Johnson
Gabriel Genellina gagsl-py2 at yahoo.com.ar writes:

 
 By far, the most common problem extending/embedding Python is to do wrong  
 reference counts.
 Read http://docs.python.org/ext/refcounts.html again (I hope you already  
 did!) and make sure you don't hold a pointer to an object without  
 incrementing its reference count, by example.
 If you can shrink your application to the minimum code that shows the  
 problem, try posting it here.
 

Thanks for the advice! Adding the requisite Py_INCREFs has fixed my problem.
Please forgive my ignorance in forgetting this basic piece of information. I did
read the section you provided, and rereading it reminded me that I was only
doing one half of the equation with Py_DECREF.

-- 
http://mail.python.org/mailman/listinfo/python-list