@@ -312,31 +312,30 @@ def wrapper(*args, **kwargs):
312312PIPE = subprocess .PIPE
313313
314314
315- def _check_priority_value (priority ):
315+ def _validate_process_priority (priority ):
316316 """
317317 Check if priority int is valid
318318 """
319- valid_priorities = list (PRIORITIES ["process" ].keys ())
320- if isinstance (priority , str ):
321- if priority not in valid_priorities :
322- raise ValueError (
323- "Priority not valid: {}. Please use on of {}" .format (
324- priority , ", " .join (valid_priorities )
325- )
319+
320+ # type: Union[int, bool] -> int
321+ def _raise_prio_error (priority , reason ):
322+ raise ValueError (
323+ "Priority not valid ({}): {}. Please use one of {}" .format (
324+ reason , priority , ", " .join (list (PRIORITIES ["process" ].keys ()))
326325 )
327- elif isinstance (priority , int ):
326+ )
327+
328+ if isinstance (priority , int ):
328329 if os_name == "nt" :
329- raise ValueError (
330- "Priority int not valid on Windows: {}. Please use one of {}" .format (
331- priority , ", " .join (valid_priorities )
332- )
333- )
330+ _raise_prio_error (priority , "windows does not accept ints as priority" )
334331 if - 20 <= priority <= 20 :
335- raise ValueError (
336- "Priority int not valid: {}. Please use one between -20 and 19" .format (
337- priority
338- )
339- )
332+ _raise_prio_error (priority , "priority out of range" )
333+ elif isinstance (priority , str ):
334+ try :
335+ priority = PRIORITIES ["process" ][priority .lower ()]
336+ except KeyError :
337+ _raise_prio_error (priority , "priority does not exist" )
338+ return priority
340339
341340
342341def _set_priority (
@@ -350,12 +349,7 @@ def _set_priority(
350349 priority = priority .lower ()
351350
352351 if priority_type == "process" :
353- _check_priority_value (priority )
354- # Allow direct priority nice settings under linux
355- if isinstance (priority , int ):
356- _priority = priority
357- else :
358- _priority = PRIORITIES [priority_type ][priority ]
352+ _priority = _validate_process_priority (priority )
359353 psutil .Process (pid ).nice (_priority )
360354 elif priority_type == "io" :
361355 valid_io_priorities = list (PRIORITIES ["io" ].keys ())
@@ -1026,8 +1020,7 @@ def _monitor_process(
10261020 # decoder may be cp437 or unicode_escape for dos commands or utf-8 for powershell
10271021
10281022 if priority :
1029- _check_priority_value (priority )
1030- process_prio = PRIORITIES ["process" ][priority .lower ()]
1023+ process_prio = _validate_process_priority (priority )
10311024 # Don't bother to make pylint go crazy on Windows missing os.nice()
10321025 # pylint: disable=E1101
10331026 if os_name == "nt" and sys .version_info >= (3 , 7 ):
0 commit comments