Viewing 1 post (of 1 total)
  • Author
    Posts
  • #21382
    Yang Yang
    Participant

    Hi all, hello Yutaka, long time no see! Today I bump into a problem that the External Tool function fails to serve document content to the external tool. I’ve tested it with 32-bit v10.1.1 and 16.2.1, both on Windows 7 64 Simplified Chinese Edition. Here is the detail.

    For several years I’ve configured an external tool to run Python snippets on the fly. The external tool is actually a Go binary to call Python with document content as stdin, but here I’ll use an equivalent Python script for clarity:

    # PythonToolForEmEditor.py
    
    import subprocess
    import sys
    
    def main():
        PYTHON_CMD = r'D:\Tools\Python\python.exe -'
        ENCODING = 'utf-8'
    
        content = sys.stdin.buffer.read()  # read binary data; use ioutil.ReadAll() in Go
    
        # Should not happen: even an empty file has a newline to read.
        if not content:
            print(">>> Empty content! Debugging time. <<<", file=sys.stderr)
            return
    
        if content[-1] == 0x1a:  # strip possible trailing Ctrl-Z
            content = content[:-1]
        output = subprocess.run(PYTHON_CMD,
                                input=content,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT).stdout
        sys.stdout.buffer.write(output.decode(sys.stdout.encoding).encode(ENCODING))
    
    if __name__ == '__main__':
        main()
    

    It reads binary data from stdin, feeds it to the Python interpreter, and write the output to stdout. Both stdin and stdout are UTF-8 encoded.

    The External Tool is configured like this:

    External Tool configuration

    This has worked wonderfully for years without any issue (encoding related in particular). But with some documents, the “Should not happen” part does happen: nothing is read at all. A minimal example, min.py, is uploaded here (not pasted because space or encoding varieties may scare this issue away): https://ufile.io/78d15.

    Note that what really matters is the multi-line string in min.py. Removing any line from it will make the External Tool work. In contrast, type min.py | python PythonToolForEmEditor.py always works well.

    By the way, I’m using Python 3, so an encoding comment line is not strictly required.

    Any suggestion is appreciated. Thanks a lot!

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.