| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -196,6 +196,9 @@ def _assertTrueorder(self, ast_node, parent_pos): | |||
| 196 | 196 | def test_AST_objects(self): | |
| 197 | 197 | x = ast.AST() | |
| 198 | 198 | self.assertEqual(x._fields, ()) | |
| 199 | + x.foobar = 42 | ||
| 200 | + self.assertEqual(x.foobar, 42) | ||
| 201 | + self.assertEqual(x.__dict__["foobar"], 42) | ||
| 199 | 202 | ||
| 200 | 203 | with self.assertRaises(AttributeError): | |
| 201 | 204 | x.vararg | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,8 @@ What's New in Python 3.3.0 Alpha 2? | |||
| 10 | 10 | Core and Builtins | |
| 11 | 11 | ----------------- | |
| 12 | 12 | ||
| 13 | + - Give the ast.AST class a __dict__. | ||
| 14 | + | ||
| 13 | 15 | - Issue #1469629: Allow cycles through an object's __dict__ slot to be | |
| 14 | 16 | collected. (For example if ``x.__dict__ is x``). | |
| 15 | 17 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -603,6 +603,11 @@ class PyTypesVisitor(PickleVisitor): | |||
| 603 | 603 | ||
| 604 | 604 | def visitModule(self, mod): | |
| 605 | 605 | self.emit(""" | |
| 606 | + typedef struct { | ||
| 607 | + PyObject_HEAD; | ||
| 608 | + PyObject *dict; | ||
| 609 | + } AST_object; | ||
| 610 | + | ||
| 606 | 611 | static int | |
| 607 | 612 | ast_type_init(PyObject *self, PyObject *args, PyObject *kw) | |
| 608 | 613 | { | |
@@ -681,10 +686,15 @@ def visitModule(self, mod): | |||
| 681 | 686 | {NULL} | |
| 682 | 687 | }; | |
| 683 | 688 | ||
| 689 | + static PyGetSetDef ast_type_getsets[] = { | ||
| 690 | + {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, | ||
| 691 | + {NULL} | ||
| 692 | + }; | ||
| 693 | + | ||
| 684 | 694 | static PyTypeObject AST_type = { | |
| 685 | 695 | PyVarObject_HEAD_INIT(&PyType_Type, 0) | |
| 686 | 696 | "_ast.AST", | |
| 687 | - sizeof(PyObject), | ||
| 697 | + sizeof(AST_object), | ||
| 688 | 698 | 0, | |
| 689 | 699 | 0, /* tp_dealloc */ | |
| 690 | 700 | 0, /* tp_print */ | |
@@ -711,12 +721,12 @@ def visitModule(self, mod): | |||
| 711 | 721 | 0, /* tp_iternext */ | |
| 712 | 722 | ast_type_methods, /* tp_methods */ | |
| 713 | 723 | 0, /* tp_members */ | |
| 714 | - 0, /* tp_getset */ | ||
| 724 | + ast_type_getsets, /* tp_getset */ | ||
| 715 | 725 | 0, /* tp_base */ | |
| 716 | 726 | 0, /* tp_dict */ | |
| 717 | 727 | 0, /* tp_descr_get */ | |
| 718 | 728 | 0, /* tp_descr_set */ | |
| 719 | - 0, /* tp_dictoffset */ | ||
| 729 | + offsetof(AST_object, dict),/* tp_dictoffset */ | ||
| 720 | 730 | (initproc)ast_type_init, /* tp_init */ | |
| 721 | 731 | PyType_GenericAlloc, /* tp_alloc */ | |
| 722 | 732 | PyType_GenericNew, /* tp_new */ | |
@@ -1185,6 +1195,8 @@ def main(srcfile): | |||
| 1185 | 1195 | p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") | |
| 1186 | 1196 | f = open(p, "w") | |
| 1187 | 1197 | f.write(auto_gen_msg) | |
| 1198 | + f.write('#include <stddef.h>\n') | ||
| 1199 | + f.write('\n') | ||
| 1188 | 1200 | f.write('#include "Python.h"\n') | |
| 1189 | 1201 | f.write('#include "%s-ast.h"\n' % mod.name) | |
| 1190 | 1202 | f.write('\n') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,7 @@ | |||
| 1 | 1 | /* File automatically generated by Parser/asdl_c.py. */ | |
| 2 | 2 | ||
| 3 | + #include <stddef.h> | ||
| 4 | + | ||
| 3 | 5 | #include "Python.h" | |
| 4 | 6 | #include "Python-ast.h" | |
| 5 | 7 | ||
@@ -453,6 +455,11 @@ static char *withitem_fields[]={ | |||
| 453 | 455 | }; | |
| 454 | 456 | ||
| 455 | 457 | ||
| 458 | + typedef struct { | ||
| 459 | + PyObject_HEAD; | ||
| 460 | + PyObject *dict; | ||
| 461 | + } AST_object; | ||
| 462 | + | ||
| 456 | 463 | static int | |
| 457 | 464 | ast_type_init(PyObject *self, PyObject *args, PyObject *kw) | |
| 458 | 465 | { | |
@@ -531,10 +538,15 @@ static PyMethodDef ast_type_methods[] = { | |||
| 531 | 538 | {NULL} | |
| 532 | 539 | }; | |
| 533 | 540 | ||
| 541 | + static PyGetSetDef ast_type_getsets[] = { | ||
| 542 | + {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, | ||
| 543 | + {NULL} | ||
| 544 | + }; | ||
| 545 | + | ||
| 534 | 546 | static PyTypeObject AST_type = { | |
| 535 | 547 | PyVarObject_HEAD_INIT(&PyType_Type, 0) | |
| 536 | 548 | "_ast.AST", | |
| 537 | - sizeof(PyObject), | ||
| 549 | + sizeof(AST_object), | ||
| 538 | 550 | 0, | |
| 539 | 551 | 0, /* tp_dealloc */ | |
| 540 | 552 | 0, /* tp_print */ | |
@@ -561,12 +573,12 @@ static PyTypeObject AST_type = { | |||
| 561 | 573 | 0, /* tp_iternext */ | |
| 562 | 574 | ast_type_methods, /* tp_methods */ | |
| 563 | 575 | 0, /* tp_members */ | |
| 564 | - 0, /* tp_getset */ | ||
| 576 | + ast_type_getsets, /* tp_getset */ | ||
| 565 | 577 | 0, /* tp_base */ | |
| 566 | 578 | 0, /* tp_dict */ | |
| 567 | 579 | 0, /* tp_descr_get */ | |
| 568 | 580 | 0, /* tp_descr_set */ | |
| 569 | - 0, /* tp_dictoffset */ | ||
| 581 | + offsetof(AST_object, dict),/* tp_dictoffset */ | ||
| 570 | 582 | (initproc)ast_type_init, /* tp_init */ | |
| 571 | 583 | PyType_GenericAlloc, /* tp_alloc */ | |
| 572 | 584 | PyType_GenericNew, /* tp_new */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments