Skip to content

gh-115233: Fix currentframe to get the frame of original caller #115241

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

Closed
wants to merge 10 commits into from

Conversation

Agent-Hellboy
Copy link
Contributor

@Agent-Hellboy Agent-Hellboy commented Feb 10, 2024

@Agent-Hellboy
Copy link
Contributor Author

Agent-Hellboy commented Feb 10, 2024 via email

@vsajip
Copy link
Member

vsajip commented Feb 10, 2024

I deleted my comment because just after I posted it, I remembered the earlier issue whose fix was in this area. I think the test should include calls both via the adapter and not, to ensure that the correct funcName value is computed in either case.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

If it fixes LoggerAdapter, it breaks Logger.

I think that the proper fix should add the stacklevel parameter in LoggerAdapter._log() and call self.logger._log() with the proper value of stacklevel. You need also to balance stacklevel for LoggerAdapter.log() and other logging methods. The simplest way is to move some code from LoggerAdapter.log() to LoggerAdapter._log() and use ._log() instead of log() in other methods.

@bedevere-app
Copy link

bedevere-app bot commented Feb 10, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@Agent-Hellboy
Copy link
Contributor Author

Agent-Hellboy commented Feb 11, 2024

Hi @serhiy-storchaka , I have made the requested changes; please review again, but it has changed the way records are getting pushed in the handler,
should I fix the test or did I do anything wrong?
https://github.com/python/cpython/actions/runs/7859692843/job/21446066942?pr=115241#step:18:820

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

There is a test for formatting funcName in Logger: test_find_caller_with_stacklevel. Just add a similar test, but with LoggerAdapter (in class LoggerAdapterTest).

"""
Delegate a debug call to the underlying logger.
"""
self.log(DEBUG, msg, *args, **kwargs)
self._log(DEBUG, msg, *args, stacklevel=stacklevel + 1, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

It will be simpler to just pass **kwargs, as in Logger methods, and just do a double correction in _log().

Also, it will be more efficient to pass args instead of *args.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, please check

@Agent-Hellboy
Copy link
Contributor Author

There is a test for formatting funcName in Logger: test_find_caller_with_stacklevel. Just add a similar test, but with LoggerAdapter (in class LoggerAdapterTest).

sure, let me explore, I will add it, btw, I am still figuring out why below is failing

FAIL: test_nested (__main__.LoggerAdapterTest.test_nested)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/agent-hellboy/cpython/Lib/test/test_logging.py", line 5515, in test_nested
    self.assertEqual(record.args, (self.recording,))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Tuples differ: ((<RecordingHandler (NOTSET)>,),) != (<RecordingHandler (NOTSET)>,)

First differing element 0:
(<RecordingHandler (NOTSET)>,)
<RecordingHandler (NOTSET)>

- ((<RecordingHandler (NOTSET)>,),)
? -                              --

+ (<RecordingHandler (NOTSET)>,)

@vsajip
Copy link
Member

vsajip commented Feb 11, 2024

I am still figuring out why below is failing

An extra comma somewhere making something a tuple when that's not intended?

@Agent-Hellboy
Copy link
Contributor Author

Agent-Hellboy commented Feb 11, 2024

I am still figuring out why the below is failing

An extra comma somewhere making something a tuple when that's not intended?

Hi @vsajip
one adapter took the args and passed it to another and the other consumed it as an element ((args,),)
I added one more layer of the adapter and it started giving me (((<RecordingHandler (NOTSET)>,),),)

this is expected now as after restructuring, every adapter is going to hit _log and it will update the (extra context) args @serhiy-storchaka
I have verified it after running it on the main and my PR

previously, it was calling logger.log now it is calling logger._log which saves context.

    def test_nested(self):
        class Adapter(logging.LoggerAdapter):
            prefix = 'Adapter'

            def process(self, msg, kwargs):
                return f"{self.prefix} {msg}", kwargs

        msg = 'Adapters can be nested, yo.'
        adapter = Adapter(logger=self.logger, extra=None)
        adapter_adapter = Adapter(logger=adapter, extra=None)
        adapter_adapter.prefix = 'AdapterAdapter'
        self.assertEqual(repr(adapter), repr(adapter_adapter))
        adapter_adapter.log(logging.CRITICAL, msg, self.recording)
        self.assertEqual(len(self.recording.records), 1)
        record = self.recording.records[0]

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Feb 12, 2024

It was surprising that new tests are passed with the unmodified code. It means that either the code was not broken, or that the new tests do not work as intended. I played with the code yesterday, and came to conclusion that both are true. The new tests did not test LoggerAdapter, and the code worked as intended while we use standard classes, the problem was in the example.

So I created a new PR #115325 which includes the fixed example and fixed tests.

@Agent-Hellboy
Copy link
Contributor Author

Okay, got you.

@Agent-Hellboy
Copy link
Contributor Author

should i close my PR? @serhiy-storchaka

@serhiy-storchaka
Copy link
Member

Yes, I think nothing to do here. This issue was not as it seemed at first glance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants