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
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"