Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95479 > unrolled thread

Re: Logging to a file from a C-extension

Started byLaura Creighton <lac@openend.se>
First post2015-08-19 10:13 +0200
Last post2015-08-19 10:13 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Logging to a file from a C-extension Laura Creighton <lac@openend.se> - 2015-08-19 10:13 +0200

#95479 — Re: Logging to a file from a C-extension

FromLaura Creighton <lac@openend.se>
Date2015-08-19 10:13 +0200
SubjectRe: Logging to a file from a C-extension
Message-ID<mailman.4.1439972077.28100.python-list@python.org>
In a message of Tue, 18 Aug 2015 08:07:51 -0500, Al Pfalzgraf writes:
> If a logging file is opened at the level of a Python application, how would the log file name be communicated to a C-extension so that logging from the extension would be sent to the same log file?

To convert a file to an integer file descriptor, use PyFile_FromFd()

PyObject *fobj;     /* File object (already obtained somehow) */
int fd = PyObject_AsFileDescriptor(fobj);
if (fd < 0) {
   return NULL;
   }

This works for files, sockets, anything that produces a file descriptor.
Remember to flush your file on the Python side before you hand it to your
C extension, or your data will arrive scrambled, or you can lose some.

If you need to go the other way:

int fd;     /* Existing file descriptor (already open) */
PyObject *fobj = PyFile_FromFd(fd, "filename","r",-1,NULL,NULL,NULL,1);

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web