From 48e58859feaf43a4cd226f6e84a2b38f55866db1 Mon Sep 17 00:00:00 2001 From: Tim Dawborn Date: Thu, 22 Dec 2016 15:51:23 +1100 Subject: [PATCH] Change the Python codegen for string quoting to match the behaviour of `repr` on a string in CPython. --- generators/python.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/generators/python.js b/generators/python.js index 3b7aa5727..390ee0482 100644 --- a/generators/python.js +++ b/generators/python.js @@ -193,9 +193,18 @@ Blockly.Python.quote_ = function(string) { // Can't use goog.string.quote since % must also be escaped. string = string.replace(/\\/g, '\\\\') .replace(/\n/g, '\\\n') - .replace(/\%/g, '\\%') - .replace(/'/g, '\\\''); - return '\'' + string + '\''; + .replace(/\%/g, '\\%'); + + // Follow the CPython behaviour of repr() for a non-byte string. + var quote = '\''; + if (string.indexOf('\'') !== -1) { + if (string.indexOf('"') === -1) { + quote = '"'; + } else { + string = string.replace(/'/g, '\\\''); + } + }; + return quote + string + quote; }; /**