Skip to content

Commit

Permalink
AVRO-2577: Fix Bare Excepts (#665)
Browse files Browse the repository at this point in the history
* AVRO-2577: Fix Bare Excepts

* AVRO-2577: Don't Count Failure as Success
  • Loading branch information
kojiromike committed Oct 25, 2019
1 parent e321938 commit 1f638fb
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lang/py/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions lang/py/src/avro/datafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

from __future__ import absolute_import, division, print_function

import os
import random
import zlib

from avro import io, schema
Expand Down Expand Up @@ -397,8 +399,6 @@ def close(self):

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)]
31 changes: 13 additions & 18 deletions lang/py/src/avro/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,7 +37,7 @@
# Exceptions
#

class ProtocolParseException(schema.AvroException):
class ProtocolParseException(avro.schema.AvroException):
pass

#
Expand All @@ -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)
Expand Down Expand Up @@ -97,21 +92,21 @@ def __init__(self, name, namespace=None, types=None, messages=None):

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
if types is not None:
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]))
Expand All @@ -128,7 +123,7 @@ def set_prop(self, key, value):
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:
Expand All @@ -153,20 +148,20 @@ def _parse_request(self, request, names):
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
Expand Down Expand Up @@ -195,7 +190,7 @@ def __str__(self):

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)
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lang/py/src/avro/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,10 @@ def __init__(self, values, names=None, other_props=None):
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)

Expand Down
2 changes: 1 addition & 1 deletion lang/py/test/sample_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def make_requestor(server_host, server_port, protocol):

try:
num_messages = int(sys.argv[4])
except:
except IndexError:
num_messages = 1

# build the parameters for the request
Expand Down
15 changes: 7 additions & 8 deletions lang/py/test/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,13 @@ def test_valid_cast_to_string_after_parse(self):
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))
Expand Down
2 changes: 1 addition & 1 deletion lang/py/test/txsample_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def make_requestor(server_host, server_port, protocol):

try:
num_messages = int(sys.argv[4])
except:
except IndexError:
num_messages = 1

# build the parameters for the request
Expand Down
2 changes: 1 addition & 1 deletion lang/py3/avro/tests/sample_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def make_requestor(server_host, server_port, protocol):

try:
num_messages = int(sys.argv[4])
except:
except IndexError:
num_messages = 1

# build the parameters for the request
Expand Down
15 changes: 7 additions & 8 deletions lang/py3/avro/tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,13 @@ def testValidCastToStringAfterParse(self):
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'
Expand Down
2 changes: 1 addition & 1 deletion lang/py3/avro/tests/txsample_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def make_requestor(server_host, server_port, protocol):

try:
num_messages = int(sys.argv[4])
except:
except IndexError:
num_messages = 1

# build the parameters for the request
Expand Down
2 changes: 1 addition & 1 deletion lang/py3/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1f638fb

Please sign in to comment.