Please, someone tell me what's going wrong here. ;>

Michael P. Reilly arcege at shore.net
Tue Jan 11 19:50:34 EST 2000


Jason Maskell <backov at nospam.csolve.net> wrote:
: I posted a while back on embedding, and after scouring deja and reading the
: demos, I can't even get this simple example to work. It's essentially a C++
: Builder'ized version of the simple embedding demo.. PyRun_SimpleString
: returns -1 and foo is never executed.

: TForm1 *Form1;

: __fastcall TForm1::TForm1(TComponent* Owner)
:     : TForm(Owner)
: {
: }

: void initxyzzy(); /* Forward */

: /* A static module */

: static PyObject * xyzzy_foo(PyObject *self, PyObject *args)
: {
:     Form1->Memo1->Lines->Add("Blah");

:  return PyInt_FromLong(42L);
: }

: static PyMethodDef xyzzy_methods[] = {
:  {"foo",  xyzzy_foo, METH_VARARGS},
:  {NULL,  NULL}  /* sentinel */
: };

: void initxyzzy()
: {
:  PyImport_AddModule("xyzzy");
:  Py_InitModule("xyzzy", xyzzy_methods);
: }

: void __fastcall TForm1::Button1Click(TObject *Sender)
: {
:     int a;
:     Py_SetProgramName(Application->ExeName.c_str());
:     Py_Initialize();
:     initxyzzy();
:     a=PyRun_SimpleString("xyzzy.foo()");
:     Py_Finalize();

: }

: void __fastcall TForm1::FormDestroy(TObject *Sender)
: {
:     Py_Finalize();
: }


I would try to stay away from calling the module initializers, they
are designed with a purpose in mind.  Go for the documented route:

void initxyzzy()
  {
    (void)Py_InitModule("xyzzy", xyzzy_methods);
  }

void TForm1::Button1Click(TObject *Sender)
{
    int a;
    Py_SetProgramName(Application->ExeName.c_str());
    Py_Initialize();
    a=PyRun_SimpleString("import xyzzy"); /* import into the namespace */
    a=PyRun_SimpleString("xyzzy.foo()");
    Py_Finalize();
}

The module never got imported into the __main__ module, only
initialized.

  -Arcege




More information about the Python-list mailing list