Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
""" Force the execution of the function with the given arguments and
persist the output values.
"""
start_time = time.time()
func_id, args_id = self._get_output_identifiers(*args, **kwargs)
if self._verbose > 0:
print(format_call(self.func, args, kwargs))
output = self.func(*args, **kwargs)
self.store_backend.dump_item(
[func_id, args_id], output, verbose=self._verbose)
duration = time.time() - start_time
metadata = self._persist_input(duration, args, kwargs)
if self._verbose > 0:
_, name = get_func_name(self.func)
msg = '%s - %s' % (name, format_time(duration))
print(max(0, (80 - len(msg))) * '_' + msg)
return output, metadata
try:
old_func_code, old_first_line =\
extract_first_line(
self.store_backend.get_cached_func_code([func_id]))
except (IOError, OSError): # some backend can also raise OSError
self._write_func_code(func_code, first_line)
return False
if old_func_code == func_code:
return True
# We have differing code, is this because we are referring to
# different functions, or because the function we are referring to has
# changed?
_, func_name = get_func_name(self.func, resolv_alias=False,
win_characters=False)
if old_first_line == first_line == -1 or func_name == '':
if not first_line == -1:
func_description = ("{0} ({1}:{2})"
.format(func_name, source_file,
first_line))
else:
func_description = func_name
warnings.warn(JobLibCollisionWarning(
"Cannot detect name collisions for function '{0}'"
.format(func_description)), stacklevel=stacklevel)
# Fetch the code at the old location and compare it. If it is the
# same than the code store, we have a collision: the code in the
# file has not changed, but the name we have is pointing to a new
# code block.
Returns
-------
dict containing:
{key_base}_name: function name
{key_base}_module: fully-qualified module name containing function
{key_base}_args: args to pass to function
{key_base}_kwargs: kwargs to pass to function
"""
entry = {}
if func is None:
logger.warning(f"serialize_partial: `{key_base}` is None. Ignoring.")
return entry
func = partial(func)
entry[f'{key_base}_module'] = ".".join(jfi.get_func_name(func.func)[0])
entry[f'{key_base}_name'] = jfi.get_func_name(func.func)[1]
entry[f'{key_base}_args'] = func.args
entry[f'{key_base}_kwargs'] = func.keywords
return entry
possible_collision = (on_disk_func_code.rstrip() ==
old_func_code.rstrip())
else:
possible_collision = source_file.startswith(' 10:
_, func_name = get_func_name(self.func, resolv_alias=False)
self.warn("Function {0} (identified by {1}) has changed"
".".format(func_name, func_id))
self.clear(warn=True)
return False
def format_signature(self, func, *args, **kwds):
# XXX: This should be moved out to a function
# XXX: Should this use inspect.formatargvalues/formatargspec?
module, name = get_func_name(func)
module = [m for m in module if m]
if module:
module.append(name)
module_path = '.'.join(module)
else:
module_path = name
arg_str = list()
previous_length = 0
for arg in args:
arg = self.format(arg, indent=2)
if len(arg) > 1500:
arg = '%s...' % arg[:700]
if previous_length > 80:
arg = '\n%s' % arg
previous_length = len(arg)
arg_str.append(arg)
def _get_func_dir(self, mkdir=True):
""" Get the directory corresponding to the cache for the
function.
"""
module, name = get_func_name(self.func)
module.append(name)
func_dir = os.path.join(self.cachedir, *module)
if mkdir:
mkdirp(func_dir)
return func_dir