@@ -339,21 +339,24 @@ def __init__(
339339 """Initialize serial port configuration."""
340340 super ().__init__ ()
341341
342- if not isinstance (stopbits , StopBits ):
343- stopbits = StopBits (stopbits )
342+ normalized_stopbits = (
343+ stopbits if isinstance (stopbits , StopBits ) else StopBits (stopbits )
344+ )
344345
345346 if parity is None :
346- parity = Parity .NONE
347- elif not isinstance (parity , Parity ):
348- parity = Parity (parity )
347+ normalized_parity = Parity .NONE
348+ elif isinstance (parity , Parity ):
349+ normalized_parity = parity
350+ else :
351+ normalized_parity = Parity (parity )
349352
350353 self ._path = path
351354 self ._baudrate = baudrate
352- self ._stopbits = stopbits
355+ self ._stopbits = normalized_stopbits
353356 self ._xonxoff = xonxoff
354357 self ._rtscts = rtscts
355358 self ._dsrdtr = dsrdtr
356- self ._parity = parity
359+ self ._parity = normalized_parity
357360 self ._byte_size = byte_size
358361 self ._exclusive = exclusive
359362 self ._read_timeout = read_timeout
@@ -397,9 +400,12 @@ def _check_broken(self) -> None:
397400 def from_url (cls , url : str , * args : Any , ** kwargs : Any ) -> BaseSerial :
398401 """Create the appropriate serial port subclass for the given URL."""
399402 handler = get_uri_handler (url )
403+ target = url
400404 if handler .strip_uri_scheme :
401- url = url .removeprefix (handler .scheme ).removeprefix (handler .unique_scheme )
402- return handler .sync_cls (url , * args , ** kwargs )
405+ target = url .removeprefix (handler .scheme ).removeprefix (
406+ handler .unique_scheme
407+ )
408+ return handler .sync_cls (target , * args , ** kwargs )
403409
404410 @maybe_wrap_exceptions
405411 def open (self ) -> None :
@@ -471,8 +477,9 @@ def set_modem_pins(
471477 """Set modem control bits."""
472478 self ._check_broken ()
473479
474- if modem_pins is None :
475- modem_pins = ModemPins (
480+ pins = modem_pins
481+ if pins is None :
482+ pins = ModemPins (
476483 le = PinState .convert (le ),
477484 dtr = PinState .convert (dtr ),
478485 rts = PinState .convert (rts ),
@@ -484,7 +491,7 @@ def set_modem_pins(
484491 dsr = PinState .convert (dsr ),
485492 )
486493
487- return self ._set_modem_pins (modem_pins )
494+ return self ._set_modem_pins (pins )
488495
489496 @abstractmethod
490497 def _get_modem_pins (self ) -> ModemPins :
@@ -500,8 +507,8 @@ def _set_modem_pins(self, modem_pins: ModemPins) -> None:
500507 def readinto (self , b : Buffer , * , timeout : float | None = None ) -> int :
501508 """Read bytes from serial port into buffer."""
502509 self ._check_broken ()
503- timeout = self ._read_timeout if timeout is None else timeout
504- return self ._readinto (b , timeout = timeout )
510+ effective_timeout = self ._read_timeout if timeout is None else timeout
511+ return self ._readinto (b , timeout = effective_timeout )
505512
506513 @abstractmethod
507514 def _readinto (self , b : Buffer , * , timeout : float | None ) -> int :
@@ -512,8 +519,8 @@ def _readinto(self, b: Buffer, *, timeout: float | None) -> int:
512519 def write (self , data : Buffer , * , timeout : float | None = None ) -> int :
513520 """Write bytes to serial port."""
514521 self ._check_broken ()
515- timeout = self ._write_timeout if timeout is None else timeout
516- return self ._write (data , timeout = timeout )
522+ effective_timeout = self ._write_timeout if timeout is None else timeout
523+ return self ._write (data , timeout = effective_timeout )
517524
518525 @abstractmethod
519526 def _write (self , data : Buffer , * , timeout : float | None ) -> int :
@@ -581,14 +588,14 @@ def readexactly(self, n: int, *, timeout: float | None = None) -> bytes:
581588 buffer = bytearray (n )
582589 view = memoryview (buffer )
583590 remaining = n
584- timeout = self .read_timeout if timeout is None else timeout
591+ remaining_timeout = self .read_timeout if timeout is None else timeout
585592
586593 while remaining > 0 :
587594 with measure_time () as get_elapsed :
588- read = self .readinto (view , timeout = timeout )
595+ read = self .readinto (view , timeout = remaining_timeout )
589596
590- if timeout is not None :
591- timeout -= get_elapsed ()
597+ if remaining_timeout is not None :
598+ remaining_timeout -= get_elapsed ()
592599
593600 view = view [read :]
594601 remaining -= read
@@ -611,14 +618,14 @@ def read_until(
611618 """Read until the expected sequence is found."""
612619 buffer = bytearray ()
613620 expected_len = len (expected )
614- timeout = self .read_timeout if timeout is None else timeout
621+ remaining_timeout = self .read_timeout if timeout is None else timeout
615622
616623 while True :
617624 with measure_time () as get_elapsed :
618- byte = self .readexactly (1 , timeout = timeout )
625+ byte = self .readexactly (1 , timeout = remaining_timeout )
619626
620- if timeout is not None :
621- timeout -= get_elapsed ()
627+ if remaining_timeout is not None :
628+ remaining_timeout -= get_elapsed ()
622629
623630 if not byte :
624631 break
@@ -1009,15 +1016,16 @@ async def connect(
10091016 ** kwargs : Unpack [ConnectKwargs ],
10101017 ) -> None :
10111018 """Connect to serial port."""
1012- if path is not None :
1013- handler = get_uri_handler (path )
1019+ target = path
1020+ if target is not None :
1021+ handler = get_uri_handler (target )
10141022 if handler .strip_uri_scheme :
1015- path = path .removeprefix (handler .scheme ).removeprefix (
1023+ target = target .removeprefix (handler .scheme ).removeprefix (
10161024 handler .unique_scheme
10171025 )
10181026
10191027 try :
1020- await self ._connect (path = path , ** kwargs )
1028+ await self ._connect (path = target , ** kwargs )
10211029 except BaseException :
10221030 # Intentionally catch cancellation too: callers should only observe
10231031 # connect failure/cancel after transport resources are released.
@@ -1058,8 +1066,9 @@ async def set_modem_pins(
10581066 """Set modem control bits."""
10591067 self ._check_broken ()
10601068
1061- if modem_pins is None :
1062- modem_pins = ModemPins (
1069+ pins = modem_pins
1070+ if pins is None :
1071+ pins = ModemPins (
10631072 le = PinState .convert (le ),
10641073 dtr = PinState .convert (dtr ),
10651074 rts = PinState .convert (rts ),
@@ -1071,7 +1080,7 @@ async def set_modem_pins(
10711080 dsr = PinState .convert (dsr ),
10721081 )
10731082
1074- return await self ._set_modem_pins (modem_pins )
1083+ return await self ._set_modem_pins (pins )
10751084
10761085 async def flush (self ) -> None :
10771086 """Flush write buffers, waiting until all data is written."""
0 commit comments