You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by ko...@apache.org on 2019/10/25 21:58:44 UTC
[avro] branch master updated: AVRO-2577: Fix Bare Excepts (#665)
This is an automated email from the ASF dual-hosted git repository.
kojiromike pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 1f638fb AVRO-2577: Fix Bare Excepts (#665)
1f638fb is described below
commit 1f638fbad382881c5a7f0be7a29e29330e87e71f
Author: Michael A. Smith <mi...@smith-li.com>
AuthorDate: Fri Oct 25 17:58:36 2019 -0400
AVRO-2577: Fix Bare Excepts (#665)
* AVRO-2577: Fix Bare Excepts
* AVRO-2577: Don't Count Failure as Success
---
lang/py/setup.cfg | 2 +-
lang/py/src/avro/datafile.py | 8 ++++----
lang/py/src/avro/protocol.py | 31 ++++++++++++-----------------
lang/py/src/avro/schema.py | 7 ++++---
lang/py/test/sample_http_client.py | 2 +-
lang/py/test/test_protocol.py | 15 +++++++-------
lang/py/test/txsample_http_client.py | 2 +-
lang/py3/avro/tests/sample_http_client.py | 2 +-
lang/py3/avro/tests/test_protocol.py | 15 +++++++-------
lang/py3/avro/tests/txsample_http_client.py | 2 +-
lang/py3/setup.cfg | 2 +-
11 files changed, 41 insertions(+), 47 deletions(-)
diff --git a/lang/py/setup.cfg b/lang/py/setup.cfg
index 4beaeee..2b53378 100644
--- a/lang/py/setup.cfg
+++ b/lang/py/setup.cfg
@@ -20,6 +20,6 @@ known_third_party=zope
[pycodestyle]
exclude = .eggs,build
-ignore = E101,E111,E114,E121,E122,E124,E125,E126,E127,E128,E129,E201,E202,E203,E222,E226,E225,E231,E241,E251,E261,E262,E265,E266,E301,E302,E303,E305,E306,E402,E501,E701,E703,E704,E711,E722,W191,W291,W292,W293,W391,W503,W504,W601
+ignore = E101,E111,E114,E121,E122,E124,E125,E126,E127,E128,E129,E201,E202,E203,E222,E226,E225,E231,E241,E251,E261,E262,E265,E266,E301,E302,E303,E305,E306,E402,E501,E701,E703,E704,E711,W191,W291,W292,W293,W391,W503,W504,W601
max-line-length = 150
statistics = True
diff --git a/lang/py/src/avro/datafile.py b/lang/py/src/avro/datafile.py
index 562a325..75a8e4a 100644
--- a/lang/py/src/avro/datafile.py
+++ b/lang/py/src/avro/datafile.py
@@ -21,6 +21,8 @@
from __future__ import absolute_import, division, print_function
+import os
+import random
import zlib
from avro import io, schema
@@ -397,8 +399,6 @@ class DataFileReader(object):
def generate_sixteen_random_bytes():
try:
- import os
return os.urandom(16)
- except:
- import random
- return [ chr(random.randrange(256)) for i in range(16) ]
+ except NotImplementedError:
+ return [chr(random.randrange(256)) for i in range(16)]
diff --git a/lang/py/src/avro/protocol.py b/lang/py/src/avro/protocol.py
index 452fc7b..117401c 100644
--- a/lang/py/src/avro/protocol.py
+++ b/lang/py/src/avro/protocol.py
@@ -21,15 +21,10 @@
from __future__ import absolute_import, division, print_function
+import hashlib
import json
-from avro import schema
-
-try:
- from hashlib import md5
-except ImportError:
- from md5 import md5
-
+import avro.schema
#
# Constants
@@ -42,7 +37,7 @@ VALID_TYPE_SCHEMA_TYPES = ('enum', 'record', 'error', 'fixed')
# Exceptions
#
-class ProtocolParseException(schema.AvroException):
+class ProtocolParseException(avro.schema.AvroException):
pass
#
@@ -54,7 +49,7 @@ class Protocol(object):
def _parse_types(self, types, type_names):
type_objects = []
for type in types:
- type_object = schema.make_avsc_object(type, type_names)
+ type_object = avro.schema.make_avsc_object(type, type_names)
if type_object.type not in VALID_TYPE_SCHEMA_TYPES:
fail_msg = 'Type %s not an enum, fixed, record, or error.' % type
raise ProtocolParseException(fail_msg)
@@ -97,7 +92,7 @@ class Protocol(object):
self._props = {}
self.set_prop('name', name)
- type_names = schema.Names()
+ type_names = avro.schema.Names()
if namespace is not None:
self.set_prop('namespace', namespace)
type_names.default_namespace = namespace
@@ -105,13 +100,13 @@ class Protocol(object):
self.set_prop('types', self._parse_types(types, type_names))
if messages is not None:
self.set_prop('messages', self._parse_messages(messages, type_names))
- self._md5 = md5(str(self)).digest()
+ self._md5 = hashlib.md5(str(self)).digest()
# read-only properties
name = property(lambda self: self.get_prop('name'))
namespace = property(lambda self: self.get_prop('namespace'))
fullname = property(lambda self:
- schema.Name(self.name, self.namespace).fullname)
+ avro.schema.Name(self.name, self.namespace).fullname)
types = property(lambda self: self.get_prop('types'))
types_dict = property(lambda self: dict([(type.name, type)
for type in self.types]))
@@ -128,7 +123,7 @@ class Protocol(object):
def to_json(self):
to_dump = {}
to_dump['protocol'] = self.name
- names = schema.Names(default_namespace=self.namespace)
+ names = avro.schema.Names(default_namespace=self.namespace)
if self.namespace:
to_dump['namespace'] = self.namespace
if self.types:
@@ -153,20 +148,20 @@ class Message(object):
if not isinstance(request, list):
fail_msg = 'Request property not a list: %s' % request
raise ProtocolParseException(fail_msg)
- return schema.RecordSchema(None, None, request, names, 'request')
+ return avro.schema.RecordSchema(None, None, request, names, 'request')
def _parse_response(self, response, names):
if isinstance(response, basestring) and names.has_name(response, None):
return names.get_name(response, None)
else:
- return schema.make_avsc_object(response, names)
+ return avro.schema.make_avsc_object(response, names)
def _parse_errors(self, errors, names):
if not isinstance(errors, list):
fail_msg = 'Errors property not a list: %s' % errors
raise ProtocolParseException(fail_msg)
errors_for_parsing = {'type': 'error_union', 'declared_errors': errors}
- return schema.make_avsc_object(errors_for_parsing, names)
+ return avro.schema.make_avsc_object(errors_for_parsing, names)
def __init__(self, name, request, response, errors=None, names=None):
self._name = name
@@ -195,7 +190,7 @@ class Message(object):
def to_json(self, names=None):
if names is None:
- names = schema.Names()
+ names = avro.schema.Names()
to_dump = {}
to_dump['request'] = self.request.to_json(names)
to_dump['response'] = self.response.to_json(names)
@@ -221,7 +216,7 @@ def parse(json_string):
"""Constructs the Protocol from the JSON text."""
try:
json_data = json.loads(json_string)
- except:
+ except ValueError:
raise ProtocolParseException('Error parsing JSON: %s' % json_string)
# construct the Avro Protocol object
diff --git a/lang/py/src/avro/schema.py b/lang/py/src/avro/schema.py
index d0091c9..06c1aeb 100644
--- a/lang/py/src/avro/schema.py
+++ b/lang/py/src/avro/schema.py
@@ -616,9 +616,10 @@ class MapSchema(Schema):
else:
try:
values_schema = make_avsc_object(values, names)
- except:
- fail_msg = 'Values schema not a valid Avro schema.'
- raise SchemaParseException(fail_msg)
+ except SchemaParseException:
+ raise
+ except Exception:
+ raise SchemaParseException('Values schema is not a valid Avro schema.')
self.set_prop('values', values_schema)
diff --git a/lang/py/test/sample_http_client.py b/lang/py/test/sample_http_client.py
index 96c7489..02b8421 100644
--- a/lang/py/test/sample_http_client.py
+++ b/lang/py/test/sample_http_client.py
@@ -77,7 +77,7 @@ if __name__ == '__main__':
try:
num_messages = int(sys.argv[4])
- except:
+ except IndexError:
num_messages = 1
# build the parameters for the request
diff --git a/lang/py/test/test_protocol.py b/lang/py/test/test_protocol.py
index 57b41bc..4d8e783 100644
--- a/lang/py/test/test_protocol.py
+++ b/lang/py/test/test_protocol.py
@@ -402,14 +402,13 @@ class TestProtocol(unittest.TestCase):
for example in VALID_EXAMPLES:
protocol_data = protocol.parse(example.protocol_string)
try:
- try:
- protocol.parse(str(protocol_data))
- debug_msg = "%s: STRING CAST SUCCESS" % example.name
- num_correct += 1
- except:
- debug_msg = "%s: STRING CAST FAILURE" % example.name
- finally:
- print(debug_msg)
+ protocol.parse(str(protocol_data))
+ except (ValueError, ProtocolParseException):
+ debug_msg = "%s: STRING CAST FAILURE" % example.name
+ else:
+ debug_msg = "%s: STRING CAST SUCCESS" % example.name
+ num_correct += 1
+ print(debug_msg)
fail_msg = "Cast to string success on %d out of %d protocols" % \
(num_correct, len(VALID_EXAMPLES))
diff --git a/lang/py/test/txsample_http_client.py b/lang/py/test/txsample_http_client.py
index e593ebb..28c2c28 100644
--- a/lang/py/test/txsample_http_client.py
+++ b/lang/py/test/txsample_http_client.py
@@ -79,7 +79,7 @@ if __name__ == '__main__':
try:
num_messages = int(sys.argv[4])
- except:
+ except IndexError:
num_messages = 1
# build the parameters for the request
diff --git a/lang/py3/avro/tests/sample_http_client.py b/lang/py3/avro/tests/sample_http_client.py
index da8d030..47212a1 100644
--- a/lang/py3/avro/tests/sample_http_client.py
+++ b/lang/py3/avro/tests/sample_http_client.py
@@ -74,7 +74,7 @@ if __name__ == '__main__':
try:
num_messages = int(sys.argv[4])
- except:
+ except IndexError:
num_messages = 1
# build the parameters for the request
diff --git a/lang/py3/avro/tests/test_protocol.py b/lang/py3/avro/tests/test_protocol.py
index abd17e8..7dabd59 100644
--- a/lang/py3/avro/tests/test_protocol.py
+++ b/lang/py3/avro/tests/test_protocol.py
@@ -453,14 +453,13 @@ class TestProtocol(unittest.TestCase):
proto = protocol.Parse(example.protocol_string)
try:
protocol.Parse(str(proto))
- logging.debug(
- 'Successfully reparsed protocol:\n%s',
- example.protocol_string)
- num_correct += 1
- except:
- logging.debug(
- 'Failed to reparse protocol:\n%s',
- example.protocol_string)
+ except ProtocolParseException:
+ logging.debug('Failed to reparse protocol:\n%s',
+ example.protocol_string)
+ continue
+ logging.debug('Successfully reparsed protocol:\n%s',
+ example.protocol_string)
+ num_correct += 1
fail_msg = (
'Cast to string success on %d out of %d protocols'
diff --git a/lang/py3/avro/tests/txsample_http_client.py b/lang/py3/avro/tests/txsample_http_client.py
index fede960..f3e573a 100644
--- a/lang/py3/avro/tests/txsample_http_client.py
+++ b/lang/py3/avro/tests/txsample_http_client.py
@@ -76,7 +76,7 @@ if __name__ == '__main__':
try:
num_messages = int(sys.argv[4])
- except:
+ except IndexError:
num_messages = 1
# build the parameters for the request
diff --git a/lang/py3/setup.cfg b/lang/py3/setup.cfg
index f01c1a4..dbf6a03 100644
--- a/lang/py3/setup.cfg
+++ b/lang/py3/setup.cfg
@@ -73,6 +73,6 @@ known_third_party=zope
[pycodestyle]
exclude = .eggs,build
-ignore = E111,E114,E121,E122,E124,E127,E128,E129,E201,E202,E203,E221,E225,E231,E241,E261,E301,E302,E303,E305,E402,E701,E703,E722,W503,W504
+ignore = E111,E114,E121,E122,E124,E127,E128,E129,E201,E202,E203,E221,E225,E231,E241,E261,E301,E302,E303,E305,E402,E701,E703,W503,W504
max-line-length = 150
statistics = True