Skip to content

Add a command-line flag and an environment variable to enable pegen #29

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

Merged
merged 12 commits into from
Apr 3, 2020

Conversation

lysnikolaou
Copy link
Member

@lysnikolaou lysnikolaou commented Mar 28, 2020

This is certainly a WIP and in no way complete. I'm just opening a PR for feedback, for things I might have missed etc.

Closes #4 and closes #5.

@lysnikolaou
Copy link
Member Author

For example, a question that arises is what exactly the difference between _PyConfig_InitCompatConfig, PyConfig_InitPythonConfig and PyConfig_InitIsolatedConfig is.

@pablogsal
Copy link

For example, a question that arises is what exactly the difference between _PyConfig_InitCompatConfig, PyConfig_InitPythonConfig and PyConfig_InitIsolatedConfig is.

The first one is private and I think is used because a subset of the config is needed to initialize some parts first but I can ask Victor Stinner for it, for the other two you can read https://docs.python.org/3/c-api/init_config.html#init-python-config and https://docs.python.org/3/c-api/init_config.html#init-isolated-conf

@lysnikolaou
Copy link
Member Author

So, at this time invoking python with the -p flag indeed enables pegen. But test_embed still fails and I can't really find out why. My instinct is that we need to add the new PyConfig field to some other function in initconfig.c as well. Any other ideas on what's missing?

@lysnikolaou
Copy link
Member Author

I dropped Victor an email and he told me what the problem was. Turns out everything is documented here for anyone who might be interested: https://pythondev.readthedocs.io/pyconfig.html.

Copy link

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also like to see -p in the help output; otherwise this seems to be functional.

I suppose the environment variable (#5) and the explicit APIs (#6) will be separate PRs?

@lysnikolaou lysnikolaou changed the title WIP: Start work on adding a command-line flag to enable pegen Add a command-line flag and an environment variable to enable pegen Apr 1, 2020
@lysnikolaou
Copy link
Member Author

I suppose the environment variable (#5) and the explicit APIs (#6) will be separate PRs?

Since adding the environment variable is such a small change, I did the work on this PR.

@lysnikolaou
Copy link
Member Author

In PyRun_InteractiveOneObjectEx there is the "problem" that the filename string is not present. Would we prefer to add another argument filename_str to that function or create a new pegen C API function called PyPegen_ASTFromFilePointer or something similar?

@pablogsal
Copy link

pablogsal commented Apr 1, 2020

We should run the test suite with the new parser or at least some selected files like test_ast, test_grammar, ... to know that it works.

@pablogsal
Copy link

We should run the test suite with the new parser or at least some selected files like test_ast, test_grammar, ... to know that it works.

We can do this maybe by exporting the environment variable in the test.

Copy link

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just thought of something. I realize this may invalidate most of the work here, but I'd like to point it out anyway (I apologize and should have thought of this before):

When we switch to making the new parser the default, would we just invert the meaning of the -p flag and the PYTHONPEG env var? That seems weird, because it would mean there'd be no safe way for early adopters to force the PEG parser on (or off). It would be nicer if we could instead have the flag syntax be -p old and -p new, where we'd just switch the default. For the env var maybe we could use PYTHONPARSER=old and PYTHONPARSER=new. Other flag values would be errors; other env var values would be ignored.

Also, do we want compile() to use the new parser (if the flag is set)? I think so. Note that this will also affect ast.parse(), since it just calls compile(). That seems fine, but it means there would be no explicit way to call the old parser.

@pablogsal
Copy link

pablogsal commented Apr 2, 2020

When we switch to making the new parser the default, would we just invert the meaning of the -p flag and the PYTHONPEG env var? That seems weird, because it would mean there'd be no safe way for early adopters to force the PEG parser on (or off). It would be nicer if we could instead have the flag syntax be -p old and -p new, where we'd just switch the default. For the env var maybe we could use PYTHONPARSER=old and PYTHONPARSER=new. Other flag values would be errors; other env var values would be ignored.

I think that makes sense. There are similar environment variables already like PYTHONMALLOC=debug or PYTHONMALLOC=malloc. Another possibility is just to design the flag so it only allows to call the old parser and just start with the new by default. This will simplify things a lot and there isn't much time until the first beta, so it should not make much difference since many people won't have time to use it.

@gvanrossum
Copy link

Another possibility is just to design the flag so it only allows to call the old parser and just start with the new by default.

Hm, maybe we've been acting with too much caution. We'd have to change this in the PEP. @lysnikolaou what do you think?

@lysnikolaou
Copy link
Member Author

Another possibility is just to design the flag so it only allows to call the old parser and just start with the new by default.

Hm, maybe we've been acting with too much caution. We'd have to change this in the PEP. @lysnikolaou what do you think?

This is definitely true. There's only around 26 days between alpha6 and beta1. Many people won't really know about the change, let alone use and test it in these first 26 days.

Then again, having something -p new and PYTHONPARSER=new doesn't seem like that much extra work and it gives us a bit more flexibility, if you're reluctant about having pegen be the default.

I'm certainly leaning on making pegen the default parser. I don't think it makes much of a difference. Also, it would certainly give me much more confidence, because it would force that the whole CPython test suite passes with pegen enabled (which it still doesn't) really early on, which I think is a good thing.

@gvanrossum
Copy link

Let's implement the new/old option. That also gives us a negotiating position -- if people think it's too soon for Python 3.9, we can propose to have it available but off by default in 3.9, and reconsider in 3.10.

Also it seems that if the flag was on by default the test suite doesn't pass yet? We would need to fix that before turning it on by default (and the fix oughtn't be "run the test suite with the old parser" :-). That would be a separate PR I presume.

@lysnikolaou
Copy link
Member Author

Also it seems that if the flag was on by default the test suite doesn't pass yet? We would need to fix that before turning it on by default (and the fix oughtn't be "run the test suite with the old parser" :-).

It's going to be multiple PRs most probably. Starting with #32.

@lysnikolaou
Copy link
Member Author

In PyRun_InteractiveOneObjectEx there is the "problem" that the filename string is not present. Would we prefer to add another argument filename_str to that function or create a new pegen C API function called PyPegen_ASTFromFilePointer or something similar?

This is still an open question.

Copy link

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two more.

@gvanrossum
Copy link

I just discovered that interactive mode doesn't use the new parser yet even if you specify -pnew.

(Also, I noticed a few more PEP 7 violations -- basically you cannot have } else.)

@lysnikolaou
Copy link
Member Author

I just discovered that interactive mode doesn't use the new parser yet even if you specify -pnew.

This is related to my comment above.

@gvanrossum
Copy link

This is related to my comment above.

How? Sorry for being dense.

@lysnikolaou
Copy link
Member Author

This is related to my comment above.

How? Sorry for being dense.

In order for interactive mode to use pegen, we need to have the same if check in PyRun_InteractiveOneObjectEx. To do so, we first need to solve the problem, that we don't the filename string in that function. There is only a FILE * and a PyObject *.

We can either implement a new C API function that accepts a FILE * or we can just call PyUnicode_AsUTF8 with the filename object. What do you think is best?

@gvanrossum
Copy link

We can either implement a new C API function that accepts a FILE * or we can just call PyUnicode_AsUTF8 with the filename object. What do you think is best?

We need to make a function taking a FILE *, because in this case the file is already open. Typically it's the UNIX process stdin, which may not have a filename (e.g. a pipe).

Copy link

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let's land this first and then deal with interactive mode.

@lysnikolaou lysnikolaou merged commit 33b77af into pegen Apr 3, 2020
@lysnikolaou lysnikolaou deleted the cmd-flag branch April 3, 2020 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Environment variable to enable and disable the new parser Command-line flag to enable and disable the new parser
3 participants