# Buggy code
def format_meter(
    n,
    total,
    elapsed,
    ncols=None,
    prefix="",
    ascii=False,
    unit="it",
    unit_scale=False,
    rate=None,
    bar_format=None,
    postfix=None,
    unit_divisor=1000,
    **extra_kwargs,
):
    """
    Return a string-based progress bar given some parameters

    Parameters
    ----------
    n  : int or float
        Number of finished iterations.
    total  : int or float
        The expected total number of iterations. If meaningless (None),
        only basic progress statistics are displayed (no ETA).
    elapsed  : float
        Number of seconds passed since start.
    ncols  : int, optional
        The width of the entire output message. If specified,
        dynamically resizes `{bar}` to stay within this bound
        [default: None]. If `0`, will not print any bar (only stats).
        The fallback is `{bar:10}`.
    prefix  : str, optional
        Prefix message (included in total width) [default: ''].
        Use as {desc} in bar_format string.
    ascii  : bool, optional or str, optional
        If not set, use unicode (smooth blocks) to fill the meter
        [default: False]. The fallback is to use ASCII characters
        " 123456789#".
    unit  : str, optional
        The iteration unit [default: 'it'].
    unit_scale  : bool or int or float, optional
        If 1 or True, the number of iterations will be printed with an
        appropriate SI metric prefix (k = 10^3, M = 10^6, etc.)
        [default: False]. If any other non-zero number, will scale
        `total` and `n`.
    rate  : float, optional
        Manual override for iteration rate.
        If [default: None], uses n/elapsed.
    bar_format  : str, optional
        Specify a custom bar string formatting. May impact performance.
        [default: '{l_bar}{bar}{r_bar}'], where
        l_bar='{desc}: {percentage:3.0f}%|' and
        r_bar='| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, '
          '{rate_fmt}{postfix}]'
        Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt,
          percentage, elapsed, elapsed_s, ncols, desc, unit,
          rate, rate_fmt, rate_noinv, rate_noinv_fmt,
          rate_inv, rate_inv_fmt, postfix, unit_divisor,
          remaining, remaining_s.
        Note that a trailing ": " is automatically removed after {desc}
        if the latter is empty.
    postfix  : *, optional
        Similar to `prefix`, but placed at the end
        (e.g. for additional stats).
        Note: postfix is usually a string (not a dict) for this method,
        and will if possible be set to postfix = ', ' + postfix.
        However other types are supported (#382).
    unit_divisor  : float, optional
        [default: 1000], ignored unless `unit_scale` is True.

    Returns
    -------
    out  : Formatted meter and stats, ready to display.
    """

    # sanity check: total
    if total and n > total:
        total = None

    # apply custom scale if necessary
    if unit_scale and unit_scale not in (True, 1):
        if total:
            total *= unit_scale
        n *= unit_scale
        if rate:
            rate *= unit_scale  # by default rate = 1 / self.avg_time
        unit_scale = False

    elapsed_str = tqdm.format_interval(elapsed)

    # if unspecified, attempt to use rate = average speed
    # (we allow manual override since predicting time is an arcane art)
    if rate is None and elapsed:
        rate = n / elapsed
    inv_rate = 1 / rate if rate else None
    format_sizeof = tqdm.format_sizeof
    rate_noinv_fmt = (
        (
            (format_sizeof(rate) if unit_scale else "{0:5.2f}".format(rate))
            if rate
            else "?"
        )
        + unit
        + "/s"
    )
    rate_inv_fmt = (
        (
            (format_sizeof(inv_rate) if unit_scale else "{0:5.2f}".format(inv_rate))
            if inv_rate
            else "?"
        )
        + "s/"
        + unit
    )
    rate_fmt = rate_inv_fmt if inv_rate and inv_rate > 1 else rate_noinv_fmt

    if unit_scale:
        n_fmt = format_sizeof(n, divisor=unit_divisor)
        total_fmt = (
            format_sizeof(total, divisor=unit_divisor) if total is not None else "?"
        )
    else:
        n_fmt = str(n)
        total_fmt = str(total) if total is not None else "?"

    try:
        postfix = ", " + postfix if postfix else ""
    except TypeError:
        pass

    remaining = (total - n) / rate if rate and total else 0
    remaining_str = tqdm.format_interval(remaining) if rate else "?"

    # format the stats displayed to the left and right sides of the bar
    if prefix:
        # old prefix setup work around
        bool_prefix_colon_already = prefix[-2:] == ": "
        l_bar = prefix if bool_prefix_colon_already else prefix + ": "
    else:
        l_bar = ""

    r_bar = "| {0}/{1} [{2}<{3}, {4}{5}]".format(
        n_fmt, total_fmt, elapsed_str, remaining_str, rate_fmt, postfix
    )

    # Custom bar formatting
    # Populate a dict with all available progress indicators
    format_dict = dict(
        # slight extension of self.format_dict
        n=n,
        n_fmt=n_fmt,
        total=total,
        total_fmt=total_fmt,
        elapsed=elapsed_str,
        elapsed_s=elapsed,
        ncols=ncols,
        desc=prefix or "",
        unit=unit,
        rate=inv_rate if inv_rate and inv_rate > 1 else rate,
        rate_fmt=rate_fmt,
        rate_noinv=rate,
        rate_noinv_fmt=rate_noinv_fmt,
        rate_inv=inv_rate,
        rate_inv_fmt=rate_inv_fmt,
        postfix=postfix,
        unit_divisor=unit_divisor,
        # plus more useful definitions
        remaining=remaining_str,
        remaining_s=remaining,
        l_bar=l_bar,
        r_bar=r_bar,
        **extra_kwargs,
    )

    # total is known: we can predict some stats
    if total:
        # fractional and percentage progress
        frac = n / total
        percentage = frac * 100

        l_bar += "{0:3.0f}%|".format(percentage)

        if ncols == 0:
            return l_bar[:-1] + r_bar[1:]

        format_dict.update(l_bar=l_bar)
        if bar_format:
            format_dict.update(percentage=percentage)

            # auto-remove colon for empty `desc`
            if not prefix:
                bar_format = bar_format.replace("{desc}: ", "")
        else:
            bar_format = "{l_bar}{bar}{r_bar}"

        full_bar = FormatReplace()
        nobar = bar_format.format(bar=full_bar, **format_dict)
        if not full_bar.format_called:
            # no {bar}, we can just format and return
            return nobar

        # Formatting progress bar space available for bar's display
        full_bar = Bar(
            frac,
            max(1, ncols - _text_width(RE_ANSI.sub("", nobar))) if ncols else 10,
            charset=Bar.ASCII if ascii is True else ascii or Bar.UTF,
        )
        if not _is_ascii(full_bar.charset) and _is_ascii(bar_format):
            bar_format = _unicode(bar_format)
        return bar_format.format(bar=full_bar, **format_dict)

    elif bar_format:
        # user-specified bar_format but no total
        l_bar += "|"
        format_dict.update(l_bar=l_bar, percentage=0)
        full_bar = FormatReplace()
        nobar = bar_format.format(bar=full_bar, **format_dict)
        if not full_bar.format_called:
            return nobar
        full_bar = Bar(
            0,
            max(1, ncols - _text_width(RE_ANSI.sub("", nobar))) if ncols else 10,
            charset=Bar.BLANK,
        )
        return bar_format.format(bar=full_bar, **format_dict)
    else:
        # no total: no progressbar, ETA, just progress stats
        return ((prefix + ": ") if prefix else "") + "{0}{1} [{2}, {3}{4}]".format(
            n_fmt, unit, elapsed_str, rate_fmt, postfix
        )


# Traceback (UnicodeEncodeError)
import tqdm, sys
print(tqdm.__version__, sys.version, sys.platform)
('4.34.0', '2.7.15+ (default, Oct  7 2019, 17:39:04) \n[GCC 7.4.0]', 'linux2')
pbar = tqdm.tqdm(total=10, leave=False)
0%|          | 0/10 [00:00<?, ?it/s]
pbar.set_description(u"áéíóú")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "tqdm/_tqdm.py", line 1289, in set_description
self.refresh()
File "tqdm/_tqdm.py", line 1251, in refresh
self.display()
File "tqdm/_tqdm.py", line 1374, in display
self.sp(self.__repr__() if msg is None else msg)
File "tqdm/_tqdm.py", line 1020, in __repr__
return self.format_meter(**self.format_dict)
File "tqdm/_tqdm.py", line 462, in format_meter
nobar = bar_format.format(bar=full_bar, **format_dict)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

# Fixed code
def format_meter(
    n,
    total,
    elapsed,
    ncols=None,
    prefix="",
    ascii=False,
    unit="it",
    unit_scale=False,
    rate=None,
    bar_format=None,
    postfix=None,
    unit_divisor=1000,
    **extra_kwargs,
):
    """
    Return a string-based progress bar given some parameters

    Parameters
    ----------
    n  : int or float
        Number of finished iterations.
    total  : int or float
        The expected total number of iterations. If meaningless (None),
        only basic progress statistics are displayed (no ETA).
    elapsed  : float
        Number of seconds passed since start.
    ncols  : int, optional
        The width of the entire output message. If specified,
        dynamically resizes `{bar}` to stay within this bound
        [default: None]. If `0`, will not print any bar (only stats).
        The fallback is `{bar:10}`.
    prefix  : str, optional
        Prefix message (included in total width) [default: ''].
        Use as {desc} in bar_format string.
    ascii  : bool, optional or str, optional
        If not set, use unicode (smooth blocks) to fill the meter
        [default: False]. The fallback is to use ASCII characters
        " 123456789#".
    unit  : str, optional
        The iteration unit [default: 'it'].
    unit_scale  : bool or int or float, optional
        If 1 or True, the number of iterations will be printed with an
        appropriate SI metric prefix (k = 10^3, M = 10^6, etc.)
        [default: False]. If any other non-zero number, will scale
        `total` and `n`.
    rate  : float, optional
        Manual override for iteration rate.
        If [default: None], uses n/elapsed.
    bar_format  : str, optional
        Specify a custom bar string formatting. May impact performance.
        [default: '{l_bar}{bar}{r_bar}'], where
        l_bar='{desc}: {percentage:3.0f}%|' and
        r_bar='| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, '
          '{rate_fmt}{postfix}]'
        Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt,
          percentage, elapsed, elapsed_s, ncols, desc, unit,
          rate, rate_fmt, rate_noinv, rate_noinv_fmt,
          rate_inv, rate_inv_fmt, postfix, unit_divisor,
          remaining, remaining_s.
        Note that a trailing ": " is automatically removed after {desc}
        if the latter is empty.
    postfix  : *, optional
        Similar to `prefix`, but placed at the end
        (e.g. for additional stats).
        Note: postfix is usually a string (not a dict) for this method,
        and will if possible be set to postfix = ', ' + postfix.
        However other types are supported (#382).
    unit_divisor  : float, optional
        [default: 1000], ignored unless `unit_scale` is True.

    Returns
    -------
    out  : Formatted meter and stats, ready to display.
    """

    # sanity check: total
    if total and n > total:
        total = None

    # apply custom scale if necessary
    if unit_scale and unit_scale not in (True, 1):
        if total:
            total *= unit_scale
        n *= unit_scale
        if rate:
            rate *= unit_scale  # by default rate = 1 / self.avg_time
        unit_scale = False

    elapsed_str = tqdm.format_interval(elapsed)

    # if unspecified, attempt to use rate = average speed
    # (we allow manual override since predicting time is an arcane art)
    if rate is None and elapsed:
        rate = n / elapsed
    inv_rate = 1 / rate if rate else None
    format_sizeof = tqdm.format_sizeof
    rate_noinv_fmt = (
        (
            (format_sizeof(rate) if unit_scale else "{0:5.2f}".format(rate))
            if rate
            else "?"
        )
        + unit
        + "/s"
    )
    rate_inv_fmt = (
        (
            (format_sizeof(inv_rate) if unit_scale else "{0:5.2f}".format(inv_rate))
            if inv_rate
            else "?"
        )
        + "s/"
        + unit
    )
    rate_fmt = rate_inv_fmt if inv_rate and inv_rate > 1 else rate_noinv_fmt

    if unit_scale:
        n_fmt = format_sizeof(n, divisor=unit_divisor)
        total_fmt = (
            format_sizeof(total, divisor=unit_divisor) if total is not None else "?"
        )
    else:
        n_fmt = str(n)
        total_fmt = str(total) if total is not None else "?"

    try:
        postfix = ", " + postfix if postfix else ""
    except TypeError:
        pass

    remaining = (total - n) / rate if rate and total else 0
    remaining_str = tqdm.format_interval(remaining) if rate else "?"

    # format the stats displayed to the left and right sides of the bar
    if prefix:
        # old prefix setup work around
        bool_prefix_colon_already = prefix[-2:] == ": "
        l_bar = prefix if bool_prefix_colon_already else prefix + ": "
    else:
        l_bar = ""

    r_bar = "| {0}/{1} [{2}<{3}, {4}{5}]".format(
        n_fmt, total_fmt, elapsed_str, remaining_str, rate_fmt, postfix
    )

    # Custom bar formatting
    # Populate a dict with all available progress indicators
    format_dict = dict(
        # slight extension of self.format_dict
        n=n,
        n_fmt=n_fmt,
        total=total,
        total_fmt=total_fmt,
        elapsed=elapsed_str,
        elapsed_s=elapsed,
        ncols=ncols,
        desc=prefix or "",
        unit=unit,
        rate=inv_rate if inv_rate and inv_rate > 1 else rate,
        rate_fmt=rate_fmt,
        rate_noinv=rate,
        rate_noinv_fmt=rate_noinv_fmt,
        rate_inv=inv_rate,
        rate_inv_fmt=rate_inv_fmt,
        postfix=postfix,
        unit_divisor=unit_divisor,
        # plus more useful definitions
        remaining=remaining_str,
        remaining_s=remaining,
        l_bar=l_bar,
        r_bar=r_bar,
        **extra_kwargs,
    )

    # total is known: we can predict some stats
    if total:
        # fractional and percentage progress
        frac = n / total
        percentage = frac * 100

        l_bar += "{0:3.0f}%|".format(percentage)

        if ncols == 0:
            return l_bar[:-1] + r_bar[1:]

        format_dict.update(l_bar=l_bar)
        if bar_format:
            format_dict.update(percentage=percentage)

            # auto-remove colon for empty `desc`
            if not prefix:
                bar_format = bar_format.replace("{desc}: ", "")
        else:
            bar_format = "{l_bar}{bar}{r_bar}"

        full_bar = FormatReplace()
        if _is_ascii(bar_format) and any(
            not _is_ascii(i) for i in format_dict.values()
        ):
            bar_format = _unicode(bar_format)
        nobar = bar_format.format(bar=full_bar, **format_dict)
        if not full_bar.format_called:
            # no {bar}, we can just format and return
            return nobar

        # Formatting progress bar space available for bar's display
        full_bar = Bar(
            frac,
            max(1, ncols - _text_width(RE_ANSI.sub("", nobar))) if ncols else 10,
            charset=Bar.ASCII if ascii is True else ascii or Bar.UTF,
        )
        if not _is_ascii(full_bar.charset) and _is_ascii(bar_format):
            bar_format = _unicode(bar_format)
        return bar_format.format(bar=full_bar, **format_dict)

    elif bar_format:
        # user-specified bar_format but no total
        l_bar += "|"
        format_dict.update(l_bar=l_bar, percentage=0)
        full_bar = FormatReplace()
        nobar = bar_format.format(bar=full_bar, **format_dict)
        if not full_bar.format_called:
            return nobar
        full_bar = Bar(
            0,
            max(1, ncols - _text_width(RE_ANSI.sub("", nobar))) if ncols else 10,
            charset=Bar.BLANK,
        )
        return bar_format.format(bar=full_bar, **format_dict)
    else:
        # no total: no progressbar, ETA, just progress stats
        return ((prefix + ": ") if prefix else "") + "{0}{1} [{2}, {3}{4}]".format(
            n_fmt, unit, elapsed_str, rate_fmt, postfix
        )