teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

コメントを受け追記

2020/09/08 00:48

投稿

bsdfan
bsdfan

スコア4920

answer CHANGED
@@ -9,4 +9,23 @@
9
9
  dll = cdll.LoadLibrary('hogehoge.dll') # ここで何か出力される?
10
10
 
11
11
  outputs = out.getvalue()
12
+ ```
13
+
14
+ ### 追記
15
+ 今回のケースでは`io.StringIO`とかは使えないんですね・・・
16
+
17
+ 元のプログラムで気になったところとしては、`sys.stdout`からreadするのではなく、`TemporaryFile`をいったん別変数に代入しておいて、そこからreadしたほうがいいのではないかというところと、readの前にseek(0)が必要ではないかというところです。
18
+
19
+ (`contextlib.redirect_stdout`という便利な関数があるようです)
20
+
21
+ ```python
22
+ from contextlib
23
+ from tempfile
24
+
25
+ with tempfile.TemporaryFile(mode='w+b') as tmp:
26
+ with contextlib.redirect_stdout(tmp):
27
+ dll = cdll.LoadLibrary('hogehoge.dll')
28
+ tmp.seek(0)
29
+ for line in iter(tmp.readline, b''):
30
+ logger.info(line.rstrip().decode(cnf.Proc_Encoding, 'replace'))
12
31
  ```

1

コード修正

2020/09/08 00:48

投稿

bsdfan
bsdfan

スコア4920

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  やりたいことが理解できていないかもしれませんが、`io.StringIO`か`io.BytesIO`を使ってみてはどうでしょう。
2
2
 
3
3
  ```python
4
- import os
4
+ import io
5
5
  import sys
6
6
 
7
7
  out = io.StringIO()