-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enable CPython's _ast module #983
Conversation
Hey @aisk are you still interested in working on this? With the CPython parser in-tree now, I think it should be much easier to do this now. |
@kmod Hi I will continue trying this in this weekends 😄 |
@aisk Glad to hear, I need this module too. |
Update: |
2962fa8
to
647debd
Compare
Nice! Looks like the issue is that it's having trouble with some of the static objects defined in the Python-ast.c file -- I think the fix should be a matter of adding If you could, I think it would be nice to take a look at enabling |
Oh there were other issues in |
Yeah looks like the handling of "module"-type objects isn't great and needs to be fleshed out a little bit. (I didn't/don't know how it works so I just had it abort.) It looks like we don't have any test coverage of the As for the specific problem, what I think is happening here is different handling of different module types. This is metadata about the string that changes the way the string is interpreted, and is what the "eval"/"exec"/"single" strings mean. Pyston code handles this completely at parse time by post-processing the raw AST node to reflect the metadata (this happens in parseEval/parseExec). CPython, on the other hand, just stores this metadata as part of the returned AST object, and then does the actual transformation when compiling to bytecode. So when we go through a code path that uses CPython's parser but our compiler, neither part will do the transformation (see below for the kind of thing that ends up getting missed). One way forward could be to use CPython's parser, and then in cpythonToPystonAST, do the transformation (ie call The transformation is some simple things like "in 'single' mode, print out all bare expressions". Here's an example of something that isn't working in this PR: a = compile("'hello world'", "test.py", "single", 0x400)
exec compile(a, "test.py", "single") This should print Sorry this is messy! As we switch closer to the way CPython handles it, things will become more straightforward. |
Hi @kmod , sorry since the In the old parse process in builtin But with it, we must compile the AST which follows the mode param. If we use the So the next thing to do is:
Is this right, or some misunderstanding I am making? |
Sounds good! I think we're coming at this from slightly different perspectives but I think we're getting to the same place. Concretely, I would recommend handling |
8761830
to
d0e5b84
Compare
if (!Or_singleton) return 0; | ||
operator_type = make_type("operator", &AST_type, NULL, 0); | ||
if (!operator_type) return 0; | ||
if (!add_attributes(operator_type, NULL, 0)) return 0; | ||
Add_type = make_type("Add", operator_type, NULL, 0); | ||
if (!Add_type) return 0; | ||
Add_singleton = PyType_GenericNew(Add_type, NULL, NULL); | ||
Add_singleton = PyGC_AddRoot(PyGC_AddRoot(PyType_GenericNew(Add_type, NULL, NULL))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyGC_AddRoot(PyGC_AddRoot( -> PyGC_AddRoot(
I'm not really sure what to think about this added Looks good on first sight! I will test it out later but I noticed completely lack of testing. Could you please add some tests. Thanks! |
@undingen Thanks for review, I'll add more testing now! |
Oh I'm not familiar with the GC, but before |
I think we should call The |
@undingen sorry, I'll update this PR as soon as I can. And should I keep the syntax check in cpythonToPystonAST as this PR now, or just reset it back? |
I think it's best to keep the syntax checks otherwise this will break existing tests. Disabling the failing parts of the new cpython AST test is fine (in the test/tests/ copy). I had to do something similar in #1045 |
@@ -633,8 +633,9 @@ class Converter { | |||
raiseSyntaxError("'break' outside loop", stmt->lineno, stmt->col_offset, fn, "", true); | |||
return new AST_Break(); | |||
case Continue_kind: | |||
if (loop_depth == 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you meant to do a if (loop_depth == 0)
error check not two in_finally
checks
560bc6a
to
7c552e1
Compare
function compile
LGTM, Thanks for the patch! |
😀 |
Just copied & modified CPython's asdl stuff to generate _ast module.
issule: #902