Ejemplo n.º 1
0
def _idval(val, argname, idx, idfn, config=None):
    if idfn:
        s = None
        try:
            s = idfn(val)
        except Exception:
            # See issue https://github.com/pytest-dev/pytest/issues/2169
            import warnings
            msg = "Raised while trying to determine id of parameter %s at position %d." % (
                argname, idx)
            msg += '\nUpdate your code as this will raise an error in pytest-4.0.'
            warnings.warn(msg, DeprecationWarning)
        if s:
            return ascii_escaped(s)

    if config:
        hook_id = config.hook.pytest_make_parametrize_id(config=config,
                                                         val=val,
                                                         argname=argname)
        if hook_id:
            return hook_id

    if isinstance(val, STRING_TYPES):
        return ascii_escaped(val)
    elif isinstance(val, (float, int, bool, NoneType)):
        return str(val)
    elif isinstance(val, REGEX_TYPE):
        return ascii_escaped(val.pattern)
    elif enum is not None and isinstance(val, enum.Enum):
        return str(val)
    elif isclass(val) and hasattr(val, '__name__'):
        return val.__name__
    return str(argname) + str(idx)
Ejemplo n.º 2
0
def _idval(val, argname, idx, idfn, config=None):
    if idfn:
        s = None
        try:
            s = idfn(val)
        except Exception:
            # See issue https://github.com/pytest-dev/pytest/issues/2169
            import warnings
            msg = "Raised while trying to determine id of parameter %s at position %d." % (argname, idx)
            msg += '\nUpdate your code as this will raise an error in pytest-4.0.'
            warnings.warn(msg, DeprecationWarning)
        if s:
            return ascii_escaped(s)

    if config:
        hook_id = config.hook.pytest_make_parametrize_id(
            config=config, val=val, argname=argname)
        if hook_id:
            return hook_id

    if isinstance(val, STRING_TYPES):
        return ascii_escaped(val)
    elif isinstance(val, (float, int, bool, NoneType)):
        return str(val)
    elif isinstance(val, REGEX_TYPE):
        return ascii_escaped(val.pattern)
    elif enum is not None and isinstance(val, enum.Enum):
        return str(val)
    elif isclass(val) and hasattr(val, '__name__'):
        return val.__name__
    return str(argname) + str(idx)
Ejemplo n.º 3
0
def _idval(val, argname, idx, idfn, item, config):
    if idfn:
        s = None
        try:
            s = idfn(val)
        except Exception as e:
            # See issue https://github.com/pytest-dev/pytest/issues/2169
            msg = "{}: error raised while trying to determine id of parameter '{}' at position {}\n"
            msg = msg.format(item.nodeid, argname, idx)
            # we only append the exception type and message because on Python 2 reraise does nothing
            msg += "  {}: {}\n".format(type(e).__name__, e)
            six.raise_from(ValueError(msg), e)
        if s:
            return ascii_escaped(s)

    if config:
        hook_id = config.hook.pytest_make_parametrize_id(
            config=config, val=val, argname=argname
        )
        if hook_id:
            return hook_id

    if isinstance(val, STRING_TYPES):
        return ascii_escaped(val)
    elif isinstance(val, (float, int, bool, NoneType)):
        return str(val)
    elif isinstance(val, REGEX_TYPE):
        return ascii_escaped(val.pattern)
    elif enum is not None and isinstance(val, enum.Enum):
        return str(val)
    elif (isclass(val) or isfunction(val)) and hasattr(val, "__name__"):
        return val.__name__
    return str(argname) + str(idx)
Ejemplo n.º 4
0
def _idval(val, argname, idx, idfn, item, config):
    if idfn:
        s = None
        try:
            s = idfn(val)
        except Exception as e:
            # See issue https://github.com/pytest-dev/pytest/issues/2169
            msg = "{}: error raised while trying to determine id of parameter '{}' at position {}\n"
            msg = msg.format(item.nodeid, argname, idx)
            # we only append the exception type and message because on Python 2 reraise does nothing
            msg += "  {}: {}\n".format(type(e).__name__, e)
            six.raise_from(ValueError(msg), e)
        if s:
            return ascii_escaped(s)

    if config:
        hook_id = config.hook.pytest_make_parametrize_id(
            config=config, val=val, argname=argname
        )
        if hook_id:
            return hook_id

    if isinstance(val, STRING_TYPES):
        return ascii_escaped(val)
    elif isinstance(val, (float, int, bool, NoneType)):
        return str(val)
    elif isinstance(val, REGEX_TYPE):
        return ascii_escaped(val.pattern)
    elif enum is not None and isinstance(val, enum.Enum):
        return str(val)
    elif (isclass(val) or isfunction(val)) and hasattr(val, "__name__"):
        return val.__name__
    return str(argname) + str(idx)
Ejemplo n.º 5
0
def warning_record_to_str(warning_message):
    """Convert a warnings.WarningMessage to a string, taking in account a lot of unicode shenaningans in Python 2.

    When Python 2 support is dropped this function can be greatly simplified.
    """
    warn_msg = warning_message.message
    unicode_warning = False
    if compat._PY2 and any(
            isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args):
        new_args = []
        for m in warn_msg.args:
            new_args.append(
                compat.ascii_escaped(m) if isinstance(m, compat.UNICODE_TYPES
                                                      ) else m)
        unicode_warning = list(warn_msg.args) != new_args
        warn_msg.args = new_args

    msg = warnings.formatwarning(
        warn_msg,
        warning_message.category,
        warning_message.filename,
        warning_message.lineno,
        warning_message.line,
    )
    if unicode_warning:
        warnings.warn(
            "Warning is using unicode non convertible to ascii, "
            "converting to a safe representation:\n  %s" % msg,
            UnicodeWarning,
        )
    return msg
Ejemplo n.º 6
0
def warning_record_to_str(warning_message):
    """Convert a warnings.WarningMessage to a string, taking in account a lot of unicode shenaningans in Python 2.

    When Python 2 support is dropped this function can be greatly simplified.
    """
    warn_msg = warning_message.message
    unicode_warning = False
    if compat._PY2 and any(isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args):
        new_args = []
        for m in warn_msg.args:
            new_args.append(
                compat.ascii_escaped(m) if isinstance(m, compat.UNICODE_TYPES) else m
            )
        unicode_warning = list(warn_msg.args) != new_args
        warn_msg.args = new_args

    msg = warnings.formatwarning(
        warn_msg,
        warning_message.category,
        warning_message.filename,
        warning_message.lineno,
        warning_message.line,
    )
    if unicode_warning:
        warnings.warn(
            "Warning is using unicode non convertible to ascii, "
            "converting to a safe representation:\n  {!r}".format(compat.safe_str(msg)),
            UnicodeWarning,
        )
    return msg
Ejemplo n.º 7
0
def _idvalset(idx, parameterset, argnames, idfn, ids, config=None):
    if parameterset.id is not None:
        return parameterset.id
    if ids is None or (idx >= len(ids) or ids[idx] is None):
        this_id = [_idval(val, argname, idx, idfn, config)
                   for val, argname in zip(parameterset.values, argnames)]
        return "-".join(this_id)
    else:
        return ascii_escaped(ids[idx])
Ejemplo n.º 8
0
def _idvalset(idx, parameterset, argnames, idfn, ids, config=None):
    if parameterset.id is not None:
        return parameterset.id
    if ids is None or (idx >= len(ids) or ids[idx] is None):
        this_id = [_idval(val, argname, idx, idfn, config)
                   for val, argname in zip(parameterset.values, argnames)]
        return "-".join(this_id)
    else:
        return ascii_escaped(ids[idx])
Ejemplo n.º 9
0
def catch_warnings_for_item(item):
    """
    catches the warnings generated during setup/call/teardown execution
    of the given item and after it is done posts them as warnings to this
    item.
    """
    args = item.config.getoption("pythonwarnings") or []
    inifilters = item.config.getini("filterwarnings")
    with warnings.catch_warnings(record=True) as log:
        for arg in args:
            warnings._setoption(arg)

        for arg in inifilters:
            _setoption(warnings, arg)

        for mark in item.iter_markers(name="filterwarnings"):
            for arg in mark.args:
                warnings._setoption(arg)

        yield

        for warning in log:
            warn_msg = warning.message
            unicode_warning = False

            if (
                compat._PY2
                and any(isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args)
            ):
                new_args = []
                for m in warn_msg.args:
                    new_args.append(
                        compat.ascii_escaped(m)
                        if isinstance(m, compat.UNICODE_TYPES)
                        else m
                    )
                unicode_warning = list(warn_msg.args) != new_args
                warn_msg.args = new_args

            msg = warnings.formatwarning(
                warn_msg,
                warning.category,
                warning.filename,
                warning.lineno,
                warning.line,
            )
            item.warn("unused", msg)

            if unicode_warning:
                warnings.warn(
                    "Warning is using unicode non convertible to ascii, "
                    "converting to a safe representation:\n  %s" % msg,
                    UnicodeWarning,
                )
Ejemplo n.º 10
0
def catch_warnings_for_item(item):
    """
    catches the warnings generated during setup/call/teardown execution
    of the given item and after it is done posts them as warnings to this
    item.
    """
    args = item.config.getoption("pythonwarnings") or []
    inifilters = item.config.getini("filterwarnings")
    with warnings.catch_warnings(record=True) as log:
        for arg in args:
            warnings._setoption(arg)

        for arg in inifilters:
            _setoption(warnings, arg)

        for mark in item.iter_markers(name="filterwarnings"):
            for arg in mark.args:
                warnings._setoption(arg)

        yield

        for warning in log:
            warn_msg = warning.message
            unicode_warning = False

            if compat._PY2 and any(
                    isinstance(m, compat.UNICODE_TYPES)
                    for m in warn_msg.args):
                new_args = []
                for m in warn_msg.args:
                    new_args.append(
                        compat.ascii_escaped(m) if isinstance(
                            m, compat.UNICODE_TYPES) else m)
                unicode_warning = list(warn_msg.args) != new_args
                warn_msg.args = new_args

            msg = warnings.formatwarning(
                warn_msg,
                warning.category,
                warning.filename,
                warning.lineno,
                warning.line,
            )
            item.warn("unused", msg)

            if unicode_warning:
                warnings.warn(
                    "Warning is using unicode non convertible to ascii, "
                    "converting to a safe representation:\n  %s" % msg,
                    UnicodeWarning,
                )