3

I have a try/except block around a call to an API. It appears to me that once I get an exception, all valid try cases after that exception will see the same exception. The only way I have been able to get it to work is to re-start my Python script. I googled around and found PyErr_clear() but that is for the C-API. Is there something I can call from plain-old Python that will clear the exception state?

Here is the basic idea of what I am doing:

def get_and_print_data(key):
   try:
      data = get_data_from_some_3rd_party_api(key)
   except Exception as ex:
      print("Failed to get data for",key,": ",str(ex))
      return

   print("data=",data)

Then in main I have

get_and_print_data("Valid Key")     ## This works
get_and_print_data("INvalid Key")   ## This gets exception, as expected
get_and_print_data("Valid Key")     ## This and all subsequent calls to get_and_print_data() fail with the same exception.
5
  • 1
    Can you show your code? I don't know what you mean by "all valid try cases after that exception will see the same exception". Commented Oct 5, 2018 at 16:40
  • The question doesn't make sense without code. It's not a feature of python, it's something you've implemented in code Commented Oct 5, 2018 at 16:42
  • 2
    Honestly that seems like a problem with get_data_from_some_3rd_party_api(key) failing after receiving an invalid key. I don't expect that behavior from your try/except. Commented Oct 5, 2018 at 16:48
  • @Jenner Felton - I was thinking that perhaps something in the 3rd party api needs to be cleared, and that may be so; but then I saw this docs.python.org/3/c-api/exceptions.html which seems to indicate that once set, the exception state stays set until cleared. I have contacted the owner of the API, but thought I would try here as well (while waiting for a response) in case this is a language feature (I'm a C/C++ coder; new to Python). Commented Oct 5, 2018 at 16:50
  • 1
    Can I ask which 3rd party API you are using? I'm pretty convinced it's an issue there. Commented Oct 5, 2018 at 16:54

1 Answer 1

1

As an example of why I think it's the 3rd party API that is having issues:

def get_data_from_some_3rd_party_api(key):
    if key == "Valid Key":
        return "This is some good data."
    else:
        raise ValueError("Invalid Key")

def get_and_print_data(key):
   try:
      data = get_data_from_some_3rd_party_api(key)
   except Exception as ex:
      print("Failed to get data for",key,": ",str(ex))
      return
   print("data=",data)

get_and_print_data("Valid Key")     ## This works
get_and_print_data("INvalid Key")   ## This gets exception, as expected
get_and_print_data("Valid Key")     ## This works

Try running this locally, and you should see that the subsequent valid keys still work.

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

5 Comments

Thanks. Ran it. That's very convincing. Guess I'll have to wait to hear back from the 3rd party.
@DanielGoldfarb Is the 3rd party an open source API with documentation? Happy to help there if it is.
I figured out the problem, thanks to your proof that the issue is definitely not Python. The api I am calling is get_daily() from alpha_vantage (github.com/RomelTorres/alpha_vantage) which is a Python wrapper around the alphavantage.co web API. The problem is the free version of the web API is limited to 5 requests per minute; and the Python wrapper defaults to 5 retries on a failure, so when sent an invalid key it burned up my 5 requests per minute. When I set retries=0 everything worked fine. Thanks for your help!
@DanielGoldfarb Nice sleuthing and congrats on solving your problem! Glad you figured it out!
Although you blame the 3rd Party, this also happens if you just use the plain-old C bindings to python. Calling PyErr_Clear() does not seem to actually clear the error. Investigating now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.