Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #68249
| 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 | <barry@barrys-emacs.org> |
| 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 <barry@barrys-emacs.org> |
| 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 <galaxyblue63@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8067.1394579420.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On 5 Mar 2014, at 00:14, Bill <galaxyblue63@gmail.com> wrote:
> Hello:
>
> I can't figure out how to create an instance
> of a python class from 'C++':
>
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. )
>
> In a .py file I create an ABC and then specialize it:
>
> from MyMod import *
> from abc import ABCMeta, abstractmethod
>
> # Declare an abstract base class.
> class Base(metaclass=ABCMeta):
> """Base class."""
> @abstractmethod
> def description(self):
> return "From the base class."
>
> # Declare a class that inerits from the base.
> class Derived(Base):
> """Derived class."""
> def description(self):
> return "From the Derived."
>
> # Register the derived class.
> RegisterClass(Derived)
>
> Then from 'C++' (my implementation of RegisterClass)
> I try to create an instance
>
> static PyObject *
> RegisterClass( PyObject *, PyObject *args ) { // This gets called ok.
>
> PyObject *class_decl;
> if( ! PyArg_ParseTuple(args, "O", &class_decl) )
> return NULL;
> Py_INCREF(class_decl);
>
> PyTypeObject *typ = class_decl->ob_type;
>
> // Tried this.
> // PyObject *an = _PyObject_New(class_decl->ob_type); assert(an);
> // PyObject *desc = PyObject_CallMethod(an,"description",NULL); assert(desc);
>
> // Tried this.
> // PyObject *an = 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);
>
> // This returns something.
> assert(class_decl); assert(class_decl->ob_type); assert(class_decl->ob_type->tp_new);
> PyObject *an_inst = class_decl->ob_type->tp_new(class_decl->ob_type,NULL, NULL); assert(an_inst);
> assert(class_decl->ob_type->tp_init);
>
> // This crashes.
> int ret = class_decl->ob_type->tp_init(an_inst,NULL, NULL); assert(ret == 0);
> // PyObject_CallMethod(an_inst,"__init__",NULL);
> // PyObject *an_inst = PyObject_CallMethod(class_decl,"__new__",NULL); assert(an_inst);
>
> // Never get here.
> PyObject *desc = PyObject_CallMethod(an_inst,"description",NULL); assert(desc);
> char *cp = _PyUnicode_AsString(desc);
> cerr << "Description:" << cp << endl;
>
> return PyLong_FromLong(0);
> }
>
> static PyMethodDef MyModMethods[] = {
> { "RegisterClass", RegisterClass, METH_VARARGS, "Register class." },
> { NULL, NULL, 0, NULL }
> };
>
> static struct PyModuleDef MyModMod = {
> PyModuleDef_HEAD_INIT,
> "MyMod", // name of module
> NULL, // module documentation, may be NULL
> -1,
> MyModMethods,
> NULL,
> NULL,
> NULL,
> NULL
> };
>
> PyMODINIT_FUNC
> PyInit_MyMod( void ) {
> PyObject* m = PyModule_Create(&MyModMod);
> if( m == NULL )
> return NULL;
> return m;
> }
>
> int
> main( int, char ** ) {
>
> PyImport_AppendInittab( "MyMod", PyInit_MyMod );
>
> Py_Initialize();
>
> const char *file_name = "z.py";
> FILE *fp = fopen(file_name,"r");
> if( fp ) {
> PyRun_SimpleFileExFlags(fp,file_name,1,0);
> }
>
> Py_Finalize();
>
> return 0;
> }
> --
> https://mail.python.org/mailman/listinfo/python-list
>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to create an instance of a python class from C++ Bill <galaxyblue63@gmail.com> - 2014-03-04 16:14 -0800
Re: How to create an instance of a python class from C++ Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-04 18:55 -0700
Re: How to create an instance of a python class from C++ Bill <galaxyblue63@gmail.com> - 2014-03-05 05:42 -0800
Re: How to create an instance of a python class from C++ Grant Edwards <invalid@invalid.invalid> - 2014-03-05 16:08 +0000
Re: How to create an instance of a python class from C++ Alister <alister.ware@ntlworld.com> - 2014-03-05 17:00 +0000
Re: How to create an instance of a python class from C++ Grant Edwards <invalid@invalid.invalid> - 2014-03-05 17:14 +0000
Re: How to create an instance of a python class from C++ Gene Heskett <gheskett@wdtv.com> - 2014-03-05 17:21 -0500
Re: How to create an instance of a python class from C++ Barry Scott <barry@barrys-emacs.org> - 2014-03-11 21:37 +0000
Re: How to create an instance of a python class from C++ Stefan Behnel <stefan_ml@behnel.de> - 2014-03-12 18:10 +0100
csiph-web