Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > FILE object in Python3.0 extension modules

Reply
Thread Tools

FILE object in Python3.0 extension modules

 
 
Joachim Dahl
Guest
Posts: n/a
 
      05-29-2009
In Python2.x, I used PyFile_Check(obj) to check if a parameter was a
file object.

Now, in Python3.0 the same object (obtained as open('file.bin','wb'))
is an
io.BufferedWriter object.

How do I perform type checking for such an object in the extension
module,
and how do I extract a FILE * object from it? I browsed the C API
documentation, but
couldn't find an answer.

The purpose is to dump the contents of a Python extension type to disk
as
binary data using C's fwrite() function.
 
Reply With Quote
 
 
 
 
Benjamin Peterson
Guest
Posts: n/a
 
      05-29-2009
Joachim Dahl <dahl.joachim <at> gmail.com> writes:
>
> How do I perform type checking for such an object in the extension
> module,
> and how do I extract a FILE * object from it? I browsed the C API
> documentation, but
> couldn't find an answer.


You use PyObject_IsInstance to test if the object is an instance of io.IOBase.




 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-30-2009
En Fri, 29 May 2009 06:52:15 -0300, Joachim Dahl <(E-Mail Removed)>
escribiˇ:

> In Python2.x, I used PyFile_Check(obj) to check if a parameter was a
> file object.
>
> Now, in Python3.0 the same object (obtained as open('file.bin','wb'))
> is an
> io.BufferedWriter object.
>
> How do I perform type checking for such an object in the extension
> module,


I don't know which is the preferred way to check for a file object in 3.x
- I hope someone can answer this more precisely. In principle, a file
inherits from io.IOBase, but this class is defined in io.py and probably
isn't intended to be used in C code. Other alternatives are _io._IOBase,
PyIOBase_Type, and io.FileIO/_io.FileIO

> and how do I extract a FILE * object from it? I browsed the C API
> documentation, but
> couldn't find an answer.


I'd use PyObject_AsFileDescriptor
http://docs.python.org/dev/py3k/c-api/file.html

(Notice that the documentation is outdated; the PyFileObject type does not
exist anymore, and a file isn't a wrapper around a FILE struct either)

> The purpose is to dump the contents of a Python extension type to disk
> as
> binary data using C's fwrite() function.


From the above, I'd use write() with the file descriptor obtained from
PyObject_AsFileDescriptor.

--
Gabriel Genellina

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-30-2009
En Fri, 29 May 2009 08:48:26 -0300, Benjamin Peterson
<(E-Mail Removed)> escribiˇ:

> Joachim Dahl <dahl.joachim <at> gmail.com> writes:
>>
>> How do I perform type checking for such an object in the extension
>> module,
>> and how do I extract a FILE * object from it? I browsed the C API
>> documentation, but
>> couldn't find an answer.

>
> You use PyObject_IsInstance to test if the object is an instance of
> io.IOBase.


But you have to import the io module first, don't you? That's not usually
necesary for most built in types -- e.g. PyFloat_Check just checks for a
float object.

--
Gabriel Genellina

 
Reply With Quote
 
Benjamin Peterson
Guest
Posts: n/a
 
      05-30-2009
Gabriel Genellina <gagsl-py2 <at> yahoo.com.ar> writes:
> But you have to import the io module first, don't you? That's not usually
> necesary for most built in types -- e.g. PyFloat_Check just checks for a
> float object.


Well, in 3.x, file is not longer a builtin type.




 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-30-2009
En Fri, 29 May 2009 23:24:32 -0300, Benjamin Peterson
<(E-Mail Removed)> escribiˇ:

> Gabriel Genellina <gagsl-py2 <at> yahoo.com.ar> writes:
>> But you have to import the io module first, don't you? That's not
>> usually
>> necesary for most built in types -- e.g. PyFloat_Check just checks for a
>> float object.

>
> Well, in 3.x, file is not longer a builtin type.


Ok, seems the old "file" type has been demoted and atomized...

--
Gabriel Genellina

 
Reply With Quote
 
Martin v. L÷wis
Guest
Posts: n/a
 
      06-03-2009
> The purpose is to dump the contents of a Python extension type to disk
> as binary data using C's fwrite() function.


This isn't really possible anymore - the Python IO library has stopped
using stdio. There are a couple of alternatives:

1. don't use fwrite(3) to write the binary data, but instead use
PyObject_CallMethod to call .write on the file object.
2. don't use fwrite(3), but write(2). To do so, fetch the file
descriptor from the Python file object, and use that. Make sure
you flush the stream before writing to it, or else you may get
the data in the wrong order.
3. use fdopen to obtain a FILE*; the comments for 2) apply.
In addition, make sure to flush and discard the FILE* before
letting Python continue to write to the file.

Regards,
Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When does a binary extension gets the file extension '.pyd' and whenis it '.so' llothar Python 17 04-07-2008 02:54 AM
How to convert a .txt file extension to a .xls file extension? Steve ASP .Net 3 08-25-2006 05:43 PM
extension modules in C for python, check for korrekt Object Torsten Mohr Python 4 06-02-2004 02:23 PM
Unload extension modules when python22.dll unloads... [using C extension interpreter] Anand Python 3 11-08-2003 05:50 AM



Advertisments