Bug 438206 - Fails to build against Python 3.10
Summary: Fails to build against Python 3.10
Status: RESOLVED FIXED
Alias: None
Product: kdev-python
Classification: Developer tools
Component: Language support (show other bugs)
Version: 5.6.2
Platform: Fedora RPMs Linux
: NOR normal
Target Milestone: ---
Assignee: Francis Herne
URL:
Keywords:
: 443167 (view as bug list)
Depends on:
Blocks:
 
Reported: 2021-06-07 11:54 UTC by Jan Grulich
Modified: 2022-11-23 08:26 UTC (History)
9 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Grulich 2021-06-07 11:54:44 UTC
There is a new version of Python (3.9) coming to Fedora 35 and it seems that kdev-python is not able to build against this new version.

See Fedora bug: https://bugzilla.redhat.com/show_bug.cgi?id=1898116
Comment 1 Francis Herne 2021-09-07 08:41:21 UTC
Noted, we will need to add support for Python 3.10 by next release.

As I mentioned previously, we tend to leave this until late in the Python release cycle because the API is prone to breaking changes even in late betas.

This time Python upstream has made life particularly inconvenient for us (c.f.https://bugzilla.redhat.com/show_bug.cgi?id=1898116#c12) and we'll have to work around their deliberate obstruction. C'est la vie.
Comment 2 Luke Horwell 2021-12-14 13:14:37 UTC
A heads up that Arch Linux is now affected with their recent rebuild to Python 3.10.1. The package was removed from their repositories:

https://pkgbuild.com/~foutrelis/failed-py310-builds/kdevelop-python.log
https://github.com/archlinux/svntogit-packages/commits/packages/kdevelop-python
https://archlinux.org/todo/remaining-rebuilds-for-python-310/
Comment 3 Øystein S. Haaland 2021-12-21 07:42:37 UTC
Hei,

I use kdevelop for python development in my day-to-day job. It seem now that kdev-python has stopped working on arch, which I am currently using. I might in any case like to change distros, but it seems inevitable that this particular python version change will become an issue down the road for other distros as well.

Is there any way I could help out? If at all possible, I would really like to fund someone directly to work on this. I don't think I would have time to devote myself, in particular since I'm not very familiar with c++.
Comment 4 Luke Horwell 2021-12-21 09:24:57 UTC
(In reply to Øystein S. Haaland from comment #3)
> I use kdevelop for python development in my day-to-day job. It seem now that
> kdev-python has stopped working on arch, which I am currently using.

I use KDevelop on Arch nearly every day too. A workaround I found is to install Python 3.9 (`python39` in AUR) and install `kdevelop-python` (now in AUR). Syntax highlighting and parsing works again as before, and so far, it is processing modules installed in /usr/lib/python3.10 for auto-completion, etc. I imagine Python 3.10-specific syntax won't work (I haven't checked) but at least things can continue to work for Python <= 3.9 projects.
Comment 5 Øystein S. Haaland 2021-12-21 12:15:24 UTC
(In reply to Luke Horwell from comment #4)
> (In reply to Øystein S. Haaland from comment #3)
> > I use kdevelop for python development in my day-to-day job. It seem now that
> > kdev-python has stopped working on arch, which I am currently using.
> 
> I use KDevelop on Arch nearly every day too. A workaround I found is to
> install Python 3.9 (`python39` in AUR) and install `kdevelop-python` (now in
> AUR). Syntax highlighting and parsing works again as before, and so far, it
> is processing modules installed in /usr/lib/python3.10 for auto-completion,
> etc. I imagine Python 3.10-specific syntax won't work (I haven't checked)
> but at least things can continue to work for Python <= 3.9 projects.

I too found the kdev-python AUR, installed that, and it seems to work as before. On my setup, I have py3.9 installed, not 3.10 yet. So in principle, I guess kdev-python should still have worked.
Comment 6 Francis Herne 2022-03-01 15:16:13 UTC
*** Bug 443167 has been marked as a duplicate of this bug. ***
Comment 7 Sven Brauch 2022-03-01 18:25:47 UTC
Unfortunately Python completely removed the Parser module we were using from their public API, so there is no easy fix for this issue.

The most realistic option at this point is probably to write a Python snippet which uses the "ast" module to do the parsing (which is effectively exactly the same) and serializes the result somehow, e.g. into an XML or ProtocolBuffer stream. Then we can execute this snippet from C++ code (this works without creating a process using Python's C API) and deserialize it, giving us the same data structure as before.
Comment 8 Sven Brauch 2022-03-01 18:27:19 UTC
From an end-user perspective, you can use the KDevelop AppImage from our website, which we can build against Python 3.9 until this issue is resolved somehow. It will of course not support Python 3.10 syntax, so if you require that, you are out of luck for now.
Comment 9 Øystein S. Haaland 2022-03-02 21:28:30 UTC
(In reply to Sven Brauch from comment #7)
> Unfortunately Python completely removed the Parser module we were using from
> their public API, so there is no easy fix for this issue.
> 
> The most realistic option at this point is probably to write a Python
> snippet which uses the "ast" module to do the parsing (which is effectively
> exactly the same) and serializes the result somehow, e.g. into an XML or
> ProtocolBuffer stream. Then we can execute this snippet from C++ code (this
> works without creating a process using Python's C API) and deserialize it,
> giving us the same data structure as before.

Thanks a lot for sketching a possible route for implementing this!

I would be more than willing to put some effort into this if I had the time. But maybe with some guidance it would still be possible to have some progress every now and then.

The first part seem easy enough;  write the code that reads a file with python code and loads it into the ast.

You mention a few ways serialization can be done. Are there any examples of similar approaches already in the code base? Are there already places where de-serialization is done with one of the formats mentioned (or other formats for that matter)?

Any pointers for tools/tests in the repo that can be useful for working with this would be much appreciated.

Btw. would it make things easier if one used cython or mypyc to compile the python part to a c module?

Again, if there is any way I could fund this work more or less directly, I would also be interested in hearing about that.
Comment 10 Francis Herne 2022-03-02 21:38:55 UTC
My own thinking is that it's probably easier to fork (or, so far as possible, vendor in the most recent version of) CPython's parser code.

The amount of code involved is relatively small - see https://github.com/python/typed_ast/tree/master/ast3 which is another fork.

(in the past I'd considered changing kdev-python to use that typed-ast library instead of linking against the CPython parser, but it's now deprecated with the features added to CPython upstream).

Sadly I haven't yet had time to work on this, but I hope to in a few weeks. Right now I'm busy buying and moving onto a houseboat...
Comment 11 Francis Herne 2022-03-02 21:50:15 UTC
To address the development comments:

It's great that you're interested in contributing to kdev-python. As you can see it's kind of needed at the moment. Consider dropping by #kdevelop on Libera IRC if you want to discuss it (ping FLHerne [me] or scummos [Sven]).

Tools/tests:
There are quite a lot of unit/end-to-end tests. Run `ninja test` (or `make test`) after building kdev-python, or for more detailed output run the test binaries individually (e.g. build/parser/tests/pyasttest , build/duchain/tests/pyduchaintest ).

parser/generated.h converts from CPython's AST structures to kdev-python's. It's generated based on parser/python39.sdef by the script conversionGenerator.py.

The script pythonpythonparser.py spits out the AST as an XML representation, using the Python `ast` module API. In recent years it's been used only as a tool for debugging and development. I believe it was once used in kdevelop before it called into the C API instead, and could be resurrected as such to implement Sven's suggestion. I'm not a big fan of this idea, I suspect performance would suffer considerably.
Comment 12 Sven Brauch 2022-03-02 22:43:08 UTC
Any help is certainly very welcome. I am not very active on IRC currently, but I definitely read and answer kdevelop-devel@kde.org.

In principle, the task is simple: in parser/ast.h we have our own data structure for an AST, and that needs to be populated. The current way to do that is based on a... let's say, "creative" domain-specific language which describes how CPython's internal AST representation maps to ours from ast.h. C code is generated from this DSL file. You could just as well write that C code by hand -- it's not actually that much, a few hundred lines.

This doesn't work any more, because CPython's representation is gone.

There are certainly several different approaches to fix this, I think my suggestion above is the most straightforward one since it doesn't structurally change anything. Though, if one has to touch all this stuff *anyways*, it might be worth evaluating other parser libraries which support also e.g. proper incomplete parsing. I'd honestly opt for whatever the person doing it feels most comfortable with.

I wouldn't discard approaches involving serializing/deserializing the AST (e.g. to/from XML) because of performance concerns; XML stream serialization/deserialization (i.e. if you avoid building a DOM) is extremely fast, and I'm pretty sure that the impact is negligible compared to the semantic analysis effort later on. The only thing I'd really try to avoid is spawning a process for each parse job.
Comment 13 Sven Brauch 2022-03-13 23:32:35 UTC
Hi, I've drafted a possible way forward in the "python310" branch in the kdev-python repo. The AST is written to an XML stream from a tiny Python program and read using QXmlStreamReader in C++. So far, only Module, Assignment, Name and Constant AST nodes can be deserialized, so we can basically parse programs of the form "a = 3" and nothing else ;)

But the whole structure is there, most of what's left is filling in all the 63 or so missing NodeReader<> template specializations for all the AST nodes, see https://invent.kde.org/kdevelop/kdev-python/-/blob/python310/parser/astfromxml.cpp#L164

In addition, we need to deal with 1. error reporting (parse errors, plus invalid XML issues), and 2. running the ast-to-xml Python code in-process, that should make it reasonably fast. But I'm fairly certain that the most difficult step is done.
Comment 14 Sven Brauch 2022-03-21 10:36:21 UTC
Let's move any discussion on this topic to https://invent.kde.org/kdevelop/kdev-python/-/merge_requests/16.
Comment 15 Sven Brauch 2022-11-06 16:27:07 UTC
Master now supports Python 3.10.
Comment 16 Thomas Bettler 2022-11-20 19:12:27 UTC
@Sven do you know whether python 3.11 would work and could be enabled as well?
Comment 17 Sven Brauch 2022-11-20 19:18:26 UTC
Not just like that, but it shouldn't be a large effort to support it now. I should be able to look into that sometime in the next few weeks, I think.
Comment 18 Sven Brauch 2022-11-22 21:44:26 UTC
Master now also supports Python 3.11, where "supports" means "builds and runs without crashing".

It's too late to get into Applications 22.12, but it will be in the next release.
Comment 19 Thomas Bettler 2022-11-22 22:11:59 UTC
great to know - THX a lot for all your work