Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'url:sourceforge': 0.03; 'static': 0.04; 'base.': 0.05; 'derived': 0.09; 'null,': 0.09; 'subject:create': 0.09; 'subject:instance': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '"""base': 0.16; '"from': 0.16; '"register': 0.16; '*args': 0.16; '-1,': 0.16; '.py': 0.16; 'from:addr:barry': 0.16; 'hello:': 0.16; 'null);': 0.16; 'python3.': 0.16; 'subject:class': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'module': 0.19; 'skip:p 40': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'char': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'class.': 0.26; 'skip:_ 20': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'skip:p 30': 0.29; '0);': 0.29; 'skip:@ 10': 0.30; 'url:mailman': 0.30; '(my': 0.31; 'int,': 0.31; 'null;': 0.31; 'ok.': 0.31; 'struct': 0.31; 'void': 0.31; 'file': 0.32; 'class': 0.32; 'this.': 0.32; 'figure': 0.32; 'url:python': 0.33; 'subject:from': 0.34; "can't": 0.35; 'url:listinfo': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'skip:_ 30': 0.39; 'does': 0.39; 'bill': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'url:mail': 0.40; 'how': 0.40; 'skip:c 50': 0.60; 'skip:a 30': 0.61; 'new': 0.61; 'header:Message-Id:1': 0.63; 'name': 0.63; 'relatively': 0.65; 'to:addr:gmail.com': 0.65; 'mar': 0.68; 'specialize': 0.74; 'heavy': 0.81; '2014,': 0.84; 'abc': 0.84; 'const': 0.84; 'examples.': 0.84; 'lifting': 0.84; 'pytypeobject': 0.84 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 7.2 \(1874\)) Subject: Re: How to create an instance of a python class from C++ From: Barry Scott In-Reply-To: <0ab424e9-3a3f-4111-9f41-ff50ce73d70d@googlegroups.com> Date: Tue, 11 Mar 2014 21:37:34 +0000 Content-Transfer-Encoding: quoted-printable References: <0ab424e9-3a3f-4111-9f41-ff50ce73d70d@googlegroups.com> To: Bill X-Mailer: Apple Mail (2.1874) Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 141 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394579420 news.xs4all.nl 2884 [2001:888:2000:d::a6]:49451 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68249 On 5 Mar 2014, at 00:14, Bill wrote: > Hello: >=20 > I can't figure out how to create an instance > of a python class from 'C++': >=20 Why not use pycxx from http://sourceforge.net/projects/cxx/? This lib does all the heavy lifting for you for both python2 and = python3. Has docs and examples. Barry PyCXX maintainer. > ( I am relatively new to Python so excuse some of > the following. ) >=20 > In a .py file I create an ABC and then specialize it: >=20 > from MyMod import * > from abc import ABCMeta, abstractmethod >=20 > # Declare an abstract base class. > class Base(metaclass=3DABCMeta): > """Base class.""" > @abstractmethod > def description(self): > return "=46rom the base class." >=20 > # Declare a class that inerits from the base. > class Derived(Base): > """Derived class.""" > def description(self): > return "=46rom the Derived." >=20 > # Register the derived class. > RegisterClass(Derived) >=20 > Then from 'C++' (my implementation of RegisterClass) > I try to create an instance >=20 > static PyObject * > RegisterClass( PyObject *, PyObject *args ) { // This gets = called ok. >=20 > PyObject *class_decl; > if( ! PyArg_ParseTuple(args, "O", &class_decl) ) > return NULL; > Py_INCREF(class_decl); >=20 > PyTypeObject *typ =3D class_decl->ob_type; >=20 > // Tried this. > // PyObject *an =3D _PyObject_New(class_decl->ob_type); = assert(an); > // PyObject *desc =3D = PyObject_CallMethod(an,"description",NULL); assert(desc); >=20 > // Tried this. > // PyObject *an =3D PyType_GenericNew((PyTypeObject = *)class_decl->ob_type, NULL, NULL); assert(an); > // assert(class_decl); assert(class_decl->ob_type); = assert(class_decl->ob_type->tp_new); >=20 > // This returns something. > assert(class_decl); assert(class_decl->ob_type); = assert(class_decl->ob_type->tp_new); > PyObject *an_inst =3D = class_decl->ob_type->tp_new(class_decl->ob_type,NULL, NULL); = assert(an_inst); > assert(class_decl->ob_type->tp_init); >=20 > // This crashes. > int ret =3D class_decl->ob_type->tp_init(an_inst,NULL, NULL); = assert(ret =3D=3D 0); > // PyObject_CallMethod(an_inst,"__init__",NULL); > // PyObject *an_inst =3D = PyObject_CallMethod(class_decl,"__new__",NULL); assert(an_inst); >=20 > // Never get here. > PyObject *desc =3D = PyObject_CallMethod(an_inst,"description",NULL); assert(desc); > char *cp =3D _PyUnicode_AsString(desc); > cerr << "Description:" << cp << endl; >=20 > return PyLong_FromLong(0); > } >=20 > static PyMethodDef MyModMethods[] =3D { > { "RegisterClass", RegisterClass, METH_VARARGS, "Register = class." }, > { NULL, NULL, 0, NULL = } > }; >=20 > static struct PyModuleDef MyModMod =3D { > PyModuleDef_HEAD_INIT, > "MyMod", // name of module > NULL, // module documentation, may be NULL > -1, > MyModMethods, > NULL, > NULL, > NULL, > NULL > }; >=20 > PyMODINIT_FUNC > PyInit_MyMod( void ) { > PyObject* m =3D PyModule_Create(&MyModMod); > if( m =3D=3D NULL ) > return NULL; > return m; > } >=20 > int > main( int, char ** ) { >=20 > PyImport_AppendInittab( "MyMod", PyInit_MyMod ); >=20 > Py_Initialize(); >=20 > const char *file_name =3D "z.py"; > FILE *fp =3D fopen(file_name,"r"); > if( fp ) { > PyRun_SimpleFileExFlags(fp,file_name,1,0); > } >=20 > Py_Finalize(); >=20 > return 0; > } > --=20 > https://mail.python.org/mailman/listinfo/python-list >=20