From a74449ad5e7de34ca6cadedd062d9ee4307db911 Mon Sep 17 00:00:00 2001 From: Jason Madden Date: Wed, 28 Apr 2021 12:11:34 -0500 Subject: Make socket.error with errno EBADF get raised as OSError. Fixes #17 --- CHANGES.rst | 8 ++++++++ MANIFEST.in | 2 ++ runtests.py | 4 ++-- setup.py | 5 ++++- tests/test_py33_exceptions.py | 35 +++++++++++++++++++++++++++++++++++ trollius/py33_exceptions.py | 3 +++ 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 CHANGES.rst create mode 100644 tests/test_py33_exceptions.py diff --git a/CHANGES.rst b/CHANGES.rst new file mode 100644 index 0000000..5082340 --- /dev/null +++ b/CHANGES.rst @@ -0,0 +1,8 @@ +========= + Changes +========= + +2.2.1 (unreleased) +================== + +- Properly reraise socket.error with an errno of EBADF as an OSError. diff --git a/MANIFEST.in b/MANIFEST.in index 5d4033f..017fcfb 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -14,3 +14,5 @@ include examples/*.py include tests/*.crt tests/*.pem tests/*.key include tests/*.py + +include *.rst diff --git a/runtests.py b/runtests.py index 541a47e..7eea28c 100755 --- a/runtests.py +++ b/runtests.py @@ -151,7 +151,7 @@ def randomize_tests(tests, seed): random.shuffle(tests._tests) -class TestsFinder: +class TestsFinder(object): def __init__(self, testsdir, includes=(), excludes=()): self._testsdir = testsdir @@ -167,7 +167,7 @@ class TestsFinder: mods = [mod for mod, _ in load_modules(self._testsdir)] for mod in mods: for name in set(dir(mod)): - if name.endswith('Tests'): + if name.endswith('Tests') or name.startswith('Test'): self._test_factories.append(getattr(mod, name)) def load_tests(self): diff --git a/setup.py b/setup.py index 7abc9f9..20c654b 100644 --- a/setup.py +++ b/setup.py @@ -5,6 +5,9 @@ from setuptools import setup, Extension with open("README.rst") as fp: long_description = fp.read() +with open('CHANGES.rst') as fp: + long_description += '\n\n' + fp.read() + extensions = [] if os.name == 'nt': ext = Extension( @@ -18,7 +21,7 @@ if sys.version_info < (3,): setup( name="trollius", - version="2.2.post1", + version="2.2.1", license="Apache License 2.0", author='Victor Stinner', author_email='victor.stinner@gmail.com', diff --git a/tests/test_py33_exceptions.py b/tests/test_py33_exceptions.py new file mode 100644 index 0000000..42fb4e2 --- /dev/null +++ b/tests/test_py33_exceptions.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +""" +Tests for py33_exceptions. + +""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + + +import unittest +from trollius import py33_exceptions + +class TestWrapErrors(unittest.TestCase): + + def test_ebadf_wrapped_to_OSError(self): + # https://github.com/jamadden/trollius/issues/17 + import socket + import os + import errno + s = socket.socket() + os.close(s.fileno()) + + with self.assertRaises(socket.error) as exc: + s.send(b'abc') + + self.assertEqual(exc.exception.errno, errno.EBADF) + + with self.assertRaises(OSError) as exc: + py33_exceptions.wrap_error(s.send, b'abc') + + self.assertEqual(exc.exception.errno, errno.EBADF) + +if __name__ == '__main__': + unittest.main() diff --git a/trollius/py33_exceptions.py b/trollius/py33_exceptions.py index f10dfe9..477aadd 100644 --- a/trollius/py33_exceptions.py +++ b/trollius/py33_exceptions.py @@ -79,6 +79,9 @@ _MAP_ERRNO = { errno.ESRCH: ProcessLookupError, } +if hasattr(errno, 'EBADF') and errno.EBADF not in _MAP_ERRNO: + _MAP_ERRNO[errno.EBADF] = OSError + if sys.platform == 'win32': from trollius import _overlapped _MAP_ERRNO.update({ -- cgit v1.2.1