3535from time import sleep
3636
3737
38+ # Avoid checking os type numerous times
39+ os_name = os .name
40+
3841try :
3942 import psutil
4043except ImportError :
4144 # Don't bother with an error since we need command_runner to work without dependencies
4245 pass
4346try :
4447 # Also make sure we directly import priority classes so we can reuse them
45- if os . name == "nt" :
48+ if os_name == "nt" :
4649 from psutil import (
4750 ABOVE_NORMAL_PRIORITY_CLASS ,
4851 BELOW_NORMAL_PRIORITY_CLASS ,
@@ -266,7 +269,7 @@ def _set_priority(
266269 priority = priority .lower ()
267270
268271 if priority_type == "process" :
269- if isinstance (priority , int ) and os . name != "nt" and - 20 <= priority <= 20 :
272+ if isinstance (priority , int ) and os_name != "nt" and - 20 <= priority <= 20 :
270273 raise ValueError ("Bogus process priority int given: {}" .format (priority ))
271274 if priority not in ["low" , "normal" , "high" ]:
272275 raise ValueError (
@@ -276,7 +279,7 @@ def _set_priority(
276279 if priority_type == "io" and priority not in ["low" , "normal" , "high" ]:
277280 raise ValueError ("Bogus {} priority given: {}" .format (priority_type , priority ))
278281
279- if os . name == "nt" :
282+ if os_name == "nt" :
280283 priorities = {
281284 "process" : {
282285 "low" : BELOW_NORMAL_PRIORITY_CLASS ,
@@ -452,7 +455,7 @@ def _process_killer(
452455 pid , 15
453456 ) # 15 being signal.SIGTERM or SIGKILL depending on the platform
454457 except OSError as exc :
455- if os . name == "nt" :
458+ if os_name == "nt" :
456459 # We'll do an ugly hack since os.kill() has some pretty big caveats on Windows
457460 # especially for Python 2.7 where we can get Access Denied
458461 os .system ("taskkill /F /pid {}" .format (pid ))
@@ -536,13 +539,13 @@ def command_runner(
536539 # Choose default encoding when none set
537540 # cp437 encoding assures we catch most special characters from cmd.exe
538541 # Unless encoding=False in which case nothing gets encoded except Exceptions and logger strings for Python 2
539- error_encoding = "cp437" if os . name == "nt" else "utf-8"
542+ error_encoding = "cp437" if os_name == "nt" else "utf-8"
540543 if encoding is None :
541544 encoding = error_encoding
542545
543546 # Fix when unix command was given as single string
544547 # This is more secure than setting shell=True
545- if os . name == "posix" :
548+ if os_name == "posix" :
546549 if not shell and isinstance (command , str ):
547550 command = shlex .split (command )
548551 elif shell and isinstance (command , list ):
@@ -559,7 +562,7 @@ def command_runner(
559562 windows_no_window
560563 and sys .version_info [0 ] >= 3
561564 and sys .version_info [1 ] >= 7
562- and os . name == "nt"
565+ and os_name == "nt"
563566 ):
564567 # Disable the following pylint error since the code also runs on nt platform, but
565568 # triggers an error on Unix
@@ -1218,7 +1221,7 @@ def deferred_command(command, defer_time=300):
12181221 seconds after it finished
12191222 """
12201223 # Use ping as a standard timer in shell since it's present on virtually *any* system
1221- if os . name == "nt" :
1224+ if os_name == "nt" :
12221225 deferrer = "ping 127.0.0.1 -n {} > NUL & " .format (defer_time )
12231226 else :
12241227 deferrer = "sleep {} && " .format (defer_time )
0 commit comments