I need to write an extension for a C function so that I can call it
from python.
C code (myapp.c)
======
typedef unsigned long MY_LONG;
char *DoStuff(char *input, MY_LONG *x) { ... }
so you pass in a string and an array of MY_LONGS...such as
MY_LONGS vals[10] = {....};
char *input = "this is my input";
char *result;
result = DoStuff(input, vals);
....I need to create an extension so that I can call this from Python
such as... import myapp vals = [1,2,3,4,5,6,7,8 ,9,10] input = "this is my input" result = myapp.DoStuff(i nput, vals) print result
ideally the result would be a String, vals would be a list and input
would be a string.
I was hoping to follow along with how to write an extension, as shown
here http://docs.python.org/ext/contents.html.
....but a better example would help.
Thanks. 10 2242
"Java and Swing" wrote: I need to write an extension for a C function so that I can call it from python.
C code (myapp.c) ====== typedef unsigned long MY_LONG;
char *DoStuff(char *input, MY_LONG *x) { ... }
so you pass in a string and an array of MY_LONGS...such as
MY_LONGS vals[10] = {....}; char *input = "this is my input"; char *result;
result = DoStuff(input, vals);
...I need to create an extension so that I can call this from Python such as...
import myapp vals = [1,2,3,4,5,6,7,8 ,9,10] input = "this is my input" result = myapp.DoStuff(i nput, vals) print result ideally the result would be a String, vals would be a list and input would be a string.
$ more module.c
/* $Id$ */
/* a module */
#include "Python.h"
static long*
get_long_array( PyObject* data, int* data_size)
{
int i, size;
long* out;
PyObject* seq;
seq = PySequence_Fast (data, "expected a sequence");
if (!seq)
return NULL;
size = PySequence_Size (seq);
if (size < 0)
return NULL;
if (data_size)
*data_size = size;
out = (long*) PyMem_Malloc(si ze * sizeof(long));
if (!out) {
Py_DECREF(seq);
PyErr_NoMemory( );
return NULL;
}
for (i = 0; i < size; i++)
out[i] = PyInt_AsLong(Py Sequence_Fast_G ET_ITEM(seq, i));
Py_DECREF(seq);
if (PyErr_Occurred ()) {
PyMem_Free(out) ;
out = NULL;
}
return out;
}
static PyObject*
do_stuff(PyObje ct* self, PyObject* args)
{
int i;
long* vals;
int vals_size;
char* my_result;
char* input;
PyObject* vals_in;
if (!PyArg_ParseTu ple(args, "sO:do_stuf f", &input, &vals_in))
return NULL;
vals = get_long_array( vals_in, &vals_size);
if (!vals)
return NULL;
if (vals_size != 10) {
PyErr_SetString (PyExc_ValueErr or, "expected 10 values");
PyMem_Free(vals );
return NULL;
}
/* do stuff */
printf("input = %s\n", input);
for (i = 0; i < 10; i++)
printf("vals[%d] = %ld\n", i, vals[i]);
my_result = "result";
/* done */
PyMem_Free(vals );
return PyString_FromSt ring(my_result) ;
}
static PyMethodDef functions[] = {
{"do_stuff", do_stuff, METH_VARARGS},
{NULL, NULL}
};
PyMODINIT_FUNC initmodule(void )
{
Py_InitModule4(
"module", functions, "my module", NULL, PYTHON_API_VERS ION
);
}
$ more setup.py
# $Id$
# a setup file
from distutils.core import setup, Extension
setup(
name="module",
ext_modules = [Extension("modu le", ["module.c"])]
)
$ python setup.py build_ext -i
....
$ python import module vals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] input = "this is some input" result = module.do_stuff (input, vals)
input = this is some input
vals[0] = 1
vals[1] = 2
vals[2] = 3
vals[3] = 4
vals[4] = 5
vals[5] = 6
vals[6] = 7
vals[7] = 8
vals[8] = 9
vals[9] = 10 result
'result'
</F>
So is "module.c" a new C file or do I add it to my existing, myapp.c?
Fredrik Lundh wrote: seq = PySequence_Fast (data, "expected a sequence"); if (!seq) return NULL;
here's some more information on the PySequence_Fast API:
http://www.effbot.org/zone/python-capi-sequences.htm
</F>
also, I noticed you did python build-ext ....does that mean for any
computer I want to run myapp on, I need to build the extension on it as
well? Or can I build it once and just copy the files to another
computer that has python already installed? If I just copy files,
which files and where would they go?
Thanks!
Java and Swing wrote: So is "module.c" a new C file or do I add it to my existing, myapp.c?
Fredrik Lundh wrote: seq = PySequence_Fast (data, "expected a sequence"); if (!seq) return NULL;
here's some more information on the PySequence_Fast API:
http://www.effbot.org/zone/python-capi-sequences.htm
</F>
Java and Swing wrote: So is "module.c" a new C file or do I add it to my existing, myapp.c?
it's a complete module. if you want it to do something other than printing
the arguments, replace the "do stuff" section with your own code. if you
want to call it something else, rename it. if you want to change the API,
change it. it's not that large; you should be able to figure out what it does
and how it does it in no time at all.
</F>
Fredrik,
I now have this.
myapp.c
--------
#include <string.h>
#include <stdlib.h>
#include "Python.h"
int doStuff(const char *input, const char *d) { ... }
static PyObject *wrap_doStuff(P yObject *self, PyObject *args) {
// get the arguments from Python
int result;
char *input = 0;
char *d = 0;
int ok = PyArg_ParseTupl e(args, "ss", &input, &d);
if (!ok) return 0;
// make the function call
result = doStfuff(input, d);
// return the result
return PyBuildValue("i ", result);
}
static PyMethodDef functions[] =
{
{"PyDoStuff" , wrap_doStuff, METH_VARARGS, "some documentation"} ,
{NULL, NULL}
};
extern PyMODINIT_FUNC initDLLTester(v oid)
{
Py_InitModule4(
"DLLTester" , functions, "my doStfuff function", NULL,
PYTHON_API_VERS ION
);
}
....when I try to compile in Visual C++ 6, I get
Linking...
Creating library Release/DLLTester.lib and object
Release/DLLTester.exp
test.obj : error LNK2001: unresolved external symbol _PyBuildValue
Release/DLLTester.dll : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Any ideas what's happening here?
DLLTester.dll - 2 error(s), 0 warning(s)
Fredrik Lundh wrote: Java and Swing wrote:
So is "module.c" a new C file or do I add it to my existing, myapp.c?
it's a complete module. if you want it to do something other than printing the arguments, replace the "do stuff" section with your own code. if you want to call it something else, rename it. if you want to change the API, change it. it's not that large; you should be able to figure out what it does and how it does it in no time at all.
</F>
...ok I modified wrap_doStuff, so it just returns 0 instead of return
PyBuildValue... .this fixed the problems I had with compiling.
however, when I try using it in Python..I get a SystemError: error
return without exception set.
Anyhow, I need PyBuildValue to work. One other note, the VC++ 6
compiler gives two warnings, one of which is...
"warning C4013: 'PyBuildValue' undefined; assuming extern returning
int"
thanks. from DLLTester import * x = doStuff("1,2,3, 4,5", ",")
Java and Swing wrote: Fredrik, I now have this.
myapp.c -------- #include <string.h> #include <stdlib.h> #include "Python.h"
int doStuff(const char *input, const char *d) { ... }
static PyObject *wrap_doStuff(P yObject *self, PyObject *args) { // get the arguments from Python int result; char *input = 0; char *d = 0; int ok = PyArg_ParseTupl e(args, "ss", &input, &d); if (!ok) return 0;
// make the function call result = doStfuff(input, d);
// return the result return PyBuildValue("i ", result); }
static PyMethodDef functions[] = { {"PyDoStuff" , wrap_doStuff, METH_VARARGS, "some documentation"} , {NULL, NULL} };
extern PyMODINIT_FUNC initDLLTester(v oid) { Py_InitModule4( "DLLTester" , functions, "my doStfuff function", NULL, PYTHON_API_VERS ION );
}
...when I try to compile in Visual C++ 6, I get
Linking... Creating library Release/DLLTester.lib and object Release/DLLTester.exp test.obj : error LNK2001: unresolved external symbol _PyBuildValue Release/DLLTester.dll : fatal error LNK1120: 1 unresolved externals Error executing link.exe.
Any ideas what's happening here?
DLLTester.dll - 2 error(s), 0 warning(s)
Fredrik Lundh wrote: Java and Swing wrote:
So is "module.c" a new C file or do I add it to my existing, myapp.c?
it's a complete module. if you want it to do something other than printing the arguments, replace the "do stuff" section with your own code. if you want to call it something else, rename it. if you want to change the API, change it. it's not that large; you should be able to figure out what it does and how it does it in no time at all.
</F>
On Tue, 2005-10-11 at 15:14, Java and Swing wrote: Anyhow, I need PyBuildValue to work.
Try Py_BuildValue.
HTH,
Carsten Haese.
Carsten..thanks so much...that was it! DUH!
Carsten Haese wrote: On Tue, 2005-10-11 at 15:14, Java and Swing wrote: Anyhow, I need PyBuildValue to work.
Try Py_BuildValue.
HTH,
Carsten Haese. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jose Vicente Nunez Z |
last post by:
Greetings,
I wrote a couple of custom dummy extensions in Python (one a pure Python
and the other a C) and i managed to compile and install them without a
problem:
$ make
python2 setup.py build
running build
|
by: Matthew Hanna |
last post by:
I am writing a short OpenGL application in Visual Basic. I need an
extension that I can't get from the VB side. I create a dll named
glFogCoordf_Vb.dll to do the work for me. The dll fails to see the OpenGL
commands. I am at a loss as to how this can be fixed. Any ideas on how to
fix the problem? The dll is being created in MS Visual C/C++ 6.0 using the
Win32 Dynamic-Link Library Wizard. My code is as follows...
-- ...
|
by: Jack-2 |
last post by:
Hi!
Three days ago I wrote in this group for ask help in the use of
"GetDetailsOf".
I want show the name of a file without the extension, and for this, a member
of this group suggestion me that
I will use this code:
name = fldr.GetDetailsOf(items, 0);
name = name.substring(0, name.lastIndexOf('.'));
|
by: baustin75 |
last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie
only when debugging in php designer 2005
--------------------------------------------------------------------------------
Hello,
I have a very simple problem but cannot seem to figure it out. I have a
very simple php script that sends a test email to myself. When I debug
it in PHP designer, it works with no problems, I get the test email. If
|
by: SStory |
last post by:
Hi all,
I really needed to get the icons associated with each file that I want to
show in a listview.
I used the follow modified code sniplets found on the internet.
I have left in commented code for anyone else who may be looking to do the
same.
| |
by: Rahul |
last post by:
Hi Everybody
I have some problem in my script. please help me. This is script file.
I have one *.inq file. I want run this script in XML files. But this
script errors shows . If u want i am attach this script
files and inq files. I cant understand this error. Please suggest me.
You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online.
Plz....Plz..Plz...
|
by: ameshkin |
last post by:
Hi,
Im pretty new at PHP and need help with something very simple. I wrote
a function which watermarks an image. The function works, but i can't
figure out how to output the image into a file.
I want to feed this function a URL, $image..and at the end of this
code, I want to overwrite the $image file with the new watermarked
$image file. Im getting an error stating that this file cannot be
opened.
|
by: gunimpi |
last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431
********************************************************
VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help
wanted
********************************************************
For this teeny job, please refer to:
http://feeds.reddit.com/feed/8fu/?o=25
|
by: cuties |
last post by:
Hi all....
i'm very new to this programming language. i'm required to fulfill this task in the company i'm doing my practical. i hope i can get guide for my problem...
Here is the script i already wrote but i'm having problem to move forward.
my problem is :
1. how do i assign each checkbox to have equal value with the value of the d_id?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |