Merge pull request #263 from jayvdb/bare-except

Fix bare except
This commit is contained in:
Jacob Alexander
2021-06-01 13:30:55 -07:00
committed by GitHub
5 changed files with 75 additions and 31 deletions

View File

@@ -32,7 +32,7 @@ jobs:
- name: Lint with flake8 - name: Lint with flake8
run: | run: |
pip install flake8 pip install flake8
flake8 . --filename '*.py,*.pyx,*.pxd' --count --max-complexity=10 --max-line-length=120 --ignore=E211,E225,E226,E227,E231,E251,E261,E262,E265,E402,E722,E999 --show-source --statistics --exclude benchmark,build,capnp/templates/module.pyx flake8 . --filename '*.py,*.pyx,*.pxd' --count --max-complexity=10 --max-line-length=120 --ignore=E211,E225,E226,E227,E231,E251,E261,E262,E265,E402,E999 --show-source --statistics --exclude benchmark,build,capnp/templates/module.pyx
flake8 . --count --max-complexity=10 --max-line-length=120 --show-source --statistics --exclude benchmark,build flake8 . --count --max-complexity=10 --max-line-length=120 --show-source --statistics --exclude benchmark,build
- name: Packaging - name: Packaging
run: | run: |

View File

@@ -89,7 +89,7 @@ cdef api VoidPromise * call_server_method(PyObject * _server,
warning_msg = ( warning_msg = (
"Server function ({}) returned a value that was not a Promise: return = {}" "Server function ({}) returned a value that was not a Promise: return = {}"
.format(method_name, str(ret))) .format(method_name, str(ret)))
except: except Exception:
warning_msg = 'Server function (%s) returned a value that was not a Promise' % (method_name) warning_msg = 'Server function (%s) returned a value that was not a Promise' % (method_name)
_warnings.warn_explicit( _warnings.warn_explicit(
warning_msg, UserWarning, _inspect.getsourcefile(func), _inspect.getsourcelines(func)[1]) warning_msg, UserWarning, _inspect.getsourcefile(func), _inspect.getsourcelines(func)[1])
@@ -104,7 +104,7 @@ cdef api VoidPromise * call_server_method(PyObject * _server,
warning_msg = ( warning_msg = (
"Server function ({}) returned a value that was not a Promise: return = {}" "Server function ({}) returned a value that was not a Promise: return = {}"
.format(method_name, str(ret))) .format(method_name, str(ret)))
except: except Exception:
warning_msg = 'Server function (%s) returned a value that was not a Promise' % (method_name) warning_msg = 'Server function (%s) returned a value that was not a Promise' % (method_name)
_warnings.warn_explicit( _warnings.warn_explicit(
warning_msg, UserWarning, _inspect.getsourcefile(func), _inspect.getsourcelines(func)[1]) warning_msg, UserWarning, _inspect.getsourcefile(func), _inspect.getsourcelines(func)[1])
@@ -289,7 +289,7 @@ cdef api object get_exception_info(object exc_type, object exc_obj, object exc_t
return (exc_tb.tb_frame.f_code.co_filename.encode(), return (exc_tb.tb_frame.f_code.co_filename.encode(),
exc_tb.tb_lineno, exc_tb.tb_lineno,
(repr(exc_type) + ":" + str(exc_obj)).encode()) (repr(exc_type) + ":" + str(exc_obj)).encode())
except: except Exception:
return (b'', 0, b"Couldn't determine python exception") return (b'', 0, b"Couldn't determine python exception")
@@ -1147,8 +1147,10 @@ cdef class _DynamicStructReader:
cpdef _which_str(self): cpdef _which_str(self):
try: try:
return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr() return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr()
except: except RuntimeError as e:
if str(e) == "Member was null.":
raise KjException("Attempted to call which on a non-union type") raise KjException("Attempted to call which on a non-union type")
raise
cpdef _DynamicEnumField _which(self): cpdef _DynamicEnumField _which(self):
"""Returns the enum corresponding to the union in this struct """Returns the enum corresponding to the union in this struct
@@ -1161,8 +1163,10 @@ cdef class _DynamicStructReader:
try: try:
which = _DynamicEnumField()._init( which = _DynamicEnumField()._init(
_StructSchemaField()._init(helpers.fixMaybe(self.thisptr.which()), self).proto) _StructSchemaField()._init(helpers.fixMaybe(self.thisptr.which()), self).proto)
except: except RuntimeError as e:
if str(e) == "Member was null.":
raise KjException("Attempted to call which on a non-union type") raise KjException("Attempted to call which on a non-union type")
raise
return which return which
@@ -1445,8 +1449,10 @@ cdef class _DynamicStructBuilder:
cpdef _which_str(self): cpdef _which_str(self):
try: try:
return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr() return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr()
except: except RuntimeError as e:
if str(e) == "Member was null.":
raise KjException("Attempted to call which on a non-union type") raise KjException("Attempted to call which on a non-union type")
raise
cpdef _DynamicEnumField _which(self): cpdef _DynamicEnumField _which(self):
"""Returns the enum corresponding to the union in this struct """Returns the enum corresponding to the union in this struct
@@ -1459,8 +1465,10 @@ cdef class _DynamicStructBuilder:
try: try:
which = _DynamicEnumField()._init( which = _DynamicEnumField()._init(
_StructSchemaField()._init(helpers.fixMaybe(self.thisptr.which()), self).proto) _StructSchemaField()._init(helpers.fixMaybe(self.thisptr.which()), self).proto)
except: except RuntimeError as e:
if str(e) == "Member was null.":
raise KjException("Attempted to call which on a non-union type") raise KjException("Attempted to call which on a non-union type")
raise
return which return which
@@ -1835,16 +1843,26 @@ cpdef remove_event_loop(ignore_errors=False):
if C_DEFAULT_EVENT_LOOP: if C_DEFAULT_EVENT_LOOP:
try: try:
C_DEFAULT_EVENT_LOOP._remove() C_DEFAULT_EVENT_LOOP._remove()
except: except Exception as e:
if not ignore_errors: if isinstance(ignore_errors, Exception):
if isinstance(e, ignore_errors):
ignore_errors = True
if ignore_errors is True:
pass
else:
raise raise
C_DEFAULT_EVENT_LOOP = None C_DEFAULT_EVENT_LOOP = None
if len(_THREAD_LOCAL_EVENT_LOOPS) > 0: if len(_THREAD_LOCAL_EVENT_LOOPS) > 0:
for loop in _THREAD_LOCAL_EVENT_LOOPS: for loop in _THREAD_LOCAL_EVENT_LOOPS:
try: try:
loop._remove() loop._remove()
except: except Exception as e:
if not ignore_errors: if isinstance(ignore_errors, Exception):
if isinstance(e, ignore_errors):
ignore_errors = True
if ignore_errors is True:
pass
else:
raise raise
_THREAD_LOCAL_EVENT_LOOPS = [] _THREAD_LOCAL_EVENT_LOOPS = []
_C_DEFAULT_EVENT_LOOP_LOCAL = None _C_DEFAULT_EVENT_LOOP_LOCAL = None
@@ -1966,7 +1984,7 @@ cdef class _Promise:
argspec = None argspec = None
try: try:
argspec = _inspect.getfullargspec(func) argspec = _inspect.getfullargspec(func)
except: except (TypeError, ValueError):
pass pass
if argspec: if argspec:
args_length = len(argspec.args) if argspec.args else 0 args_length = len(argspec.args) if argspec.args else 0
@@ -2034,7 +2052,7 @@ cdef class _VoidPromise:
argspec = None argspec = None
try: try:
argspec = _inspect.getfullargspec(func) argspec = _inspect.getfullargspec(func)
except: except (TypeError, ValueError):
pass pass
if argspec: if argspec:
args_length = len(argspec.args) if argspec.args else 0 args_length = len(argspec.args) if argspec.args else 0
@@ -2138,7 +2156,7 @@ cdef class _RemotePromise:
argspec = None argspec = None
try: try:
argspec = _inspect.getfullargspec(func) argspec = _inspect.getfullargspec(func)
except: except (TypeError, ValueError):
pass pass
if argspec: if argspec:
args_length = len(argspec.args) if argspec.args else 0 args_length = len(argspec.args) if argspec.args else 0

View File

@@ -4,6 +4,8 @@ import os
import tempfile import tempfile
import sys import sys
from capnp.lib.capnp import KjException
this_dir = os.path.dirname(__file__) this_dir = os.path.dirname(__file__)
@@ -37,9 +39,13 @@ def test_which_builder(addressbook):
assert bob.employment.which == addressbook.Person.Employment.unemployed assert bob.employment.which == addressbook.Person.Employment.unemployed
assert bob.employment.which == "unemployed" assert bob.employment.which == "unemployed"
with pytest.raises(Exception): with pytest.raises(KjException):
addresses.which addresses._which()
with pytest.raises(Exception):
with pytest.raises(KjException):
addresses._which_str()
with pytest.raises(KjException):
addresses.which addresses.which
@@ -71,9 +77,13 @@ def test_which_reader(addressbook):
bob = people[1] bob = people[1]
assert bob.employment.which == "unemployed" assert bob.employment.which == "unemployed"
with pytest.raises(Exception): with pytest.raises(KjException):
addresses.which addresses._which_str()
with pytest.raises(Exception):
with pytest.raises(KjException):
addresses._which()
with pytest.raises(KjException):
addresses.which addresses.which

View File

@@ -9,6 +9,9 @@ import threading
import pytest import pytest
import capnp import capnp
from capnp.lib.capnp import KjException
import test_capability_capnp import test_capability_capnp
@@ -35,11 +38,19 @@ def test_making_threaded_event_loop():
''' '''
Threaded event loop test Threaded event loop test
''' '''
capnp.remove_event_loop(True) # The following raises a KjException, and if not caught causes an SIGABRT:
capnp.create_event_loop(True) # kj/async.c++:973: failed: expected head == nullptr; EventLoop destroyed with events still in the queue.
# Memory leak?; head->trace() = kj::_::ForkHub<kj::_::Void>
# kj::_::AdapterPromiseNode<kj::_::Void, kj::_::PromiseAndFulfillerAdapter<void> >
# stack: ...
# python(..) malloc: *** error for object 0x...: pointer being freed was not allocated
# python(..) malloc: *** set a breakpoint in malloc_error_break to debug
# Fatal Python error: Aborted
capnp.remove_event_loop(KjException)
capnp.create_event_loop(KjException)
capnp.remove_event_loop() capnp.remove_event_loop()
capnp.create_event_loop(True) capnp.create_event_loop(KjException)
class Server(test_capability_capnp.TestInterface.Server): class Server(test_capability_capnp.TestInterface.Server):

View File

@@ -1,13 +1,18 @@
[tox] [tox]
envlist = py27,py34,py35,py36 envlist = py37,py38,py39
skipsdist = True
[testenv] [testenv]
deps= deps=
pkgconfig
Jinja2
pytest pytest
cython cython
commands = commands =
py.test python setup.py install
py.test {posargs}
setenv = setenv =
CFLAGS='-stdlib=libc++' CFLAGS='-stdlib=libc++'
CXXFLAGS='-stdlib=libc++'