Repository: reportportal/client-Python

Issue:
Wrong pathname logging in python 3.11
**Describe the bug**

Following up from reportportal/agent-python-pytest#319

I'm seeing some strange behavior with python 3.11 as well, and it may indeed be a result of some change in core python, but one that may require a fix in the RP client nevertheless.

Please see this issue on cpython for reference: https://github.com/python/cpython/issues/97941

What I'm seeing on reportportal client is that this line is now being returned as the caller: https://github.com/reportportal/client-Python/blob/bd50b91f55f9d8dd9ea0dab649e207562ab3a843/reportportal_client/logs/__init__.py#L65

This is probably a result of the change in cpython, which could potentially explain the "hijacking" described in the original ticket too.

Discussion:

Solution patch:
diff --git a/reportportal_client/aio/client.py b/reportportal_client/aio/client.py
index c969460f..126f1eca 100644
--- a/reportportal_client/aio/client.py
+++ b/reportportal_client/aio/client.py
@@ -161,9 +161,9 @@ async def session(self) -> RetryingClientSession:
             ssl_config = False
         else:
             if type(self.verify_ssl) is str:
-                ssl_config = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH, cafile=self.verify_ssl)
+                ssl_config = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=self.verify_ssl)
             else:
-                ssl_config = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH, cafile=certifi.where())
+                ssl_config = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=certifi.where())
 
         connection_params = {
             'ssl': ssl_config,
diff --git a/reportportal_client/logs/__init__.py b/reportportal_client/logs/__init__.py
index c5c7a700..c699bdc7 100644
--- a/reportportal_client/logs/__init__.py
+++ b/reportportal_client/logs/__init__.py
@@ -58,6 +58,8 @@ def _log(self, level, msg, args, exc_info=None, extra=None,
             # exception on some versions of IronPython. We trap it here so that
             # IronPython can use logging.
             try:
+                if sys.version_info >= (3, 11):
+                    kwargs.setdefault('stacklevel', 2)
                 if 'stacklevel' in kwargs:
                     fn, lno, func, sinfo = \
                         self.findCaller(stack_info, kwargs['stacklevel'])


Test patch:
diff --git a/tests/logs/test_rp_logger.py b/tests/logs/test_rp_logger.py
index a6beefb1..7ecaaa40 100644
--- a/tests/logs/test_rp_logger.py
+++ b/tests/logs/test_rp_logger.py
@@ -40,6 +40,7 @@ def test_record_make(logger_handler):
     logger.info('test_log')
     record = verify_record(logger_handler)
     assert not getattr(record, 'attachment')
+    assert record.pathname == __file__
 
 
 @mock.patch('reportportal_client.logs.logging.Logger.handle')
@@ -86,3 +87,4 @@ def test_stacklevel_record_make(logger_handler):
                      stack_info=inspect.stack(), stacklevel=2)
     record = verify_record(logger_handler)
     assert record.stack_info.endswith("logger.error('test_log', exc_info=RuntimeError('test'),")
+    assert record.pathname == __file__