1

Here is my code (almost full version for @cdhowie :)):

def getResult(method, argument=None):
  result = None

  while True:
    print('### loop')
    try:
      print ('### try hard...')
      if argument:
        result = method(argument)
      else:
        result = method()
      break
    except Exception as e:
      print('### GithubException')
      if 403 == e.status:
        print('Warning: ' + str(e.data))
        print('I will try again after 10 minutes...')
      else:
        raise e

  return result

def getUsernames(locations, gh):
  usernames = set()

  for location in locations:
    print location
    result = getResult(gh.legacy_search_users, location)
    for user in result:
      usernames.add(user.login)
      print user.login,

  return usernames

# "main.py"
gh = Github()
locations = ['Washington', 'Berlin']
# "main.py", line 12 is bellow
usernames = getUsernames(locations, gh)

The problem is, that exception is raised, but I can't handle it. Here is an output:

### loop
### try hard...
Traceback (most recent call last):
  File "main.py", line 12, in <module>
    usernames = getUsernames(locations, gh)
  File "/home/ciembor/projekty/github-rank/functions.py", line 39, in getUsernames
    for user in result:
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 33, in __iter__
    newElements = self.__grow()
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 45, in __grow
    newElements = self._fetchNextPage()
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py", line 37, in _fetchNextPage
    return self.get_page(page)
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py", line 48, in get_page
    None
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Requester.py", line 69, in requestAndCheck
    raise GithubException.GithubException(status, output)
github.GithubException.GithubException: 403 {u'message': u'API Rate Limit Exceeded for 11.11.11.11'}

Why it doesn't print ### handling exception?

8
  • 6
    Your stack trace does not match the calls given in your example code. Please post the code you're actually using, not sanitized pseudo-code. (Sanitize only when necessary.) Commented Oct 17, 2012 at 18:20
  • What happens if you change except Execption as e to except BaseException as e ? (only use this for debugging purposes) Commented Oct 17, 2012 at 18:20
  • @mgilson: I took a peek at the pygithub source. That custom exception does subclass Exception Commented Oct 17, 2012 at 18:21
  • @jdi -- phew. If they subclassed BaseException instead, I was about to have to be very upset :) Commented Oct 17, 2012 at 18:21
  • github.GithubException is derived from Exception so this cannot be your real code or it is happening somewhere else. Where is getUsernames called from? Commented Oct 17, 2012 at 18:22

1 Answer 1

6

Take a close look at the stack trace in the exception:

Traceback (most recent call last):
  File "main.py", line 12, in <module>
    usernames = getUsernames(locations, gh)
  File "/home/ciembor/projekty/github-rank/functions.py", line 39, in getUsernames
    for user in result:
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 33, in __iter__
    newElements = self.__grow()
  ...

The exception is being thrown from code being called by the line for user in result: after getResult finishes executing. This means that the API you're using is using lazy evaluation, so the actual API request doesn't quite happen when you expect it to.

In order to catch and handle this exception, you'll need to wrap the code inside the getUsernames function with a try/except handler.

Sign up to request clarification or add additional context in comments.

1 Comment

Can I do that somehow inside this loop? For example by scoping it in anonymous function?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.