[Python-checkins] cpython (2.7): Issue #19656: Running Python with the -3 option now also warns about

serhiy.storchaka python-checkins at python.org
Sun Jun 1 21:14:00 CEST 2014


http://hg.python.org/cpython/rev/6bd21268876e
changeset:   90946:6bd21268876e
branch:      2.7
parent:      90943:fcbb15edb73a
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Jun 01 22:13:39 2014 +0300
summary:
  Issue #19656: Running Python with the -3 option now also warns about
non-ascii bytes literals.

files:
  Lib/test/test_py3kwarn.py |   5 +++++
  Misc/NEWS                 |   3 +++
  Python/ast.c              |  22 +++++++++++++++++-----
  3 files changed, 25 insertions(+), 5 deletions(-)


diff --git a/Lib/test/test_py3kwarn.py b/Lib/test/test_py3kwarn.py
--- a/Lib/test/test_py3kwarn.py
+++ b/Lib/test/test_py3kwarn.py
@@ -307,6 +307,11 @@
             w.reset()
             self.assertWarning(sequenceIncludes(range(3), 2), w, seq_warn)
 
+    def test_nonascii_bytes_literals(self):
+        expected = "non-ascii bytes literals not supported in 3.x"
+        with check_py3k_warnings((expected, SyntaxWarning)):
+            exec "b'\xbd'"
+
 
 class TestStdlibRemovals(unittest.TestCase):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #19656: Running Python with the -3 option now also warns about
+  non-ascii bytes literals.
+
 - Issue #21523: Fix over-pessimistic computation of the stack effect of
   some opcodes in the compiler.  This also fixes a quadratic compilation
   time issue noticeable when compiling code with a large number of "and"
diff --git a/Python/ast.c b/Python/ast.c
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -37,7 +37,7 @@
 static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
 
 static PyObject *parsenumber(struct compiling *, const char *);
-static PyObject *parsestr(struct compiling *, const char *);
+static PyObject *parsestr(struct compiling *, const node *n, const char *);
 static PyObject *parsestrplus(struct compiling *, const node *n);
 
 #ifndef LINENO
@@ -3444,13 +3444,14 @@
  * parsestr parses it, and returns the decoded Python string object.
  */
 static PyObject *
-parsestr(struct compiling *c, const char *s)
+parsestr(struct compiling *c, const node *n, const char *s)
 {
-        size_t len;
+        size_t len, i;
         int quote = Py_CHARMASK(*s);
         int rawmode = 0;
         int need_encoding;
         int unicode = c->c_future_unicode;
+        int bytes = 0;
 
         if (isalpha(quote) || quote == '_') {
                 if (quote == 'u' || quote == 'U') {
@@ -3460,6 +3461,7 @@
                 if (quote == 'b' || quote == 'B') {
                         quote = *++s;
                         unicode = 0;
+                        bytes = 1;
                 }
                 if (quote == 'r' || quote == 'R') {
                         quote = *++s;
@@ -3489,6 +3491,16 @@
                         return NULL;
                 }
         }
+        if (Py_Py3kWarningFlag && bytes) {
+            for (i = 0; i < len; i++) {
+                if ((unsigned char)s[i] > 127) {
+                    if (!ast_warn(c, n,
+                        "non-ascii bytes literals not supported in 3.x"))
+                        return NULL;
+                    break;
+                }
+            }
+        }
 #ifdef Py_USING_UNICODE
         if (unicode || Py_UnicodeFlag) {
                 return decode_unicode(c, s, len, rawmode, c->c_encoding);
@@ -3531,11 +3543,11 @@
         PyObject *v;
         int i;
         REQ(CHILD(n, 0), STRING);
-        if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
+        if ((v = parsestr(c, n, STR(CHILD(n, 0)))) != NULL) {
                 /* String literal concatenation */
                 for (i = 1; i < NCH(n); i++) {
                         PyObject *s;
-                        s = parsestr(c, STR(CHILD(n, i)));
+                        s = parsestr(c, n, STR(CHILD(n, i)));
                         if (s == NULL)
                                 goto onError;
                         if (PyString_Check(v) && PyString_Check(s)) {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list