- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我尝试记录用 Python 和 C 编写的程序的所有输出。但是,从 Python 打印会导致 IOError: [Errno 9] Bad file descriptor
请问有人知道问题出在哪里以及如何解决吗?
PS:它在 Windows XP、Python 2.6 和 MinGW GCC 上
#include <windows.h>
#include <fcntl.h>
#include "Python.h"
int main()
{
int fds[2];
_pipe(fds, 1024, O_BINARY);
_dup2(fds[1], 1);
setvbuf(stdout, NULL, _IONBF, 0);
/* alternative version: */
// HANDLE hReadPipe, hWritePipe;
// int fd;
// DWORD nr;
// CreatePipe(&hReadPipe, &hWritePipe, NULL, 0);
// fd = _open_osfhandle((intptr_t)hWritePipe, _O_BINARY);
// _dup2(fd, 1);
// setvbuf(stdout, NULL, _IONBF, 0);
write(1, "write\n", 6);
printf("printf\n");
Py_Initialize();
PyRun_SimpleString("print 'print'"); // this breaks
Py_Finalize();
char buffer[1024];
fprintf(stderr, "buffer size: %d\n", read(fds[0], buffer, 1024)); // should always be more than 0
/* alternative version: */
// CloseHandle(hWritePipe);
// char buffer[1024];
// ReadFile(hReadPipe, buffer, 1024, &nr, NULL);
// fprintf(stderr, "buffer size: %d\n", nr); // should always be more than 0
}
最佳答案
我认为这可能与不同的 C 运行时有关。我知道你不能在不同的 C 运行时之间传递文件描述符 - Python 是用 MSVC 构建的(你需要检查哪个版本) - 所以你可以尝试让 MinGW 针对相同的 C 运行时构建 - 我认为有一些选择这在 MinGW 中类似于 -lmsvcrt80(或任何合适的版本),但出于许可原因,他们无法分发这些库,因此您必须在您的系统上找到它们。抱歉,我现在没有更多详细信息,但希望这是谷歌搜索的一个开始。
一个更简单的方法是在 Python 中完成这一切......只需创建一个公开 write
和可能的 flush
方法的类并将其分配给 sys.标准输出。例如,对于一个文件,您可以只传递一个打开的文件对象——为您的管道做类似的事情可能很简单。然后只需导入它和 sys 并在 PyRun_SimpleString 中设置 sys.stdout。
关于 python ,C : redirected stdout fires [Errno 9],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1749762/
我有以下代码来捕获和处理运行命令输出。 如何修改它以便运行命令窗口显示输出并同时记录输出?更换 @SW_HIDE与 @SW_SHOW (或等效的)只显示一个空白的命令窗口。 类似于linux的东西te
[已编辑] 用于处理 subprocess.Popen 标准输出的 2 个选项是 stdout="a_file_name" 和 stdout=subprocess.PIPE。 stderr 可以通过
c99 的 7.19.3/7 节指出: At program start-up, three text streams are predefined and need not be opened ex
我正在运行以下 proc_open 函数。加载页面时,出现错误: Use of undefined constant STDOUT - assumed 'STDOUT'` 如何正确设置STDOUT和S
我有一个运行多个进程的开发堆栈:网络服务器、自动测试、后台编译等。所有这些都是基本的命令行命令,例如 node app.js 或 lein midje :autotest. 是否可以使用一个脚本在“后
我正在使用 SLURM 在 super 计算机上调度作业。我已设置 --output=log.out 选项,将作业标准输出中的内容放入文件 (log.out)。我发现该文件每 30-60 分钟更新一次
ansible/ansible-playbook 版本:2.1.2.0/2.2.0.0 我正在尝试使用 yum/apt 安装软件包,但由于安装软件包的存储库位于 packagecloud.io 中,有
bala@hp:~$ echo "Hello World" > stdout bala@hp:~$ cat stdout Hello World bala@hp:~$ echo "Hello Worl
在从 Fortran 编写的外部库中调用嘈杂的函数之前,我正在使用如下代码重定向标准输出: // copy standard output out = dup(STDOUT_FILENO); // c
这个问题在这里已经有了答案: How can I pipe stderr, and not stdout? (11 个答案) 关闭 6 年前。 我有一个程序,我想检查其 STDERR 输出并在其上运
我正在从 perl 运行一个 java 应用程序 这是脚本 #!/usr/bin/perl use strict; use warnings; $| = 1; my $args = join (" "
我正在尝试将 python 脚本的 STDOUT 重定向到一个文件。 如果 STDOUT 是从 sys 导入的,脚本的输出不会被重定向到一个文件: from sys import stdout std
我正在尝试使用 PHP 和 Apache(在 Docker 的前台运行)写入 stdout(或 stderr)。 这些作品: file_put_contents( "php://stderr","wo
我正在尝试重定向标准输出,以便 Windows 应用程序中的 printf 将转到我选择的文件。 我这样做: outFile = fopen("log.txt", "w"); *stdout = *o
在 Ruby 中,$stdout(前面有一个美元符号)和 STDOUT(全部大写)有什么区别?在进行输出重定向时,应该使用哪个,为什么? $stderr 和 STDERR 也是如此。 编辑: 刚找到一
让我们看看这个Hello World程序 #include int main(int argc, char ** argv) { printf("Hello, World!"); c
我在 64 位机器上运行 Ubuntu 20.04。 我想将 stdout 重定向到从 memfd_create 获得的描述符。似乎使用 memfd_create 创建的匿名文件只有在 stdout
我想在 Python 脚本末尾捕获控制台输出。也就是说,我既想正常打印到控制台,又想在执行结束时将控制台输出保存到文件中。 我看过各种相关的 SO 问题 1 , 2 , 3尽管他们要么简单地重定向输出
我知道可以将两者都重定向到特定文件: ./command 1> out.log 2> err.log 或 ./command 1>test.log 2>&1 将两者写入文件。但是我不知道在只打印其中一
我知道可以将两者都重定向到特定文件: ./command 1> out.log 2> err.log 或 ./command 1>test.log 2>&1 将两者写入文件。但是我不知道在只打印其中一
我是一名优秀的程序员,十分优秀!