Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
>>> with sys_pipes(): # doctest: +SKIP
... call_some_c_function()
See the Wurlitzer package for usage of `wurlitzer.pipes()`;
see also https://github.com/manodeep/Corrfunc/issues/157.
'''
kwargs = {'stdout':None if sys.stdout.isatty() else sys.stdout,
'stderr':None if sys.stderr.isatty() else sys.stderr }
# Redirection might break for any number of reasons, like
# stdout/err already being closed/redirected. We probably
# prefer not to crash in that case and instead continue
# without any redirection.
try:
with wurlitzer.pipes(**kwargs):
yield
except:
yield
np_ctype = eval("np." + ctype)
cost_matrix = np_ctype(cost_matrix)
print("Computing optimal assignment on a {}x{} matrix...".format(
cost_matrix.shape[0], cost_matrix.shape[1]))
from lapjv import lapjv
if v is not None and (platform.system() == "Linux" or platform.system() == "Darwin") and v.gui:
try:
from wurlitzer import pipes, STDOUT
from wurlitzer import Wurlitzer
Wurlitzer.flush_interval = 0.1
wrapper = JVOutWrapper(v)
with pipes(stdout=wrapper, stderr=STDOUT):
_, cols, cost = lapjv(cost_matrix, verbose=1)
wrapper.finish()
except ImportError:
_, cols, cost = lapjv(cost_matrix)
else:
_, cols, cost = lapjv(cost_matrix)
cost = cost[0]
print("Total assignment cost:", cost)
print("Time taken: {}s".format((np.round(time.time() - t, 2))))
# sometimes the cost matrix may be extremely large
# manually delete it to free memory
del cost_matrix
np_ctype = eval("np." + ctype)
cost_matrix = np_ctype(cost_matrix)
print("Computing optimal assignment on a {}x{} matrix...".format(
cost_matrix.shape[0], cost_matrix.shape[1]))
from lapjv import lapjv
if v is not None and (platform.system() == "Linux" or platform.system() == "Darwin") and v.gui:
try:
from wurlitzer import pipes, STDOUT
from wurlitzer import Wurlitzer
Wurlitzer.flush_interval = 0.1
wrapper = JVOutWrapper(v)
with pipes(stdout=wrapper, stderr=STDOUT):
_, cols, cost = lapjv(cost_matrix, verbose=1)
wrapper.finish()
except ImportError:
_, cols, cost = lapjv(cost_matrix)
else:
_, cols, cost = lapjv(cost_matrix)
cost = cost[0]
del cost_matrix
paired = np.array(imgs)[cols]
white = np.ones(imgs[0].shape, np.uint8)
white[:, :, :] = [background[2], background[1], background[0]]
result_arg = ffi.new(result_arg_name+" *")
args.append(result_arg)
def run():
error_code = module.lib.transform(*args)
if error_code != 0:
return error_code, None
if result_schema["type"] == "object":
result = unpack_result_struct(args[-1], result_schema)
elif result_schema["type"] == "array":
result = unpack_result_array_struct(args[-1], result_schema)
else:
result = args[-1][0]
return 0, result
with wurlitzer.pipes() as (stdout, stderr):
error_code, result = run()
sys.stderr.write(stderr.read())
sys.stdout.write(stdout.read())
ARRAYS.clear()
if error_code != 0:
raise SeamlessStreamTransformationError("Compiled transformer returned non-zero value: {}".format(error_code))
def execute(name, code,
injector, module_workspace,
identifier, namespace,
inputs, output_name, celltype, result_queue
):
assert identifier is not None
try:
old_stdio = sys.stdout, sys.stderr
stdout, stderr = FakeStdStream(sys.stdout), FakeStdStream(sys.stderr)
sys.stdout, sys.stderr = stdout, stderr
with wurlitzer.pipes() as (stdout2, stderr2):
result = _execute(name, code,
injector, module_workspace,
identifier, namespace,
inputs, output_name, celltype, result_queue
)
msg_code, msg = result
if msg_code == 2: # SeamlessTransformationError, propagate
result_queue.put((1, msg))
elif msg_code in (1, 10):
std = ""
sout = stdout.read() + stdout2.read()
sys.stdout, sys.stderr = old_stdio
if len(sout):
if not len(std):
std = "\n"
# compute pair-wise distances
cost_matrix = cdist(img_keys, dest_img, metric=metric)
np_ctype = eval("np." + ctype)
cost_matrix = np_ctype(cost_matrix)
print("Computing optimal assignment on a {}x{} matrix...".format(
cost_matrix.shape[0], cost_matrix.shape[1]))
from lapjv import lapjv
if v is not None and (platform.system() == "Linux" or platform.system() == "Darwin") and v.gui:
try:
from wurlitzer import pipes, STDOUT
from wurlitzer import Wurlitzer
Wurlitzer.flush_interval = 0.1
wrapper = JVOutWrapper(v)
with pipes(stdout=wrapper, stderr=STDOUT):
_, cols, cost = lapjv(cost_matrix, verbose=1)
wrapper.finish()
except ImportError:
_, cols, cost = lapjv(cost_matrix)
else:
_, cols, cost = lapjv(cost_matrix)
cost = cost[0]
print("Total assignment cost:", cost)
print("Time taken: {}s".format((np.round(time.time() - t, 2))))
# sometimes the cost matrix may be extremely large
stderr_w = stdout_w
elif stderr == PIPE:
stderr_r, stderr_w = os.pipe()
stderr_w = os.fdopen(stderr_w, 'wb')
if encoding:
stderr_r = io.open(stderr_r, 'r', encoding=encoding)
else:
stderr_r = os.fdopen(stderr_r, 'rb')
stderr_pipe = True
else:
stderr_r = stderr_w = stderr
if stdout_pipe or stderr_pipe:
capture_encoding = None
else:
capture_encoding = encoding
w = Wurlitzer(stdout=stdout_w, stderr=stderr_w, encoding=capture_encoding)
try:
with w:
yield stdout_r, stderr_r
finally:
# close pipes
if stdout_pipe:
stdout_w.close()
if stderr_pipe:
stderr_w.close()
def _safe_wrapper(name, *args, **kwargs):
with sys_pipes():
if not hasattr(_lib, name):
raise RuntimeError(f"Function {name} not in library")
if getattr(_lib, name)(*args, **kwargs):
raise RuntimeError(f"Function {name} call failed")