Python throws an error when the clipboard contains newline.
allrobot opened this issue · comments
Description
I assigned the content of the clipboard to a variable in Python, but Espanso detected newline characters and caused a SyntaxError: EOL (End of Line) while scanning a string literal.
If the content of the clipboard does not contain newline characters, it won't cause an error. Python will handle it normally and produce the output.
- trigger: :st
replace: "{{output}}"
vars:
- name: string
type: clipboard
- name: output
type: script
params:
args:
- python
- -c
- |
import re
clipboard_string="{{string}}"
# 获取剪切板文本内容
if '\n' not in clipboard_string:
# 删掉一串英文的首个英文,转换.+
clipboard_string=re.sub('(\s+|)(?:[^a-zA-Z/\r\n]|^)(\s+|)[a-zA-Z]|(?<=^/)[A-Za-z]','.+',clipboard_string)
clipboard_string=re.sub(r'([\s\S]+)',r'/\1/',clipboard_string)
# output
print(clipboard_string)
else:
print()
Steps to reproduce
Expected behavior
I would like Espanso to handle the content of the clipboard without throwing an error, even if it contains '\n'. It should continue executing the script without any issues.
Screenshots
Added.
Logs
16:38:08 [worker(66020)] [WARN] script command exited with code: exit code: 1 and error: File "", line 3
clipboard_string="if '\n' not in clipboard_string:
^
SyntaxError: EOL while scanning string literal
16:38:08 [worker(66020)] [WARN] extension 'script' on var: 'output' reported an error: script reported error: ' File "<string>", line 3 clipboard_string="if '\n' not in clipboard_string: ^ SyntaxError: EOL while scanning string literal
'
16:38:08 [worker(66020)] [ERROR] error during rendering: rendering error
Caused by:
script reported error: ' File "<string>", line 3 clipboard_string="if '\n' not in clipboard_string: ^ SyntaxError: EOL while scanning string literal
'
Your environment
- Public source code: python311
- Environment name and version (e.g., Rust 1.59.0): ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x64-mingw-ucrt]
- Operating system and version (e.g., Ubuntu 20.04.2 LTS): Windows10 Enterprise 21H2
- Espanso version (e.g., 2.1.4-beta): espanso 2.1.5-beta
Self-service
- I'd be willing to fix this bug myself.
I think this is an issue with Python, rather than Espanso:
The following test script works with multiline clipboard content if I include the triple quote-marks:
- trigger: :st
replace: "{{output}}"
vars:
- name: string
type: clipboard
- name: output
type: script
params:
args:
- python
- -c
- |
print("""{{string}}""")
However, it will fall over if the copied text contains unescaped double-quotes.
However, it will fall over if the copied text contains unescaped double-quotes.
You are correct. When I use print(sys.version), I found that it is calling the Python 2 version. I will changed it to Python 3.11 in the path. However, I noticed that the output is displaying garbled characters. Where should I set the correct encoding to ensure that it outputs correctly without displaying garbled characters?
https://www.ff98sha.me/tools/messycode/
I found that it doesn't seem to be an encoding issue because I can't find the corresponding original text for the garbled characters: 切板文本内容
This note in the docs, perhaps?