是否有捷径可寻?我从来没有在任何地方见过这个(除了 Adobe/... Firefox 插件),所以我对此表示怀疑......
如果没有,是否有一种可靠的、hacky 的方法(例如Xlib通过 挂钩到该进程的调用LD_PRELOAD)?
如果重要的话,假设外部进程是mplayer,我的编程语言是C。我有一种预感,Xlib直接使用是我最好的选择,但请随意建议其他选择。仅有mplayer- 的解决方案是不够的。
在调用cython命令行时,可以告诉它创建一个int main()嵌入Python解释器的方法:
$ cython --embed main.pyx
$ grep 'int main' main.c
int main(int argc, char** argv) {
Run Code Online (Sandbox Code Playgroud)
但是,当您import Cython直接(例如从distutils setup.py脚本)时,该embed选项似乎被忽略:
$ python3
>>> from Cython.Compiler import Options
>>> Options.embed = True
>>> from Cython.Build import cythonize
>>> cythonize('main.pyx')
[1/1] Cythonizing main.pyx
>>>
$ grep 'int main' main.c
$
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
我正在编写一个在终端上生成彩色输出的linux程序.由于程序标准输出可以重定向到文本文件,或者通常重定向到非终端接收器,并且方法应该保持尽可能通用,我需要调用isatty(int fd)以确定是否应该发送ASCII颜色转义码.
由于我不确定在每次调用printf()之前调用isatty()对性能的影响,我已经实现了一个缓冲区,缓冲了前16个fds的isatty()结果:
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#define TERM_NORMAL "\x1b\x5bm"
#define TERM_BRIGHT "\x1b\x5b\x31m"
#define TERM_BOLD "\x1b\x5b\x31m"
#define TERM_BLINK "\x1b\x5b\x35m"
#define TERM_RED "\x1b\x5b\x33\x31m"
//to prevent unnecessary isatty() calls, provide this lookup table
//for the first 16 fds
//0 means that it has not been checked yet
//1 means the fd is not a tty
//2 means the fd is a tty
char isattybuf[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …Run Code Online (Sandbox Code Playgroud) int在调用repror 时pprint.pformat,是否有任何方法可以更改-type 对象转换为字符串的方式,以便
repr(dict(a=5, b=100))
Run Code Online (Sandbox Code Playgroud)
会给"{a: 0x5, b: 0x64}"而不是"{a: 5, b: 100}"?
我想子类化int类型将是一个选项:
class IntThatPrintsAsHex(int):
def __repr__(self):
return hex(self)
def preprocess_for_repr(obj):
if isinstance(obj, dict):
return {preprocess_for_repr(k): preprocess_for_repr(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [preprocess_for_repr(e) for e in obj]
elif isinstance(obj, tuple):
return tuple(preprocess_for_repr(e) for e in obj)
elif isinstance(obj, int) and not isinstance(obj, bool):
return IntThatPrintsAsHex(obj)
elif isinstance(obj, set):
return {preprocess_for_repr(e) for e in obj}
elif …Run Code Online (Sandbox Code Playgroud)