Skip to content

Refactor _from_server_socket()#819

Closed
julianz- wants to merge 1 commit into
cherrypy:mainfrom
julianz-:refactor-from-server-socket
Closed

Refactor _from_server_socket()#819
julianz- wants to merge 1 commit into
cherrypy:mainfrom
julianz-:refactor-from-server-socket

Conversation

@julianz-

@julianz- julianz- commented Apr 7, 2026

Copy link
Copy Markdown
Member

This function in connections.py 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()

What kind of change does this PR introduce?

  • 🐞 bug fix
  • 🐣 feature
  • 📋 docs update
  • 📋 tests/coverage improvement
  • 📋 refactoring
  • 💥 other

📋 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)

  • I wrote descriptive pull request text above
  • I think the code is well written
  • I wrote good commit messages
  • I have squashed related commits together after
    the changes have been approved
  • Unit tests for the changes exist
  • Integration tests for the changes exist (if applicable)
  • I used the same coding conventions as the rest of the project
  • The new code doesn't generate linter offenses
  • Documentation reflects the changes
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences

@codecov

codecov Bot commented Apr 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 64.44444% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.20%. Comparing base (2ffb0ba) to head (4d67561).
✅ All tests successful. No failed tests found.

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     

@read-the-docs-community

read-the-docs-community Bot commented Apr 7, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cheroot | 🛠️ Build #32145455 | 📁 Comparing 4d67561 against latest (2ffb0ba)

  🔍 Preview build  

Show files changed (6 files in total): 📝 6 modified | ➕ 0 added | ➖ 0 deleted
File Status
genindex/index.html 📝 modified
history/index.html 📝 modified
pkg/cheroot/index.html 📝 modified
pkg/cheroot.connections/index.html 📝 modified
pkg/cheroot.server/index.html 📝 modified
pkg/cheroot.ssl.pyopenssl/index.html 📝 modified

@julianz- julianz- force-pushed the refactor-from-server-socket branch from ed64521 to 21e36e0 Compare April 7, 2026 07:22
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided A mark meaning that a new change log entry is present within the patch. label Apr 7, 2026
@julianz- julianz- force-pushed the refactor-from-server-socket branch 2 times, most recently from 7f77695 to 53c228c Compare April 7, 2026 07:38
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()``
@julianz- julianz- force-pushed the refactor-from-server-socket branch from 53c228c to 4d67561 Compare April 7, 2026 07:49
@julianz- julianz- marked this pull request as draft April 7, 2026 20:58

@avinashkamat48 avinashkamat48 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@webknjaz webknjaz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old unsubmitted comments

Comment thread cheroot/connections.py
conn.close()

def _from_server_socket(self, server_socket): # noqa: C901 # FIXME
def _setup_conn_addr(self, conn, s, addr):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-- 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cheroot/connections.py
if self.server.stats['Enabled']:
self.server.stats['Socket Errors'] += 1

if s:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, the if-block would be nicer inside the CM?

Comment thread cheroot/connections.py

err_code = ex.args[0] if ex.args else None
ignored_groups = (
errors.socket_error_eintr,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you expand all the iterables like this

Suggested change
errors.socket_error_eintr,
*errors.socket_error_eintr,

then you won't need that double-loop generation expression in the if-block.

Comment thread cheroot/connections.py
# Our socket was closed.
# See https://github.com/cherrypy/cherrypy/issues/686.
)
if any(err_code in group for group in ignored_groups):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, there's itertools.chain() for unwrapping nested things.

Comment thread cheroot/connections.py
# 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'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dynamic locals lookup seems weird here — it doesn't make sense to me.

Comment thread cheroot/connections.py
s, addr = server_socket.accept()
if self.server.stats['Enabled']:
self.server.stats['Accepts'] += 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid making formatting changes in functional PRs.

Comment thread cheroot/connections.py
# See https://github.com/cherrypy/cherrypy/issues/686.
return None
raise
# if socket.accept() fails s may not be defined

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So why not initialize it as None earlier, then?

@julianz- julianz- closed this Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided A mark meaning that a new change log entry is present within the patch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants