|
9 | 9 | import selectors |
10 | 10 | import collections |
11 | 11 |
|
12 | | -from . import QtCore, with_logger |
| 12 | +from . import QtCore, with_logger, _fileno |
13 | 13 |
|
14 | 14 |
|
15 | 15 | EVENT_READ = (1 << 0) |
16 | 16 | EVENT_WRITE = (1 << 1) |
17 | 17 |
|
18 | 18 |
|
19 | | -def _fileobj_to_fd(fileobj): |
20 | | - """ |
21 | | - Return a file descriptor from a file object. |
22 | | -
|
23 | | - Parameters: |
24 | | - fileobj -- file object or file descriptor |
25 | | -
|
26 | | - Returns: |
27 | | - corresponding file descriptor |
28 | | -
|
29 | | - Raises: |
30 | | - ValueError if the object is invalid |
31 | | -
|
32 | | - """ |
33 | | - if isinstance(fileobj, int): |
34 | | - fd = fileobj |
35 | | - else: |
36 | | - try: |
37 | | - fd = int(fileobj.fileno()) |
38 | | - except (AttributeError, TypeError, ValueError) as ex: |
39 | | - raise ValueError("Invalid file object: {!r}".format(fileobj)) from ex |
40 | | - if fd < 0: |
41 | | - raise ValueError("Invalid file descriptor: {}".format(fd)) |
42 | | - return fd |
43 | | - |
44 | | - |
45 | 19 | class _SelectorMapping(collections.abc.Mapping): |
46 | 20 |
|
47 | 21 | """Mapping of file objects to selector keys.""" |
@@ -81,14 +55,14 @@ def select(self, *args, **kwargs): |
81 | 55 | def _fileobj_lookup(self, fileobj): |
82 | 56 | """Return a file descriptor from a file object. |
83 | 57 |
|
84 | | - This wraps _fileobj_to_fd() to do an exhaustive search in case |
| 58 | + This wraps _fileno() to do an exhaustive search in case |
85 | 59 | the object is invalid but we still have it in our map. This |
86 | 60 | is used by unregister() so we can unregister an object that |
87 | 61 | was previously registered even if it is closed. It is also |
88 | 62 | used by _SelectorMapping. |
89 | 63 | """ |
90 | 64 | try: |
91 | | - return _fileobj_to_fd(fileobj) |
| 65 | + return _fileno(fileobj) |
92 | 66 | except ValueError: |
93 | 67 | # Do an exhaustive search. |
94 | 68 | for key in self._fd_to_key.values(): |
|
0 commit comments