Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 019934b

Browse files
committed
Fix PEP 263 code --without-unicode. Fixes #591943.
1 parent 63d5bea commit 019934b

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

Parser/tokenizer.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,18 @@ check_coding_spec(const char* line, int size, struct tok_state *tok,
256256
strcmp(cs, "iso-8859-1") == 0) {
257257
tok->encoding = cs;
258258
} else {
259+
#ifdef Py_USING_UNICODE
259260
r = set_readline(tok, cs);
260261
if (r) {
261262
tok->encoding = cs;
262263
tok->decoding_state = -1;
263264
}
265+
#else
266+
/* Without Unicode support, we cannot
267+
process the coding spec. Since there
268+
won't be any Unicode literals, that
269+
won't matter. */
270+
#endif
264271
}
265272
} else { /* then, compare cs with BOM */
266273
r = (strcmp(tok->encoding, cs) == 0);
@@ -317,6 +324,10 @@ check_bom(int get_char(struct tok_state *),
317324
static char *
318325
fp_readl(char *s, int size, struct tok_state *tok)
319326
{
327+
#ifndef Py_USING_UNICODE
328+
/* In a non-Unicode built, this should never be called. */
329+
abort();
330+
#else
320331
PyObject* utf8;
321332
PyObject* buf = tok->decoding_buffer;
322333
if (buf == NULL) {
@@ -338,6 +349,7 @@ fp_readl(char *s, int size, struct tok_state *tok)
338349
if (s[0] == '\0') return NULL; /* EOF */
339350
return s;
340351
}
352+
#endif
341353
}
342354

343355
/* Set the readline function for TOK to a StreamReader's
@@ -487,6 +499,7 @@ static int buf_setreadl(struct tok_state *tok, const char* enc) {
487499
/* Return a UTF-8 encoding Python string object from the
488500
C byte string STR, which is encoded with ENC. */
489501

502+
#ifdef Py_USING_UNICODE
490503
static PyObject *
491504
translate_into_utf8(const char* str, const char* enc) {
492505
PyObject *utf8;
@@ -497,6 +510,7 @@ translate_into_utf8(const char* str, const char* enc) {
497510
Py_DECREF(buf);
498511
return utf8;
499512
}
513+
#endif
500514

501515
/* Decode a byte string STR for use as the buffer of TOK.
502516
Look for encoding declarations inside STR, and record them
@@ -514,12 +528,14 @@ decode_str(const char *str, struct tok_state *tok)
514528
return NULL;
515529
str = tok->str; /* string after BOM if any */
516530
assert(str);
531+
#ifdef Py_USING_UNICODE
517532
if (tok->enc != NULL) {
518533
utf8 = translate_into_utf8(str, tok->enc);
519534
if (utf8 == NULL)
520535
return NULL;
521536
str = PyString_AsString(utf8);
522537
}
538+
#endif
523539
for (s = str;; s++) {
524540
if (*s == '\0') break;
525541
else if (*s == '\n') {
@@ -530,13 +546,15 @@ decode_str(const char *str, struct tok_state *tok)
530546
tok->enc = NULL;
531547
if (!check_coding_spec(str, s - str, tok, buf_setreadl))
532548
return NULL;
549+
#ifdef Py_USING_UNICODE
533550
if (tok->enc != NULL) {
534551
assert(utf8 == NULL);
535552
utf8 = translate_into_utf8(str, tok->enc);
536553
if (utf8 == NULL)
537554
return NULL;
538555
str = PyString_AsString(utf8);
539556
}
557+
#endif
540558
assert(tok->decoding_buffer == NULL);
541559
tok->decoding_buffer = utf8; /* CAUTION */
542560
return str;

Python/compile.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,9 @@ parsenumber(struct compiling *co, char *s)
11851185
static PyObject *
11861186
decode_utf8(char **sPtr, char *end, char* encoding)
11871187
{
1188+
#ifndef Py_USING_UNICODE
1189+
abort();
1190+
#else
11881191
PyObject *u, *v;
11891192
char *s, *t;
11901193
t = s = *sPtr;
@@ -1197,6 +1200,7 @@ decode_utf8(char **sPtr, char *end, char* encoding)
11971200
v = PyUnicode_AsEncodedString(u, encoding, NULL);
11981201
Py_DECREF(u);
11991202
return v;
1203+
#endif
12001204
}
12011205

12021206
static PyObject *
@@ -1312,12 +1316,18 @@ parsestr(struct compiling *com, char *s)
13121316
strcmp(encoding, "iso-8859-1") != 0);
13131317
if (rawmode || strchr(s, '\\') == NULL) {
13141318
if (need_encoding) {
1319+
#ifndef Py_USING_UNICODE
1320+
/* This should not happen - we never see any other
1321+
encoding. */
1322+
abort();
1323+
#else
13151324
PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL);
13161325
if (u == NULL)
13171326
return NULL;
13181327
v = PyUnicode_AsEncodedString(u, encoding, NULL);
13191328
Py_DECREF(u);
13201329
return v;
1330+
#endif
13211331
} else {
13221332
return PyString_FromStringAndSize(s, len);
13231333
}

0 commit comments

Comments
 (0)