1818/* Data structure used internally */
1919struct compiling {
2020 char * c_encoding ; /* source encoding */
21+ int c_future_unicode ; /* __future__ unicode literals flag */
2122 PyArena * c_arena ; /* arena for allocating memeory */
2223 const char * c_filename ; /* filename */
2324};
@@ -36,7 +37,7 @@ static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
3637static expr_ty ast_for_call (struct compiling * , const node * , expr_ty );
3738
3839static PyObject * parsenumber (const char * );
39- static PyObject * parsestr (const char * s , const char * encoding );
40+ static PyObject * parsestr (struct compiling * , const char * );
4041static PyObject * parsestrplus (struct compiling * , const node * n );
4142
4243#ifndef LINENO
@@ -198,6 +199,7 @@ PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
198199 } else {
199200 c .c_encoding = NULL ;
200201 }
202+ c .c_future_unicode = flags && flags -> cf_flags & CO_FUTURE_UNICODE_LITERALS ;
201203 c .c_arena = arena ;
202204 c .c_filename = filename ;
203205
@@ -3247,13 +3249,13 @@ decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
32473249 * parsestr parses it, and returns the decoded Python string object.
32483250 */
32493251static PyObject *
3250- parsestr (const char * s , const char * encoding )
3252+ parsestr (struct compiling * c , const char * s )
32513253{
32523254 size_t len ;
32533255 int quote = Py_CHARMASK (* s );
32543256 int rawmode = 0 ;
32553257 int need_encoding ;
3256- int unicode = 0 ;
3258+ int unicode = c -> c_future_unicode ;
32573259
32583260 if (isalpha (quote ) || quote == '_' ) {
32593261 if (quote == 'u' || quote == 'U' ) {
@@ -3262,6 +3264,7 @@ parsestr(const char *s, const char *encoding)
32623264 }
32633265 if (quote == 'b' || quote == 'B' ) {
32643266 quote = * ++ s ;
3267+ unicode = 0 ;
32653268 }
32663269 if (quote == 'r' || quote == 'R' ) {
32673270 quote = * ++ s ;
@@ -3293,12 +3296,12 @@ parsestr(const char *s, const char *encoding)
32933296 }
32943297#ifdef Py_USING_UNICODE
32953298 if (unicode || Py_UnicodeFlag ) {
3296- return decode_unicode (s , len , rawmode , encoding );
3299+ return decode_unicode (s , len , rawmode , c -> c_encoding );
32973300 }
32983301#endif
3299- need_encoding = (encoding != NULL &&
3300- strcmp (encoding , "utf-8" ) != 0 &&
3301- strcmp (encoding , "iso-8859-1" ) != 0 );
3302+ need_encoding = (c -> c_encoding != NULL &&
3303+ strcmp (c -> c_encoding , "utf-8" ) != 0 &&
3304+ strcmp (c -> c_encoding , "iso-8859-1" ) != 0 );
33023305 if (rawmode || strchr (s , '\\' ) == NULL ) {
33033306 if (need_encoding ) {
33043307#ifndef Py_USING_UNICODE
@@ -3310,7 +3313,7 @@ parsestr(const char *s, const char *encoding)
33103313 PyObject * v , * u = PyUnicode_DecodeUTF8 (s , len , NULL );
33113314 if (u == NULL )
33123315 return NULL ;
3313- v = PyUnicode_AsEncodedString (u , encoding , NULL );
3316+ v = PyUnicode_AsEncodedString (u , c -> c_encoding , NULL );
33143317 Py_DECREF (u );
33153318 return v ;
33163319#endif
@@ -3320,7 +3323,7 @@ parsestr(const char *s, const char *encoding)
33203323 }
33213324
33223325 return PyString_DecodeEscape (s , len , NULL , unicode ,
3323- need_encoding ? encoding : NULL );
3326+ need_encoding ? c -> c_encoding : NULL );
33243327}
33253328
33263329/* Build a Python string object out of a STRING atom. This takes care of
@@ -3333,11 +3336,11 @@ parsestrplus(struct compiling *c, const node *n)
33333336 PyObject * v ;
33343337 int i ;
33353338 REQ (CHILD (n , 0 ), STRING );
3336- if ((v = parsestr (STR (CHILD (n , 0 )), c -> c_encoding )) != NULL ) {
3339+ if ((v = parsestr (c , STR (CHILD (n , 0 )))) != NULL ) {
33373340 /* String literal concatenation */
33383341 for (i = 1 ; i < NCH (n ); i ++ ) {
33393342 PyObject * s ;
3340- s = parsestr (STR (CHILD (n , i )), c -> c_encoding );
3343+ s = parsestr (c , STR (CHILD (n , i )));
33413344 if (s == NULL )
33423345 goto onError ;
33433346 if (PyString_Check (v ) && PyString_Check (s )) {
0 commit comments