diff options
author | Thomas Wouters <thomas@python.org> | 2007-02-23 19:56:57 (GMT) |
---|---|---|
committer | Thomas Wouters <thomas@python.org> | 2007-02-23 19:56:57 (GMT) |
commit | 00e41defe8801ef37548fb60abacb3be13156d2a (patch) | |
tree | 863d072e568fee2b8f4959016b5954de457c7f4c /Python | |
parent | cf297e46b85257396560774e5492e9d71a40f32e (diff) | |
download | cpython-00e41defe8801ef37548fb60abacb3be13156d2a.zip cpython-00e41defe8801ef37548fb60abacb3be13156d2a.tar.gz cpython-00e41defe8801ef37548fb60abacb3be13156d2a.tar.bz2 |
Bytes literal.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/Python-ast.c | 41 | ||||
-rw-r--r-- | Python/ast.c | 50 | ||||
-rw-r--r-- | Python/ceval.c | 13 | ||||
-rw-r--r-- | Python/compile.c | 7 |
4 files changed, 99 insertions, 12 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c index ae3a396..390ba15 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -2,7 +2,7 @@ /* - __version__ 53731. + __version__ 53866. This module must be committed separately after each AST grammar change; The __version__ number is set to the revision number of the commit @@ -216,6 +216,10 @@ static PyTypeObject *Str_type; static char *Str_fields[]={ "s", }; +static PyTypeObject *Bytes_type; +static char *Bytes_fields[]={ + "s", +}; static PyTypeObject *Ellipsis_type; static PyTypeObject *Attribute_type; static char *Attribute_fields[]={ @@ -547,6 +551,8 @@ static int init_types(void) if (!Num_type) return 0; Str_type = make_type("Str", expr_type, Str_fields, 1); if (!Str_type) return 0; + Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1); + if (!Bytes_type) return 0; Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0); if (!Ellipsis_type) return 0; Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3); @@ -1587,6 +1593,27 @@ Str(string s, int lineno, int col_offset, PyArena *arena) } expr_ty +Bytes(string s, int lineno, int col_offset, PyArena *arena) +{ + expr_ty p; + if (!s) { + PyErr_SetString(PyExc_ValueError, + "field s is required for Bytes"); + return NULL; + } + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); + if (!p) { + PyErr_NoMemory(); + return NULL; + } + p->kind = Bytes_kind; + p->v.Bytes.s = s; + p->lineno = lineno; + p->col_offset = col_offset; + return p; +} + +expr_ty Ellipsis(int lineno, int col_offset, PyArena *arena) { expr_ty p; @@ -2550,6 +2577,15 @@ ast2obj_expr(void* _o) goto failed; Py_DECREF(value); break; + case Bytes_kind: + result = PyType_GenericNew(Bytes_type, NULL, NULL); + if (!result) goto failed; + value = ast2obj_string(o->v.Bytes.s); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "s", value) == -1) + goto failed; + Py_DECREF(value); + break; case Ellipsis_kind: result = PyType_GenericNew(Ellipsis_type, NULL, NULL); if (!result) goto failed; @@ -3089,7 +3125,7 @@ init_ast(void) if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return; if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0) return; - if (PyModule_AddStringConstant(m, "__version__", "53731") < 0) + if (PyModule_AddStringConstant(m, "__version__", "53866") < 0) return; if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return; if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) @@ -3155,6 +3191,7 @@ init_ast(void) if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return; if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return; if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return; + if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return; if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0) return; if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < diff --git a/Python/ast.c b/Python/ast.c index a7d5713..9d5caf8 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -33,8 +33,9 @@ static expr_ty ast_for_testlist_gexp(struct compiling *, const node *); static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); static PyObject *parsenumber(const char *); -static PyObject *parsestr(const char *s, const char *encoding); -static PyObject *parsestrplus(struct compiling *, const node *n); +static PyObject *parsestr(const node *n, const char *encoding, int *bytesmode); +static PyObject *parsestrplus(struct compiling *, const node *n, + int *bytesmode); #ifndef LINENO #define LINENO(n) ((n)->n_lineno) @@ -1383,6 +1384,7 @@ ast_for_atom(struct compiling *c, const node *n) | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING+ */ node *ch = CHILD(n, 0); + int bytesmode = 0; switch (TYPE(ch)) { case NAME: @@ -1390,12 +1392,15 @@ ast_for_atom(struct compiling *c, const node *n) changed. */ return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena); case STRING: { - PyObject *str = parsestrplus(c, n); + PyObject *str = parsestrplus(c, n, &bytesmode); if (!str) return NULL; PyArena_AddPyObject(c->c_arena, str); - return Str(str, LINENO(n), n->n_col_offset, c->c_arena); + if (bytesmode) + return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena); + else + return Str(str, LINENO(n), n->n_col_offset, c->c_arena); } case NUMBER: { PyObject *pynum = parsenumber(STR(ch)); @@ -3254,9 +3259,10 @@ decode_unicode(const char *s, size_t len, int rawmode, const char *encoding) * parsestr parses it, and returns the decoded Python string object. */ static PyObject * -parsestr(const char *s, const char *encoding) +parsestr(const node *n, const char *encoding, int *bytesmode) { size_t len; + const char *s = STR(n); int quote = Py_CHARMASK(*s); int rawmode = 0; int need_encoding; @@ -3267,6 +3273,10 @@ parsestr(const char *s, const char *encoding) quote = *++s; unicode = 1; } + if (quote == 'b' || quote == 'B') { + quote = *++s; + *bytesmode = 1; + } if (quote == 'r' || quote == 'R') { quote = *++s; rawmode = 1; @@ -3276,6 +3286,10 @@ parsestr(const char *s, const char *encoding) PyErr_BadInternalCall(); return NULL; } + if (unicode && *bytesmode) { + ast_error(n, "string cannot be both bytes and unicode"); + return NULL; + } s++; len = strlen(s); if (len > INT_MAX) { @@ -3300,7 +3314,18 @@ parsestr(const char *s, const char *encoding) return decode_unicode(s, len, rawmode, encoding); } #endif - need_encoding = (encoding != NULL && + if (*bytesmode) { + /* Disallow non-ascii characters (but not escapes) */ + const char *c; + for (c = s; *c; c++) { + if (Py_CHARMASK(*c) >= 0x80) { + ast_error(n, "bytes can only contain ASCII " + "literal characters."); + return NULL; + } + } + } + need_encoding = (!*bytesmode && encoding != NULL && strcmp(encoding, "utf-8") != 0 && strcmp(encoding, "iso-8859-1") != 0); if (rawmode || strchr(s, '\\') == NULL) { @@ -3332,18 +3357,25 @@ parsestr(const char *s, const char *encoding) * pasting the intermediate results together. */ static PyObject * -parsestrplus(struct compiling *c, const node *n) +parsestrplus(struct compiling *c, const node *n, int *bytesmode) { PyObject *v; int i; REQ(CHILD(n, 0), STRING); - if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) { + v = parsestr(CHILD(n, 0), c->c_encoding, bytesmode); + if (v != NULL) { /* String literal concatenation */ for (i = 1; i < NCH(n); i++) { PyObject *s; - s = parsestr(STR(CHILD(n, i)), c->c_encoding); + int subbm = 0; + s = parsestr(CHILD(n, i), c->c_encoding, &subbm); if (s == NULL) goto onError; + if (*bytesmode != subbm) { + ast_error(n, "cannot mix bytes and nonbytes" + "literals"); + goto onError; + } if (PyString_Check(v) && PyString_Check(s)) { PyString_ConcatAndDel(&v, s); if (v == NULL) diff --git a/Python/ceval.c b/Python/ceval.c index 0194687..5ceb743 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1885,6 +1885,19 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) PUSH(x); if (x != NULL) continue; break; + + case MAKE_BYTES: + w = POP(); + if (PyString_Check(w)) + x = PyBytes_FromStringAndSize( + PyString_AS_STRING(w), + PyString_GET_SIZE(w)); + else + x = NULL; + Py_DECREF(w); + PUSH(x); + if (x != NULL) continue; + break; case LOAD_ATTR: w = GETITEM(names, oparg); diff --git a/Python/compile.c b/Python/compile.c index 927569a..9655765 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -789,6 +789,8 @@ opcode_stack_effect(int opcode, int oparg) return 1-oparg; case BUILD_MAP: return 1; + case MAKE_BYTES: + return 0; case LOAD_ATTR: return 0; case COMPARE_OP: @@ -3077,6 +3079,10 @@ compiler_visit_expr(struct compiler *c, expr_ty e) case Str_kind: ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts); break; + case Bytes_kind: + ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts); + ADDOP(c, MAKE_BYTES); + break; case Ellipsis_kind: ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts); break; @@ -3426,7 +3432,6 @@ compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) return compiler_handle_subscr(c, kindname, ctx); } - /* End of the compiler section, beginning of the assembler section */ /* do depth-first search of basic block graph, starting with block. |