Skip to content

Commit

Permalink
Avoid reading __code__ if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoehaines committed May 17, 2024
1 parent 00b79fa commit 9c4603e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion bugsnag/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ def delivery(self, value):
# this should be made mandatory in the next major release
if (
hasattr(value, 'deliver_sessions') and
callable(value.deliver_sessions)
callable(value.deliver_sessions) and
# Mock objects don't allow accessing or mocking '__code__' so
# ensure it exists before attempting to read it
# __code__ should always be present in a real delivery object
hasattr(value.deliver_sessions, '__code__')
):
parameter_names = value.deliver_sessions.__code__.co_varnames

Expand Down
5 changes: 4 additions & 1 deletion bugsnag/sessiontracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ def __deliver(self, sessions: List[Dict], asynchronous=True):

deliver = self.config.delivery.deliver_sessions

if 'options' in deliver.__code__.co_varnames:
if (
hasattr(deliver, '__code__') and
'options' in deliver.__code__.co_varnames
):
try:
post_delivery_callback = self._request_tracker.new_request()

Expand Down
17 changes: 17 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
FeatureFlag
)

from bugsnag.delivery import Delivery
import bugsnag.legacy as legacy
from tests.utils import (
BrokenDelivery,
Expand Down Expand Up @@ -124,6 +125,22 @@ def deliver(foo, config, payload, options={}):
self.assertTrue(self.called)
del self.called

# test for a regression caused by reading '__code__', which does not exist
# on Mock objects, nor can it be mocked
# see: https://github.com/bugsnag/bugsnag-python/commit/77e11747c293ba715bc764d17b49fb32918c030a#r142130768
def test_delivery_can_be_mocked(self):
delivery = Mock(spec=Delivery)

client = Client(delivery=delivery, api_key='abc')
client.notify(Exception('Oh no'))

assert delivery.deliver.call_count == 1

client.session_tracker.start_session()
client.session_tracker.send_sessions()

assert delivery.deliver_sessions.call_count == 1

# Capture

def test_notify_capture(self):
Expand Down

0 comments on commit 9c4603e

Please sign in to comment.