Refactor _from_server_socket()#819
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #819 +/- ##
==========================================
- Coverage 78.32% 78.20% -0.12%
==========================================
Files 41 41
Lines 4788 4803 +15
Branches 547 549 +2
==========================================
+ Hits 3750 3756 +6
- Misses 900 907 +7
- Partials 138 140 +2 |
Documentation build overview
Show files changed (6 files in total): 📝 6 modified | ➕ 0 added | ➖ 0 deleted
|
ed64521 to
21e36e0
Compare
7f77695 to
53c228c
Compare
This function had become highly nested and complex, triggering linter warnings (C901, WPS505). Decomposed the logic into several smaller private methods: - ``_wrap_tls_socket()`` - ``_create_conn()`` - ``_handle_socket_error()`` - ``_setup_conn_addr()``
53c228c to
4d67561
Compare
avinashkamat48
left a comment
There was a problem hiding this comment.
This looks superseded by #820: it refactors the same _from_server_socket() path, is from the same author, and the newer PR has additional commits/tests while this one still has both test checklist items unchecked. To avoid reviewing/merging two divergent versions of the same refactor, it would be cleaner to close this older PR and keep the discussion on #820.
| conn.close() | ||
|
|
||
| def _from_server_socket(self, server_socket): # noqa: C901 # FIXME | ||
| def _setup_conn_addr(self, conn, s, addr): |
There was a problem hiding this comment.
Let's use full words for variable names (things like i/j/k/s aren't really descriptive, nor are the shortened words). It'd also be a good idea to settle for all the args being keyword-only or positional-only (seems like in this case positional args of different types make up bad DX).
| This function had become highly nested and complex, triggering | ||
| linter warnings (C901, WPS505). Decomposed the logic into | ||
| several smaller private methods. | ||
| -- by :user:`julianz-` |
There was a problem hiding this comment.
| -- by :user:`julianz-` | |
| -- by :user:`julianz-` |
| @@ -0,0 +1,5 @@ | |||
| Refactored ``_from_server_socket()`` in :py:class:`~cheroot.connections.ConnectionManager`. | |||
| This function had become highly nested and complex, triggering | |||
| linter warnings (C901, WPS505). Decomposed the logic into | |||
There was a problem hiding this comment.
It's probably better to spell out what the violations are instead of using the trampoline codes — people don't tend to memorize what these numbers are anyway.
| if self.server.stats['Enabled']: | ||
| self.server.stats['Socket Errors'] += 1 | ||
|
|
||
| if s: |
There was a problem hiding this comment.
Perhaps, the if-block would be nicer inside the CM?
|
|
||
| err_code = ex.args[0] if ex.args else None | ||
| ignored_groups = ( | ||
| errors.socket_error_eintr, |
There was a problem hiding this comment.
If you expand all the iterables like this
| errors.socket_error_eintr, | |
| *errors.socket_error_eintr, |
then you won't need that double-loop generation expression in the if-block.
| # Our socket was closed. | ||
| # See https://github.com/cherrypy/cherrypy/issues/686. | ||
| ) | ||
| if any(err_code in group for group in ignored_groups): |
There was a problem hiding this comment.
Alternatively, there's itertools.chain() for unwrapping nested things.
| # or if the handshake fails it may exist but | ||
| # will need to be closed. | ||
| # pass it over to error handler if it exists | ||
| return self._handle_socket_error(ex, locals().get('s')) |
There was a problem hiding this comment.
The dynamic locals lookup seems weird here — it doesn't make sense to me.
| s, addr = server_socket.accept() | ||
| if self.server.stats['Enabled']: | ||
| self.server.stats['Accepts'] += 1 | ||
|
|
There was a problem hiding this comment.
Let's avoid making formatting changes in functional PRs.
| # See https://github.com/cherrypy/cherrypy/issues/686. | ||
| return None | ||
| raise | ||
| # if socket.accept() fails s may not be defined |
There was a problem hiding this comment.
So why not initialize it as None earlier, then?
This function in
connections.pyhad become highly nested and complex, triggering linter warnings (C901, WPS505). Decomposed the logic into several smaller private methods:_wrap_tls_socket()_create_conn()_handle_socket_error()_setup_conn_addr()❓ What kind of change does this PR introduce?
📋 What is the related issue number (starting with
#)Resolves #
❓ What is the current behavior? (You can also link to an open issue here)
❓ What is the new behavior (if this is a feature change)?
📋 Other information:
📋 Contribution checklist:
(If you're a first-timer, check out
this guide on making great pull requests)
the changes have been approved
and description in grammatically correct, complete sentences