Repository: jamadden/trollius (language: python)

Issue:
On Python 2, socket.error with errno == EBADF should be mapped to OSError
See https://github.com/zopefoundation/ZEO/issues/176


Discussion / hints:

Fix patch:
diff --git a/CHANGES.rst b/CHANGES.rst
new file mode 100644
index 00000000..5082340a
--- /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 5d4033fc..017fcfba 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/setup.py b/setup.py
index 7abc9f96..20c654b8 100644
--- a/setup.py
+++ b/setup.py
@@ -5,6 +5,9 @@
 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 @@
 
 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/trollius/py33_exceptions.py b/trollius/py33_exceptions.py
index f10dfe9e..477aaddf 100644
--- a/trollius/py33_exceptions.py
+++ b/trollius/py33_exceptions.py
@@ -79,6 +79,9 @@ class ProcessLookupError(OSError):
     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({


Test patch:
diff --git a/runtests.py b/runtests.py
index 541a47ea..7eea28c1 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 @@ def find_available_tests(self):
         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/tests/test_py33_exceptions.py b/tests/test_py33_exceptions.py
new file mode 100644
index 00000000..42fb4e23
--- /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()