如何重定向标准错误在Python?通过Python C API? [英] How to redirect stderr in Python? Via Python C API?
问题描述
这是我的两个问题最近一个组合:结果
[1] <一个href=\"http://stackoverflow.com/questions/1954494/python-instance-method-in-c\">http://stackoverflow.com/questions/1954494/python-instance-method-in-c
[2] <一个href=\"http://stackoverflow.com/questions/1956142/how-to-redirect-stderr-in-python\">http://stackoverflow.com/questions/1956142/how-to-redirect-stderr-in-python
This is a combination of my two recent questions:
[1] http://stackoverflow.com/questions/1954494/python-instance-method-in-c
[2] http://stackoverflow.com/questions/1956142/how-to-redirect-stderr-in-python
我想记录从python脚本标准输出和错误的输出。
I would like to log the output of both stdout and stderr from a python script.
我要问的是,根据创建一个新的类型的东西[1]似乎相当复杂。难道它简化了东西,如果没有必要揭露新型Python的,即它会只在C存在?
The thing I want to ask is, to create a new type according to [1] seems fairly complicated. Does it simplifies the things if there was no need to expose the new type to Python, i.e. it would only exist in C?
我的意思是,当在Python的PyFile_WriteObject打印的东西它去对象/ fileobject.c,并有它检查是否有可能写入它的参数:
I mean, when Python prints something it goes to "Objects/fileobject.c" and there in "PyFile_WriteObject" it check whether it is possible to write to its argument:
writer = PyObject_GetAttrString(f, "write");
if (writer == NULL)
...
另外,还可能得到输出和错误是这样的:
Also, it is possible to get stdout and stderr like this:
PyObject* out = PySys_GetObject("stdout");
PyObject* err = PySys_GetObject("stderr");
我的问题是那么,它在某种程度上可以构造满足上述必要的PyObject'PyObject_GetAttrString(F,写)',是可调用,所以我可以这样写:
My question is then, is it somehow possible to construct necessary PyObject which satisfies the above 'PyObject_GetAttrString(f, "write")' and is callable so I can write:
PySys_SetObject("stdout", <my writer object / class / type / ?>);
的http://docs.python.org/c-api/sys.html?highlight=pysys%5Fsetobject#PySys%5FSetObject
这样的话,就没有必要暴露新的作家型到Python脚本的其余部分,所以我想这可能是一个有点简单写code ...?
This way, there would be no need to expose the new "writer type" to the rest of Python script so I thought it might be a bit simpler to write the code...?
推荐答案
只是做一个模块对象(你这样做,无论如何,如果你正在使用C API - !),并使其有一个合适的写
功能 - 该模块的对象将是适合作为第二个参数 PySys_SetObject
Just make a module object (you're doing that anyway, if you're using the C API!-) and make it have a suitable write
function -- that module object will be suitable as the second argument to PySys_SetObject
.
在我回答你的其他问题我指出你 xxmodule.c
,在Python的C程序源代码示例文件,这是有很多例子,包括类型的模块和各种功能 - 你可以从那里即使(神秘地对我)工作,你考虑作出新的类型的一部分太难了 - 。)
In my answer to your other question I pointed you to xxmodule.c
, an example file in Python's C sources, which is a module with a lot of examples including types and functions of various kinds -- you can work from there even if (mysteriously to me) you consider the "make a new type" part too difficult;-).
修改:这里有一个简单的工作示例( aview.py
)
Edit: here's a trivial working example (aview.py
):
#include "Python.h"
#include <stdio.h>
static PyObject *
aview_write(PyObject *self, PyObject *args)
{
const char *what;
if (!PyArg_ParseTuple(args, "s", &what))
return NULL;
printf("==%s==", what);
return Py_BuildValue("");
}
static PyMethodDef a_methods[] = {
{"write", aview_write, METH_VARARGS, "Write something."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initaview(void)
{
PyObject *m = Py_InitModule("aview", a_methods);
if (m == NULL) return;
PySys_SetObject("stdout", m);
}
一旦 aview
模块已正确安装:
$ python
Python 2.5.4 (r254:67917, Dec 23 2008, 14:57:27)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import aview
>>> print 'ciao'
==ciao====
==>>>
...排放标准输出任何字符串中写入 ==
周围的迹象(这打印
电话 .WRITE
两次:用'侨'
,然后用新行再次)
...any string emitted to standard output is written with ==
signs around it (and this print
calls .write
twice: with 'ciao'
, and then again with a newline).
这篇关于如何重定向标准错误在Python?通过Python C API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!