bernie2436
bernie2436

Reputation: 23901

How can I debug this python code?

I am trying to use a python wrapper to post to an API.

When I step into the following code from the library in pycharm

    # Make the request
    self._make_request(
        self.BASE_URI + method,
        params.encode("utf-8"),
    )

it jumps to this retry method

def retry(ExceptionToCheck, tries=3, delay=2, backoff=2):
    """
    Retry decorator published by Saltry Crane.

    http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
    """

I think that it jumps there because of the following decorator in _make_request (altho I do not understand Python decorators yet):

@retry(Exception, tries=3)
def _make_request(self, url, params=None, opener=None):

However, I am at a dead end because if I set breakpoints in @retry and look at the ExceptionToCheck it just shows a blank BaseException with no message and no args.

Does this code somehow skip the _make_request method (this is what the step into implies in pycharm) or does it somehow just jump to @retry.

What are the next steps that I can take to debug this?

Upvotes: 1

Views: 116

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599480

A decorator wraps a function. That means, when you call a function that has a decorator, the decorator function is called instead. But the decorator function will then (usually) in turn call the decorated one. So, you need to keep stepping through retry until you get to the bit where it calls the original function. Since the original was a parameter to the decorator, you'll probably see something like return func(*args) or whatever.

Upvotes: 0

Hugh Bothwell
Hugh Bothwell

Reputation: 56624

Keep stepping - retry is applied as a wrapper around _make_request, it will eventually call that code.

Upvotes: 1

Related Questions