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

Skip to content

Commit 7e0dbfb

Browse files
committed
give the AST class a __dict__
1 parent 61a4161 commit 7e0dbfb

4 files changed

Lines changed: 35 additions & 6 deletions

File tree

Lib/test/test_ast.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ def _assertTrueorder(self, ast_node, parent_pos):
196196
def test_AST_objects(self):
197197
x = ast.AST()
198198
self.assertEqual(x._fields, ())
199+
x.foobar = 42
200+
self.assertEqual(x.foobar, 42)
201+
self.assertEqual(x.__dict__["foobar"], 42)
199202

200203
with self.assertRaises(AttributeError):
201204
x.vararg

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.3.0 Alpha 2?
1010
Core and Builtins
1111
-----------------
1212

13+
- Give the ast.AST class a __dict__.
14+
1315
- Issue #1469629: Allow cycles through an object's __dict__ slot to be
1416
collected. (For example if ``x.__dict__ is x``).
1517

Parser/asdl_c.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@ class PyTypesVisitor(PickleVisitor):
603603

604604
def visitModule(self, mod):
605605
self.emit("""
606+
typedef struct {
607+
PyObject_HEAD;
608+
PyObject *dict;
609+
} AST_object;
610+
606611
static int
607612
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
608613
{
@@ -681,10 +686,15 @@ def visitModule(self, mod):
681686
{NULL}
682687
};
683688
689+
static PyGetSetDef ast_type_getsets[] = {
690+
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
691+
{NULL}
692+
};
693+
684694
static PyTypeObject AST_type = {
685695
PyVarObject_HEAD_INIT(&PyType_Type, 0)
686696
"_ast.AST",
687-
sizeof(PyObject),
697+
sizeof(AST_object),
688698
0,
689699
0, /* tp_dealloc */
690700
0, /* tp_print */
@@ -711,12 +721,12 @@ def visitModule(self, mod):
711721
0, /* tp_iternext */
712722
ast_type_methods, /* tp_methods */
713723
0, /* tp_members */
714-
0, /* tp_getset */
724+
ast_type_getsets, /* tp_getset */
715725
0, /* tp_base */
716726
0, /* tp_dict */
717727
0, /* tp_descr_get */
718728
0, /* tp_descr_set */
719-
0, /* tp_dictoffset */
729+
offsetof(AST_object, dict),/* tp_dictoffset */
720730
(initproc)ast_type_init, /* tp_init */
721731
PyType_GenericAlloc, /* tp_alloc */
722732
PyType_GenericNew, /* tp_new */
@@ -1185,6 +1195,8 @@ def main(srcfile):
11851195
p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c")
11861196
f = open(p, "w")
11871197
f.write(auto_gen_msg)
1198+
f.write('#include <stddef.h>\n')
1199+
f.write('\n')
11881200
f.write('#include "Python.h"\n')
11891201
f.write('#include "%s-ast.h"\n' % mod.name)
11901202
f.write('\n')

Python/Python-ast.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* File automatically generated by Parser/asdl_c.py. */
22

3+
#include <stddef.h>
4+
35
#include "Python.h"
46
#include "Python-ast.h"
57

@@ -453,6 +455,11 @@ static char *withitem_fields[]={
453455
};
454456

455457

458+
typedef struct {
459+
PyObject_HEAD;
460+
PyObject *dict;
461+
} AST_object;
462+
456463
static int
457464
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
458465
{
@@ -531,10 +538,15 @@ static PyMethodDef ast_type_methods[] = {
531538
{NULL}
532539
};
533540

541+
static PyGetSetDef ast_type_getsets[] = {
542+
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
543+
{NULL}
544+
};
545+
534546
static PyTypeObject AST_type = {
535547
PyVarObject_HEAD_INIT(&PyType_Type, 0)
536548
"_ast.AST",
537-
sizeof(PyObject),
549+
sizeof(AST_object),
538550
0,
539551
0, /* tp_dealloc */
540552
0, /* tp_print */
@@ -561,12 +573,12 @@ static PyTypeObject AST_type = {
561573
0, /* tp_iternext */
562574
ast_type_methods, /* tp_methods */
563575
0, /* tp_members */
564-
0, /* tp_getset */
576+
ast_type_getsets, /* tp_getset */
565577
0, /* tp_base */
566578
0, /* tp_dict */
567579
0, /* tp_descr_get */
568580
0, /* tp_descr_set */
569-
0, /* tp_dictoffset */
581+
offsetof(AST_object, dict),/* tp_dictoffset */
570582
(initproc)ast_type_init, /* tp_init */
571583
PyType_GenericAlloc, /* tp_alloc */
572584
PyType_GenericNew, /* tp_new */

0 commit comments

Comments
 (0)