From eb4700d08d085dec678e94ae9add0afe738f17b4 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 10 May 2021 22:23:52 -0500 Subject: [PATCH] fix: set utf-8 encoding for Python --- lib/grammars/python.js | 13 +++++++++++++ spec/fixtures/issue_1166.py | 1 + 2 files changed, 14 insertions(+) create mode 100644 spec/fixtures/issue_1166.py diff --git a/lib/grammars/python.js b/lib/grammars/python.js index b29f1dae..f6be4b5d 100644 --- a/lib/grammars/python.js +++ b/lib/grammars/python.js @@ -2,10 +2,20 @@ import GrammarUtils from "../grammar-utils" +// https://github.com/atom-community/atom-script/issues/214#issuecomment-418766763 +let encodingSet = false +function setEncoding() { + if (!encodingSet) { + process.env.PYTHONIOENCODING = "utf-8" + encodingSet = true + } +} + export const Python = { "Selection Based": { command: "python", args(context) { + setEncoding() const code = context.getCode() const tmpFile = GrammarUtils.createTempFileWithCode(code) return ["-u", tmpFile] @@ -15,6 +25,7 @@ export const Python = { "File Based": { command: "python", args({ filepath }) { + setEncoding() return ["-u", filepath] }, }, @@ -26,6 +37,7 @@ export const Sage = { "Selection Based": { command: "sage", args(context) { + setEncoding() const code = context.getCode() const tmpFile = GrammarUtils.createTempFileWithCode(code) return [tmpFile] @@ -35,6 +47,7 @@ export const Sage = { "File Based": { command: "sage", args({ filepath }) { + setEncoding() return [filepath] }, }, diff --git a/spec/fixtures/issue_1166.py b/spec/fixtures/issue_1166.py new file mode 100644 index 00000000..da2d21e0 --- /dev/null +++ b/spec/fixtures/issue_1166.py @@ -0,0 +1 @@ +print ('汉语/漢語')