Skip to content

Commit

Permalink
refactored subshell execution code for greater readability and moved …
Browse files Browse the repository at this point in the history
…it to utils
  • Loading branch information
Titus-von-Koeller committed Aug 1, 2022
1 parent 54efd87 commit 3fd06fb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
11 changes: 0 additions & 11 deletions bitsandbytes/cuda_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,13 @@
"""

import ctypes
import shlex
import subprocess
from os import environ as env
from pathlib import Path
from typing import Set, Union

from .utils import print_err, warn_of_missing_prerequisite


def execute_and_return(strCMD):
proc = subprocess.Popen(
shlex.split(strCMD), stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
out, err = proc.communicate()
out, err = out.decode("UTF-8").strip(), err.decode("UTF-8").strip()
return out, err


def check_cuda_result(cuda, result_val):
if result_val != 0:
cuda.cuGetErrorString(result_val, ctypes.byref(error_str))
Expand Down
22 changes: 22 additions & 0 deletions bitsandbytes/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import shlex
import subprocess
import sys


def execute_and_return(command_string: str) -> Tuple[str, str]:
def _decode(subprocess_err_out_tuple):
return tuple(
to_decode.decode("UTF-8").strip()
for to_decode in subprocess_err_out_tuple
)

def execute_and_return_decoded_std_streams(command_string):
return _decode(
subprocess.Popen(
shlex.split(command_string),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).communicate()
)

std_out, std_err = execute_and_return_decoded_std_streams()
return std_out, std_err


def print_err(s: str) -> None:
print(s, file=sys.stderr)

Expand Down

0 comments on commit 3fd06fb

Please sign in to comment.