Obtaining the PyObject * of a class

Bronner, Gregory gregory.bronner at lehman.com
Wed Mar 12 16:04:48 CET 2008


The short answer is that you don't need to:
 
Anything that has a 'handle' attribute that is an int should be fine to
pass into the function, and your typemap will be fine (with some
additional checking)
 
 
If you really insist on absolute type safety, however, you'll need an
instance of the Pin class to compare to. Actually, all you need is an
instance of the Pin Type -- you can use PyObject_TypeCheck (if I
recall).
 
Chances are, the name 'Pin' is already in your locals or globals, so you
find the object, grab its type, and then check the type.
Personally, I wouldn't bother.
 
 
 
 

________________________________

From: Cooper, Andrew [mailto:ACooper at cimtek.com] 
Sent: Tuesday, March 11, 2008 7:34 PM
To: python-list at python.org
Subject: RE: Obtaining the PyObject * of a class



The part I'm having problem with is as follows:

 

I want to replace the 'strcmp' below with a call to
PyObject_IsInstance(o, pinClassPyObject)

But I don't know how to get the PyObject* for the Pin class that is
defined in the %pythoncode section

 

 

 

Here is a cut down version of the interface file

 

%module example

 

typedef long pin;

typedef unsigned short ushort;

 

ushort wkDefinePin(char *, char *, pin *OUTPUT);

 

%pythoncode

{

  class Pin(object):

    def __init__(self, name, tic):

      self.handle = wkDefinePin(name,tic)[1]

      return     

}         

 

 

 

%typemap(in) (pin tp)

{

  //

  // TODO: really need to change this to IsInstance type code

  //

  if(strcmp($input->ob_type->tp_name,"Pin") == 0)

  {

    $1 = PyInt_AsLong(PyObject_GetAttrString($input,"handle"));

  }

  else

  {

    PyErr_SetString(PyExc_TypeError,"arg must be type Pin");

    return NULL;

  }

}

 

%typemap(in) (int nCnt_tp, pin *tp)

{

  /* Check if is a list */

  if (PyList_Check($input))

  {

    int i;

    $1 = PyList_Size($input);

    $2 = (pin *) malloc(($1) * sizeof(pin));

    for (i = 0; i < $1; i++) 

    {

      //

      // TODO: really need to change this to IsInstance type code

      //

      PyObject *o = PyList_GetItem($input,i);

      if (strcmp(o->ob_type->tp_name, "Pin") == 0)

      {

        $2[i] = PyInt_AsLong(PyObject_GetAttrString(o,"handle"));

      }

      else 

      {

        PyErr_SetString(PyExc_TypeError,"list must contain Pins");

        free($2);

        return NULL;

      }

    }

    $2[i] = 0;

  }

  else

  {

    PyErr_SetString(PyExc_TypeError,"not a list");

    return NULL;

  }

}

 

-- 

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

 

From: python-list-bounces+acooper=cimtek.com at python.org
[mailto:python-list-bounces+acooper=cimtek.com at python.org] On Behalf Of
Bronner, Gregory
Sent: 11 March 2008 20:52
To: Michael Wieher; python-list at python.org; accooper at cimtek.com
Subject: RE: Obtaining the PyObject * of a class

 

I'd strongly disagree.

SWIG is very useful for wrapping large scale projects in a
non-interfering manner. If you have to generate bindings for 1000+
classes, it is by far the easiest way to do things.

 

 

It isn't clear what you are doing that requires the PyObject*, or which
one you'd like.

 

In general, the output one is found in $result, and $input is input
PyObject for that typemap.

 

 

 

________________________________

From: Michael Wieher [mailto:michael.wieher at gmail.com] 
Sent: Tuesday, March 11, 2008 3:09 PM
To: python-list at python.org
Subject: Re: Obtaining the PyObject * of a class

2 things:

1st. there is a python mailing list for people interested in C++
extension type stuff
2nd. SWIG is useless and overly complicated, its much easier to just
generate your own C++ code by hand, less confusion, and much more
clarity.  I find no value in using anything else.
People complain about the "boilerplate" code, but honestly, copy &
paste, change three characters, and you're done.  And you know exactly
what is happening, how when and why.

2008/3/11, Chris Mellon <arkanes at gmail.com>: 

On Tue, Mar 11, 2008 at 12:13 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>
>  "Cooper, Andrew" <ACooper at cimtek.com> wrote in message
>
news:C03234BF0E2A8C419C4C307D0914EB730251D3A9 at MAILSERVER.cimtek.on.ca...
>
> | Are there any Python C API experts/SWIG experts out there that can
help
>  | me with this issue please.
>
> | I',m currently using SWIG to generate a python interface to a C DLL.
>
>  Some people have switched to using ctypes for this, and many other
SWIG
>  users have stopped reading clp.  But I hope someone answers who can.
>


Using Pyrex or Cython is likely to be much easier than using SWIG for
this.

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

 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - This message is intended only for the personal and
confidential use of the designated recipient(s) named above. If you are
not the intended recipient of this message you are hereby notified that
any review, dissemination, distribution or copying of this message is
strictly prohibited. This communication is for information purposes only
and should not be regarded as an offer to sell or as a solicitation of
an offer to buy any financial product, an official confirmation of any
transaction, or as an official statement of Lehman Brothers. Email
transmission cannot be guaranteed to be secure or error-free. Therefore,
we do not represent that this information is complete or accurate and it
should not be relied upon as such. All information is subject to change
without notice. -------- IRS Circular 230 Disclosure: Please be advised
that any discussion of U.S. tax matters contained within this
communication (including any attachments) is not intended or written to
be used and cannot be used for the purpose of (i) avoiding U.S. tax
related penalties or (ii) promoting, marketing or recommending to
another party any transaction or matter addressed herein. 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.

--------
IRS Circular 230 Disclosure:
Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080312/d55b76cb/attachment.html>


More information about the Python-list mailing list