Writing to stdout and a log file
Jeff Epler
jepler at unpythonic.net
Tue Apr 19 22:43:50 EDT 2005
In that case, it looks like you won't be able to get what you want
without modifying CPython. PRINT_ITEM calls PyFile_SoftSpace,
PyFile_WriteString, and PyFile_WriteObject, which all use
PyFile_Check(). It might be as simple as changing these to
PyFile_CheckExact() calls in PyFile_WriteString / PyFile_WriteObject,
but I have no idea whether the test suite still works after this change
is made. It does make this program work (it prints things from X.write):
class X(file):
def write(self, s):
print "X.write", `s`
return file.write(self, s)
import sys
x = X("/tmp/out.txt", "w")
print >>x, 42
I don't care to be the champion of this patch, or to submit it to
sourceforge; I suspect there should be a better review of PyFile_Check
vs PyFile_CheckExact uses in fileobject.c, instead of just picking the
few spots that make this usage work. Before being submitted as a patch,
a testcase should be added too. Feel free to run with this if you feel
strongly about it.
Jeff
Index: Objects/fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.193
diff -u -u -r2.193 fileobject.c
--- Objects/fileobject.c 7 Nov 2004 14:15:28 -0000 2.193
+++ Objects/fileobject.c 20 Apr 2005 02:41:32 -0000
@@ -2012,7 +2012,7 @@
PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
return -1;
}
- else if (PyFile_Check(f)) {
+ else if (PyFile_CheckExact(f)) {
FILE *fp = PyFile_AsFile(f);
#ifdef Py_USING_UNICODE
PyObject *enc = ((PyFileObject*)f)->f_encoding;
@@ -2082,7 +2082,7 @@
"null file for PyFile_WriteString");
return -1;
}
- else if (PyFile_Check(f)) {
+ else if (PyFile_CheckExact(f)) {
FILE *fp = PyFile_AsFile(f);
if (fp == NULL) {
err_closed();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050419/0ca565bf/attachment.sig>
More information about the Python-list
mailing list