簡體   English   中英

控制Python腳本的C ++輸出

[英]Controlling C++ Output from Python Script

我這里有點問題。 我有一個Python腳本,它調用從C ++編譯的二進制文件。 Python腳本有自己的一組輸出(標准輸出和錯誤),這些輸出很容易禁用。 C ++二進制文件也有自己的輸出集(標准輸出和錯誤,以及其他輸出); 來源可以改變,但我不是原作者。 這是一個問題,因為我不想在我的最終程序中輸出C ++,我也不希望將來的用戶需要編輯C ++源代碼。

我希望能夠做的是有一些Python方法,它將捕獲發送到標准輸出或錯誤的C ++代碼輸出。 這可能嗎? 如果是這樣,有人會指出我正確的方向嗎?

謝謝!!

一種方法是:

  • 使用os.dup在python中復制stdoutstderr os.dup
  • 使用reopen (從C的stdio )重定向原始stdoutstderr以寫入您選擇的文件。

注意: reopen不能直接從python中獲得,但您應該能夠像下面的示例中那樣調用它,或者使用任何其他可用的包裝器。

完成后:

  • 在C ++中對coutcerr每次寫入都將寫入輸出文件。
  • python中的每個print語句都將寫入輸出文件。

但是,由於原始描述符是重復的,您仍然可以(參見下面的示例):

  • 使用sdout.writestdout.err打印到原始stdout / stderr
  • 正確配置stream參數后使用logging方法

下面的代碼使用即時庫來測試使用SWIG包裝到python中的真實C ++代碼,它應該類似於您擁有的庫:

import sys, os
import logging
from instant import inline

print 'This is printed from python to stdout'
stdout = os.fdopen(os.dup(sys.stdout.fileno()), 'w')
stderr = os.fdopen(os.dup(sys.stderr.fileno()), 'w')

logging.basicConfig(stream=stderr, level=logging.DEBUG)

redirect = inline("""                                                                                                                    
void redirect(void) {                                                                                                                    
    freopen("my_stdout.txt", "w", stdout);                                                                                               
    freopen("my_stderr.txt", "w", stderr);                                                                                               
}                                                                                                                                        
""")
redirect()

cout = inline("""                                                                                                                        
void cout(void) {                                                                                                                        
    std::cout << "This is written from C++ to my_stdout.txt" << std::endl;                                                               
    std::cerr << "This is written from C++ to my_stderr.txt" << std::endl;                                                               
}                                                                                                                                        
""")
cout()

print 'This is written from python to my_stdout.txt'

stdout.write('This is printed from python to stdout\n')
stderr.write('This is printed from python to stderr\n')
logging.info('This is printed to stderr from python using logging')

此示例的輸出是:

$ python test.py
This is printed from python to stdout
This is printed from python to stdout
This is printed from python to stderr
INFO:root:This is printed to stderr from python using logging
$ cat my_stdout.txt 
This is written from C++ to my_stdout.txt
This is written from python to my_stdout.txt
$ cat my_stderr.txt 
This is written from C++ to my_stderr.txt

注意:第一次執行代碼時,您可能會收到gcc編譯消息(我已刪除它們以使示例更清晰)。

您是否使用subprocess來編譯C ++? 如果是這樣,您可以設置stderr和stdout的位置:

nowhere = StringIO()
subprocess.call("exit 1", shell=True, stdout=nowhere, stderr=nowhere)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM