[C++-sig] Accessing Class Member Function from Python

Nagaraju srirangamnagaraju at gmail.com
Sat Jun 9 10:20:37 CEST 2012


Hi All,

Sorry for spamming. I got it worked.

Regards,
Raju.

On Sat, Jun 9, 2012 at 11:28 AM, Nagaraju <srirangamnagaraju at gmail.com>wrote:

> Hi Nat and All,
>
> I have created a TestProject.pyd file and imported it into Python as
> import TestProject
> planet = TestProject.MyClass()
> planet.add(1,1)
>
> When I execute the last statement, Python is giving following error:
> Traceback (most recent call last):
>   File "<pyshell#11>", line 1, in <module>
>     planet.add(int(1),int(1))
> ArgumentError: Python argument types in
>     MyClass.add(MyClass, int, int)
> did not match C++ signature:
>     add(int, int)
>
> Python version is: 2.7
> Can anybody tell me how to resolve this issue?
>
> Thanks in advance.
>
> Regards,
> Raju.
>
>  On Wed, Jun 6, 2012 at 7:08 PM, Nagaraju <srirangamnagaraju at gmail.com>wrote:
>
>> Hi Nat,
>>
>> Thank you very much for the reply.
>> I tried making the target as ".pyd" and I am able to import it in Python
>> script.
>>
>> Thanks again.
>>
>> Regards,
>> Raju.
>>  On Wed, Jun 6, 2012 at 6:57 PM, Nat Linden <nat at lindenlab.com> wrote:
>>
>>> On Wed, Jun 6, 2012 at 6:57 AM, Nagaraju <srirangamnagaraju at gmail.com>
>>> wrote:
>>>
>>> > Thank you very much for your reply. I am sorry if I did not explain
>>> > something clearly.
>>> >
>>> > I am doing as below after implementing the MyClass in the same file:
>>> >
>>> > BOOST_PYTHON_MODULE(hello){
>>> > ...
>>> > }
>>> >
>>> > I am using CDLL from ctypes to load this Test.DLL. Say
>>> > planet = CDLL("Test.DLL").
>>> >
>>> > Now I want to create an object of MyClass and call add function. How
>>> can I
>>> > do this?
>>>
>>> I think you're mixing two different tactics here.
>>>
>>> You can use ctypes to load a plain DLL that publishes extern "C"
>>> functions, and call those functions. But I don't believe it supports
>>> the notion of a Python class defined in that DLL.
>>>
>>> Or you can use Boost.Python to prepare a special DLL that Python will
>>> recognize as an extension module. Such modules can be loaded with
>>> 'import'. In this case you don't use ctypes because the module
>>> contents describe themselves for the Python runtime to know how to use
>>> them directly, classes and methods and functions and data.
>>> Boost.Python is a succinct way to provide such a description.
>>>
>>> But it's my belief that a DLL containing the description compiled from
>>> BOOST_PYTHON_MODULE(hello) must be named "hello.pyd" for the Python
>>> interpreter to successfully import it.
>>>
>>> Once you're able to import hello, you should be able to instantiate
>>> hello.MyClass() and proceed from there.
>>>  _______________________________________________
>>> Cplusplus-sig mailing list
>>> Cplusplus-sig at python.org
>>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120609/687d06c5/attachment.html>


More information about the Cplusplus-sig mailing list