X Tutup
The Wayback Machine - https://web.archive.org/web/20230129025931/https://github.com/python/cpython/issues/99478
Skip to content
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

We need to add the set_exc_info() function to the standard library #99478

Closed
pomponchik opened this issue Nov 14, 2022 · 6 comments
Closed

We need to add the set_exc_info() function to the standard library #99478

pomponchik opened this issue Nov 14, 2022 · 6 comments
Labels
pending The issue will be closed if no feedback is provided type-feature A feature request or enhancement

Comments

@pomponchik
Copy link

pomponchik commented Nov 14, 2022

Feature or enhancement

We need to move the set_exc_info() function from the _testcapi module to the standard library.

Pitch

I am a developer of a logging library named polog. In this library, it is possible to hang a decorator on any function that will record what is happening in it. It looks like:

from polog import log

@log
def function():
    ...

function()

In the case when an exception is raised inside this function, this decorator records information about it. But the decorator itself does not affect the exception traceback, does not "clog" it. The traceback that you will see in the console looks the same as if there was no decorator. Because of this, logging becomes completely transparent to the user, and he no longer has any reason to avoid logging his code. I implemented this using the function set_exc_info() from the _testcapi module. There is something similar to the code of my library:

tp, exc, tb = exc_info()
set_exc_info(tp, exc, tb.tb_next)

This code contains some problems. First: it worked on python 3.9 and 3.10, but it stopped working on python 3.11. Second: this code is not officially supported as part of the python standard library. This creates a risk that the library that depends on this feature will not work properly or will stop working altogether.

@pomponchik pomponchik added the type-feature A feature request or enhancement label Nov 14, 2022
@sobolevn
Copy link
Member

I implemented this using the function set_exc_info() from the _testcapi

_testcapi is very private :) You should not use anything from there. We have no guarantees for it, ever. This is a module with test utils and test cases.

First: it worked on python 3.9 and 3.10, but it stopped working on python 3.11

Most likely it is related to the new exception features: https://docs.python.org/3/library/sys.html#sys.exception (but, again, we never promised that anything from _testcapi will work for you)

CC @iritkatriel

@ericvsmith
Copy link
Member

I don’t think this is a bug report about a private API changing. Rather, it’s a request to expose and support the equivalent functionality in a future release.

@pomponchik
Copy link
Author

Yes, I'm saying that it would be good to add this functionality in a future release. We don't need to fix it in the module _testcapi, we need to move it to the main officially supported part of the standard library.

@iritkatriel
Copy link
Member

In 3.11 you can modify the traceback on the "exc_info" exception and it will stick:

>>> def raiseTypeError(): raise TypeError
... 
>>> try: raiseTypeError()
... except Exception as e: te = e
... 
>>> def raiseValueError(): raise ValueError
... 
>>> try: raiseValueError()
... except Exception as e:
...     e.__traceback__ = te.__traceback__
...     raise
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in raiseTypeError
ValueError 

See how raiseTypeError raised a ValueError?

You can also use sys.exception() instead of sys.exc_info().

@iritkatriel iritkatriel added the pending The issue will be closed if no feedback is provided label Nov 15, 2022
@iritkatriel
Copy link
Member

Marking as pending because I don't think I see a use case for exposing set_exc_info, given my previous comment.

@iritkatriel
Copy link
Member

Closing. If you do still have an issue this can't solve, please reopen or create a new issue.

@iritkatriel iritkatriel closed this as not planned Won't fix, can't repro, duplicate, stale Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants
X Tutup