Christian Heimes added the comment:

Improved patch

----------
nosy: +gvanrossum
Added file: http://bugs.python.org/file8648/py3k_preliminary_stderr2.patch

__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1352>
__________________________________
Index: Python/pythonrun.c
===================================================================
--- Python/pythonrun.c	(revision 58695)
+++ Python/pythonrun.c	(working copy)
@@ -151,7 +151,7 @@
 {
 	PyInterpreterState *interp;
 	PyThreadState *tstate;
-	PyObject *bimod, *sysmod;
+	PyObject *bimod, *sysmod, *stderr;
 	char *p;
 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
 	char *codeset;
@@ -228,6 +228,13 @@
 	PyDict_SetItemString(interp->sysdict, "modules",
 			     interp->modules);
 
+	/* set up a dump stderr printer until we have enough infrastructure
+           for the io module in place */
+	stderr = PyFile_NewStdPrinter(2);
+	if (stderr == NULL)
+		Py_FatalError("Py_Initialize: can't set preliminary stderr");
+	PySys_SetObject("stderr", stderr);
+
 	_PyImport_Init();
 
 	/* initialize builtin exceptions */
@@ -736,6 +743,9 @@
 	PySys_SetObject("stdout", std);
 	Py_DECREF(std);
 
+	/* Remove old stderr printer */
+	PySys_SetObject("stderr", NULL);
+
 	/* Set sys.stderr */
 	if (!(std = PyFile_FromFd(fileno(stderr), "<stderr>", "w", -1,
 				      NULL, "\n"))) {
Index: Include/fileobject.h
===================================================================
--- Include/fileobject.h	(revision 58695)
+++ Include/fileobject.h	(working copy)
@@ -8,6 +8,10 @@
 
 #define PY_STDIOTEXTMODE "b"
 
+PyAPI_DATA(PyTypeObject) PyStdPrinter_Type;
+
+PyAPI_FUNC(PyObject *) PyFile_NewStdPrinter(int);
+
 PyAPI_FUNC(PyObject *) PyFile_FromFd(int, char *, char *, int, char *, char *);
 PyAPI_FUNC(PyObject *) PyFile_GetLine(PyObject *, int);
 PyAPI_FUNC(int) PyFile_WriteObject(PyObject *, PyObject *, int);
Index: Objects/object.c
===================================================================
--- Objects/object.c	(revision 58695)
+++ Objects/object.c	(working copy)
@@ -1595,6 +1595,9 @@
 
 	if (PyType_Ready(&PyCode_Type) < 0)
 		Py_FatalError("Can't initialize 'code'");
+
+	if (PyType_Ready(&PyStdPrinter_Type) < 0)
+		Py_FatalError("Can't initialize StderrPrinter");
 }
 
 
Index: Objects/fileobject.c
===================================================================
--- Objects/fileobject.c	(revision 58695)
+++ Objects/fileobject.c	(working copy)
@@ -325,6 +325,129 @@
 	return buf;
 }
 
+/* **************************** std printer **************************** */
+
+typedef struct {
+	PyObject_HEAD
+	int fd : 1;
+} PyStdPrinter_Object;
+
+static PyObject *
+stdprinter_new(PyTypeObject *type, PyObject *args, PyObject *kews)
+{
+	PyStdPrinter_Object *self;
+
+	assert(type != NULL && type->tp_alloc != NULL);
+
+	self = (PyStdPrinter_Object *) type->tp_alloc(type, 0);
+	if (self != NULL) {
+		self->fd = -1;
+	}
+
+	return (PyObject *) self;
+}
+
+PyObject *
+PyFile_NewStdPrinter(int fd)
+{
+	PyStdPrinter_Object *self;
+
+	if (fd != 1 && fd != 2) {
+		PyErr_BadInternalCall();
+		return NULL;
+	}
+
+	self = PyObject_New(PyStdPrinter_Object,
+			    &PyStdPrinter_Type);
+	self->fd = fd;
+	return (PyObject*)self;
+}
+
+PyObject *
+stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
+{
+	PyObject *d;
+	char *c = NULL;
+	Py_ssize_t size = -1;
+
+	if (!PyArg_ParseTuple(args, "O:write", &d))
+		return NULL;
+
+	if (PyBytes_Check(d)) {
+		c = PyBytes_AS_STRING(d);
+		size = PyBytes_Size(d);
+	}
+	else if (PyString_Check(d)) {
+		if (PyString_AsStringAndSize(d, &c, &size) == 0) {
+			c = NULL;
+		}
+	} else if (PyUnicode_Check(d)) {
+		c = PyUnicode_AsStringAndSize(d, &size);
+        } else {
+		PyErr_SetString(PyExc_TypeError, 
+				"Can only write bytes, string and unicode!");
+		return NULL;
+	}
+
+	if (c == NULL || size < 0) {
+		PyErr_SetString(PyExc_ValueError, "no string");
+		return NULL;
+	}
+
+	write(self->fd, c, size);
+
+	Py_RETURN_NONE;
+}
+
+static PyMethodDef stdprinter_methods[] = {
+	{"write", (PyCFunction)stdprinter_write, METH_VARARGS, ""},
+ 	{NULL,		NULL}		/* sentinel */
+};
+
+PyTypeObject PyStdPrinter_Type = {
+	PyVarObject_HEAD_INIT(&PyType_Type, 0)
+	"stderrprinter",			/* tp_name */
+	sizeof(PyStdPrinter_Object),		/* tp_basicsize */
+	0,					/* tp_itemsize */
+	/* methods */
+	0,					/* tp_dealloc */
+	0,					/* tp_print */
+	0,					/* tp_getattr */
+	0,					/* tp_setattr */
+	0,					/* tp_compare */
+	0,					/* tp_repr */
+	0,					/* tp_as_number */
+	0,					/* tp_as_sequence */
+	0,					/* tp_as_mapping */
+	0,					/* tp_hash */
+	0,					/* tp_call */
+	0,					/* tp_str */
+	PyObject_GenericGetAttr,		/* tp_getattro */
+	0,					/* tp_setattro */
+	0,					/* tp_as_buffer */
+	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+	0,					/* tp_doc */
+	0,					/* tp_traverse */
+	0,					/* tp_clear */
+	0,					/* tp_richcompare */
+	0,					/* tp_weaklistoffset */
+	0,					/* tp_iter */
+	0,					/* tp_iternext */
+	stdprinter_methods,			/* tp_methods */
+	0,					/* tp_members */
+	0,					/* tp_getset */
+	0,					/* tp_base */
+	0,					/* tp_dict */
+	0,					/* tp_descr_get */
+	0,					/* tp_descr_set */
+	0,					/* tp_dictoffset */
+	0,					/* tp_init */
+	PyType_GenericAlloc,			/* tp_alloc */
+	stdprinter_new,				/* tp_new */
+	PyObject_Del,				/* tp_free */
+};
+
+
 #ifdef __cplusplus
 }
 #endif
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to