def default_quota(cls, user, provider): """ Load each Default Quota Plugin and call `plugin.get_default_quota(user, provider)` """ _default_quota = None for DefaultQuotaPlugin in cls.load_plugins(cls.list_of_classes): plugin = DefaultQuotaPlugin() try: inspect.getcallargs( getattr(plugin, 'get_default_quota'), user=user, provider=provider ) except AttributeError: logger.info( "Validation plugin %s missing method 'get_default_quota'" % DefaultQuotaPlugin ) except TypeError: logger.info( "Validation plugin %s does not accept kwargs `user` & `provider`" % DefaultQuotaPlugin ) _default_quota = plugin.get_default_quota( user=user, provider=provider ) if _default_quota: return _default_quota return _default_quota
def is_expired(cls, user): """ Load each ExpirationPlugin and call `plugin.is_expired(user)` """ _is_expired = False for ExpirationPlugin in cls.load_plugins(cls.list_of_classes): plugin = ExpirationPlugin() try: inspect.getcallargs(getattr(plugin, 'is_expired'), user=user) except AttributeError: logger.info( "Expiration plugin %s does not have a 'is_expired' method" % ExpirationPlugin ) except TypeError: logger.info( "Expiration plugin %s does not accept kwarg `user`" % ExpirationPlugin ) try: # TODO: Set a reasonable timeout but don't let it hold this indefinitely _is_expired = plugin.is_expired(user=user) except Exception as exc: logger.info( "Expiration plugin %s encountered an error: %s" % (ExpirationPlugin, exc) ) _is_expired = True if _is_expired: return True return _is_expired
def run(self, image, output): with image.get_willow_image() as willow: original_format = willow.format_name # Fix orientation of image willow = willow.auto_orient() env = { 'original-format': original_format, } for operation in self.operations: # Check that the operation can take the "env" argument try: inspect.getcallargs(operation.run, willow, image, env) accepts_env = True except TypeError: # Check that the paramters fit the old style, so we don't # raise a warning if there is a coding error inspect.getcallargs(operation.run, willow, image) accepts_env = False warnings.warn("ImageOperation run methods should take 4 " "arguments. %d.run only takes 3.", RemovedInWagtail19Warning) # Call operation if accepts_env: willow = operation.run(willow, image, env) or willow else: willow = operation.run(willow, image) or willow # Find the output format to use if 'output-format' in env: # Developer specified an output format output_format = env['output-format'] else: # Default to outputting in original format output_format = original_format # Convert BMP files to PNG if original_format == 'bmp': output_format = 'png' # Convert unanimated GIFs to PNG as well if original_format == 'gif' and not willow.has_animation(): output_format = 'png' if output_format == 'jpeg': # Allow changing of JPEG compression quality if 'jpeg-quality' in env: quality = env['jpeg-quality'] elif hasattr(settings, 'WAGTAILIMAGES_JPEG_QUALITY'): quality = settings.WAGTAILIMAGES_JPEG_QUALITY else: quality = 85 return willow.save_as_jpeg(output, quality=quality, progressive=True, optimize=True) elif output_format == 'png': return willow.save_as_png(output) elif output_format == 'gif': return willow.save_as_gif(output)
def onReceivedElement(self, element): """ This is called by the transport's ElementReader to process an entire received Data or Interest element. :param element: The bytes of the incoming element. :type element: An array type with int elements """ # First, decode as Interest or Data. interest = None data = None decoder = TlvDecoder(element) if decoder.peekType(Tlv.Interest, len(element)): interest = Interest() interest.wireDecode(element, TlvWireFormat.get()) elif decoder.peekType(Tlv.Data, len(element)): data = Data() data.wireDecode(element, TlvWireFormat.get()) # Now process as Interest or Data. if interest != None: # Call all interest filter callbacks which match. for i in range(len(self._interestFilterTable)): entry = self._interestFilterTable[i] if entry.getFilter().doesMatch(interest.getName()): includeFilter = True # Use getcallargs to test if onInterest accepts 5 args. try: inspect.getcallargs(entry.getOnInterest(), None, None, None, None, None) except TypeError: # Assume onInterest is old-style with 4 arguments. includeFilter = False if includeFilter: try: entry.getOnInterest()( entry.getFilter().getPrefix(), interest, entry.getFace(), entry.getInterestFilterId(), entry.getFilter()) except: logging.exception("Error in onInterest") else: # Old-style onInterest without the filter argument. We # still pass a Face instead of Transport since Face also # has a send method. try: entry.getOnInterest()( entry.getFilter().getPrefix(), interest, entry.getFace(), entry.getInterestFilterId()) except: logging.exception("Error in onInterest") elif data != None: pendingInterests = self._extractEntriesForExpressedInterest( data.getName()) for pendingInterest in pendingInterests: try: pendingInterest.getOnData()(pendingInterest.getInterest(), data) except: logging.exception("Error in onData")
def _wrap_callback(self, fn): if fn is None: return None elif not callable(fn): raise InputDeviceError('value must be None or a callable') elif inspect.isbuiltin(fn): # We can't introspect the prototype of builtins. In this case we # assume that the builtin has no (mandatory) parameters; this is # the most reasonable assumption on the basis that pre-existing # builtins have no knowledge of gpiozero, and the sole parameter # we would pass is a gpiozero object return fn else: # Try binding ourselves to the argspec of the provided callable. # If this works, assume the function is capable of accepting no # parameters try: inspect.getcallargs(fn) return fn except TypeError: try: # If the above fails, try binding with a single parameter # (ourselves). If this works, wrap the specified callback inspect.getcallargs(fn, self) @wraps(fn) def wrapper(): return fn(self) return wrapper except TypeError: raise InputDeviceError( 'value must be a callable which accepts up to one ' 'mandatory parameter')
def authenticate(djsqla_sessions, **credentials): """ If the given credentials are valid, return a User object. """ for backend, backend_path in _get_backends(return_tuples=True): # assert False, backend try: inspect.getcallargs(backend.authenticate, **credentials) except TypeError: # This backend doesn't accept these credentials as arguments. Try the next one. continue if isinstance(backend, SQLAlchemyUserBackend): backend_db = backend.get_db_name() if hasattr(djsqla_sessions, backend_db): session = getattr(djsqla_sessions, backend_db) setattr(backend, "djsqla_session", session) try: user = backend.authenticate(**credentials) except PermissionDenied: # This backend says to stop in our tracks - this user should not be allowed in at all. return None if user is None: continue # Annotate the user object with the path of the backend. user.backend = backend_path return user # The credentials supplied are invalid to all backends, fire signal user_login_failed.send(sender=__name__, credentials=_clean_credentials(credentials))
def _wrap_callback(self, fn): if fn is None: return None elif not callable(fn): raise InputDeviceError('value must be None or a callable') else: # Try binding ourselves to the argspec of the provided callable. # If this works, assume the function is capable of accepting no # parameters try: inspect.getcallargs(fn) return fn except TypeError: try: # If the above fails, try binding with a single parameter # (ourselves). If this works, wrap the specified callback inspect.getcallargs(fn, self) @wraps(fn) def wrapper(): return fn(self) return wrapper except TypeError: raise InputDeviceError( 'value must be a callable which accepts up to one ' 'mandatory parameter')
def authenticate(request=None, **credentials): """ If the given credentials are valid, return a User object. """ for backend, backend_path in _get_backends(return_tuples=True): args = (request,) try: inspect.getcallargs(backend.authenticate, request, **credentials) except TypeError: try: inspect.getcallargs(backend.authenticate, **credentials) except TypeError: # This backend doesn't accept these credentials as arguments. Try the next one. continue else: args = () warnings.warn( "Update authentication backend %s to accept a " "positional `request` argument." % backend_path, RemovedInDjango21Warning ) try: user = backend.authenticate(*args, **credentials) except PermissionDenied: # This backend says to stop in our tracks - this user should not be allowed in at all. break if user is None: continue # Annotate the user object with the path of the backend. user.backend = backend_path return user # The credentials supplied are invalid to all backends, fire signal user_login_failed.send(sender=__name__, credentials=_clean_credentials(credentials), request=request)
def new_f(*args, **kwargs): try: inspect.getcallargs(f, *args, **kwargs) except TypeError as t: raise JSONRPCInvalidParamsError(t) else: return f(*args, **kwargs)
def _valid_formatter(f): """Return whether an object is a valid formatter Cases checked: - bound methods OK - unbound methods NO - callable with zero args OK """ if f is None: return False elif isinstance(f, type(str.find)): # unbound methods on compiled classes have type method_descriptor return False elif isinstance(f, types.BuiltinFunctionType): # bound methods on compiled classes have type builtin_function return True elif callable(f): # anything that works with zero args should be okay try: inspect.getcallargs(f) except Exception: return False else: return True return False
def __init__(self, func, pattern, argument_mappings=None, return_mapping=None, doc=None): if not callable(func): raise RuntimeError('Can not construct a Func-object from a non callable object.') self.func = func if isinstance(pattern, string_types): pattern = regex(pattern) self.matcher = pattern if argument_mappings is None: argument_mappings = self.matcher.argument_mappings or None try: inspect.getcallargs(func, *[None] * self.matcher.arg_count) except TypeError: raise RuntimeError( 'The number of arguments for function \'{}\' matched by pattern ' '\'{}\' is not compatible with number of defined ' 'groups in pattern ({}).'.format( getattr(func, '__name__', repr(func)), self.matcher.pattern, self.matcher.arg_count )) if argument_mappings is not None and (self.matcher.arg_count != len(argument_mappings)): raise RuntimeError( 'Supplied argument mappings for function matched by pattern \'{}\' specify {} ' 'argument(s), but the function has {} arguments.'.format( self.matcher, len(argument_mappings), self.matcher.arg_count)) self.argument_mappings = argument_mappings self.return_mapping = return_mapping self.doc = doc or (inspect.getdoc(self.func) if callable(self.func) else None)
def authenticate(**credentials): """ If the given credentials are valid, return a User object. """ for backend, backend_path in _get_backends(return_tuples=True): try: inspect.getcallargs(backend.authenticate, **credentials) except TypeError: # This backend doesn't accept these credentials as arguments. Try the next one. continue try: user = backend.authenticate(**credentials) except PermissionDenied: # This backend says to stop in our tracks - this user should not be allowed in at all. return None if user is None: continue # Annotate the user object with the path of the backend. user.backend = backend_path return user # The credentials supplied are invalid to all backends, fire signal user_login_failed.send(sender=__name__, credentials=_clean_credentials(credentials))
def _wrap_callback(self, fn): # Shamelessley nicked (with some variation) from GPIO Zero :) @wraps(fn) def wrapper(event): return fn() if fn is None: return None elif not callable(fn): raise ValueError('value must be None or a callable') elif inspect.isbuiltin(fn): # We can't introspect the prototype of builtins. In this case we # assume that the builtin has no (mandatory) parameters; this is # the most reasonable assumption on the basis that pre-existing # builtins have no knowledge of InputEvent, and the sole parameter # we would pass is an InputEvent return wrapper else: # Try binding ourselves to the argspec of the provided callable. # If this works, assume the function is capable of accepting no # parameters and that we have to wrap it to ignore the event # parameter try: inspect.getcallargs(fn) return wrapper except TypeError: try: # If the above fails, try binding with a single tuple # parameter. If this works, return the callback as is inspect.getcallargs(fn, ()) return fn except TypeError: raise ValueError( 'value must be a callable which accepts up to one ' 'mandatory parameter')
def ensure_user_allocation_sources(cls, user, provider=None): """Load each Allocation Source Plugin and call `plugin.ensure_user_allocation_source(user)` Depending on the plugin this may create allocation sources if they don't already exist. :param user: The user to check :type user: core.models.AtmosphereUser :param provider: The provider (optional, not used by all plugins) :type provider: core.models.Provider :return: Whether the user has valid allocation sources :rtype: bool """ _has_valid_allocation_sources = False for AllocationSourcePlugin in cls.load_plugins(cls.list_of_classes): plugin = AllocationSourcePlugin() try: inspect.getcallargs( getattr(plugin, 'ensure_user_allocation_source'), user=user, provider=provider) except AttributeError: logger.info( "Allocation Source plugin %s missing method 'ensure_user_allocation_source'", AllocationSourcePlugin) except TypeError: logger.info( "Allocation Source plugin %s does not accept kwargs `user` & `provider`", AllocationSourcePlugin) _has_valid_allocation_sources = plugin.ensure_user_allocation_source(user=user, provider=provider) if _has_valid_allocation_sources: return _has_valid_allocation_sources return _has_valid_allocation_sources
def verifyData(self, data, onVerified, onValidationFailed, stepCount = 0): """ Check the signature on the Data object and call either onVerify or onValidationFailed. We use callback functions because verify may fetch information to check the signature. :param Data data: The Data object with the signature to check. :param onVerified: If the signature is verified, this calls onVerified(data). NOTE: The library will log any exceptions raised by this callback, but for better error handling the callback should catch and properly handle any exceptions. :type onVerified: function object :param onValidationFailed: If the signature check fails or can't find the public key, this calls onValidationFailed(data, reason) with the Data object and reason string. This also supports the deprecated callback onValidationFailed(data) but you should use the callback with the reason string. NOTE: The library will log any exceptions raised by this callback, but for better error handling the callback should catch and properly handle any exceptions. :type onValidationFailed: function object :param int stepCount: (optional) The number of verification steps that have been done. If omitted, use 0. """ # If onValidationFailed is not a function nor a method assumes it is a # calleable object if (not inspect.isfunction(onValidationFailed) and not inspect.ismethod(onValidationFailed)): onValidationFailed = onValidationFailed.__call__ # Use getcallargs to test if onValidationFailed accepts 2 args. try: inspect.getcallargs(onValidationFailed, None, None) except TypeError: # Assume onValidationFailed is old-style with 1 argument. Wrap it # with a function that takes and ignores the reason string. oldValidationFailed = onValidationFailed onValidationFailed = lambda d, reason: oldValidationFailed(d) if self._policyManager.requireVerify(data): nextStep = self._policyManager.checkVerificationPolicy( data, stepCount, onVerified, onValidationFailed) if nextStep != None: self._face.expressInterest( nextStep.interest, self._makeOnCertificateData(nextStep), self._makeOnCertificateInterestTimeout( nextStep.retry, onValidationFailed, data, nextStep)) elif self._policyManager.skipVerifyAndTrust(data): try: onVerified(data) except: logging.exception("Error in onVerified") else: try: onValidationFailed( data, "The packet has no verify rule but skipVerifyAndTrust is false") except: logging.exception("Error in onValidationFailed")
def check_signature(self, args, kwargs): service_cls = self.container.service_cls fn = getattr(service_cls, self.method_name) try: service_instance = None # fn is unbound inspect.getcallargs(fn, service_instance, *args, **kwargs) except TypeError as exc: raise IncorrectSignature(str(exc))
def mangle(fname, f, args, kwargs): # We uniquely identify functions by their list of arguments # as resolved by the function definition. print(fname, f, args, kwargs) print(getcallargs) print(json.dumps(sorted(getcallargs(f, *args, **kwargs).items(), key=lambda x: x[0]))) return "@" + fname + "_" + json.dumps(getcallargs(f, *args, **kwargs))
def _check_callable(cls, method, *args, **kwargs): if hasattr(inspect, 'getcallargs'): # for Python 2.7 or later. inspect.getcallargs(getattr(cls, method), cls, *args, **kwargs) else: # for Python 2.6 or earlier. if not hasattr(cls, method): raise AttributeError, 'This class has not attr: ' + method
def _resolve_lookup(self, context): """ Performs resolution of a real variable (i.e. not a literal) against the given context. As indicated by the method's name, this method is an implementation detail and shouldn't be called by external code. Use Variable.resolve() instead. """ current = context try: # catch-all for silent variable failures for bit in self.lookups: try: # dictionary lookup current = current[bit] # ValueError/IndexError are for numpy.array lookup on # numpy < 1.9 and 1.9+ respectively except (TypeError, AttributeError, KeyError, ValueError, IndexError): try: # attribute lookup # Don't return class attributes if the class is the context: if isinstance(current, BaseContext) and getattr(type(current), bit): raise AttributeError current = getattr(current, bit) except (TypeError, AttributeError) as e: # Reraise an AttributeError raised by a @property if (isinstance(e, AttributeError) and not isinstance(current, BaseContext) and bit in dir(current)): raise try: # list-index lookup current = current[int(bit)] except (IndexError, # list index out of range ValueError, # invalid literal for int() KeyError, # current is a dict without `int(bit)` key TypeError): # unsubscriptable object raise VariableDoesNotExist("Failed lookup for key " "[%s] in %r", (bit, current)) # missing attribute if callable(current): if getattr(current, 'do_not_call_in_templates', False): pass elif getattr(current, 'alters_data', False): current = context.engine.string_if_invalid else: try: # method call (assuming no args required) current = current() except TypeError: try: getcallargs(current) except TypeError: # arguments *were* required current = context.engine.string_if_invalid # invalid method call else: raise except Exception as e: if getattr(e, 'silent_variable_failure', False): current = context.engine.string_if_invalid else: raise return current
def __init__(self, __interact_f, __options={}, **kwargs): VBox.__init__(self, _dom_classes=['widget-interact']) self.result = None self.args = [] self.kwargs = {} self.f = f = __interact_f self.clear_output = kwargs.pop('clear_output', True) self.manual = __options.get("manual", False) self.manual_name = __options.get("manual_name", "Run Interact") self.auto_display = __options.get("auto_display", False) new_kwargs = self.find_abbreviations(kwargs) # Before we proceed, let's make sure that the user has passed a set of args+kwargs # that will lead to a valid call of the function. This protects against unspecified # and doubly-specified arguments. try: check_argspec(f) except TypeError: # if we can't inspect, we can't validate pass else: getcallargs(f, **{n:v for n,v,_ in new_kwargs}) # Now build the widgets from the abbreviations. self.kwargs_widgets = self.widgets_from_abbreviations(new_kwargs) # This has to be done as an assignment, not using self.children.append, # so that traitlets notices the update. We skip any objects (such as fixed) that # are not DOMWidgets. c = [w for w in self.kwargs_widgets if isinstance(w, DOMWidget)] # If we are only to run the function on demand, add a button to request this. if self.manual: self.manual_button = Button(description=self.manual_name) c.append(self.manual_button) self.out = Output() c.append(self.out) self.children = c # Wire up the widgets # If we are doing manual running, the callback is only triggered by the button # Otherwise, it is triggered for every trait change received # On-demand running also suppresses running the function with the initial parameters if self.manual: self.manual_button.on_click(self.update) # Also register input handlers on text areas, so the user can hit return to # invoke execution. for w in self.kwargs_widgets: if isinstance(w, Text): w.on_submit(self.update) else: for widget in self.kwargs_widgets: widget.observe(self.update, names='value') self.on_displayed(self.update)
def wrapper(*args, **kwargs): provider_instance = args[0] if provider_instance.is_first_day_of_month(): provider_instance.request_limit_reached = False if not provider_instance.request_limit_reached: return func(*args, **kwargs) else: getcallargs(func, *args)["logger"].warning("%s API limit was exceeded. Rate won't be requested.", provider_instance.name) return return_value
def _caller(self, method, args, kwargs): # custom dispatcher called by RPCDispatcher._dispatch() # when provided with the address of a custom dispatcher. # Used to generate a customized error message when the # function signature doesn't match the parameter list. try: inspect.getcallargs(method, *args, **kwargs) except TypeError: raise JSONRPCInvalidParamsError() else: return method(*args, **kwargs)
def authenticate(self, request, remote_user, shib_meta): """ The username passed as ``remote_user`` is considered trusted. This method simply returns the ``User`` object with the given username, creating a new ``User`` object if ``create_unknown_user`` is ``True``. Returns None if ``create_unknown_user`` is ``False`` and a ``User`` object with the given username is not found in the database. """ if not remote_user: return User = get_user_model() username = self.clean_username(remote_user) field_names = [x.name for x in User._meta.get_fields()] shib_user_params = dict([(k, shib_meta[k]) for k in field_names if k in shib_meta]) # Note that this could be accomplished in one try-except clause, but # instead we use get_or_create when creating unknown users since it has # built-in safeguards for multiple threads. if self.create_unknown_user: user, created = User.objects.get_or_create(username=username, defaults=shib_user_params) if created: """ @note: setting password for user needs on initial creation of user instead of after auth.login() of middleware. because get_session_auth_hash() returns the salted_hmac value of salt and password. If it remains after the auth.login() it will return a different auth_hash than what's stored in session "request.session[HASH_SESSION_KEY]". Also we don't need to update the user's password everytime he logs in. """ user.set_unusable_password() user.save() args = (request, user) try: inspect.signature(self.configure_user).bind(request, user) except AttributeError: # getcallargs required for Python 2.7 support, deprecated after 3.5 try: inspect.getcallargs(self.configure_user, request, user) except TypeError: args = (user,) except TypeError: args = (user,) user = self.configure_user(*args) else: try: user = User.objects.get(username=username) except User.DoesNotExist: return # After receiving a valid user, we update the the user attributes according to the shibboleth # parameters. Otherwise the parameters (like mail address, sure_name or last_name) will always # be the initial values from the first login. Only save user object if there are any changes. if not min([getattr(user, k) == v for k, v in shib_user_params.items()]): user.__dict__.update(**shib_user_params) user.save() return user if self.user_can_authenticate(user) else None
def _resolve_lookup(self, context): """ Performs resolution of a real variable (i.e. not a literal) against the given context. As indicated by the method's name, this method is an implementation detail and shouldn't be called by external code. Use Variable.resolve() instead. """ current = context try: # catch-all for silent variable failures for bit in self.lookups: try: # dictionary lookup current = current[bit] except (TypeError, AttributeError, KeyError, ValueError): try: # attribute lookup # Don't return class attributes if the class is the context: if isinstance(current, BaseContext) and getattr(type(current), bit): raise AttributeError current = getattr(current, bit) except (TypeError, AttributeError): try: # list-index lookup current = current[int(bit)] except ( IndexError, # list index out of range ValueError, # invalid literal for int() KeyError, # current is a dict without `int(bit)` key TypeError, ): # unsubscriptable object raise VariableDoesNotExist( "Failed lookup for key " "[%s] in %r", (bit, current) ) # missing attribute if callable(current): if getattr(current, "do_not_call_in_templates", False): pass elif getattr(current, "alters_data", False): current = settings.TEMPLATE_STRING_IF_INVALID else: try: # method call (assuming no args required) current = current() except TypeError: try: getcallargs(current) except TypeError: # arguments *were* required current = settings.TEMPLATE_STRING_IF_INVALID # invalid method call else: raise except Exception as e: if getattr(e, "silent_variable_failure", False): current = settings.TEMPLATE_STRING_IF_INVALID else: raise return current
def caller(*args, **kwargs): try: for key in kwargs: if key in k: raise KeywordRepeatedTypeError( "TypeError: %s() got multiple values " "for keyword argument '%s'" % (f.func_name, key) ) kwargs.update(k) args, kwargs = insert_keyword_first(f, a + args, kwargs) getcallargs(f, *args, **kwargs) except KeywordRepeatedTypeError, e: raise TypeError(e)
def _verify_arguments(method, method_name, args, kwargs): try: getcallargs(method, *args, **kwargs) except TypeError as e: if not _is_python_function(method): raise VerifyingBuiltinDoubleArgumentError(str(e)) raise VerifyingDoubleArgumentError(str(e)) except IndexError as e: if _is_python_33(): _raise_doubles_error_from_index_error(method_name) else: raise e
def __call__(self, *args, **kw): #try: try: getcallargs(self.__func, *args) except TypeError as e: printHelp(self.name) console.fail("%s" % e) raise retval = self.__func(*args) return retval
def arg_checker(func, *args, **kwargs): # getcallargs doesn't support partials, hence this workaround if isinstance(func, partial): args += func.args if func.keywords: kwargs.update(func.keywords) func = func.func try: getcallargs(func, *args, **kwargs) except TypeError: return False else: return True
def wrapper(*args, **kwargs): config = get_config() for i, argname in enumerate(func_args): if len(args) > i or argname in kwargs: continue elif argname in config: kwargs[argname] = config[argname] try: getcallargs(func, *args, **kwargs) except TypeError as exc: msg = "{}\n{}".format(exc.args[0], PALLADIUM_CONFIG_ERROR) exc.args = (msg,) raise exc return func(*args, **kwargs)
def interactive(__interact_f, **kwargs): """Build a group of widgets to interact with a function.""" f = __interact_f co = kwargs.pop('clear_output', True) kwargs_widgets = [] container = ContainerWidget() container.result = None container.args = [] container.kwargs = dict() kwargs = kwargs.copy() new_kwargs = _find_abbreviations(f, kwargs) # Before we proceed, let's make sure that the user has passed a set of args+kwargs # that will lead to a valid call of the function. This protects against unspecified # and doubly-specified arguments. getcallargs(f, **{n:v for n,v in new_kwargs}) # Now build the widgets from the abbreviations. kwargs_widgets.extend(_widgets_from_abbreviations(new_kwargs)) kwargs_widgets.extend(_widgets_from_abbreviations(sorted(kwargs.items(), key = lambda x: x[0]))) # This has to be done as an assignment, not using container.children.append, # so that traitlets notices the update. We skip any objects (such as fixed) that # are not DOMWidgets. c = [w for w in kwargs_widgets if isinstance(w, DOMWidget)] container.children = c # Build the callback def call_f(name, old, new): container.kwargs = {} for widget in kwargs_widgets: value = widget.value container.kwargs[widget.description] = value if co: clear_output(wait=True) try: container.result = f(**container.kwargs) except Exception as e: ip = get_ipython() if ip is None: container.log.warn("Exception in interact callback: %s", e, exc_info=True) else: ip.showtraceback() # Wire up the widgets for widget in kwargs_widgets: widget.on_trait_change(call_f, 'value') container.on_displayed(lambda _: call_f(None, None, None)) return container
def print_out(self): print(self.multiply()) def get_user_attributes(cls): boring = dir(type('dummy', (object, ), {})) return [item for item in inspect.getmembers(cls) if item[0] not in boring] if __name__ == "__main__": # print(inspect.getargspec(A.multiply)) # t = A(1) print("isFunc ", inspect.isfunction(B)) print("GetArgSpec ", inspect.getargspec(B)) print("CallArgs ", inspect.getcallargs(B, 1, 2, 3)) print("GetArgs ", inspect.getcallargs(B, 1, 2, 3)) print("GetValues ", inspect.ArgInfo(B, "a", "b", "c")) print("GetMembers ", inspect.getmembers(A, inspect.isfunction)) print(" ", inspect.ArgInfo(B, 1, 2, 3)) print(inspect.getsourcelines(B)) print(inspect.getsourcelines(A)) print(inspect.getsourcelines(array)) # print(" ", inspect.CO_VARARGS(B)) # print(inspect.getmembers(A,inspect.isfunction)) # for ob in inspect.getmembers(A,inspect.isfunction): # print(ob[0]) # print(inspect.getfullargspec(ob[1])) # print(inspect.getmembers(array,inspect.isfunction)) # for ob in inspect.getmembers(array,inspect.isfunction):
def method(self, *a, **kw): callargs = inspect.getcallargs(getattr(BaseTSDB, key), self, *a, **kw) backend = selector_func(key, callargs)[0] return getattr(getattr(self, backend), key)(*a, **kw)
def wrapper(*args, **kwargs): func_args = inspect.getcallargs(func, *args, **kwargs) if func_args.get('username') != 'admin': raise Exception("This user is not allowed to put/get elem") return f(*args, **kwargs)
def _add_action(self, name, *args, **kwargs): """Process action calls in plans. :param name: action name :type name: str :param args: positional arguments of the action call :type args: list :param kwargs: keyword arguments of the action call :type kwargs: dict """ if self.ignore_disabled and not self.env.enabled: return plan_line = 'unknown filename:unknown lineno' # Retrieve the plan line try: caller_frames = inspect.getouterframes( frame=inspect.currentframe()) caller_frame = None for frame in caller_frames: if isinstance(frame, tuple): # py2-only frame_filename = frame[1] else: # py3-only frame_filename = frame.filename if frame_filename.endswith(self.plan.plan_ext): caller_frame = frame except Exception: # defensive code # do not crash when inspect frames fails pass else: if caller_frame is None: # defensive code # No information ? pass elif isinstance(caller_frame, tuple): # py2-only plan_line = '{}:{}'.format(caller_frame[1], caller_frame[2]) else: # py3-only plan_line = '{}:{}'.format(caller_frame.filename, caller_frame.lineno) # First create our initial object based on current scope env result = self.env.copy() # Process arguments call_args = inspect.getcallargs(self.actions[name], *args, **kwargs) # Push function arguments into the result object. If an arg value # is None then environment is not updated (handling of defaults # coming from environment). For the build, host and target arguments # a special processing is done to make the corresponding set_env # call on the result BaseEnv object platform = {'build': None, 'host': None, 'target': None} # Likewise board argument is used to change only the machine name # of the target. ??? change name ??? board = None for k, v in call_args.iteritems(): if k in platform: platform[k] = v elif k == 'board': board = v elif v is not None or not hasattr(result, k): setattr(result, k, v) # Handle propagation of environment from the context if platform['host'] is None and result.is_canadian: platform['host'] = 'host' if platform['target'] is None and result.is_cross: platform['target'] = 'target' if platform['target'] == result.host.platform: # ??? This special case is temporary ??? # the goal is avoid cross from a -> a which are # not current supported. # Plans should be updated to use target='host' # instead of target=env.host.platform platform['target'] = 'host' result.set_env(**platform) # If necessary adjust target machine name if board is not None: result.set_target(result.target.platform, result.target.os.version, board, result.target.os.mode) # Set action attribute (with action name) result.action = name result.plan_line = plan_line result.plan_args = result.to_dict() result.plan_call_args = call_args # Push the action in the current list self.action_list.append(result)
# -*- coding: utf-8 -*- import inspect class Foo: pass def bar(id_, data, commit=True): pass if __name__ == "__main__": foo = Foo() module = inspect.getmodule(foo) print(f"{module.__name__}.{type(foo).__name__}") callargs = inspect.getcallargs(bar, 1, {}) print(f"{callargs}") signature = inspect.signature(bar) default_args = { k: v.default for k, v in signature.parameters.items() if v.default is not inspect.Parameter.empty } print(f"{default_args}")
def __call__(self, *args, **kwargs): callargs = inspect.getcallargs(self._func, *args, **kwargs) frame = self._vm.make_frame( self.func_code, callargs, self.func_globals, {} ) return self._vm.run_fr(frame)
def get_call_args(func, args, kwargs, traceback=None): try: return inspect.getcallargs(unwrapper(func), *args, **kwargs) except TypeError as e: six.reraise(RQTypeError, RQTypeError(*e.args), traceback)
def _get_parameter(function, func_args, func_kargs, argname): """Get the parameter passed as argname to function.""" args_binding = inspect.getcallargs(function, *func_args, **func_kargs) return args_binding.get(argname) or args_binding.get("kargs").get(argname)
def run(self, image, output): with image.get_willow_image() as willow: original_format = willow.format_name # Fix orientation of image willow = willow.auto_orient() env = { 'original-format': original_format, } for operation in self.operations: # Check that the operation can take the "env" argument try: inspect.getcallargs(operation.run, willow, image, env) accepts_env = True except TypeError: # Check that the paramters fit the old style, so we don't # raise a warning if there is a coding error inspect.getcallargs(operation.run, willow, image) accepts_env = False warnings.warn( "ImageOperation run methods should take 4 " "arguments. %d.run only takes 3.", RemovedInWagtail19Warning) # Call operation if accepts_env: willow = operation.run(willow, image, env) or willow else: willow = operation.run(willow, image) or willow # Find the output format to use if 'output-format' in env: # Developer specified an output format output_format = env['output-format'] else: # Default to outputting in original format output_format = original_format # Convert BMP files to PNG if original_format == 'bmp': output_format = 'png' # Convert unanimated GIFs to PNG as well if original_format == 'gif' and not willow.has_animation(): output_format = 'png' if output_format == 'jpeg': # Allow changing of JPEG compression quality if 'jpeg-quality' in env: quality = env['jpeg-quality'] elif hasattr(settings, 'WAGTAILIMAGES_JPEG_QUALITY'): quality = settings.WAGTAILIMAGES_JPEG_QUALITY else: quality = 85 return willow.save_as_jpeg(output, quality=quality, progressive=True, optimize=True) elif output_format == 'png': return willow.save_as_png(output) elif output_format == 'gif': return willow.save_as_gif(output)
def wrapper(wrapped_self, role_id, *args, **kwargs): """Send a notification if the wrapped callable is successful. NOTE(stevemar): The reason we go through checking kwargs and args for possible target and actor values is because the create_grant() (and delete_grant()) method are called differently in various tests. Using named arguments, i.e.:: create_grant(user_id=user['id'], domain_id=domain['id'], role_id=role['id']) Or, using positional arguments, i.e.:: create_grant(role_id['id'], user['id'], None, domain_id=domain['id'], None) Or, both, i.e.:: create_grant(role_id['id'], user_id=user['id'], domain_id=domain['id']) Checking the values for kwargs is easy enough, since it comes in as a dictionary The actual method signature is :: create_grant(role_id, user_id=None, group_id=None, domain_id=None, project_id=None, inherited_to_projects=False) So, if the values of actor or target are still None after checking kwargs, we can check the positional arguments, based on the method signature. """ call_args = inspect.getcallargs(f, wrapped_self, role_id, *args, **kwargs) inherited = call_args['inherited_to_projects'] initiator = call_args.get('initiator', None) target = resource.Resource(typeURI=taxonomy.ACCOUNT_USER) audit_kwargs = {} if call_args['project_id']: audit_kwargs['project'] = call_args['project_id'] elif call_args['domain_id']: audit_kwargs['domain'] = call_args['domain_id'] if call_args['user_id']: audit_kwargs['user'] = call_args['user_id'] elif call_args['group_id']: audit_kwargs['group'] = call_args['group_id'] audit_kwargs['inherited_to_projects'] = inherited audit_kwargs['role'] = role_id try: result = f(wrapped_self, role_id, *args, **kwargs) except Exception: _send_audit_notification(self.action, initiator, taxonomy.OUTCOME_FAILURE, target, self.event_type, **audit_kwargs) raise else: _send_audit_notification(self.action, initiator, taxonomy.OUTCOME_SUCCESS, target, self.event_type, **audit_kwargs) return result
def wrapper(wrapped_self, role_id, *args, **kwargs): """Send a notification if the wrapped callable is successful.""" """ NOTE(stevemar): The reason we go through checking kwargs and args for possible target and actor values is because the create_grant() (and delete_grant()) method are called differently in various tests. Using named arguments, i.e.: create_grant(user_id=user['id'], domain_id=domain['id'], role_id=role['id']) Or, using positional arguments, i.e.: create_grant(role_id['id'], user['id'], None, domain_id=domain['id'], None) Or, both, i.e.: create_grant(role_id['id'], user_id=user['id'], domain_id=domain['id']) Checking the values for kwargs is easy enough, since it comes in as a dictionary The actual method signature is create_grant(role_id, user_id=None, group_id=None, domain_id=None, project_id=None, inherited_to_projects=False) So, if the values of actor or target are still None after checking kwargs, we can check the positional arguments, based on the method signature. """ call_args = inspect.getcallargs( f, wrapped_self, role_id, *args, **kwargs) inherited = call_args['inherited_to_projects'] context = call_args['context'] initiator = _get_request_audit_info(context) target = resource.Resource(typeURI=taxonomy.ACCOUNT_USER) audit_kwargs = {} if call_args['project_id']: audit_kwargs['project'] = call_args['project_id'] elif call_args['domain_id']: audit_kwargs['domain'] = call_args['domain_id'] if call_args['user_id']: audit_kwargs['user'] = call_args['user_id'] elif call_args['group_id']: audit_kwargs['group'] = call_args['group_id'] audit_kwargs['inherited_to_projects'] = inherited audit_kwargs['role'] = role_id # For backward compatibility, send both old and new event_type. # Deprecate old format and remove it in the next release. event_types = [self.deprecated_event_type, self.event_type] versionutils.deprecated( as_of=versionutils.deprecated.KILO, remove_in=+1, what=('sending duplicate %s notification event type' % self.deprecated_event_type), in_favor_of='%s notification event type' % self.event_type) try: result = f(wrapped_self, role_id, *args, **kwargs) except Exception: for event_type in event_types: _send_audit_notification(self.action, initiator, taxonomy.OUTCOME_FAILURE, target, event_type, **audit_kwargs) raise else: for event_type in event_types: _send_audit_notification(self.action, initiator, taxonomy.OUTCOME_SUCCESS, target, event_type, **audit_kwargs) return result
def execute(*args, **kwargs): context = type(self).__state.context # If there is no context object already set in the thread local # state, we are entering the delegator for the first time and need # to create a new context. if context is None: from sentry.app import env # avoids a circular import context = Context(env.request, {}) # If this thread already has an active backend for this base class, # we can safely call that backend synchronously without delegating. if self.__backend_base in context.backends: backend = context.backends[self.__backend_base] return getattr(backend, attribute_name)(*args, **kwargs) # Binding the call arguments to named arguments has two benefits: # 1. These values always be passed in the same form to the selector # function and callback, regardless of how they were passed to # the method itself (as positional arguments, keyword arguments, # etc.) # 2. This ensures that the given arguments are those supported by # the base backend itself, which should be a common subset of # arguments that are supported by all backends. callargs = inspect.getcallargs(base_value, None, *args, **kwargs) selected_backend_names = list( self.__selector_func(context, attribute_name, callargs)) if not len(selected_backend_names) > 0: raise self.InvalidBackend("No backends returned by selector!") # Ensure that the primary backend is actually registered -- we # don't want to schedule any work on the secondaries if the primary # request is going to fail anyway. if selected_backend_names[0] not in self.__backends: raise self.InvalidBackend( "{!r} is not a registered backend.".format( selected_backend_names[0])) def call_backend_method(context, backend, is_primary): # Update the thread local state in the executor to the provided # context object. This allows the context to be propagated # across different threads. assert type(self).__state.context is None type(self).__state.context = context # Ensure that we haven't somehow accidentally entered a context # where the backend we're calling has already been marked as # active (or worse, some other backend is already active.) base = self.__backend_base assert base not in context.backends # Mark the backend as active. context.backends[base] = backend try: return getattr(backend, attribute_name)(*args, **kwargs) except Exception as e: # If this isn't the primary backend, we log any unexpected # exceptions so that they don't pass by unnoticed. (Any # exceptions raised by the primary backend aren't logged # here, since it's assumed that the caller will log them # from the calling thread.) if not is_primary: expected_raises = getattr(base_value, "__raises__", []) if not expected_raises or not isinstance( e, tuple(expected_raises)): logger.warning( "%s caught in executor while calling %r on %s.", type(e).__name__, attribute_name, type(backend).__name__, exc_info=True, ) raise finally: type(self).__state.context = None # Enqueue all of the secondary backend requests first since these # are non-blocking queue insertions. (Since the primary backend # executor queue insertion can block, if that queue was full the # secondary requests would have to wait unnecessarily to be queued # until the after the primary request can be enqueued.) # NOTE: If the same backend is both the primary backend *and* in # the secondary backend list -- this is unlikely, but possible -- # this means that one of the secondary requests will be queued and # executed before the primary request is queued. This is such a # strange usage pattern that I don't think it's worth optimizing # for.) results = [None] * len(selected_backend_names) for i, backend_name in enumerate(selected_backend_names[1:], 1): try: backend, executor = self.__backends[backend_name] except KeyError: logger.warning( "%r is not a registered backend and will be ignored.", backend_name, exc_info=True, ) else: results[i] = executor.submit( functools.partial(call_backend_method, context.copy(), backend, is_primary=False), priority=1, block=False, ) # The primary backend is scheduled last since it may block the # calling thread. (We don't have to protect this from ``KeyError`` # since we already ensured that the primary backend exists.) backend, executor = self.__backends[selected_backend_names[0]] results[0] = executor.submit( functools.partial(call_backend_method, context.copy(), backend, is_primary=True), priority=0, block=True, ) if self.__callback_func is not None: FutureSet([ _f for _f in results if _f ]).add_done_callback(lambda *a, **k: self.__callback_func( context, attribute_name, callargs, selected_backend_names, results)) return results[0].result()
def interactive(__interact_f, **kwargs): """ Builds a group of interactive widgets tied to a function and places the group into a Box container. Returns ------- container : a Box instance containing multiple widgets Parameters ---------- __interact_f : function The function to which the interactive widgets are tied. The `**kwargs` should match the function signature. **kwargs : various, optional An interactive widget is created for each keyword argument that is a valid widget abbreviation. """ f = __interact_f co = kwargs.pop('clear_output', True) manual = kwargs.pop('__manual', False) kwargs_widgets = [] container = Box(_dom_classes=['widget-interact']) container.result = None container.args = [] container.kwargs = dict() kwargs = kwargs.copy() new_kwargs = _find_abbreviations(f, kwargs) # Before we proceed, let's make sure that the user has passed a set of args+kwargs # that will lead to a valid call of the function. This protects against unspecified # and doubly-specified arguments. getcallargs(f, **{n: v for n, v, _ in new_kwargs}) # Now build the widgets from the abbreviations. kwargs_widgets.extend(_widgets_from_abbreviations(new_kwargs)) # This has to be done as an assignment, not using container.children.append, # so that traitlets notices the update. We skip any objects (such as fixed) that # are not DOMWidgets. c = [w for w in kwargs_widgets if isinstance(w, DOMWidget)] # If we are only to run the function on demand, add a button to request this if manual: manual_button = Button(description="Run %s" % f.__name__) c.append(manual_button) container.children = c # Build the callback def call_f(name=None, old=None, new=None): container.kwargs = {} for widget in kwargs_widgets: value = widget.value container.kwargs[widget._kwarg] = value if co: clear_output(wait=True) if manual: manual_button.disabled = True try: container.result = f(**container.kwargs) except Exception as e: ip = get_ipython() if ip is None: container.log.warn("Exception in interact callback: %s", e, exc_info=True) else: ip.showtraceback() finally: if manual: manual_button.disabled = False # Wire up the widgets # If we are doing manual running, the callback is only triggered by the button # Otherwise, it is triggered for every trait change received # On-demand running also suppresses running the function with the initial parameters if manual: manual_button.on_click(call_f) else: for widget in kwargs_widgets: widget.on_trait_change(call_f, 'value') container.on_displayed(lambda _: call_f(None, None, None)) return container
def wrapper(*args, **kwargs): bound_args = inspect.getcallargs(func, *args, **kwargs) for n, v in bound_args.items(): if v is not defaults.get(n, _SENTINEL) and n in validators: validators[n](args and args[0] or None, v) return func(*args, **kwargs)
def bind(self, *pos, **kw): inspect.getcallargs(self.func, *pos, **kw)
def _get_key(args, kwargs): return f.__module__, f.__name__, make_hashable( getcallargs(f, *args, **kwargs))
def wrapped(*args, **kwargs): # If we're passed a cache_context then we'll want to call its invalidate() # whenever we are invalidated invalidate_callback = kwargs.pop("on_invalidate", None) arg_dict = inspect.getcallargs(self.orig, obj, *args, **kwargs) keyargs = [arg_dict[arg_nm] for arg_nm in self.arg_names] list_args = arg_dict[self.list_name] # cached is a dict arg -> deferred, where deferred results in a # 2-tuple (`arg`, `result`) results = {} cached_defers = {} missing = [] for arg in list_args: key = list(keyargs) key[self.list_pos] = arg try: res = cache.get(tuple(key), callback=invalidate_callback) if not res.has_succeeded(): res = res.observe() res.addCallback(lambda r, arg: (arg, r), arg) cached_defers[arg] = res else: results[arg] = res.get_result() except KeyError: missing.append(arg) if missing: sequence = cache.sequence args_to_call = dict(arg_dict) args_to_call[self.list_name] = missing ret_d = defer.maybeDeferred( preserve_context_over_fn, self.function_to_call, **args_to_call ) ret_d = ObservableDeferred(ret_d) # We need to create deferreds for each arg in the list so that # we can insert the new deferred into the cache. for arg in missing: with PreserveLoggingContext(): observer = ret_d.observe() observer.addCallback(lambda r, arg: r.get(arg, None), arg) observer = ObservableDeferred(observer) key = list(keyargs) key[self.list_pos] = arg cache.update( sequence, tuple(key), observer, callback=invalidate_callback ) def invalidate(f, key): cache.invalidate(key) return f observer.addErrback(invalidate, tuple(key)) res = observer.observe() res.addCallback(lambda r, arg: (arg, r), arg) cached_defers[arg] = res if cached_defers: def update_results_dict(res): results.update(res) return results return preserve_context_over_deferred(defer.gatherResults( cached_defers.values(), consumeErrors=True, ).addCallback(update_results_dict).addErrback( unwrapFirstError )) else: return results
def evaluate(fn: t.Callable[..., t.Any], *, _globals=None, **_locals: t.Any) -> None: sig = inspect.signature(fn) for k, p in sig.parameters.items(): if k in _locals: continue if p.default == p.empty: continue _locals[k] = p.default inspect.getcallargs(fn, **_locals) # dis.dis(fn) if _globals is None: _globals = ChainMap(globals(), sys.modules["builtins"].__dict__) pos = [] stack = [] pos.append(stack) for lineno, inst in iter_instructions(fn): opname = inst.opname logger.debug("%s %s", inst.opname, inst.argrepr) if opname == "LOAD_GLOBAL": stack.append(_globals[inst.argval]) elif opname == "LOAD_FAST": stack.append(_locals[inst.argval]) elif opname == "LOAD_CONST": stack.append(inst.argval) elif opname == "STORE_FAST": # remove from stack? or not? _locals[inst.argval] = stack.pop() elif opname == "POP_TOP": stack.pop() elif opname == "RETURN_VALUE": # NG?? assert len(stack) == 1, stack return stack[-1] elif opname == "BINARY_ADD": rv = stack.pop() lv = stack.pop() stack.append(lv + rv) elif opname == "BUILD_TUPLE": args = reversed([stack.pop() for _ in range(inst.arg)]) stack.append(tuple(args)) elif opname == "CALL_FUNCTION": args = reversed([stack.pop() for _ in range(inst.arg)]) fn = stack.pop() stack.append(fn(*args)) elif opname == "CALL_FUNCTION_KW": kwargs = {k: stack.pop() for k in stack.pop()} arglen = inst.arg - len(kwargs) args = () if arglen > 0: args = reversed( [stack.pop() for _ in range(inst.arg - len(kwargs))]) fn = stack.pop() if fn.__module__ == __name__ and isinstance(fn, type): fn = get_dataclass_factory(fn) stack.append(fn(*args, **kwargs)) else: # TODO: traceback raise RuntimeError( f"unsupported opcode: {inst.opname} {inst.argrepr}, in line {lineno}\n stack(len={len(stack)})={stack!r}" )
def wrapped(*args, **kwargs): # If we're passed a cache_context then we'll want to call its # invalidate() whenever we are invalidated invalidate_callback = kwargs.pop("on_invalidate", None) arg_dict = inspect.getcallargs(self.orig, obj, *args, **kwargs) keyargs = [arg_dict[arg_nm] for arg_nm in self.arg_names] list_args = arg_dict[self.list_name] results = {} def update_results_dict(res, arg): results[arg] = res # list of deferreds to wait for cached_defers = [] missing = set() # If the cache takes a single arg then that is used as the key, # otherwise a tuple is used. if num_args == 1: def arg_to_cache_key(arg): return arg else: keylist = list(keyargs) def arg_to_cache_key(arg): keylist[self.list_pos] = arg return tuple(keylist) for arg in list_args: try: res = cache.get(arg_to_cache_key(arg), callback=invalidate_callback) if not isinstance(res, ObservableDeferred): results[arg] = res elif not res.has_succeeded(): res = res.observe() res.addCallback(update_results_dict, arg) cached_defers.append(res) else: results[arg] = res.get_result() except KeyError: missing.add(arg) if missing: # we need an observable deferred for each entry in the list, # which we put in the cache. Each deferred resolves with the # relevant result for that key. deferreds_map = {} for arg in missing: deferred = defer.Deferred() deferreds_map[arg] = deferred key = arg_to_cache_key(arg) observable = ObservableDeferred(deferred) cache.set(key, observable, callback=invalidate_callback) def complete_all(res): # the wrapped function has completed. It returns a # a dict. We can now resolve the observable deferreds in # the cache and update our own result map. for e in missing: val = res.get(e, None) deferreds_map[e].callback(val) results[e] = val def errback(f): # the wrapped function has failed. Invalidate any cache # entries we're supposed to be populating, and fail # their deferreds. for e in missing: key = arg_to_cache_key(e) cache.invalidate(key) deferreds_map[e].errback(f) # return the failure, to propagate to our caller. return f args_to_call = dict(arg_dict) args_to_call[self.list_name] = list(missing) cached_defers.append( defer.maybeDeferred( logcontext.preserve_fn(self.function_to_call), **args_to_call).addCallbacks(complete_all, errback)) if cached_defers: d = defer.gatherResults( cached_defers, consumeErrors=True, ).addCallbacks(lambda _: results, unwrapFirstError) return logcontext.make_deferred_yieldable(d) else: return results
def _add_action(self, name: str, *args: Any, **kwargs: Any) -> None: """Process action calls in plans. :param name: action name :param args: positional arguments of the action call :param kwargs: keyword arguments of the action call """ if self.ignore_disabled and not self.env.enabled: return if TYPE_CHECKING: assert self.plan is not None # ??? sometimes to understand the context it would be better to have # several lines of plan, e.g. when an action is created by calling # a function defined in a plan. Include all these lines separated # by ';'. A better fix would be to change plan_line to plan_lines # containing a tuple of plan_line. plan_line = "unknown filename:unknown lineno" # Retrieve the plan line try: caller_frames = inspect.getouterframes(frame=inspect.currentframe()) caller_frames_in_plan = [] for frame in caller_frames: frame_filename = frame.filename if frame_filename.endswith(self.plan.plan_ext): caller_frames_in_plan.append(frame) except Exception: # defensive code # do not crash when inspect frames fails pass else: if not caller_frames_in_plan: # No information ? pass else: plan_line = ";".join( f"{caller_frame.filename}:{caller_frame.lineno}" for caller_frame in caller_frames_in_plan ) # First create our initial object based on current scope env result = self.env.copy() # Process arguments call_args = inspect.getcallargs(self.actions[name], *args, **kwargs) # Push function arguments into the result object. If an arg value # is None then environment is not updated (handling of defaults # coming from environment). For the build, host and target arguments # a special processing is done to make the corresponding set_env # call on the result BaseEnv object platform: Dict[str, Optional[str]] = { "build": None, "host": None, "target": None, } # Likewise board argument is used to change only the machine name # of the target. ??? change name ??? board = None for k, v in call_args.items(): if k in platform: platform[k] = v elif k == "board": board = v elif v is not None or not hasattr(result, k): setattr(result, k, v) # Handle propagation of environment from the context if platform["host"] is None and result.is_canadian: platform["host"] = "host" if platform["target"] is None and result.is_cross: platform["target"] = "target" if platform["target"] == result.host.platform: # ??? This special case is temporary ??? # the goal is avoid cross from a -> a which are # not current supported. # Plans should be updated to use target='host' # instead of target=env.host.platform platform["target"] = "host" result.set_env(**platform) # If necessary adjust target machine name if board is not None: result.set_target( result.target.platform, result.target.os.version, board, result.target.os.mode, ) # Set action attribute (with action name) result.action = name result.plan_line = plan_line result.plan_args = result.to_dict() result.plan_call_args = call_args # Push the action in the current list self.action_list.append(result)
def _singleton(*args, **kwargs): key = frozenset( inspect.getcallargs(cls.__init__, *args, **kwargs).items()) if key not in instances: instances[key] = cls(*args, **kwargs) return instances[key]
def _wrapper(*args, **kwargs): _log.info('DRY RUN: calling %s with arguments %s', function.__name__, inspect.getcallargs(function, *args, **kwargs))
def wrapper(*args, **kwargs): argmap = inspect.getcallargs(func, *args, **kwargs) for k, map_func in six.iteritems(maps): if k in argmap: argmap[k] = map_func(argmap[k]) return func(**argmap)
def _resolve_lookup(self, context): """ Performs resolution of a real variable (i.e. not a literal) against the given context. As indicated by the method's name, this method is an implementation detail and shouldn't be called by external code. Use Variable.resolve() instead. """ current = context try: # catch-all for silent variable failures for bit in self.lookups: try: # dictionary lookup current = current[bit] # ValueError/IndexError are for numpy.array lookup on # numpy < 1.9 and 1.9+ respectively except (TypeError, AttributeError, KeyError, ValueError, IndexError): try: # attribute lookup # Don't return class attributes if the class is the context: if isinstance(current, BaseContext) and getattr( type(current), bit): raise AttributeError current = getattr(current, bit) except (TypeError, AttributeError): # Reraise if the exception was raised by a @property if not isinstance(current, BaseContext) and bit in dir(current): raise try: # list-index lookup current = current[int(bit)] except ( IndexError, # list index out of range ValueError, # invalid literal for int() KeyError, # current is a dict without `int(bit)` key TypeError): # unsubscriptable object raise VariableDoesNotExist( "Failed lookup for key " "[%s] in %r", (bit, current)) # missing attribute if callable(current): if getattr(current, 'do_not_call_in_templates', False): pass elif getattr(current, 'alters_data', False): current = context.template.engine.string_if_invalid else: try: # method call (assuming no args required) current = current() except TypeError: try: inspect.getcallargs(current) except TypeError: # arguments *were* required current = context.template.engine.string_if_invalid # invalid method call else: raise except Exception as e: template_name = getattr(context, 'template_name', None) or 'unknown' logger.debug( "Exception while resolving variable '%s' in template '%s'.", bit, template_name, exc_info=True, ) if getattr(e, 'silent_variable_failure', False): current = context.template.engine.string_if_invalid else: raise return current
def wrapper(*args, **kwargs): callargs = inspect.getcallargs(f, *args, **kwargs) for name in argnames: callargs[name] = make_iterable(callargs[name]) return f(**callargs)
def get_mock_call_args(pattern_func, mock_func): """ Return the arguments the mock function was called with interpreted by the pattern functions argument list. """ invoked_args, invoked_kargs = mock_func.call_args return getcallargs(pattern_func, *invoked_args, **invoked_kargs)
def _fn(*args, **kwargs): binding = inspect.getcallargs(fn, *args, **kwargs) binding_str = ", ".join("%s=%s" % item for item in binding.iteritems()) signature = fn.__name__ + "(%s)" % binding_str print(signature, file=sys.stderr) return fn(*args, **kwargs)
def generate_key(base, args, kwargs): argsmap = inspect.getcallargs(function, *args, **kwargs) argvals_as_strings = [str(argsmap[argname]) for argname in argnames] argstr = '_{}'.format('_'.join(argvals_as_strings)) if argnames else '' return '{}{}'.format(base, argstr)
def func_wrapper(self, *args, **kwargs): d = inspect.getcallargs(func, self, *args, **kwargs) d['args'].update({"studyID": self.studyID}) return func(**d)
def __call__(self, *args, **kwds): all = getcallargs(self._f, *args, **kwds) return cython_inline(self._body, locals=self._f.func_globals, globals=self._f.func_globals, **all)