how to convert pymedia.audio.acodec ACString to ctypes.c_char_p
Hello All Experts, I am quite new to Ctypes. I am using one c library and writing python bindings for it. I need to pass a character pointer to one function. I am reading one mp3 file and decoding it to raw pcm using pymedia. Now I need to pass this raw data in to a C function. Here is my code. # /* from ctypes import * import pymedia.audio.acodec as codec dec = None dm = muxer.Demuxer('mp3') fp = open('test.mp3', 'rb') fingerprinting_done = False while not fingerprinting_done: s = fp.read(2) frames = dm.parse(s) for fr in frames: if dec == None: dec = codec.Decoder(dm.streams[0]) r = dec.Decode(fr[1]) if r and r.data: # pass this r.data to the C function. # r.data is ACString object # I cannot do pcm_audio = c_char_p(r.data) . gives me following error # TypeError: string or integer address expected instead of ACString instance # */ I am getting error while converting r.data (which is ACstring object) in to ctypes.c_char_p. can you help me solving this problem? any help would be appericiated, Thank you, sanket -- http://mail.python.org/mailman/listinfo/python-list
segmentation fault while using ctypes
Hello All, I am dealing with this weird bug. I have a function in C and I have written python bindings for it using ctypes. I can call this function for couple of times and then suddenly it gives me seg fault. But I can call same function from a C code for any number of times. I cannot get what's going on. here is my code. /**/ /* C Function I am calling */ int get_hash(char *filename,int rate,int ch,unsigned char* hash, unsigned int* hash_size,short* avg_f,short* avg_d){ /* some variable declarations here */ fp = fopen(filename,"rb"); data = (signed short *)malloc(sizeof(signed short) * N_BLOCKS); whereami = WAVE_HEADER_SIZE; while((!feof(fp)) && (fp_more == 1) && !ferror(fp)){ fp_data_size = fread(data,sizeof(signed short),N_BLOCKS,fp); whereami += fp_data_size; fp_more = fp_feed_short(fooid,data,fp_data_size); // call to some library funtion } //end while /* some arithmetic calculations here */ n = my_fp_calculate(fooid,audio_length,fp_fingerprint,&fit,&dom); if (data != NULL) free(data) fclose(fp) return n; } /* END OF C FUNCTION */ Python code - from ctypes import * lib = cdll.LoadLibrary("/usr/lib/libclient.so") def my_func(filename,rate,ch): hash = (c_ubyte * 424)() hash_size = c_uint() avg_f = c_short(0) avg_d = c_short(0) n = lib.get_hash(filename,rate,ch,hash,byref(hash_size),byref (avg_f),byref(avg_d)) hash = None def main(): for filename in os.listdir(MY_DIR): print filename my_func(filename,100,10) print "" if __name__ == "__main__": main() == END OF PYTHON CODE == Thank you in advance, sanket -- http://mail.python.org/mailman/listinfo/python-list
Re: segmentation fault while using ctypes
On Apr 14, 4:00 pm, MRAB wrote: > sanket wrote: > > Hello All, > > > I am dealing with this weird bug. > > I have a function in C and I have written python bindings for it using > > ctypes. > > > I can call this function for couple of times and then suddenly it > > gives me seg fault. > > But I can call same function from a C code for any number of times. > > > I cannot get what's going on. > > > here is my code. > > > /**/ > > /* C Function I am calling */ > > int get_hash(char *filename,int rate,int ch,unsigned char* hash, > > unsigned int* hash_size,short* avg_f,short* avg_d){ > > > /* some variable declarations here */ > > fp = fopen(filename,"rb"); > > You should check the value of 'fp' here. > > > data = (signed short *)malloc(sizeof(signed short) * N_BLOCKS); > > You should check the value of 'data' here. > > > whereami = WAVE_HEADER_SIZE; > > What is 'whereami'? > > > while((!feof(fp)) && (fp_more == 1) && !ferror(fp)){ > > fp_data_size = fread(data,sizeof(signed short),N_BLOCKS,fp); > > 'fp_data_size' will be the number of signed shorts read, not the number > of bytes. Is this OK? > > > whereami += fp_data_size; > > The final value of 'whereami' will be WAVE_HEADER_SIZE + the total > number of signed shorts read. > > > fp_more = fp_feed_short(fooid,data,fp_data_size); // call to some > > library funtion > > } //end while > > > /* some arithmetic calculations here */ > > > n = my_fp_calculate(fooid,audio_length,fp_fingerprint,&fit,&dom); > > > if (data != NULL) > > free(data) > > I don't that 'free()' will complain if 'data' happens to be NULL, > although you should've already checked whether 'data' is NULL when you > malloc'ed! :-) > > > fclose(fp) > > return n; > > } > > > /* END OF C FUNCTION > > */ > > > > Python code > > - > > from ctypes import * > > lib = cdll.LoadLibrary("/usr/lib/libclient.so") > > > def my_func(filename,rate,ch): > > hash = (c_ubyte * 424)() > > hash_size = c_uint() > > avg_f = c_short(0) > > avg_d = c_short(0) > > n = lib.get_hash(filename,rate,ch,hash,byref(hash_size),byref > > (avg_f),byref(avg_d)) > > hash = None > > > def main(): > > for filename in os.listdir(MY_DIR): > > print filename > > my_func(filename,100,10) > > print > > "" > > > if __name__ == "__main__": > > main() > > > == END OF PYTHON CODE == > > > Thank you in advance, > > sanket > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Thank you for your reply. I will make check for fp and data pointers. But my point is this function runs fine while calling it from a C code. it only breaks while calling from python. So I wonder if there can be anything wrong with ctypes module. Thanks, sanket -- http://mail.python.org/mailman/listinfo/python-list
Re: segmentation fault while using ctypes
On Apr 14, 3:56 pm, "Diez B. Roggisch" wrote: > sanket schrieb: > > > Hello All, > > > I am dealing with this weird bug. > > I have a function in C and I have written python bindings for it using > > ctypes. > > > I can call this function for couple of times and then suddenly it > > gives me seg fault. > > But I can call same function from a C code for any number of times. > > > I cannot get what's going on. > > Try debugging it. While a python debug-build might help, I have been > getting good results with a simple > > # gdb python > $ set args testscript.py > $ run > > then when the segfaults hit, get a backtrace. You can also of course set > a berakpoint to the function in question. > > Diez Thanks Diez, I used the gdb but it just crashed and kicked my out of gdb prompt. how can I get a stack trace? Thanks, sanket -- http://mail.python.org/mailman/listinfo/python-list
Re: segmentation fault while using ctypes
On Apr 14, 9:00 pm, bieff...@gmail.com wrote: > On Apr 15, 12:39 am, sanket wrote: > > > > > Hello All, > > > I am dealing with this weird bug. > > I have a function in C and I have written python bindings for it using > > ctypes. > > > I can call this function for couple of times and then suddenly it > > gives me seg fault. > > But I can call same function from a C code for any number of times. > > > I cannot get what's going on. > > > here is my code. > > > /**/ > > /* C Function I am calling */ > > int get_hash(char *filename,int rate,int ch,unsigned char* hash, > > unsigned int* hash_size,short* avg_f,short* avg_d){ > > > /* some variable declarations here */ > > fp = fopen(filename,"rb"); > > > data = (signed short *)malloc(sizeof(signed short) * N_BLOCKS); > > > whereami = WAVE_HEADER_SIZE; > > while((!feof(fp)) && (fp_more == 1) && !ferror(fp)){ > > fp_data_size = fread(data,sizeof(signed short),N_BLOCKS,fp); > > whereami += fp_data_size; > > fp_more = fp_feed_short(fooid,data,fp_data_size); // call to some > > library funtion > > } //end while > > > /* some arithmetic calculations here */ > > > n = my_fp_calculate(fooid,audio_length,fp_fingerprint,&fit,&dom); > > > if (data != NULL) > > free(data) > > fclose(fp) > > return n; > > > } > > > /* END OF C FUNCTION > > */ > > > > Python code > > - > > from ctypes import * > > lib = cdll.LoadLibrary("/usr/lib/libclient.so") > > > def my_func(filename,rate,ch): > > hash = (c_ubyte * 424)() > > hash_size = c_uint() > > avg_f = c_short(0) > > avg_d = c_short(0) > > n = lib.get_hash(filename,rate,ch,hash,byref(hash_size),byref > > (avg_f),byref(avg_d)) > > hash = None > > > def main(): > > for filename in os.listdir(MY_DIR): > > print filename > > my_func(filename,100,10) > > print > > "" > > > if __name__ == "__main__": > > main() > > > == END OF PYTHON CODE == > > > Thank you in advance, > > sanket > > You don't show how the C function make use of parameters hash, > hash_size, avg_f, avg_d. Since you pass them by address, > I guess that are output parameters, but how do they get written? In > particular,make sure you don't write into hash more > than the 424 bytes you allocated for it in the calling python > code ... > > Ciao > > FB Yes, I am making sure that hash wouldn't be written more that 424 bytes in to it. This is something really weird. I can call this function from C as many times as I want but not from python I tracked down the problem and came to know that call to my_fp_calculate causes the seg fault. I will debug it and let you guys know. Thank you all so much for the help. I appreciate it. sanket -- http://mail.python.org/mailman/listinfo/python-list
SimpleJson is slow .... is there any C Compiled version ?
Hello All, I have created an API which fetches some data from the database. I am using simplejson to encode it and return it back. Now the problem is that, this API is being called for millions of times in a sequence. I ran a profiler and saw that most of the time is consumed in encoding my database results in to json. So I was just wondering is there any C compiled version of simplejson is available? or any help regarding this issue would be appreciated. Thank you, Sanket -- http://mail.python.org/mailman/listinfo/python-list
Re: SimpleJson is slow .... is there any C Compiled version ?
On Jul 25, 5:52 pm, Matt Nordhoff <[EMAIL PROTECTED]> wrote: > sanket wrote: > > Hello All, > > > I have created an API which fetches some data from the database. > > I am using simplejson to encode it and return it back. > > > Now the problem is that, this API is being called for millions of > > times in a sequence. > > I ran a profiler and saw that most of the time is consumed in encoding > > my database results in to json. > > So I was just wondering is there any C compiled version of simplejson > > is available? > > or any help regarding this issue would be appreciated. > > > Thank you, > > Sanket > > simplejson is not the only JSON library out there. For example, there's > python-cjson, which is written entirely in C: > > <http://pypi.python.org/pypi/python-cjson> > > There's also an enhanced version of it: > > <http://python.cx.hu/python-cjson/> > > I think simplejson has some small, optional C bits that will improve > performance if you compile them. > > Also, be aware that I think simplejson is being integrated into the > stdlib in Python 2.6. > > Also, simplejson and python-cjson might not be entirely compatible: > there's one character that one escapes and the other doesn't, or something. > -- Thanks Matt. I used cjson and it appears to be 5-6 times faster. Thank you, Sanket -- http://mail.python.org/mailman/listinfo/python-list
how to spawn a process under different user
Hello All, I am trying to use python's subprocess module to launch a process. but in order to do that I have to change the user. I am not getting any clue how to do that? so can anyone please tell me How can I spawn a process under different user than currently I am logging in as. Thank you, sanket -- http://mail.python.org/mailman/listinfo/python-list
Re: how to spawn a process under different user
On Jul 2, 1:58 pm, Tim Golden wrote: > sanket wrote: > > Hello All, > > > I am trying to use python's subprocess module to launch a process. > > but in order to do that I have to change the user. > > > I am not getting any clue how to do that? > > so can anyone please tell me How can I spawn a process under different > > user than currently I am logging in as. > > What platform are you on? If you are on Windows, > there was a thread on this subject some time in > the last month. (Short answer: switching user before > launching subprocess won't work; switching within > the subprocess will...) > > TJG Hi TJG, Thanks for the reply. I am using python 2.4 on centos. Thanks, sanket -- http://mail.python.org/mailman/listinfo/python-list