Bug 497859 - cgitb is required for python scripting but was removed from Python 3.13.
Summary: cgitb is required for python scripting but was removed from Python 3.13.
Status: RESOLVED FIXED
Alias: None
Product: krita
Classification: Applications
Component: Scripting (show other bugs)
Version: unspecified
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: Halla Rempt
URL:
Keywords:
: 497937 (view as bug list)
Depends on:
Blocks:
 
Reported: 2024-12-24 10:33 UTC by a2800276
Modified: 2025-01-06 11:42 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
attachment-1376618-0.html (1.26 KB, text/html)
2024-12-27 11:55 UTC, me
Details

Note You need to log in before you can comment on or make changes to this bug.
Description a2800276 2024-12-24 10:33:18 UTC
SUMMARY

I receive the following error starting krita, with the consequence that the Scripting environment is not functional:

```
Set PYTHONPATH environment variable
/usr/lib/krita-python-libs/krita added to PYTHONPATH
krita.scripting: "Traceback (most recent call last):"
krita.scripting: "  File \"/usr/lib/krita-python-libs/krita/__init__.py\", line 32, in <module>"
krita.scripting: "    import excepthook"
krita.scripting: "  File \"/usr/lib/krita-python-libs/krita/excepthook.py\", line 14, in <module>"
krita.scripting: "    import cgitb"
krita.scripting: "ModuleNotFoundError: No module named 'cgitb'"
krita.scripting: "Could not import krita"
```

I believe this is the consequence of my system Python version being 3.13 and that `cgitb` was removed in 3.13 after being deprecated in 3.11.  This may be the result of me using arch and I sincerely hope this is not just an arch user annoyance, I assume this will probably bite scripting users as soon as they reach 3.13.

The offending import in `excepthook.py` appears in all branches I've scrounged through on gitlab.

STEPS TO REPRODUCE
1. start krita from the commandline


OBSERVED RESULT

The error message above and an empty tools menu.


EXPECTED RESULT

A working Scripter


SOFTWARE/OS VERSIONS

```
$ python --version
Python 3.13.1
$ krita --version
krita 5.2.6
$ qtdiag6
Qt 6.8.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 14.2.1 20240910) on "wayland" 
OS: Arch Linux [linux version 6.12.6-arch1-1]
(...)
```

Thank you!
Comment 1 Halla Rempt 2024-12-24 11:24:36 UTC
It would be nice if the python documentation said what replaces this...
Comment 2 a2800276 2024-12-25 14:09:59 UTC
(In reply to Halla Rempt from comment #1)
> It would be nice if the python documentation said what replaces this...

My understanding is that cgitb is unnecessary, it just replaces the standard exception handle for one that provides more detailed error reports. It's no longer included in the standard library but can be imported via an external library which is a drop in replacement (https://pypi.org/project/legacy-cgi/) The documentation does mention this, I just didn't bother linking to it because I am a moron (https://docs.python.org/3.13/library/cgitb.html#module-cgitb)...
Comment 3 Antonio Rojas 2024-12-27 10:56:55 UTC
*** Bug 497937 has been marked as a duplicate of this bug. ***
Comment 4 Antonio Rojas 2024-12-27 11:10:16 UTC
Just install https://archlinux.org/packages/extra/any/python-legacy-cgi/
Comment 5 me 2024-12-27 11:55:00 UTC
Created attachment 176906 [details]
attachment-1376618-0.html

Yes, I could do that. But this is no fix, but a workaround. This should be considered in code.
________________________________
From: Antonio Rojas <bugzilla_noreply@kde.org>
Sent: Friday, December 27, 2024 12:10:16 PM
To: Christian Petersen <me@nordinary.com>
Subject: [krita] [Bug 497859] cgitb is required for python scripting but was removed from Python 3.13.

https://bugs.kde.org/show_bug.cgi?id=497859

--- Comment #4 from Antonio Rojas <arojas@archlinux.org> ---
Just install https://archlinux.org/packages/extra/any/python-legacy-cgi/

--
You are receiving this mail because:
You are on the CC list for the bug.
Comment 6 a2800276 2024-12-28 12:48:14 UTC
(In reply to Antonio Rojas from comment #4)
> Just install https://archlinux.org/packages/extra/any/python-legacy-cgi/

That is a hacky workaround (which I can confirm works), but I agree with me@nordinary.com that this isn't a proper solution to the root of the problem.

For one it would require every user of krita to modify their system python.  Secondly, it would continue to use a deprecated library. 

Instead it would also be possible to use `traceback` from the stdlib  which essentially contains the same functionality.
Comment 7 Antonio Rojas 2024-12-28 13:56:45 UTC
(In reply to a2800276 from comment #6)
> (In reply to Antonio Rojas from comment #4)
> > Just install https://archlinux.org/packages/extra/any/python-legacy-cgi/
> 
> That is a hacky workaround (which I can confirm works), but I agree with
> me@nordinary.com that this isn't a proper solution to the root of the
> problem.

No, it's not a "hacky workaround". It's the actual solution of the issue for Arch Linux users: install the required dependencies. That doesn't mean the code should not eventually be ported upstream.
Comment 8 a2800276 2024-12-30 09:28:38 UTC
> No, it's not a "hacky workaround". It's the actual solution of the issue for
> Arch Linux users: install the required dependencies. That doesn't mean the
> code should not eventually be ported upstream.

Is there anything I can do to facilitate a more sustainable solution? Not sure when we can expect more mainstream distros and operating systems to update to the current Python release, but they will be affected sooner or later. 
I'm glad we at least agree this needs a proper fix eventually :/
Comment 9 Halla Rempt 2024-12-30 11:00:27 UTC
I think using traceback would be the best solution. I won't be doing that right now, but I intend to figure that out and do it before we build 5.2.9.
Comment 10 Halla Rempt 2025-01-06 10:36:54 UTC
Git commit 2fef5bcd54b435aaf91da7b9ef428995143f8f03 by Halla Rempt.
Committed on 06/01/2025 at 10:36.
Pushed by rempt into branch 'master'.

Use traceback instead of cgitb

It's not as pretty, but it's standard.

M  +3    -3    plugins/extensions/pykrita/plugin/krita/excepthook.py

https://invent.kde.org/graphics/krita/-/commit/2fef5bcd54b435aaf91da7b9ef428995143f8f03
Comment 11 Halla Rempt 2025-01-06 10:37:09 UTC
Git commit 94584ad995494d332c3716b79e2ba060715925f5 by Halla Rempt.
Committed on 06/01/2025 at 10:36.
Pushed by rempt into branch 'krita/5.2'.

Use traceback instead of cgitb

It's not as pretty, but it's standard.
(cherry picked from commit 2fef5bcd54b435aaf91da7b9ef428995143f8f03)

M  +3    -3    plugins/extensions/pykrita/plugin/krita/excepthook.py

https://invent.kde.org/graphics/krita/-/commit/94584ad995494d332c3716b79e2ba060715925f5
Comment 12 Halla Rempt 2025-01-06 11:39:37 UTC
Nah, I messed up. I tested the wrong build.
Comment 13 Halla Rempt 2025-01-06 11:42:00 UTC
Git commit 19be76b07b2f8c6d1bd69ffdd9d006bb6834676b by Halla Rempt.
Committed on 06/01/2025 at 11:41.
Pushed by rempt into branch 'master'.

Properly create the backtrace string

M  +2    -2    plugins/extensions/pykrita/plugin/krita/excepthook.py

https://invent.kde.org/graphics/krita/-/commit/19be76b07b2f8c6d1bd69ffdd9d006bb6834676b
Comment 14 Halla Rempt 2025-01-06 11:42:13 UTC
Git commit 38ad4fd19ca7f9413cfe2b54d975c0fff937ce2a by Halla Rempt.
Committed on 06/01/2025 at 11:42.
Pushed by rempt into branch 'krita/5.2'.

Properly create the backtrace string
(cherry picked from commit 19be76b07b2f8c6d1bd69ffdd9d006bb6834676b)

M  +2    -2    plugins/extensions/pykrita/plugin/krita/excepthook.py

https://invent.kde.org/graphics/krita/-/commit/38ad4fd19ca7f9413cfe2b54d975c0fff937ce2a