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


Groups > comp.lang.python > #95479

Re: Logging to a file from a C-extension

From Laura Creighton <lac@openend.se>
Subject Re: Logging to a file from a C-extension
References <SNT151-W75B708F094CE1F2C687FFFB1780@phx.gbl>
Date 2015-08-19 10:13 +0200
Newsgroups comp.lang.python
Message-ID <mailman.4.1439972077.28100.python-list@python.org> (permalink)

Show all headers | View raw


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);

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web