def test_AST_objects(self):
x = ast.AST()
self.assertEqual(x._fields, ())
+ x.foobar = 42
+ self.assertEqual(x.foobar, 42)
+ self.assertEqual(x.__dict__["foobar"], 42)
with self.assertRaises(AttributeError):
x.vararg
Core and Builtins
-----------------
+- Give the ast.AST class a __dict__.
+
- Issue #1469629: Allow cycles through an object's __dict__ slot to be
collected. (For example if ``x.__dict__ is x``).
def visitModule(self, mod):
self.emit("""
+typedef struct {
+ PyObject_HEAD;
+ PyObject *dict;
+} AST_object;
+
static int
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
{
{NULL}
};
+static PyGetSetDef ast_type_getsets[] = {
+ {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
+ {NULL}
+};
+
static PyTypeObject AST_type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"_ast.AST",
- sizeof(PyObject),
+ sizeof(AST_object),
0,
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_iternext */
ast_type_methods, /* tp_methods */
0, /* tp_members */
- 0, /* tp_getset */
+ ast_type_getsets, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
- 0, /* tp_dictoffset */
+ offsetof(AST_object, dict),/* tp_dictoffset */
(initproc)ast_type_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c")
f = open(p, "w")
f.write(auto_gen_msg)
+ f.write('#include <stddef.h>\n')
+ f.write('\n')
f.write('#include "Python.h"\n')
f.write('#include "%s-ast.h"\n' % mod.name)
f.write('\n')
/* File automatically generated by Parser/asdl_c.py. */
+#include <stddef.h>
+
#include "Python.h"
#include "Python-ast.h"
};
+typedef struct {
+ PyObject_HEAD;
+ PyObject *dict;
+} AST_object;
+
static int
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
{
{NULL}
};
+static PyGetSetDef ast_type_getsets[] = {
+ {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
+ {NULL}
+};
+
static PyTypeObject AST_type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"_ast.AST",
- sizeof(PyObject),
+ sizeof(AST_object),
0,
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_iternext */
ast_type_methods, /* tp_methods */
0, /* tp_members */
- 0, /* tp_getset */
+ ast_type_getsets, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
- 0, /* tp_dictoffset */
+ offsetof(AST_object, dict),/* tp_dictoffset */
(initproc)ast_type_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */