在bash Windows 10中使用lolcat

出于明显的原因,我试图在终端上使用lolcat,但是由于某些东西不重要,因此无法正常工作。

$ lolcat
Traceback (most recent call last):
File "C:/Users/eriku/Anaconda3/Scripts/lolcat",line 18,in <module>
from signal import signal,SIGPIPE,SIG_DFL
ImportError: cannot import name 'SIGPIPE' from 'signal' (C:\users\eriku\anaconda3\lib\signal.py)

此引用的导入是以下python代码:

import _signal
from _signal import *
from functools import wraps as _wraps
from enum import IntEnum as _IntEnum

    _globals = globals()

    _IntEnum._convert(
            'Signals',__name__,lambda name:
                name.isupper()
                and (name.startswith('SIG') and not name.startswith('SIG_'))
                or name.startswith('CTRL_'))

    _IntEnum._convert(
            'Handlers',lambda name: name in ('SIG_DFL','SIG_IGN'))

    if 'pthread_sigmask' in _globals:
        _IntEnum._convert(
                'Sigmasks',lambda name: name in ('SIG_BLOCK','SIG_UNBLOCK','SIG_SETMASK'))


    def _int_to_enum(value,enum_klass):
        """Convert a numeric value to an IntEnum member.
        If it's not a known member,return the numeric value itself.
        """
        try:
            return enum_klass(value)
        except ValueError:
            return value


    def _enum_to_int(value):
        """Convert an IntEnum member to a numeric value.
        If it's not an IntEnum member return the value itself.
        """
        try:
            return int(value)
        except (ValueError,TypeError):
            return value


    @_wraps(_signal.signal)
    def signal(signalnum,handler):
        handler = _signal.signal(_enum_to_int(signalnum),_enum_to_int(handler))
        return _int_to_enum(handler,Handlers)


    @_wraps(_signal.getsignal)
    def getsignal(signalnum):
        handler = _signal.getsignal(signalnum)
        return _int_to_enum(handler,Handlers)


    if 'pthread_sigmask' in _globals:
        @_wraps(_signal.pthread_sigmask)
        def pthread_sigmask(how,mask):
            sigs_set = _signal.pthread_sigmask(how,mask)
            return set(_int_to_enum(x,Signals) for x in sigs_set)
        pthread_sigmask.__doc__ = _signal.pthread_sigmask.__doc__


    if 'sigpending' in _globals:
        @_wraps(_signal.sigpending)
        def sigpending():
            sigs = _signal.sigpending()
            return set(_int_to_enum(x,Signals) for x in sigs)


    if 'sigwait' in _globals:
        @_wraps(_signal.sigwait)
        def sigwait(sigset):
            retsig = _signal.sigwait(sigset)
            return _int_to_enum(retsig,Signals)
        sigwait.__doc__ = _signal.sigwait

    del _globals,_wraps

这显然不是我的代码,它是计算机上Anaconda安装的一部分。我也尝试在Anaconda提示符下执行lolcat,因为当涉及到不同的终端时,它可能会非常占优势。有什么想法吗?

love14521 回答:在bash Windows 10中使用lolcat

检查 lolcat's source code 表明这是 Python CLI 程序尝试 avoid printing IOError: [Errno 32] Broken pipe when receiving SIGPIPE on Linux 但忘记 Windows 的典型问题。

我在我的代码中使用以下函数,而其他人可能更喜欢 check if sys.platform == "win32"

def reset_sigpipe_handling():
    """Restore the default `SIGPIPE` handler on supporting platforms.
    
    Python's `signal` library traps the SIGPIPE signal and translates it
    into an IOError exception,forcing the caller to handle it explicitly.

    Simpler applications that would rather silently die can revert to the
    default handler. See https://stackoverflow.com/a/30091579/1026 for details.
    """
    try:
        from signal import signal,SIGPIPE,SIG_DFL
        signal(SIGPIPE,SIG_DFL)
    except ImportError:  # If SIGPIPE is not available (win32),pass             # we don't have to do anything to ignore it.

另请注意 official docs recommend handling the exception instead:

不要将 SIGPIPE 的处置设置为 SIG_DFL 以避免 BrokenPipeError。这样做会导致您的程序意外退出,只要您的程序仍在写入时任何套接字连接被中断。

...但是该解决方案建议不完整(如果您需要在不重置 SIGPIPE 处理程序的情况下解决此问题,请参阅 this)。

最后,"OSError: [Errno 22] Invalid argument" on Windows with print() and output piped 表明根本问题也存在于 Windows 上,但表现为 “OSError: [Errno 22] Invalid argument” 而不是 BrokenPipeError。

,

Windows似乎不支持SIGPIPE

请参见https://docs.python.org/3/library/signal.html#module-contents上的 SIG* 部分:

  

请注意,并非所有系统都定义相同的信号名称集。此模块仅定义系统定义的那些名称。

本文链接:https://www.f2er.com/3156733.html

大家都在问