Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   extending python with a C-written dll (http://www.velocityreviews.com/forums/t339476-extending-python-with-a-c-written-dll.html)

Jean-Baptiste PERIN 12-20-2004 02:23 PM

extending python with a C-written dll
 
Hi,

I'm trying to make a windows dll reachable from a python script .. and
I'm encountering troubles during link step ..

I use "lcc" to compile and link
I use python 2.3 under win XP

Here's the compile command which seems to work well :
-----------------------------------------------------
lcclnk.exe -entry inithello -dll -o hello.dll hello.obj -L
"C:\python23\libs" python23.lib


Here's the link command which fails:
------------------------------------
lcclnk.exe -entry inithello -dll -o hello.dll hello.obj -L
"C:\python23\libs" python23.lib


Here are the errors :
---------------------
Error c:\...\plugins\hello.c 14 undefined reference to __imp__Py_BuildValue
Error c:\...\plugins\hello.c 46 undefined reference to __imp__Py_InitModule4



Here's the C code which compile but does not link :
--------------------------------------------------

#include <Python.h>

PyObject *helloHello(PyObject *self, PyObject *args)
{
printf("Hello World!");
return Py_BuildValue("i", 1);
}

PyObject *helloHelloString(PyObject *self, PyObject *args)
{
return Py_BuildValue("s", "Hello World!");
}

static char MHello__doc__[] = "This is a simple Python C extension example";
static char Hello__doc__[] = "Prints in the console";
static char HelloString__doc__[] = "Returns message as string";

PyMethodDef helloMethods[] = {
{"Hello", helloHello, METH_VARARGS, Hello__doc__},
{"HelloString", helloHelloString, METH_VARARGS, HelloString__doc__},
{NULL, NULL}
};

void __declspec(dllexport) inithello()
{
(void)Py_InitModule3("hello", helloMethods, MHello__doc__);
}

Can anyone help me?

Grant Edwards 12-20-2004 02:53 PM

Re: extending python with a C-written dll
 
On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo.fr> wrote:

> I'm trying to make a windows dll reachable from a python script..


FWIW, you can call dll's using the ctypes modules without
mucking around in C. There may be performance reasons to build
a "real" python module, but I haven't run across them yet.

--
Grant Edwards grante Yow! I KAISER ROLL?! What
at good is a Kaiser Roll
visi.com without a little COLE SLAW
on the SIDE?

Jean-Baptiste PERIN 12-20-2004 03:11 PM

Re: extending python with a C-written dll
 
Grant Edwards a écrit :
> On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo.fr> wrote:
>
>
>>I'm trying to make a windows dll reachable from a python script..

>
>
> FWIW, you can call dll's using the ctypes modules without
> mucking around in C. There may be performance reasons to build
> a "real" python module, but I haven't run across them yet.
>


Not really a performance reason .. but mainly because I have a huge code
produced in C and no time to port it in python ..
Moreover .. I need the python program to interact with the C code
...(perfom calls to dll-defined functions)

I don't know ctypes module .. do you really think it can help ?
Do you have a link to quickly learn about it ?

The only thing I found is :
http://www.python.org/workshops/1994...Classes_5.html






Grant Edwards 12-20-2004 03:44 PM

Re: extending python with a C-written dll
 
On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo.fr> wrote:

>>>I'm trying to make a windows dll reachable from a python script..

>>
>> FWIW, you can call dll's using the ctypes modules without
>> mucking around in C. There may be performance reasons to build
>> a "real" python module, but I haven't run across them yet.

>
> Not really a performance reason .. but mainly because I have a
> huge code produced in C and no time to port it in python ..
> Moreover .. I need the python program to interact with the C
> code ..(perfom calls to dll-defined functions)
>
> I don't know ctypes module .. do you really think it can help?


It lets you easily call functions in DLLs.

> Do you have a link to quickly learn about it ?


http://starship.python.net/crew/theller/ctypes/
http://starship.python.net/crew/thel.../tutorial.html

> The only thing I found is :
> http://www.python.org/workshops/1994...Classes_5.html


I think that is unrelated.

--
Grant Edwards grante Yow! I HAVE a towel.
at
visi.com

Jean-Baptiste PERIN 12-20-2004 04:05 PM

Re: extending python with a C-written dll
 
Grant Edwards a écrit :

> On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo.fr> wrote:
>
>
>>>>I'm trying to make a windows dll reachable from a python script..
>>>
>>>FWIW, you can call dll's using the ctypes modules without
>>>mucking around in C. There may be performance reasons to build
>>>a "real" python module, but I haven't run across them yet.

>>
>>Not really a performance reason .. but mainly because I have a
>>huge code produced in C and no time to port it in python ..
>>Moreover .. I need the python program to interact with the C
>>code ..(perfom calls to dll-defined functions)
>>
>>I don't know ctypes module .. do you really think it can help?

>
>
> It lets you easily call functions in DLLs.
>
>
>>Do you have a link to quickly learn about it ?

>
>
> http://starship.python.net/crew/theller/ctypes/
> http://starship.python.net/crew/thel.../tutorial.html
>
>
>>The only thing I found is :
>>http://www.python.org/workshops/1994...Classes_5.html

>
>
> I think that is unrelated.
>


OK .. ctypes looks great .. and I plan to use it for futur purposes

The fact is that the python interpreter I use is not a standard one
It is a python interpreter delivered within a software named Blender.
I don't know whether it is possible or not to add ctypes to it ..(I
don't even have a python shell to perform the setup)

I'm sure it is possible to use dll-built with it ..
So please .. can you tell me where I can find Py_BuildValue and
Py_InitModule4 to link with ?




Grant Edwards 12-20-2004 04:22 PM

Re: extending python with a C-written dll
 
On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo.fr> wrote:

> OK .. ctypes looks great .. and I plan to use it for futur
> purposes
>
> The fact is that the python interpreter I use is not a
> standard one It is a python interpreter delivered within a
> software named Blender. I don't know whether it is possible or
> not to add ctypes to it ..(I don't even have a python shell to
> perform the setup)
>
> I'm sure it is possible to use dll-built with it .. So please
> .. can you tell me where I can find Py_BuildValue and
> Py_InitModule4 to link with ?


Sorry, I don't know anything about doing C lanugage extensions
under Windows.

--
Grant Edwards grante Yow! Boy, am I glad it's
at only 1971...
visi.com

Diez B. Roggisch 12-20-2004 04:29 PM

Re: extending python with a C-written dll
 
> The fact is that the python interpreter I use is not a standard one
> It is a python interpreter delivered within a software named Blender.
> I don't know whether it is possible or not to add ctypes to it ..(I
> don't even have a python shell to perform the setup)
>
> I'm sure it is possible to use dll-built with it ..
> So please .. can you tell me where I can find Py_BuildValue and
> Py_InitModule4 to link with ?


I doubt that adding new modules as dlls is easier than incorporating
pure-python ones or a mixture of both - as ctypes most probably is. After
all, its pythons module loading mechanism that kicks in for all cases.

If ctypes suits your needs outside of blender, I'd go for trying to
incorporate it into the blender interpreter.

--
Regards,

Diez B. Roggisch

Thomas Heller 12-20-2004 04:35 PM

Re: extending python with a C-written dll
 
Jean-Baptiste PERIN <jb_perin@yahoo.fr> writes:

> Grant Edwards a écrit :
>
>> On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo.fr> wrote:
>>
>>>>>I'm trying to make a windows dll reachable from a python script..
>>>>
>>>>FWIW, you can call dll's using the ctypes modules without
>>>>mucking around in C. There may be performance reasons to build
>>>>a "real" python module, but I haven't run across them yet.
>>>
>>>Not really a performance reason .. but mainly because I have a
>>>huge code produced in C and no time to port it in python ..
>>>Moreover .. I need the python program to interact with the C
>>>code ..(perfom calls to dll-defined functions)
>>>
>>>I don't know ctypes module .. do you really think it can help?

>> It lets you easily call functions in DLLs.
>>
>>>Do you have a link to quickly learn about it ?

>> http://starship.python.net/crew/theller/ctypes/
>> http://starship.python.net/crew/thel.../tutorial.html
>>
>>>The only thing I found is :
>>>http://www.python.org/workshops/1994...Classes_5.html

>> I think that is unrelated.
>>

>
> OK .. ctypes looks great .. and I plan to use it for futur purposes
>
> The fact is that the python interpreter I use is not a standard one
> It is a python interpreter delivered within a software named Blender.
> I don't know whether it is possible or not to add ctypes to it ..(I
> don't even have a python shell to perform the setup)


You could try to download the ctypes windows installer, use winzip or
equivalent to unpack the contained files (you need _ctypes.pyd, and the
ctypes directory containing __init__.py), and copy them to some
directory where python can find them.

Thomas

Jean-Baptiste PERIN 12-20-2004 04:54 PM

Re: extending python with a C-written dll
 
thanks for your valuable help .. ctypes is not lost for ever ..I'm
going to explore a little bit .. it may suit my need in the end ..


I had read :

http://www.python.org/dev/doc/devel/ext/intro.html

and eveything looked so simple in it ..
I'm sad to realize that it only works for linux plateform :(

I've tried to build and install ctypes from sources

I ran the following command:

>> python setup.py build


and I got the following message:

>> error: Python was built with version 6 of Visual Studio, and

extensions need to
be built with the same version of the compiler, but it isn't installed.


:( :( :( :( So sad ....

Jean-Baptiste PERIN 12-20-2004 04:55 PM

Re: extending python with a C-written dll
 
Diez B. Roggisch a écrit :

>>The fact is that the python interpreter I use is not a standard one
>>It is a python interpreter delivered within a software named Blender.
>>I don't know whether it is possible or not to add ctypes to it ..(I
>>don't even have a python shell to perform the setup)
>>
>>I'm sure it is possible to use dll-built with it ..
>>So please .. can you tell me where I can find Py_BuildValue and
>>Py_InitModule4 to link with ?

>
>
> I doubt that adding new modules as dlls is easier than incorporating
> pure-python ones or a mixture of both - as ctypes most probably is. After
> all, its pythons module loading mechanism that kicks in for all cases.
>
> If ctypes suits your needs outside of blender, I'd go for trying to
> incorporate it into the blender interpreter.
>



I've tried to build and install ctypes from sources

I ran the following command:

>> python setup.py build


and I got the following message:

>> error: Python was built with version 6 of Visual Studio, and

extensions need to
be built with the same version of the compiler, but it isn't installed.


:( :( :( :( So sad ....



All times are GMT. The time now is 03:43 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.