为什么我的 python 交互式控制台无法正常工作?

发布于 2024-09-29 16:15:38 字数 1856 浏览 0 评论 0原文

我制作了一个非常简单的交互式控制台,我想在复杂的抓取应用程序中使用它。它看起来像这样:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys, codecs, code
sys.__stdout__ = codecs.getwriter('utf8')(sys.__stdout__)
sys.__stderr__ = codecs.getwriter('utf8')(sys.__stderr__)

if 'DEBUG' in os.environ:
    import pdb 
    import sys 
    oeh = sys.excepthook
    def debug_exceptions(type, value, traceback):
        pdb.post_mortem(traceback)
        oeh(type, value, traceback)
    sys.excepthook = debug_exceptions

class CLI(code.InteractiveConsole):
    def __init__(self, locals=None, filename="<console>", histfile=None):
        code.InteractiveConsole.__init__(self, locals, filename)
        try:
            import readline
        except ImportError:
            pass
        else:
            try:
                import rlcompleter
                readline.set_completer(rlcompleter.Completer(locals).complete)
            except ImportError:
                pass
            readline.parse_and_bind("tab: complete")
        self.interact()

if __name__ == "__main__":
    hello="I am a local"
    CLI(locals=locals())

如果我从另一个简单的应用程序调用它,它工作得很好:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys, codecs, cli
sys.__stdout__ = codecs.getwriter('utf8')(sys.__stdout__)
sys.__stderr__ = codecs.getwriter('utf8')(sys.__stderr__)

from cli import CLI

foo="i am a local"
CLI(locals=locals())

但是,当我从我的抓取框架调用它时,该框架目前基于 twill 和 mechanize (尽管我打算将其切换到 gevent)以完全相同的方式打开 CLI,箭头键不起作用,制表符完成不起作用,事实上它的行为就像 readline 不存在一样。我尝试重新加载 readline 模块并直接向其传递 parse_and_bind 命令,但由于某种原因它无法正常播放。有什么提示或建议说明是什么被破坏了,导致它无法按预期工作,还是我必须删除所有正在使用的外部模块并将它们一一放入,看看发生了什么?

我对 twill 表示怀疑,因为它有自己的基本 CLI,但如果有人知道,我会很高兴知道是否有人知道发生了什么。

哦,请不要评论我对 stderr 和 stdout 所做的事情,这只是放入 python 文件中的样板代码,我总是从 utf8 控制台运行它们,这不是我要问的......

I made a very simple interactive console that I'd like to use in a complicated scraping application. It looks like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys, codecs, code
sys.__stdout__ = codecs.getwriter('utf8')(sys.__stdout__)
sys.__stderr__ = codecs.getwriter('utf8')(sys.__stderr__)

if 'DEBUG' in os.environ:
    import pdb 
    import sys 
    oeh = sys.excepthook
    def debug_exceptions(type, value, traceback):
        pdb.post_mortem(traceback)
        oeh(type, value, traceback)
    sys.excepthook = debug_exceptions

class CLI(code.InteractiveConsole):
    def __init__(self, locals=None, filename="<console>", histfile=None):
        code.InteractiveConsole.__init__(self, locals, filename)
        try:
            import readline
        except ImportError:
            pass
        else:
            try:
                import rlcompleter
                readline.set_completer(rlcompleter.Completer(locals).complete)
            except ImportError:
                pass
            readline.parse_and_bind("tab: complete")
        self.interact()

if __name__ == "__main__":
    hello="I am a local"
    CLI(locals=locals())

If I call it from another simple application, it works just fine:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys, codecs, cli
sys.__stdout__ = codecs.getwriter('utf8')(sys.__stdout__)
sys.__stderr__ = codecs.getwriter('utf8')(sys.__stderr__)

from cli import CLI

foo="i am a local"
CLI(locals=locals())

However, when I call it from my scraping framework which is based off twill and mechanize for now (though I intend to switch it to gevent) when call up the CLI in exactly the same way, the arrow keys don't work, tab completion doesn't work, in fact it behaves like readline doesn't exist. I've tried reloading the readline module and passing it direct parse_and_bind commands but for some reason it just will not play properly. Any hints or suggestions as to what has been clobbered that is preventing it from working as expected or am I just going to have to remove all the external modules in use and put them in, one by one to see what happened?

I'm suspicious of twill seeing as it has it's own basic CLI but if anyone knows I'd be very happy to know if anyone has a good idea what's going on.

Oh and please no comments about what I'm doing with stderr and stdout, it's just boilerplate code that gets put into python files, I always run them from utf8 consoles and it's not what I'm asking about...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒几许 2024-10-06 16:15:38

好的,我发现是我导致了问题,在我注意到它在某些情况下引起问题之前,我的旧样板代码已经习惯了:

sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)

旧代码仍然存在于我自己的文件之一中,将该代码更改为较低级别的版本:

sys.__stdout__ = codecs.getwriter('utf8')(sys.__stdout__)
sys.__stderr__ = codecs.getwriter('utf8')(sys.__stderr__)

或者完全删除它,因为它不需要位于该文件中,无论如何都可以解决问题。

OK, I found out it was ME that was causing the problem, my older boiler-plate code used to this before I noticed it causing problems in some cases:

sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)

That old code was still present in one of my own files, changing that code to the lower-level version of:

sys.__stdout__ = codecs.getwriter('utf8')(sys.__stdout__)
sys.__stderr__ = codecs.getwriter('utf8')(sys.__stderr__)

Or removing it completely, since it didn't need to be in that file anyway fixed the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文