Christian Heimes added the comment:

I've some code around which sets sys.stdin, out and err in C code. The
code is far from perfect and I haven't checked it for reference leaks
yet. I like to get your comment on the style and error catching.

----------
nosy: +tiran

__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1267>
__________________________________
Index: Python/pythonrun.c
===================================================================
--- Python/pythonrun.c	(revision 58477)
+++ Python/pythonrun.c	(working copy)
@@ -51,6 +51,7 @@
 /* Forward */
 static void initmain(void);
 static void initsite(void);
+static int initsysstd(void);
 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
 			  PyCompilerFlags *, PyArena *);
 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
@@ -241,8 +242,11 @@
 		initsigs(); /* Signal handling stuff, including initintr() */
 
 	initmain(); /* Module __main__ */
+	if (initsysstd() < 0)
+		Py_FatalError("Py_Initialize: can't initialize sys std streams");
 	if (!Py_NoSiteFlag)
 		initsite(); /* Module site */
+	
 
 	/* auto-thread-state API, if available */
 #ifdef WITH_THREAD
@@ -676,6 +680,93 @@
 	}
 }
 
+/* Initialize sys.stdin, stdout and stderr */
+static int
+initsysstd(void)
+{
+	PyObject *io=NULL, *iod, *open;
+	PyObject *bi=NULL, *bid;
+	PyObject *m, *wrapper;
+	PyObject *std, *args=NULL, *kwargs=NULL;
+
+	/* Hack to avoid a nasty recursion issue when Python is invoked
+	   in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
+	if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL)
+		return -1;
+	Py_DECREF(m);
+	
+	if ((m = PyImport_ImportModule("encodings.latin_1")) == NULL)
+		return -1;
+	Py_DECREF(m);
+
+	if ((bi = PyImport_ImportModule("__builtin__")) == NULL)
+		goto error;
+	if ((bid = PyModule_GetDict(bi)) == NULL)
+		goto error;
+
+	if ((io = PyImport_ImportModule("io")) == NULL)
+		goto error;
+	if ((iod = PyModule_GetDict(io)) == NULL)
+		goto error;
+
+	if ((wrapper = PyDict_GetItemString(iod, "OpenWrapper")) == NULL)
+		goto error;
+	Py_INCREF(wrapper);
+	if (PyDict_SetItemString(bid, "open", wrapper) == -1) {
+		Py_DECREF(wrapper); /* ??? */
+		goto error;
+	}
+
+	if ((open = PyDict_GetItemString(iod, "open")) == NULL)
+		goto error;
+
+	if ((kwargs = PyDict_New()) == NULL)
+		goto error;
+	if (PyDict_SetItemString(kwargs, "newline", PyString_FromString("\n"))
+	    == -1)
+		goto error;
+	
+	/* stdin */
+	if ((args = Py_BuildValue("(is)", 0, "r")) == NULL)
+		goto error;
+	if ((std = PyObject_Call(open, args, kwargs)) == NULL)
+		goto error;
+	PySys_SetObject("__stdin__", std);
+	PySys_SetObject("stdin", std);
+	Py_DECREF(std);
+	Py_DECREF(args);
+
+	/* stdout */
+	if ((args = Py_BuildValue("(is)", 1, "w")) == NULL)
+		goto error;
+	if ((std = PyObject_Call(open, args, kwargs)) == NULL)
+		goto error;
+	PySys_SetObject("__stdout__", std);
+	PySys_SetObject("stdout", std);
+	Py_DECREF(std);
+	Py_DECREF(args);
+
+	/* stderr */
+	if ((args = Py_BuildValue("(is)", 2, "w")) == NULL)
+		goto error;
+	if ((std = PyObject_Call(open, args, kwargs)) == NULL)
+		goto error;
+	PySys_SetObject("__stderr__", std);
+	PySys_SetObject("stderr", std);
+	Py_DECREF(std);
+	Py_DECREF(args);
+	
+	return 0;
+	
+	error:
+		Py_XDECREF(bi);
+		Py_XDECREF(io);
+		Py_XDECREF(kwargs);
+		Py_XDECREF(args);
+		return -1;
+
+}
+
 /* Parse input from a file and execute it */
 
 int
Index: Lib/site.py
===================================================================
--- Lib/site.py	(revision 58477)
+++ Lib/site.py	(working copy)
@@ -433,7 +433,7 @@
     sethelper()
     aliasmbcs()
     setencoding()
-    installnewio()
+    #installnewio()
     execsitecustomize()
     # Remove sys.setdefaultencoding() so that users cannot change the
     # encoding after initialization.  The test for presence is needed when
Index: Lib/io.py
===================================================================
--- Lib/io.py	(revision 58477)
+++ Lib/io.py	(working copy)
@@ -178,6 +178,16 @@
     return text
 
 
+class OpenWrapper:
+    """Wrapper for __builtin__.open
+
+    Trick so that open won't become a bound method when stored
+    as a class variable (as dumbdbm does)
+    """
+    def __new__(cls, *args, **kwargs):
+        return open(*args, **kwargs)
+
+
 class UnsupportedOperation(ValueError, IOError):
     pass
 
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to