从python重定向stdout进行C调用 [英] Redirect stdout from python for C calls

查看:272
本文介绍了从python重定向stdout进行C调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自此处的后续问题,专门针对其

This is a follow up question from here specifically concerning its answer.

从python模块中,我正在调用 Hello World 可执行文件,该可执行文件仅将Hello World打印到标准输出.我有兴趣将输出重定向到python StringIO并遇到答案,这几乎使我一路走到解决方案.

From a python module I am calling a Hello World executable that simply prints Hello World to the stdout. I am interested in redirecting that output to a python StringIO and ran into this answer which almost brings me all the way to the solution.

此答案的关键部分是以下代码段:

The critical part of this answer is this code segment:

1. def redirect_stdout():
2.     print "Redirecting stdout"
3.     sys.stdout.flush() # <--- important when redirecting to files
4.     newstdout = os.dup(1)
5.     devnull = os.open('/dev/null', os.O_WRONLY)
6.     os.dup2(devnull, 1)
7.     os.close(devnull)
8.     sys.stdout = os.fdopen(newstdout, 'w')

我还想恢复重定向之前的标准输出.

Also I would like to restore the stdout as it was before the redirection.

  1. 上面的函数中到底发生了什么?
    • dupdup2在做什么?
    • 什么是/dev/null?
    • 第8行在做什么? (sys.stdout = os.fdopen(newstdout, 'w'))
  1. What exactly is going on in the function above?
    • What is dup and dup2 doing?
    • What is /dev/null?
    • What is line 8 doing? (sys.stdout = os.fdopen(newstdout, 'w'))

我很确定,一旦我对问题1有了答案,问题2和3的答案就会很容易.我决定无论如何都要张贴它们,以将问题1的答案推向我想去的方向.

I am pretty sure that once I have the answer for my question 1 that the answers of questions 2 and 3 will be easy. I decided to post them anyway to maybe push the answer of question 1 into the direction where I want to go.

推荐答案

我在下面写下了一些其他注释,这些注释应该更清楚它在redirect_stdout函数内部的作用:

I've written below a few additional comments that should make clearer what it's going on inside the redirect_stdout function:

def redirect_stdout():
    print "Redirecting stdout"
    sys.stdout.flush() # <--- important when redirecting to files

    # Duplicate stdout (file descriptor 1)
    # to a different file descriptor number
    newstdout = os.dup(1)

    # /dev/null is used just to discard what is being printed
    devnull = os.open('/dev/null', os.O_WRONLY)

    # Duplicate the file descriptor for /dev/null
    # and overwrite the value for stdout (file descriptor 1)
    os.dup2(devnull, 1)

    # Close devnull after duplication (no longer needed)
    os.close(devnull)

    # Use the original stdout to still be able
    # to print to stdout within python
    sys.stdout = os.fdopen(newstdout, 'w')

要注意的一件事是,当操作系统从操作系统获取三个不同的文件描述符时,发射:

One important thing to note is that a process gets three different file descriptors from the OS when launched:

  • stdin:0
  • 标准输出:1
  • stderr:2

如评论中所述,上面的代码利用了stdout的文件描述符和文件描述符复制功能来欺骗C代码使用不同的stdout,同时仍保留对python代码中原始stdout的引用.就能打印.

As explained in the comments, the code above takes advantage of the file descriptor for stdout and the file descriptor duplication functions to make trick the C code into using a different stdout while still keeping a reference to the original stdout in the python code to be able to print.

这篇关于从python重定向stdout进行C调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆