473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

C Module question

Hello,

I'm trying to write a Python extension module in C for the first time.
I have two questions:

1. How can I pass a file-like object into the C part? The PyArg_*
functions can convert objects to all sort of types, but not FILE*.

2. How can I preserve information needed in the C part between calls
to my module functions? The way I've thought of is:
- put this information in a dynamically allocated struct
- pass it out of and into the C module as CObject into/from an
intermediate Python wrapper module where it gets stored in a Python
object

Is this the way to do it?

Thanks.
robert
Nov 10 '08 #1
11 1263
bo*******@googlemail.com wrote:
Hello,

I'm trying to write a Python extension module in C for the first time.
I have two questions:
I have a much better suggestion:

Use Cython!

Although I'm pretty experienced with the Python C API, I prefer Cython
over hand written C code for most stuff. It's so much easier to write a
wrapper in Cython than by hand.

Christian

Nov 10 '08 #2
On Mon, 10 Nov 2008 03:11:06 -0800, bo*******@googlemail.com wrote:
1. How can I pass a file-like object into the C part? The PyArg_*
functions can convert objects to all sort of types, but not FILE*.
http://docs.python.org/c-api/file.html#PyFile_AsFile
2. How can I preserve information needed in the C part between calls to
my module functions? The way I've thought of is: - put this information
in a dynamically allocated struct - pass it out of and into the C module
as CObject into/from an intermediate Python wrapper module where it gets
stored in a Python object
Why passing it in and out? Simply use the C module level to store the
information.

Ciao,
Marc 'BlackJack' Rintsch
Nov 10 '08 #3
Hi

On Nov 10, 11:11*am, "boblat...@googlemail.com"
<boblat...@googlemail.comwrote:
1. How can I pass a file-like object into the C part? The PyArg_*
functions can convert objects to all sort of types, but not FILE*.
Parse it as a generic PyObject object (format string of "O" in
PyArg_*), check the type and cast it. Or use "O!" as format string
and the typechecking can be done for you, only thing left is casting
it.

See http://docs.python.org/c-api/arg.html and http://docs.python.org/c-api/file.html
for exact details.

(2 is answered already...)

Regards
Floris
Nov 10 '08 #4
On Nov 10, 1:18*pm, Floris Bruynooghe <floris.bruynoo...@gmail.com>
wrote:
On Nov 10, 11:11*am, "boblat...@googlemail.com"

<boblat...@googlemail.comwrote:
1. How can I pass a file-like object into the C part? The PyArg_*
functions can convert objects to all sort of types, but not FILE*.

Parse it as a generic PyObject object (format string of "O" in
PyArg_*), check the type and cast it. *Or use "O!" as format string
and the typechecking can be done for you, only thing left is casting
it.

Seehttp://docs.python.org/c-api/arg.htmlandhttp://docs.python.org/c-api/file.html
for exact details.
Sorry, I probably should have mentioned you want to cast the object to
PyFileObject and then use the PyFile_AsFile() function to get the
FILE* handle.
Floris
Nov 10 '08 #5
On Nov 10, 1:16*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Mon, 10 Nov 2008 03:11:06 -0800, boblat...@googlemail.com wrote:
1. How can I pass a file-like object into the C part? The PyArg_*
functions can convert objects to all sort of types, but not FILE*.

http://docs.python.org/c-api/file.html#PyFile_AsFile
Yes, got it. At first I thought I had to use the "Parse" functions for
my args, but in fact I can of course just access the args as a tuple
(and then do the type checking myself).
Why passing it in and out? *Simply use the C module level to store the
information.
But if I create several instances of a class (in the wrapper module)
my C methods won't know which object they were called on.

robert

Nov 10 '08 #6
On Nov 10, 2:23*pm, Floris Bruynooghe <floris.bruynoo...@gmail.com>
wrote:
Sorry, I probably should have mentioned you want to cast the object to
PyFileObject and then use the PyFile_AsFile() function to get the
FILE* handle.
Yes, I figured that out by now. Sadly this doesn't work on "file-like"
objects like those that are created by opening bz2 files (using the
bz2 lib). Not that I have any idea on how that should work anyway.

I still can resolve this issue in my wrapper module and let the C part
deal with the raw buffer contents.

All in all I must say that implementing a C extension is a piece of
cake. Had I known that it was this straightforward I wouldn't have
asked my questions in the first place. Making the whole thing more
robust will be a bit more difficult, and I need to find out how to
deal with ressources that are dynamically allocated on the C side.

But that should be easy, and I'll just keep all the Python OO and
Exceptions stuff in the wrapper and call my C stuff from there in a
more or less straightforward C manner.

Thanks to everybody for helping out,

robert
(I'll be back for sure)

Nov 10 '08 #7
On Mon, 10 Nov 2008 05:36:58 -0800, bo*******@googlemail.com wrote:
On Nov 10, 1:16Â*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Mon, 10 Nov 2008 03:11:06 -0800, boblat...@googlemail.com wrote:
1. How can I pass a file-like object into the C part? The PyArg_*
functions can convert objects to all sort of types, but not FILE*.

http://docs.python.org/c-api/file.html#PyFile_AsFile

Yes, got it. At first I thought I had to use the "Parse" functions for
my args, but in fact I can of course just access the args as a tuple
(and then do the type checking myself).
>Why passing it in and out? Â*Simply use the C module level to store the
information.

But if I create several instances of a class (in the wrapper module) my
C methods won't know which object they were called on.
Well you talked about functions in the module not about methods on
objects.

Ciao,
Marc 'BlackJack' Rintsch
Nov 10 '08 #8
On Mon, 10 Nov 2008 05:44:44 -0800, bo*******@googlemail.com wrote:
All in all I must say that implementing a C extension is a piece of
cake. Had I known that it was this straightforward I wouldn't have asked
my questions in the first place. Making the whole thing more robust will
be a bit more difficult, and I need to find out how to deal with
ressources that are dynamically allocated on the C side.

But that should be easy, and I'll just keep all the Python OO and
Exceptions stuff in the wrapper and call my C stuff from there in a more
or less straightforward C manner.
Then you might considering the `ctypes` module to call your C stuff.
This way it is easier to build the extension and it is also independent
from the Python version.

Ciao,
Marc 'BlackJack' Rintsch
Nov 10 '08 #9
On Nov 11, 12:55*am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Mon, 10 Nov 2008 05:44:44 -0800, boblat...@googlemail.com wrote:
All in all I must say that implementing a C extension is a piece of
cake. Had I known that it was this straightforward I wouldn't have asked
my questions in the first place. Making the whole thing more robust will
be a bit more difficult
Forget "more robust". You need "quite robust". Seriously consider
abandoning your "piece of cake" euphoria and use Cython or ctypes
instead. At the very least check out the C code that Cython generates
for a simple task and consider whether you really want to hand-code
that.

Nov 10 '08 #10
bo*******@googlemail.com wrote:
Sadly this doesn't work on "file-like"
objects like those that are created by opening bz2 files (using the
bz2 lib).
If the C code you're calling requires a FILE *, then you're
out of luck. There's no way of getting a FILE * from an object
that's not based on an actual PyFile object, since there
simply isn't one to be had.

There is an exception to that -- if the object is one that
has a file descriptor underlying it somewhere (such as a
pipe or socket) you could get that and wrap it using fdopen().

--
Greg
Nov 11 '08 #11
En Mon, 10 Nov 2008 11:44:44 -0200, bo*******@googlemail.com
<bo*******@googlemail.comescribió:
On Nov 10, 2:23*pm, Floris Bruynooghe <floris.bruynoo...@gmail.com>
wrote:
>Sorry, I probably should have mentioned you want to cast the object to
PyFileObject and then use the PyFile_AsFile() function to get the
FILE* handle.

Yes, I figured that out by now. Sadly this doesn't work on "file-like"
objects like those that are created by opening bz2 files (using the
bz2 lib). Not that I have any idea on how that should work anyway.
Call the file-like methods as you would with any other object method. That
is, if you got an object `fobj` from Python and you want to write a C
string:

char* str="some text\n";
PyObject_CallMethod(fobj, "write", "s", str)

See also PyObject_CallObject and the other variants:
http://docs.python.org/c-api/object.html#PyObject_Call

--
Gabriel Genellina

Nov 15 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
4
by: Peter J. Bismuti | last post by:
I'm having trouble understanding how namespaces work in modules. I want to execute a module within the interpreter and then have values that are calculated persist so that other modules that get...
3
by: james | last post by:
Hi all, I did some quick searching but I haven't found anything like I want. It probably has to do with the terms I am searching for so if I describe what I want then I hope someone can point me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...
0
tracyyun
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.