Mercurial > cpython-lru_cache-weakref
comparison Python/Python-ast.c @ 71792:f578ca44193d
add a asdl bytes type, so Bytes.s be properly typechecked
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Tue, 09 Aug 2011 16:08:39 -0500 |
| parents | d3f0f72c31f8 |
| children | cfccc755c457 |
comparison
equal
deleted
inserted
replaced
| 71787:69b354a8c90f | 71792:f578ca44193d |
|---|---|
| 571 Py_INCREF((PyObject*)o); | 571 Py_INCREF((PyObject*)o); |
| 572 return (PyObject*)o; | 572 return (PyObject*)o; |
| 573 } | 573 } |
| 574 #define ast2obj_identifier ast2obj_object | 574 #define ast2obj_identifier ast2obj_object |
| 575 #define ast2obj_string ast2obj_object | 575 #define ast2obj_string ast2obj_object |
| 576 #define ast2obj_bytes ast2obj_object | |
| 576 | 577 |
| 577 static PyObject* ast2obj_int(long b) | 578 static PyObject* ast2obj_int(long b) |
| 578 { | 579 { |
| 579 return PyLong_FromLong(b); | 580 return PyLong_FromLong(b); |
| 580 } | 581 } |
| 603 | 604 |
| 604 static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena) | 605 static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena) |
| 605 { | 606 { |
| 606 if (!PyUnicode_CheckExact(obj)) { | 607 if (!PyUnicode_CheckExact(obj)) { |
| 607 PyErr_SetString(PyExc_TypeError, "AST string must be of type str"); | 608 PyErr_SetString(PyExc_TypeError, "AST string must be of type str"); |
| 609 return 1; | |
| 610 } | |
| 611 return obj2ast_object(obj, out, arena); | |
| 612 } | |
| 613 | |
| 614 static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena) | |
| 615 { | |
| 616 if (!PyBytes_CheckExact(obj)) { | |
| 617 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes"); | |
| 608 return 1; | 618 return 1; |
| 609 } | 619 } |
| 610 return obj2ast_object(obj, out, arena); | 620 return obj2ast_object(obj, out, arena); |
| 611 } | 621 } |
| 612 | 622 |
| 1771 p->col_offset = col_offset; | 1781 p->col_offset = col_offset; |
| 1772 return p; | 1782 return p; |
| 1773 } | 1783 } |
| 1774 | 1784 |
| 1775 expr_ty | 1785 expr_ty |
| 1776 Bytes(string s, int lineno, int col_offset, PyArena *arena) | 1786 Bytes(bytes s, int lineno, int col_offset, PyArena *arena) |
| 1777 { | 1787 { |
| 1778 expr_ty p; | 1788 expr_ty p; |
| 1779 if (!s) { | 1789 if (!s) { |
| 1780 PyErr_SetString(PyExc_ValueError, | 1790 PyErr_SetString(PyExc_ValueError, |
| 1781 "field s is required for Bytes"); | 1791 "field s is required for Bytes"); |
| 2802 Py_DECREF(value); | 2812 Py_DECREF(value); |
| 2803 break; | 2813 break; |
| 2804 case Bytes_kind: | 2814 case Bytes_kind: |
| 2805 result = PyType_GenericNew(Bytes_type, NULL, NULL); | 2815 result = PyType_GenericNew(Bytes_type, NULL, NULL); |
| 2806 if (!result) goto failed; | 2816 if (!result) goto failed; |
| 2807 value = ast2obj_string(o->v.Bytes.s); | 2817 value = ast2obj_bytes(o->v.Bytes.s); |
| 2808 if (!value) goto failed; | 2818 if (!value) goto failed; |
| 2809 if (PyObject_SetAttrString(result, "s", value) == -1) | 2819 if (PyObject_SetAttrString(result, "s", value) == -1) |
| 2810 goto failed; | 2820 goto failed; |
| 2811 Py_DECREF(value); | 2821 Py_DECREF(value); |
| 2812 break; | 2822 break; |
| 5507 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type); | 5517 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type); |
| 5508 if (isinstance == -1) { | 5518 if (isinstance == -1) { |
| 5509 return 1; | 5519 return 1; |
| 5510 } | 5520 } |
| 5511 if (isinstance) { | 5521 if (isinstance) { |
| 5512 string s; | 5522 bytes s; |
| 5513 | 5523 |
| 5514 if (PyObject_HasAttrString(obj, "s")) { | 5524 if (PyObject_HasAttrString(obj, "s")) { |
| 5515 int res; | 5525 int res; |
| 5516 tmp = PyObject_GetAttrString(obj, "s"); | 5526 tmp = PyObject_GetAttrString(obj, "s"); |
| 5517 if (tmp == NULL) goto failed; | 5527 if (tmp == NULL) goto failed; |
| 5518 res = obj2ast_string(tmp, &s, arena); | 5528 res = obj2ast_bytes(tmp, &s, arena); |
| 5519 if (res != 0) goto failed; | 5529 if (res != 0) goto failed; |
| 5520 Py_XDECREF(tmp); | 5530 Py_XDECREF(tmp); |
| 5521 tmp = NULL; | 5531 tmp = NULL; |
| 5522 } else { | 5532 } else { |
| 5523 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes"); | 5533 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes"); |
