Convert hex to string

2005-10-03 Thread Java and Swing
I have some output stored in a string that looks like..

>> x
'\x01\xee\x1eo\xc3+\x8b\x83\xfad\xf6E\xaa\x0ea/I\x96\x83\xf5G\xa3\rQ\xfcH\xee\r'


According to, http://docs.python.org/lib/typesseq-strings.html, this is
"Unsigned Hexidecimal (lowercase)".  How can I get this into normal
Ascii so that I can read it?

thanks.

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


Swig and Python

2005-10-04 Thread Java and Swing
I am trying to wrap some C code I have.  Currently I have something
like...

defs.h
---
typedef unsigned long MY_DIGIT;

myapp.c
-
void MakeDigits(MY_DIGIT digits[]) {

}

char* GetString(char *inMessage, MY_DIGIT *digit) {
char *results;
...
...
return results;
}


...ok so my interface for swig looks like..

%module MyApp
%{
#include "math.h"
%}

extern void MakeDigits(MY_DIGIT digits[]);
extern char* GetString(char *inMessage, MY_DIGIT *digit);

%include "cpointer.i"
%pointer_functions(MY_DIGIT, digit_array);

%include "cmalloc.i"
%malloc(int);
%free(int);

...Ok, so I have a couple questions.
1)  How would I call MakeDigits from python?  In the C code I would
normally have something like...
MY_DIGIT tmp[10];
MakeDigits(tmp);

2)  How would I call GetString?  Again, in C I would have...
char *ptr;
char msg[100] = "Hello World";
MY_DIGIT x = 98;
ptr = GetString(msg, x);

3) Did I define MY_DIGIT correctly in my SWIG interface file?

4) If I try the following Python code...
x = new_digit_array()
print x
   then Python.exe dies...any idea?

thanks for the help.  I am trying to find my answers int he SWIG
docs...either i haven't found it or I didn't understand when I came
across it.

thanks in the mean time.

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


SWIG - python.exe crash

2005-10-04 Thread Java and Swing
When I try to print an object in Python, python.exe crashes.   Below
are my files.

defs.h
--
typedef unsigned long MY_DIGIT;


myapp.c
--
void MakeDigits(MY_DIGIT digits[]) {

}

char* GetString(char *inMessage, MY_DIGIT *digit) {
char *results;
...
...
return results;
}


MyApp.i
--
%module MyApp
%{
#include "math.h"
%}

extern void MakeDigits(MY_DIGIT digits[]);
extern char* GetString(char *inMessage, MY_DIGIT *digit);

/* allows for the creation of a MY_DIGIT array */
%inline %{
 MY_DIGIT *my_digit_array(int size) {
  return (MY_DIGIT *) malloc(size * sizeof(int));
 }
%}


...I have two questions.

1) One, I call MakeDigits which should populate the "digits" array with
some values.  It seems the first time I try it the digits array is
populated.  Then if I try to re-use the digits array i get some garbage
output.  But if I recall MakeDigits...I can then re-use the digits
array over and over without anymore problems?  Any idea?
here is how I was trying it...
digits = my_digit_array(10)
MakeDigits(digits)

2) When I try to print the "my_digit_array" object, python.exe
crashes...below is the code I use to cause this problem: (within
python)
digits = my_digit_array(10)
print digits

I am using Python 2.4.1, Swig 1.3.25 on Win XP Pro.

I tried sending a message on the SWIG mailing list...but so far it
hasn't shown up on the list...I've sent it three times now.  I'll keep
trying.

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


Call C functions from Python

2005-10-04 Thread Java and Swing
Is there some other way, besides SWIG, which will allow me to call
functions inside an Ansi C DLL?

Example (C):

defs.h
---
typedef unsigned long MY_DIGIT;

myapp.c
-
#include "defs.h"

char *DoSomeStuff(char *input, MY_DIGIT *digits) {
...
}


..thats an example of something I would like to call from Python.

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


Re: Call C functions from Python

2005-10-04 Thread Java and Swing
ok i got ctypes...now i try

>> from ctypes import *
>> myApp = CDLL("C:\\myapp.dll")

..now how can I call functions on in myapp.dll?  From the tutorial I am
not sure..i try, dir(cdll.myApp) and dir(myApp)..but don't see my
functions listed.

thanks

Grant Edwards wrote:
> On 2005-10-04, Java and Swing <[EMAIL PROTECTED]> wrote:
>
> > Is there some other way, besides SWIG, which will allow me to call
> > functions inside an Ansi C DLL?
>
> ctypes
>
> --
> Grant Edwards   grante Yow!  Now KEN and BARBIE
>   at   are PERMANENTLY ADDICTED to
>visi.comMIND-ALTERING DRUGS...

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


Re: Call C functions from Python

2005-10-05 Thread Java and Swing
I used, myApp = CDLL("C:...") ...as I saw it in one of the ctypes
samples.

Anyhow, I tried...

myApp = cdll.LoadLibrary("C:\\myapp.dll")
myApp.AddNumbers(1, 4)

..I get an error...

AttributeError: function 'AddNumbers' not found

...myapp certainly has AddNumbers.

Grant Edwards wrote:
> On 2005-10-04, Java and Swing <[EMAIL PROTECTED]> wrote:
> > ok i got ctypes...now i try
> >
> >>> from ctypes import *
> >>> myApp = CDLL("C:\\myapp.dll")
>
> I've never seen that sort of usage before.  I don't know what
> CDLL does, and I can't find it in the docs anywhere.
>
> Based on my reading of the tutorial, I would have tried eitehr
>
>   myApp = cdll.myapp
> or
>   myApp = cdll.LoadLibrary("C:/myapp.dll")
>
> > ..now how can I call functions on in myapp.dll? From the
> > tutorial I am not sure..
>
> Assuming CDLL did something equivalent to cdll.LoadLibrary(),
> I'd try:
>
>myApp.FuncName()
>
> I've always done it the way it's done in the tutorial:
>
>   mylib = windll.Lib_Name
>   mylib.myFuncName()
>
> > i try, dir(cdll.myApp) and dir(myApp)..but don't see my
> > functions listed.
>
> I don't think dir() works on dll's like that.  I certainly
> don't see it mentioned in the tutorial.  What happened when you
> tried calling the function the way the tutorial does?
>
>   myapp = cdll.myapp
>   myapp.MyFunc()
>
> --
> Grant Edwards   grante Yow!  Yow! Is my fallout
>   at   shelter termite proof?
>visi.com

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


question about output

2005-10-05 Thread Java and Swing
i have printed out some numbers and they look like

10944800e
10952560d

...if i want to later assign this type of number to variable how can i
do it?

for example i can't do...

>> x = 10944800e

..since it says "invalid token" on the "e".

thanks.

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


Re: Call C functions from Python

2005-10-05 Thread Java and Swing
i tried...

>> from ctypes import *
>> myapp = cdll.LoadLibrary("c:\\myapp.dll")
>> dumpbin /exports myapp.pyd

i get, SyntaxError: invalid syntax with it pointing at the first "p" in
myapp.pyd.

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


Re: question about output

2005-10-05 Thread Java and Swing
doh!  nevermind, my original output had e and d in it.

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


Re: Call C functions from Python

2005-10-05 Thread Java and Swing
i dont have a myapp.pyd ...i have myapp.c, or are u suggesting I dump
the dll? or the swig generated python file?

the swig generated python file only has .py and .pyc.

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


Swig and pointers

2005-10-05 Thread Java and Swing
I've tried sending a email to the swig mailing list 3 times...but it
never seems to get it...not sure what is going on, still looking into
it.  Until then...

I now have a C function such as...

int doIt(char *a, MY_DIGIT **digit) {
   ...
}

so I am trying to figure out how to pass in that "digit" pointer.

My SWIG interface file looks like...

extern int doIt(char *a, MY_DIGIT **digit);
%include cpointer.i
%pointer_functions(MY_DIGIT, md_prt);
%typemap(in) (char *a, MY_DIGIT **argv) {
  /* Check if is a list */
  if (PyList_Check($input)) {
int i;
$1 = PyList_Size($input);
$2 = (MY_DIGIT **) malloc(($1+1)*sizeof(long *));
for (i = 0; i < $1; i++) {
  PyObject *o = PyList_GetItem($input,i);
  if (PyString_Check(o))
$2[i] = PyString_AsString(PyList_GetItem($input,i));
  else {
PyErr_SetString(PyExc_TypeError,"list must contain strings");
free($2);
return NULL;
  }
}
$2[i] = 0;
  } else {
PyErr_SetString(PyExc_TypeError,"not a list");
return NULL;
  }
}

%typemap(freearg) (char *a, MY_DIGIT **argv) {
  free((MY_DIGIT *) $2);
}


..from Python I am trying

>> ptr = []
>> doIt("blah", ptr)

I thought this was the correct approach as described here:
http://www.swig.org/Doc1.3/SWIGDocumentation.html#Python_nn59

However, python comes back and says "TypeError: argument number 2: a
'MY_DIGIT **' is expected, 'list([])' is received"

..any ideas?  Thanks for all the help.  The python community has been
great.

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


Re: Swig and pointers

2005-10-06 Thread Java and Swing
im sorry, why would it be md_ptr?  what is md_ptr?

i tried..
%include cpointer.i
%pointer_functions(MY_DIGIT, digit_ptr)

ptr = new_digit_ptr()
doIt("a message", ptr)
...doesnt work..still needs a DIGIT **

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


Re: Swig and pointers

2005-10-06 Thread Java and Swing
(reposting, i was just told how I should properly reply via
groups.google.com)

im sorry, why would it be md_ptr?  what is md_ptr?

i tried..
%include cpointer.i
%pointer_functions(MY_DIGIT, digit_ptr)

ptr = new_digit_ptr()
doIt("a message", ptr)
...doesnt work..still needs a DIGIT **


Miki Tebeka wrote:
> Hello Java,
>
> > ...
> > extern int doIt(char *a, MY_DIGIT **digit);
> > %include cpointer.i
> > %pointer_functions(MY_DIGIT, md_prt);
> Don't you mean md_ptr?
>
> > %typemap(in) (char *a, MY_DIGIT **argv) {
> >   /* Check if is a list */
> >   if (PyList_Check($input)) {
> > int i;
> > $1 = PyList_Size($input);
> > $2 = (MY_DIGIT **) malloc(($1+1)*sizeof(long *));
> > for (i = 0; i < $1; i++) {
> >   PyObject *o = PyList_GetItem($input,i);
> >   if (PyString_Check(o))
> > $2[i] = PyString_AsString(PyList_GetItem($input,i));
> >   else {
> > PyErr_SetString(PyExc_TypeError,"list must contain strings");
> > free($2);
> > return NULL;
> >   }
> > }
> > $2[i] = 0;
> >   } else {
> > PyErr_SetString(PyExc_TypeError,"not a list");
> > return NULL;
> >   }
> > }
> >
> > %typemap(freearg) (char *a, MY_DIGIT **argv) {
> >   free((MY_DIGIT *) $2);
> > }
> >
> >
> > ..from Python I am trying
> >
> > >> ptr = []
> > >> doIt("blah", ptr)
> >
> > I thought this was the correct approach as described here:
> > http://www.swig.org/Doc1.3/SWIGDocumentation.html#Python_nn59
> >
> > However, python comes back and says "TypeError: argument number 2: a
> > 'MY_DIGIT **' is expected, 'list([])' is received"
> >
> > ..any ideas?
> Didn't check it but from http://www.swig.org/Doc1.3/Library.html it looks
> like you need to do:
> ptr = new_md_prt()
>
> HTH.
> --
> 
> Miki Tebeka <[EMAIL PROTECTED]>
> http://tebeka.bizhat.com
> The only difference between children and adults is the price of the toys

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


SWIG + print value of pointer

2005-10-07 Thread Java and Swing
I am using SWIG to wrap a C application for use in Python.

C code
==
// returns a pointer to an array of long values in the string, "input"
// MY_DIGIT is a typedef such as, typedef unsigned long MY_DIGIT;
MY_DIGIT *Split(char *input) { ... }

..I build a DLL named, _MyApp.dll

SWIG interface
==
%module MyApp
extern MY_DIGIT *Split(char *input, char *delimiters);

%include cpointer.i
%pointer_functions(MY_DIGIT, md_ptr);

Python
=
>> from MyApp import *
>> msg = "5,7,1,0,4,6,9"
>> delims = ","
>> results = Split(msg, delims)
>> str(results)
'_401ba700_p_MY_DIGIT'
>> repr(results)
''
>> print results

...when I print "results" python.exe crashes

...any idea why it crashes?  Why can't I print it?  FYI, if it matters,
I do the same steps in the main method of my C code and there is no
problem.

thanks.

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


Help creating extension for C function

2005-10-07 Thread Java and Swing
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(input, 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.

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


Re: Help creating extension for C function

2005-10-11 Thread Java and Swing
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
> 
> 

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


Re: Help creating extension for C function

2005-10-11 Thread Java and Swing
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
> > 
> > 

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


Wrapper function

2005-10-11 Thread Java and Swing
I am having trouble with a wrapper function...

My C code looks like
---
#include 
#include 
#include "Python.h"

int doStuff(const char *input, const char *d) {...}

PyObject *wrap_doStuff(PyObject *, PyObject *args) {
int result;
char *input = 0;
char *d = 0;
int ok = PyArg_ParseTuple(args, "ss", &input, &d);
if (!ok) return 0;

result = doStuff(input, d);

return PyBuildValue("i", result);
}

...when I try to compile this I get
"error C2055: expected formal parameter list, not a type list"  and it
points to line 31...which is the line "PyObject *wrap_doStuff(...)".

I am learning based on the following link:
http://wiki.cacr.caltech.edu/danse/index.php/Lots_more_details_on_writing_wrappers

any ideas?  Note, if I remove the wrapper function, it compiles fine
(so its not an issue with Python.h...i don't think), also I am using
Visual C++ 6
on Win xp.

thanks.

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


Re: Wrapper function

2005-10-11 Thread Java and Swing
Diez, yes you were right!  But I have other problems now.

I now have...

#include 
#include 
#include "Python.h"

int doStuff(const char *input, const char *d) {...}

static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
int result;
char *input = 0;
char *d = 0;
int ok = PyArg_ParseTuple(args, "ss", &input, &d);
if (!ok) return 0;

result = doStuff(input, d);

return PyBuildValue("i", result);
}

static PyMethodDef myMethods[] =
{
{"PyDoStuff", wrap_doStuff, METH_VARARGS, "some documentation"},
{NULL, NULL}
};

extern PyMODINIT_FUNC initMyDLL(void)
{
Py_InitModule4(
"MyDLL", myMethods, "my doStuff function", NULL,
PYTHON_API_VERSION
);

}

When I compile, I get two warnings..which are ok.  Then when I build my
DLL I get..

Linking...
   Creating library Release/MyDLL.lib and object Release/MyDLL.exp
test.obj : error LNK2001: unresolved external symbol _PyBuildValue
Release/MyDLL.dll : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

MyDLL.dll - 2 error(s), 0 warning(s)

..Any ideas?

Diez B. Roggisch wrote:
> > PyObject *wrap_doStuff(PyObject *, PyObject *args) {
>   ^
> I guess you need a parameter name here
> 
> 
> Diez

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


Re: Wrapper function

2005-10-11 Thread Java and Swing
So, I write the C code...as shown previously.

Then, I do something like...

c:\> python
>> from distutils.core import setup, Extension
>>
>>setup(
>>name="DLLTester",
>>ext_modules = [Extension("DLLTester", ["test.c"])]
>>)

c:\> python setup.py build_ext -i

..is that it?  If so, then it's ok that I get those 2 errors when
trying to build in visual c++?

thanks

Robert Kern wrote:
> Java and Swing wrote:
>
> > When I compile, I get two warnings..which are ok.  Then when I build my
> > DLL I get..
> >
> > Linking...
> >Creating library Release/MyDLL.lib and object Release/MyDLL.exp
> > test.obj : error LNK2001: unresolved external symbol _PyBuildValue
> > Release/MyDLL.dll : fatal error LNK1120: 1 unresolved externals
> > Error executing link.exe.
> >
> > MyDLL.dll - 2 error(s), 0 warning(s)
> >
> > ..Any ideas?
>
> Are you using distutils to build your extension module? You should be.
>
> --
> Robert Kern
> [EMAIL PROTECTED]
>
> "In the fields of hell where the grass grows high
>  Are the graves of dreams allowed to die."
>   -- Richard Harter

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


Re: Help creating extension for C function

2005-10-11 Thread Java and Swing
Fredrik,
  I now have this.

myapp.c

#include 
#include 
#include "Python.h"

int doStuff(const char *input, const char *d) { ... }

static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
// get the arguments from Python
int result;
char *input = 0;
char *d = 0;
int ok = PyArg_ParseTuple(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(void)
{
Py_InitModule4(
"DLLTester", functions, "my doStfuff function", NULL,
PYTHON_API_VERSION
);

}

...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.
> 
> 

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


Re: Help creating extension for C function

2005-10-11 Thread Java and Swing
..ok I modified wrap_doStuff, so it just returns 0 instead of return
PyBuildValuethis 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 
> #include 
> #include "Python.h"
>
> int doStuff(const char *input, const char *d) { ... }
>
> static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
>   // get the arguments from Python
>   int result;
>   char *input = 0;
>   char *d = 0;
>   int ok = PyArg_ParseTuple(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(void)
> {
> Py_InitModule4(
> "DLLTester", functions, "my doStfuff function", NULL,
> PYTHON_API_VERSION
> );
>
> }
>
> ...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.
> > 
> > 

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


Re: Help creating extension for C function

2005-10-11 Thread Java and Swing
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.

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


Re: Help creating extension for C function

2005-10-11 Thread Java and Swing
quick question...how about returning an array of longs?  ..or in my
case I have a pointer to an array of longs.

Thanks


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.

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


C Extension - return an array of longs or pointer?

2005-10-12 Thread Java and Swing
Hi,
   I have been posting about writing a C extension for Python...so far,
so good.  At least for the "simple" functions that I need to wrap.

Ok, my c function looks like...

MY_NUM *doNumberStuff(const char *in, const char *x) { ... }

MY_NUM is defined as, typedef unsigned long MY_NUM;  (not sure if that
matters, or can i just create a wrapper which handles longs?)

anyhow..for my wrapper I have this..

static PyObject *wrap_doNumberStuff(PyObject *self, PyObject args) {
char *in = 0;
char *x = 0;
long *result = 0;
int ok = PyArg_ParseTuple(args, "ss", &in, &x);
if (!ok) return 0;

result = doNumberStuff(in, x);

return Py_BuildValue("l", result);
}

...my question is...in the c code, result is a pointer to an array of
longs, how can I get the returned result to be a list or something
similar to an array in Python?

...I also have a function which returns a character array (denoted by a
char *)...would it work the same as the previous question?

Thanks!!

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


C Wrapper Function, crashing Python?

2005-10-12 Thread Java and Swing
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
// this will store the result in a Python object
PyObject *finalResult;

// get arguments from Python
char *result = 0;
char *in= 0;
char *aString = 0;
char *bString = 0;
MY_NUM *a;
MY_NUM *b;
int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
if (!ok) return 0;

// do work to get a and b
// count - returns an int;  GetVal - returns a char *
a = GetVal(aString, count(aString, ","));
b = GetVal(bString, count(bString, ","));

// make function call, which returns a char *
result = doStuff(in, a, b);

// save result in Python string
finalResult = PyString_FromString(result);

// free memory
PyMem_Free(result);
PyMem_Free(a);
PyMem_Free(b);

// return the result as a Python string
return finalResult;
}

...from python I can call this function 4 times...works fine.  WHen I
call it for the fifth time python.exe crashes.  im thinking some memory
problem in the wrapper function perhaps...but I am not sure.  The
actually C function, doStuff can be called 5, 6,7...N times without a
problem
so i know its gotta be my wrapper.

Any ideas?  Thanks!

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


Re: C Extension - return an array of longs or pointer?

2005-10-12 Thread Java and Swing
Interesting...thanks.  Any good tutorials out there, other than the
python doc for ext?


thanks.

Brandon K wrote:
> All the veteran programmers out there can correct me, but the way I did
> it in my extension was this:
>
> static PyObject *wrap_doNumberStuff(PyObject* self, PyObject* args)
> {
>   char* in = 0;
>   char* x = 0;
>   long* result = 0;
>   int i = 0;
>   PyObject* py = PyTuple_New()
>   int ok = PyArg_ParseTuple(args,"ss",&in, &x);
>   if(!ok) return NULL;
>
>   result = doNumberStuff(in,x):
>   len = sizeof(result)/sizeof(long)
>   for(i;i < len; i++)
>   PyTuple_SET_ITEM(py, i,Py_BuildValue("l",*result[i])
> }
>
> Simple enough idea...i'm not quite sure if I've done everything
> correctly with the pointers, but I'm sure you can figure that out, the
> algorithm is simple enough.
>
> > Hi,
> >I have been posting about writing a C extension for Python...so far,
> > so good.  At least for the "simple" functions that I need to wrap.
> >
> > Ok, my c function looks like...
> >
> > MY_NUM *doNumberStuff(const char *in, const char *x) { ... }
> >
> > MY_NUM is defined as, typedef unsigned long MY_NUM;  (not sure if that
> > matters, or can i just create a wrapper which handles longs?)
> >
> > anyhow..for my wrapper I have this..
> >
> > static PyObject *wrap_doNumberStuff(PyObject *self, PyObject args) {
> > char *in = 0;
> > char *x = 0;
> > long *result = 0;
> > int ok = PyArg_ParseTuple(args, "ss", &in, &x);
> > if (!ok) return 0;
> >
> > result = doNumberStuff(in, x);
> >
> > return Py_BuildValue("l", result);
> > }
> >
> > ...my question is...in the c code, result is a pointer to an array of
> > longs, how can I get the returned result to be a list or something
> > similar to an array in Python?
> >
> > ...I also have a function which returns a character array (denoted by a
> > char *)...would it work the same as the previous question?
> >
> > Thanks!!
> >
>
>
> == Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups 
> ==
> Get Anonymous, Uncensored, Access to West and East Coast Server Farms!
> == Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM 
> ==

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


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Java and Swing
update:
if I use C's free(result), free(a) free(b) instead of PyMem_Free...I
only get one successfuly use/call of doStuff.

i.e.
// this works
doStuff(...)

// python crashes here
doStuff(...)

Java and Swing wrote:
> static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
>   // this will store the result in a Python object
>   PyObject *finalResult;
>
>   // get arguments from Python
>   char *result = 0;
>   char *in= 0;
>   char *aString = 0;
>   char *bString = 0;
>   MY_NUM *a;
>   MY_NUM *b;
>   int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
>   if (!ok) return 0;
>
>   // do work to get a and b
>   // count - returns an int;  GetVal - returns a char *
>   a = GetVal(aString, count(aString, ","));
>   b = GetVal(bString, count(bString, ","));
>
>   // make function call, which returns a char *
>   result = doStuff(in, a, b);
>
>   // save result in Python string
>   finalResult = PyString_FromString(result);
>
>   // free memory
>   PyMem_Free(result);
>   PyMem_Free(a);
>   PyMem_Free(b);
>
>   // return the result as a Python string
>   return finalResult;
> }
>
> ...from python I can call this function 4 times...works fine.  WHen I
> call it for the fifth time python.exe crashes.  im thinking some memory
> problem in the wrapper function perhaps...but I am not sure.  The
> actually C function, doStuff can be called 5, 6,7...N times without a
> problem
> so i know its gotta be my wrapper.
> 
> Any ideas?  Thanks!

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


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Java and Swing
one more update...

if I remove PyMem_Free and free(...) ...so no memory clean up...I can
still only call doStuff 4 times, the 5th attemp crashes Python.

Java and Swing wrote:
> update:
> if I use C's free(result), free(a) free(b) instead of PyMem_Free...I
> only get one successfuly use/call of doStuff.
>
> i.e.
> // this works
> doStuff(...)
>
> // python crashes here
> doStuff(...)
>
> Java and Swing wrote:
> > static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
> > // this will store the result in a Python object
> > PyObject *finalResult;
> >
> > // get arguments from Python
> > char *result = 0;
> > char *in= 0;
> > char *aString = 0;
> > char *bString = 0;
> > MY_NUM *a;
> > MY_NUM *b;
> > int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
> > if (!ok) return 0;
> >
> > // do work to get a and b
> > // count - returns an int;  GetVal - returns a char *
> > a = GetVal(aString, count(aString, ","));
> > b = GetVal(bString, count(bString, ","));
> >
> > // make function call, which returns a char *
> > result = doStuff(in, a, b);
> >
> > // save result in Python string
> > finalResult = PyString_FromString(result);
> >
> > // free memory
> > PyMem_Free(result);
> > PyMem_Free(a);
> > PyMem_Free(b);
> >
> > // return the result as a Python string
> > return finalResult;
> > }
> >
> > ...from python I can call this function 4 times...works fine.  WHen I
> > call it for the fifth time python.exe crashes.  im thinking some memory
> > problem in the wrapper function perhaps...but I am not sure.  The
> > actually C function, doStuff can be called 5, 6,7...N times without a
> > problem
> > so i know its gotta be my wrapper.
> > 
> > Any ideas?  Thanks!

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


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Java and Swing
Antoon,
   I just saw that to.  I updated the code like so...

static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
// this will store the result in a Python object
PyObject *finalResult;

// get arguments from Python
char *result = 0;
char *in= 0;
char *aString = 0;
char *bString = 0;
MY_NUM *a = (MY_NUM *) PyMem_Malloc((20 * sizeof(MY_NUM) + 1);  //
the array will be 20 long
MY_NUM *b = (MY_NUM *) PyMem_Malloc((20 * sizeof(MY_NUM) + 1); //
the array will be 20 long
int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
if (!ok) return 0;

// do work to get a and b
// count - returns an int;  GetVal - returns a MY_NUM * (a pointer
to a MY_NUM array)
a = GetVal(aString, count(aString, ","));
b = GetVal(bString, count(bString, ","));

// make function call, which returns a char *
result = doStuff(in, a, b);

// save result in Python string
finalResult = PyString_FromString(result);

// free memory
PyMem_Free(a);
PyMem_Free(b);
free(aString);
free(bString);
free(result);

// return the result as a Python string
return finalResult;
  }

..as you can see, i malloc'ed memory, and free'd the memory.  However,
I still have python crashing (after only 3 successful calls to
doStuff).  And yes, doStuff is a plain C function...nothing related to
Python.


Antoon Pardon wrote:
> Op 2005-10-12, Java and Swing schreef <[EMAIL PROTECTED]>:
> > static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
> > // this will store the result in a Python object
> > PyObject *finalResult;
> >
> > // get arguments from Python
> > char *result = 0;
> > char *in= 0;
> > char *aString = 0;
> > char *bString = 0;
> > MY_NUM *a;
> > MY_NUM *b;
> > int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
> > if (!ok) return 0;
> >
> > // do work to get a and b
> > // count - returns an int;  GetVal - returns a char *
> > a = GetVal(aString, count(aString, ","));
> > b = GetVal(bString, count(bString, ","));
> >
> > // make function call, which returns a char *
> > result = doStuff(in, a, b);
> >
> > // save result in Python string
> > finalResult = PyString_FromString(result);
> >
> > // free memory
> > PyMem_Free(result);
> > PyMem_Free(a);
> > PyMem_Free(b);
> >
> > // return the result as a Python string
> > return finalResult;
> > }
> >
> > ...from python I can call this function 4 times...works fine.  WHen I
> > call it for the fifth time python.exe crashes.  im thinking some memory
> > problem in the wrapper function perhaps...but I am not sure.  The
> > actually C function, doStuff can be called 5, 6,7...N times without a
> > problem
> > so i know its gotta be my wrapper.
> >
> > Any ideas?  Thanks!
>
> Well assuming your doStuff is a C function that knows nothing of python.
> it might be the PyMem_Free(result).
> http://docs.python.org/api/memoryInterface.html says the following:
>
> void PyMem_Free(void *p)
> Frees the memory block pointed to by p, which must have been
> returned by a previous call to PyMem_Malloc() or PyMem_Realloc().
> Otherwise, or if PyMem_Free(p) has been called before, undefined
> behavior occurs. If p is NULL, no operation is performed.
>
> But your result wasn't allocated by a PyMem_Malloc, it was returned
> to you by a C function.
> 
> -- 
> Antoon Pardon

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


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Java and Swing
Antoon,
   I just saw that to.  I updated the code like so...

static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
// this will store the result in a Python object
PyObject *finalResult;

// get arguments from Python
char *result = 0;
char *in= 0;
char *aString = 0;
char *bString = 0;
MY_NUM *a = (MY_NUM *) PyMem_Malloc((20 * sizeof(MY_NUM) + 1);  //
the array will be 20 long
MY_NUM *b = (MY_NUM *) PyMem_Malloc((20 * sizeof(MY_NUM) + 1); //
the array will be 20 long
int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
if (!ok) return 0;

// do work to get a and b
// count - returns an int;  GetVal - returns a MY_NUM * (a pointer
to a MY_NUM array)
a = GetVal(aString, count(aString, ","));
b = GetVal(bString, count(bString, ","));

// make function call, which returns a char *
result = doStuff(in, a, b);

// save result in Python string
finalResult = PyString_FromString(result);

// free memory
PyMem_Free(a);
PyMem_Free(b);
free(aString);
free(bString);
free(result);

// return the result as a Python string
return finalResult;
  }

..as you can see, i malloc'ed memory, and free'd the memory.  However,
I still have python crashing (after only 3 successful calls to
doStuff).  And yes, doStuff is a plain C function...nothing related to
Python.


Antoon Pardon wrote:
> Op 2005-10-12, Java and Swing schreef <[EMAIL PROTECTED]>:
> > static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
> > // this will store the result in a Python object
> > PyObject *finalResult;
> >
> > // get arguments from Python
> > char *result = 0;
> > char *in= 0;
> > char *aString = 0;
> > char *bString = 0;
> > MY_NUM *a;
> > MY_NUM *b;
> > int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
> > if (!ok) return 0;
> >
> > // do work to get a and b
> > // count - returns an int;  GetVal - returns a char *
> > a = GetVal(aString, count(aString, ","));
> > b = GetVal(bString, count(bString, ","));
> >
> > // make function call, which returns a char *
> > result = doStuff(in, a, b);
> >
> > // save result in Python string
> > finalResult = PyString_FromString(result);
> >
> > // free memory
> > PyMem_Free(result);
> > PyMem_Free(a);
> > PyMem_Free(b);
> >
> > // return the result as a Python string
> > return finalResult;
> > }
> >
> > ...from python I can call this function 4 times...works fine.  WHen I
> > call it for the fifth time python.exe crashes.  im thinking some memory
> > problem in the wrapper function perhaps...but I am not sure.  The
> > actually C function, doStuff can be called 5, 6,7...N times without a
> > problem
> > so i know its gotta be my wrapper.
> >
> > Any ideas?  Thanks!
>
> Well assuming your doStuff is a C function that knows nothing of python.
> it might be the PyMem_Free(result).
> http://docs.python.org/api/memoryInterface.html says the following:
>
> void PyMem_Free(void *p)
> Frees the memory block pointed to by p, which must have been
> returned by a previous call to PyMem_Malloc() or PyMem_Realloc().
> Otherwise, or if PyMem_Free(p) has been called before, undefined
> behavior occurs. If p is NULL, no operation is performed.
>
> But your result wasn't allocated by a PyMem_Malloc, it was returned
> to you by a C function.
> 
> -- 
> Antoon Pardon

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


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Java and Swing
Sorry about the double post...

anyhow, after putting in debug statements I found that it was crashing
when it called, free(result)so I removed the free(result).

now it crashes when it gets to, b = GetVal(bString, count(bString,
","));

..any ideas?

Java and Swing wrote:
> Antoon,
>I just saw that to.  I updated the code like so...
>
> static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
>   // this will store the result in a Python object
>   PyObject *finalResult;
>
>   // get arguments from Python
>   char *result = 0;
>   char *in= 0;
>   char *aString = 0;
>   char *bString = 0;
>   MY_NUM *a = (MY_NUM *) PyMem_Malloc((20 * sizeof(MY_NUM) + 1);  //
> the array will be 20 long
>   MY_NUM *b = (MY_NUM *) PyMem_Malloc((20 * sizeof(MY_NUM) + 1); //
> the array will be 20 long
>   int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
>   if (!ok) return 0;
>
>   // do work to get a and b
>   // count - returns an int;  GetVal - returns a MY_NUM * (a pointer
> to a MY_NUM array)
>   a = GetVal(aString, count(aString, ","));
>   b = GetVal(bString, count(bString, ","));
>
>   // make function call, which returns a char *
>   result = doStuff(in, a, b);
>
>   // save result in Python string
>   finalResult = PyString_FromString(result);
>
>   // free memory
>   PyMem_Free(a);
>   PyMem_Free(b);
>   free(aString);
>   free(bString);
>   free(result);
>
>   // return the result as a Python string
>   return finalResult;
>   }
>
> ..as you can see, i malloc'ed memory, and free'd the memory.  However,
> I still have python crashing (after only 3 successful calls to
> doStuff).  And yes, doStuff is a plain C function...nothing related to
> Python.
>
>
> Antoon Pardon wrote:
> > Op 2005-10-12, Java and Swing schreef <[EMAIL PROTECTED]>:
> > > static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
> > >   // this will store the result in a Python object
> > >   PyObject *finalResult;
> > >
> > >   // get arguments from Python
> > >   char *result = 0;
> > >   char *in= 0;
> > >   char *aString = 0;
> > >   char *bString = 0;
> > >   MY_NUM *a;
> > >   MY_NUM *b;
> > >   int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
> > >   if (!ok) return 0;
> > >
> > >   // do work to get a and b
> > >   // count - returns an int;  GetVal - returns a char *
> > >   a = GetVal(aString, count(aString, ","));
> > >   b = GetVal(bString, count(bString, ","));
> > >
> > >   // make function call, which returns a char *
> > >   result = doStuff(in, a, b);
> > >
> > >   // save result in Python string
> > >   finalResult = PyString_FromString(result);
> > >
> > >   // free memory
> > >   PyMem_Free(result);
> > >   PyMem_Free(a);
> > >   PyMem_Free(b);
> > >
> > >   // return the result as a Python string
> > >   return finalResult;
> > > }
> > >
> > > ...from python I can call this function 4 times...works fine.  WHen I
> > > call it for the fifth time python.exe crashes.  im thinking some memory
> > > problem in the wrapper function perhaps...but I am not sure.  The
> > > actually C function, doStuff can be called 5, 6,7...N times without a
> > > problem
> > > so i know its gotta be my wrapper.
> > >
> > > Any ideas?  Thanks!
> >
> > Well assuming your doStuff is a C function that knows nothing of python.
> > it might be the PyMem_Free(result).
> > http://docs.python.org/api/memoryInterface.html says the following:
> >
> > void PyMem_Free(void *p)
> > Frees the memory block pointed to by p, which must have been
> > returned by a previous call to PyMem_Malloc() or PyMem_Realloc().
> > Otherwise, or if PyMem_Free(p) has been called before, undefined
> > behavior occurs. If p is NULL, no operation is performed.
> >
> > But your result wasn't allocated by a PyMem_Malloc, it was returned
> > to you by a C function.
> > 
> > -- 
> > Antoon Pardon

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


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Java and Swing
ok, further digging...I found that in the C function GetVal...it is
crashing where I try to malloc some memory. Note, I have no problems
when running this from C..just from Python using my wrapper.

GetVal looks something like..
MY_NUM *GetVal(const char *in, const int x) {
MY_NUM *results, *returnResults;
results = (MY_NUM *) malloc((x * sizeof(MY_NUM) + 1);
returnResults = results;
// ..do more work..
return returnResults;
}

I put in print statements into the C code and found that it is crashing
at the line where I malloc some space for "results".

...any ideas why this is crashing when calling from Python via C
wrapper?

Java and Swing wrote:
> Sorry about the double post...
>
> anyhow, after putting in debug statements I found that it was crashing
> when it called, free(result)so I removed the free(result).
>
> now it crashes when it gets to, b = GetVal(bString, count(bString,
> ","));
>
> ..any ideas?
>
> Java and Swing wrote:
> > Antoon,
> >I just saw that to.  I updated the code like so...
> >
> > static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
> > // this will store the result in a Python object
> > PyObject *finalResult;
> >
> > // get arguments from Python
> > char *result = 0;
> > char *in= 0;
> > char *aString = 0;
> > char *bString = 0;
> > MY_NUM *a = (MY_NUM *) PyMem_Malloc((20 * sizeof(MY_NUM) + 1);  //
> > the array will be 20 long
> > MY_NUM *b = (MY_NUM *) PyMem_Malloc((20 * sizeof(MY_NUM) + 1); //
> > the array will be 20 long
> > int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
> > if (!ok) return 0;
> >
> > // do work to get a and b
> > // count - returns an int;  GetVal - returns a MY_NUM * (a pointer
> > to a MY_NUM array)
> > a = GetVal(aString, count(aString, ","));
> > b = GetVal(bString, count(bString, ","));
> >
> > // make function call, which returns a char *
> > result = doStuff(in, a, b);
> >
> > // save result in Python string
> > finalResult = PyString_FromString(result);
> >
> > // free memory
> > PyMem_Free(a);
> > PyMem_Free(b);
> > free(aString);
> > free(bString);
> > free(result);
> >
> > // return the result as a Python string
> > return finalResult;
> >   }
> >
> > ..as you can see, i malloc'ed memory, and free'd the memory.  However,
> > I still have python crashing (after only 3 successful calls to
> > doStuff).  And yes, doStuff is a plain C function...nothing related to
> > Python.
> >
> >
> > Antoon Pardon wrote:
> > > Op 2005-10-12, Java and Swing schreef <[EMAIL PROTECTED]>:
> > > > static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
> > > > // this will store the result in a Python object
> > > > PyObject *finalResult;
> > > >
> > > > // get arguments from Python
> > > > char *result = 0;
> > > > char *in= 0;
> > > > char *aString = 0;
> > > > char *bString = 0;
> > > > MY_NUM *a;
> > > > MY_NUM *b;
> > > > int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
> > > > if (!ok) return 0;
> > > >
> > > > // do work to get a and b
> > > > // count - returns an int;  GetVal - returns a char *
> > > > a = GetVal(aString, count(aString, ","));
> > > > b = GetVal(bString, count(bString, ","));
> > > >
> > > > // make function call, which returns a char *
> > > > result = doStuff(in, a, b);
> > > >
> > > > // save result in Python string
> > > > finalResult = PyString_FromString(result);
> > > >
> > > > // free memory
> > > > PyMem_Free(result);
> > > > PyMem_Free(a);
> > > > PyMem_Free(b);
> > > >
> > > > // return the result as a Python string
> > > > return finalResult;
> > > > }
> > > >
> > > > ...from python I can call this function 4 times...works fine.  WHen I
> > > > call it for the fifth time python.exe crashes.  im thinking some memory
> > > > problem in the wrapper 

Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Java and Swing
thanks for the tip, however even when I do not free aString or bString,
i'm still crashing at the malloc in the c function, not the wrapper.


Bernhard Herzog wrote:
> "Java and Swing" <[EMAIL PROTECTED]> writes:
>
> > static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
> [...]
> >   char *aString = 0;
> > char *bString = 0;
> [...]
> > int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
> [...]
> > free(aString);
> > free(bString);
>
> aString and bString are pointers to memory managed by the strings in
> your args tuple.  You must not free them!  The memory is automatically
> managed by Python.
>
>Bernhard
>
> --
> Intevation GmbH http://intevation.de/
> Skencil   http://skencil.org/
> Thuban  http://thuban.intevation.org/

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


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Java and Swing
As far as my C Wrapper functions are concerned...I no longer have the
need for free(...).  I do use PyMem_Free, for structures I allocated by
using PyMem_New(...).

In my C code I do have things such as...

char *foo(const char *in) {
char *tmp;
tmp = (char *) malloc((strlen(in) * sizeof(char)) + 1);
strcpy(tmp, in);
...
...
free(tmp);
return someValue;
}

Is that appropriate?  I was under the impression that when you malloc
memory, you free it when done.

I also have things like...

char *bar(char *in) {
char *results, *finalResults;
results = (char *) malloc(...);
finalResults = results;

while (...) { *results++ = some_val; }

return finalResults;
}

...is that correct?

As I mentioned earlier, when I run these functions from C I have no
troubles...I can run them 5, 10, 15 times, etc.  From Python, using my
wrapper function is when I have trouble.

Thanks in advance.

Bernhard Herzog wrote:
> "Java and Swing" <[EMAIL PROTECTED]> writes:
>
> > thanks for the tip, however even when I do not free aString or bString,
> > i'm still crashing at the malloc in the c function, not the wrapper.
>
> Do you have any more places where you use free incorrectly?  In my
> experience, calling free with invalid values can corrupt the data
> structures used by the memory allocator in such a way that subsequent
> malloc calls crash.
>
>Bernhard
>
> --
> Intevation GmbH http://intevation.de/
> Skencil   http://skencil.org/
> Thuban  http://thuban.intevation.org/

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


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Java and Swing

Bernhard Herzog wrote:
> "Java and Swing" <[EMAIL PROTECTED]> writes:
>
> > char *foo(const char *in) {
> > char *tmp;
> > tmp = (char *) malloc((strlen(in) * sizeof(char)) + 1);
> > strcpy(tmp, in);
> > ...
> > ...
> > free(tmp);
> > return someValue;
> > }
> >
> > Is that appropriate?  I was under the impression that when you malloc
> > memory, you free it when done.
>
> Looks fine.  I hope someValue does not point somewhere into the tmp
> buffer, though.

  someValue doesn't.

>
> > I also have things like...
> >
> > char *bar(char *in) {
> > char *results, *finalResults;
> > results = (char *) malloc(...);
> > finalResults = results;
> >
> > while (...) { *results++ = some_val; }
> >
> > return finalResults;
> > }
>
> Seems OK, too, assuming the results buffer is big enough.

results = (char *) malloc((sizeof(char) * strlen(in) + 1);
..should be big enough.  At any rate, it works four times in a row
before it fails.

I wonder what else or how else to solve this.  Why is malloc'ing
the memory crashing it!?

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


Pass a tuple (or list) to a C wrapper function

2005-10-12 Thread Java and Swing
I have a C function which takes an array of long values..

I understand that I can pass a tuple to a C wrapper function and in the
C wrapper function have..

int ok = PyArg_ParseTuple(args, "s(ll)", &a, &b, &c);

..that's great if my tuple only contained two longs..but what if it
contained 50
would I have to do..

int ok = PyArg_ParseTuple(args, "s(llll)", &a, &b,
&c, &d...) ??

how can I handle this?

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


Re: Pass a tuple (or list) to a C wrapper function

2005-10-13 Thread Java and Swing
Fredrik...I forgot about that...wish Google Groups had a way to quickly
find the topics a user posts.

anyhow, for receiving an object from python..is it

ok = PyArg_ParseTuple(args, "sO", &x, &y);

...is it "sO" or "s0" is it O (as in the letter) or 0 (as in the
number)?  I would think "O" the letter..but it looks like a zero.

thanks

Fredrik Lundh wrote:
> Jeremy Moles wrote:
>
> > Probably what you want to do though is just keep the tuple as is and
> > iterate over it using the PySequence_* protocol:
> >
> > http://docs.python.org/api/sequence.html
>
> I did post a complete and tested example a few days ago, which contained
> code that showed how to do this.  a complete waste of time, of course.
> 
> 

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


Re: Pass a tuple (or list) to a C wrapper function

2005-10-13 Thread Java and Swing
Fredrik,
  ...I tried using your code...

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(size * sizeof(long));
if (!out) {
Py_DECREF(seq);
PyErr_NoMemory();
return NULL;
}

for (i = 0; i < size; i++)
out[i] = PyInt_AsLong(PySequence_Fast_GET_ITEM(seq, i));

Py_DECREF(seq);

if (PyErr_Occurred()) {
PyMem_Free(out);
out = NULL;
}

return out;

}


and I get this error..

C:\project\myapp.c(549) : error C2040: 'get_long_array' : 'long
*(struct _object *,int *)' differs in levels of indirection from 'int
()'

any idea?

Fredrik Lundh wrote:
> Jeremy Moles wrote:
>
> > Probably what you want to do though is just keep the tuple as is and
> > iterate over it using the PySequence_* protocol:
> >
> > http://docs.python.org/api/sequence.html
>
> I did post a complete and tested example a few days ago, which contained
> code that showed how to do this.  a complete waste of time, of course.
> 
> 

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


Re: Pass a tuple (or list) to a C wrapper function

2005-10-13 Thread Java and Swing
I got it.   I had get_long_array placed after the method that was
calling it..
i.e.

void doStuf(...) {
   x = get_long_array(...);
}

static long *get_long_array(PyObject *data, int *data_size) {
...
}

...I put get_long_array before it in my code..and its fine.

Thanks

Java and Swing wrote:
> Fredrik,
>   ...I tried using your code...
>
> 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(size * sizeof(long));
> if (!out) {
> Py_DECREF(seq);
> PyErr_NoMemory();
> return NULL;
> }
>
> for (i = 0; i < size; i++)
> out[i] = PyInt_AsLong(PySequence_Fast_GET_ITEM(seq, i));
>
> Py_DECREF(seq);
>
> if (PyErr_Occurred()) {
> PyMem_Free(out);
> out = NULL;
> }
>
> return out;
>
> }
>
>
> and I get this error..
>
> C:\project\myapp.c(549) : error C2040: 'get_long_array' : 'long
> *(struct _object *,int *)' differs in levels of indirection from 'int
> ()'
>
> any idea?
>
> Fredrik Lundh wrote:
> > Jeremy Moles wrote:
> >
> > > Probably what you want to do though is just keep the tuple as is and
> > > iterate over it using the PySequence_* protocol:
> > >
> > > http://docs.python.org/api/sequence.html
> >
> > I did post a complete and tested example a few days ago, which contained
> > code that showed how to do this.  a complete waste of time, of course.
> > 
> > 

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


When to free memory for C wrapper?

2005-10-13 Thread Java and Swing
I have been posting lately about writing a C wrapper so Python can
access my C functions.

In my wrapper function I have something like...

static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
PyObject *data;
char *result;
long *d;

PyArg_ParseTuple(args, "O:wrap_doStuff", &data);

d = get_long_array(data);

result = doStuff(d);

return PyString_FromString(result);
}

Now, do I need to use PyMem_Free to free up the "PyObject *data"?

Thanks.

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


Re: When to free memory for C wrapper?

2005-10-13 Thread Java and Swing
One other thing, I was reading about Py_DECREF...and I see it says

"A safe approach is to always use the generic operations (functions
whose name begins with "PyObject_", "PyNumber_", "PySequence_" or
"PyMapping_"). These operations always increment the reference count of
the object they return. This leaves the caller with the responsibility
to call Py_DECREF() when they are done with the result; this soon
becomes second nature."
URL: http://www.python.org/doc/api/refcounts.html

So does that mean before my C wrapper function exits, or returns I
should Py_DECREF any PyObject's I have?  For example, in my previous
post in this thread..I would have to Py_DECREF(data) right?

What if in my wrapper I had

static PyObject *wrap_doStuff(...) {
PyObject *pyResult;

...

pyResult = PyString_FromString(...);

   return pyResult;
}

Is PyResult going to be de-referenced or handled automaticlly by python
in some way?  Or, do I need to somehow call Py_DECREF(pyResult)
somewhere..if so, where/when?

Thanks!

Java and Swing wrote:
> I have been posting lately about writing a C wrapper so Python can
> access my C functions.
>
> In my wrapper function I have something like...
>
> static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
> PyObject *data;
> char *result;
> long *d;
>
> PyArg_ParseTuple(args, "O:wrap_doStuff", &data);
>
> d = get_long_array(data);
>
> result = doStuff(d);
>
> return PyString_FromString(result);
> }
>
> Now, do I need to use PyMem_Free to free up the "PyObject *data"?
> 
> Thanks.

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


win32 process information, using win32 extension

2005-10-21 Thread Java and Swing
i need to get information about the processes running on a windows pc
(98, 2k, xp)

i can get the pid's using, win32process.EnumProcesses()...and I can get
a handle on a process using an id..such as

handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, 0,
pids[0])

..but how can i get the name of the process, path to process, etc.  I
have the win32 extensions (pywin32) on python 2.4.

thanks.

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