Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:file': 0.07; 'cc:addr:python-list': 0.09; 'descriptor': 0.09; 'extension,': 0.09; 'flush': 0.09; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'way:': 0.09; 'python': 0.10; 'cc:addr:lac': 0.16; 'cc:addr:openend.se': 0.16; 'descriptor,': 0.16; 'descriptor.': 0.16; 'file?': 0.16; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'message-id:@fido.openend.se': 0.16; 'pyobject': 0.16; 'received:89.233': 0.16; 'received:89.233.217': 0.16; 'received:89.233.217.133': 0.16; 'received:fido': 0.16; 'received:fido.openend.se': 0.16; 'subject:Logging': 0.16; 'integer': 0.18; '2015': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'extension': 0.20; 'skip:" 30': 0.20; 'cc:2**1': 0.22; 'null;': 0.22; 'produces': 0.22; 'cc:no real name:2**0': 0.22; 'logging': 0.27; '-0500,': 0.29; 'received:se': 0.29; 'convert': 0.29; 'int': 0.33; 'tue,': 0.34; 'file': 0.34; 'files,': 0.35; 'level': 0.35; 'skip:p 30': 0.35; 'subject:: ': 0.37; 'charset:us- ascii': 0.37; 'anything': 0.38; 'log': 0.38; 'data': 0.39; 'subject:from': 0.39; 'subject:-': 0.39; 'your': 0.60; 'header :Message-Id:1': 0.61; 'side': 0.62; 'lose': 0.63; 'obtained': 0.76; 'received:89': 0.80; 'hand': 0.82; 'header:In-reply-to:1': 0.84; 'to:addr:hotmail.com': 0.98 To: Al Pfalzgraf cc: "python-list@python.org" , lac@openend.se From: Laura Creighton Subject: Re: Logging to a file from a C-extension In-reply-to: References: Comments: In-reply-to Al Pfalzgraf message dated "Tue, 18 Aug 2015 08:07:51 -0500." MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <28030.1439972015.1@fido> Content-Transfer-Encoding: quoted-printable Date: Wed, 19 Aug 2015 10:13:35 +0200 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [89.233.217.130]); Wed, 19 Aug 2015 10:13:48 +0200 (CEST) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439972077 news.xs4all.nl 23834 [2001:888:2000:d::a6]:45895 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95479 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 wo= uld the log file name be communicated to a C-extension so that logging fro= m 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 =3D 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 =3D PyFile_FromFd(fd, "filename","r",-1,NULL,NULL,NULL,1);