Skip to content

Fix bare except #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2017
Merged

Fix bare except #279

merged 2 commits into from
Dec 7, 2017

Conversation

edenhill
Copy link
Contributor

flake8 now warns on bare exception, this PR attempts to fix that.

# bad schema - should not happen
raise ClientError("Received bad schema from registry.")
raise ClientError("Received bad schema (id %s) from registry: %s" % (schema_id, str(e)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why cast to e to str? Why not rely on the implicit conversion?

Copy link
Contributor Author

@edenhill edenhill Nov 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To favour __str__ over __repr__, but I guess __str__ kicks in here thanks to %s anyway

@@ -269,7 +270,8 @@ def test_compatibility(self, subject, avro_schema, version='latest'):
else:
log.error("Unable to check the compatibility")
False
except:
except: # noqa
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you at least restrict this to except Exception:?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I thought too, until I found this:

In situations where you need to catch all “normal” errors, such as in a framework that runs callbacks, you can catch the base class for all normal exceptions, Exception. Unfortunately in Python 2.x it is possible for third-party code to raise exceptions that do not inherit from Exception, so in Python 2.x there are some cases where you may have to use a bare except: and manually re-raise the exceptions you don’t want to catch.

from https://docs.python.org/2/howto/doanddont.html#except

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the exceptions being caught here from the app code or from this library (and other open-source libraries)?

If app code, that makes sense, but if from libraries, than I don't think it makes sense as generally most open source libs are well-behaved and if it is an issue, maintainers are generally very open to cleaning this up.

@@ -269,7 +270,8 @@ def test_compatibility(self, subject, avro_schema, version='latest'):
else:
log.error("Unable to check the compatibility")
False
except:
except: # noqa
log.error("_send_request() failed: %s" % sys.exc_info()[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best practice is to use the logging framework's built-in string interpolation:

log.level("string %s", arg1)

That way the string isn't built unless the log level is hit. For error-level, this is simply a style thing, but for debug-level logging this can occasionally have a non-trivial performance impact.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good input, thanks!

@@ -180,7 +179,7 @@ def _get_decoder_func(self, schema_id, payload):

self.id_to_decoder_func[schema_id] = lambda p: read_data(p, schema_dict)
return self.id_to_decoder_func[schema_id]
except:
except: # noqa
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except Exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

@ewencp ewencp requested a review from a team November 14, 2017 20:13
@edenhill
Copy link
Contributor Author

This should address all of Jeff's review points.

Copy link
Contributor

@jeffwidman jeffwidman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good. caught a few things I missed before.

@@ -269,7 +269,8 @@ def test_compatibility(self, subject, avro_schema, version='latest'):
else:
log.error("Unable to check the compatibility")
False
except:
except Exception as e:
log.error("_send_request() failed: %s", e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use log.exception()... it is the same as log.error() but it will automatically append the exception:

log.exception("_send_request() failed.")

else:
return schema.Parse(schema_str)
except schema.AvroException.SchemaParseException as e:
raise ClientError("Schema parse failed: %s" % (str(e)))
Copy link
Contributor

@jeffwidman jeffwidman Nov 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to cast to string...

except:
schema = None
except ClientError as e:
raise SerializerError("unable to fetch schema with id %d: %s" % (schema_id, str(e)))
Copy link
Contributor

@jeffwidman jeffwidman Nov 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to cast the exception to a string...

self.server.serve_forever()

def start(self):
""" Start, and wait for server to be fully started, before returning. """
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Per PEP-257, docstrings don't need space before/after the sentence.

@edenhill edenhill merged commit e9d7932 into master Dec 7, 2017
@edenhill
Copy link
Contributor Author

edenhill commented Dec 7, 2017

Thanks for your review, @jeffwidman !

@jeffwidman
Copy link
Contributor

No problem. Curious why you didn't implement the other points in my review of the fixes though? Was that purposeful decision or accidental oversight?

@edenhill
Copy link
Contributor Author

edenhill commented Dec 8, 2017

@jeffwidman I thought I covered them all, what did I miss?

@jeffwidman
Copy link
Contributor

Scroll here and you will see a few unaddressed comments: https://github.com/confluentinc/confluent-kafka-python/pull/279/files

No problem if you choose not to incorporate them, I mentioned only because I had a hunch you never even saw them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants