Allow no-op assignments to Server.address when connection open (#4687)

* Allow no-op assignments to Server.address when connection open

* add explanatory comment in source

Co-authored-by: Salad Dais <SaladDais@users.noreply.github.com>
Co-authored-by: Maximilian Hils <github@maximilianhils.com>
This commit is contained in:
Salad Dais 2021-07-19 06:25:21 -03:00 committed by GitHub
parent 95a9e4bdef
commit 2d866ce991
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View file

@ -291,8 +291,12 @@ class Server(Connection):
return f"Server({human.format_address(self.address)}, state={self.state.name.lower()}{tls_state}{local_port})"
def __setattr__(self, name, value):
if name == "address" and self.__dict__.get("state", ConnectionState.CLOSED) is ConnectionState.OPEN:
raise RuntimeError("Cannot change server address on open connection.")
if name == "address":
connection_open = self.__dict__.get("state", ConnectionState.CLOSED) is ConnectionState.OPEN
# assigning the current value is okay, that may be an artifact of calling .set_state().
address_changed = self.__dict__.get("address") != value
if connection_open and address_changed:
raise RuntimeError("Cannot change server address on open connection.")
return super().__setattr__(name, value)
def get_state(self):

View file

@ -85,3 +85,5 @@ class TestServer:
s.state = ConnectionState.OPEN
with pytest.raises(RuntimeError):
s.address = ("example.com", 80)
# No-op assignment, allowed because it might be triggered by a Server.set_state() call.
s.address = ("example.com", 443)