# Buggy code
def __init__(
    self,
    iterable=None,
    desc=None,
    total=None,
    leave=True,
    file=None,
    ncols=None,
    mininterval=0.1,
    maxinterval=10.0,
    miniters=None,
    ascii=None,
    disable=False,
    unit="it",
    unit_scale=False,
    dynamic_ncols=False,
    smoothing=0.3,
    bar_format=None,
    initial=0,
    position=None,
    postfix=None,
    unit_divisor=1000,
    write_bytes=None,
    gui=False,
    **kwargs,
):
    """
    Parameters
    ----------
    iterable  : iterable, optional
        Iterable to decorate with a progressbar.
        Leave blank to manually manage the updates.
    desc  : str, optional
        Prefix for the progressbar.
    total  : int, optional
        The number of expected iterations. If unspecified,
        len(iterable) is used if possible. If float("inf") or as a last
        resort, only basic progress statistics are displayed
        (no ETA, no progressbar).
        If `gui` is True and this parameter needs subsequent updating,
        specify an initial arbitrary large positive integer,
        e.g. int(9e9).
    leave  : bool, optional
        If [default: True], keeps all traces of the progressbar
        upon termination of iteration.
    file  : `io.TextIOWrapper` or `io.StringIO`, optional
        Specifies where to output the progress messages
        (default: sys.stderr). Uses `file.write(str)` and `file.flush()`
        methods.  For encoding, see `write_bytes`.
    ncols  : int, optional
        The width of the entire output message. If specified,
        dynamically resizes the progressbar to stay within this bound.
        If unspecified, attempts to use environment width. The
        fallback is a meter width of 10 and no limit for the counter and
        statistics. If 0, will not print any meter (only stats).
    mininterval  : float, optional
        Minimum progress display update interval [default: 0.1] seconds.
    maxinterval  : float, optional
        Maximum progress display update interval [default: 10] seconds.
        Automatically adjusts `miniters` to correspond to `mininterval`
        after long display update lag. Only works if `dynamic_miniters`
        or monitor thread is enabled.
    miniters  : int, optional
        Minimum progress display update interval, in iterations.
        If 0 and `dynamic_miniters`, will automatically adjust to equal
        `mininterval` (more CPU efficient, good for tight loops).
        If > 0, will skip display of specified number of iterations.
        Tweak this and `mininterval` to get very efficient loops.
        If your progress is erratic with both fast and slow iterations
        (network, skipping items, etc) you should set miniters=1.
    ascii  : bool or str, optional
        If unspecified or False, use unicode (smooth blocks) to fill
        the meter. The fallback is to use ASCII characters " 123456789#".
    disable  : bool, optional
        Whether to disable the entire progressbar wrapper
        [default: False]. If set to None, disable on non-TTY.
    unit  : str, optional
        String that will be used to define the unit of each iteration
        [default: it].
    unit_scale  : bool or int or float, optional
        If 1 or True, the number of iterations will be reduced/scaled
        automatically and a metric prefix following the
        International System of Units standard will be added
        (kilo, mega, etc.) [default: False]. If any other non-zero
        number, will scale `total` and `n`.
    dynamic_ncols  : bool, optional
        If set, constantly alters `ncols` to the environment (allowing
        for window resizes) [default: False].
    smoothing  : float, optional
        Exponential moving average smoothing factor for speed estimates
        (ignored in GUI mode). Ranges from 0 (average speed) to 1
        (current/instantaneous speed) [default: 0.3].
    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, rate, rate_fmt, rate_noinv, rate_noinv_fmt,
          rate_inv, rate_inv_fmt, elapsed, elapsed_s, remaining,
          remaining_s, desc, postfix, unit.
        Note that a trailing ": " is automatically removed after {desc}
        if the latter is empty.
    initial  : int, optional
        The initial counter value. Useful when restarting a progress
        bar [default: 0].
    position  : int, optional
        Specify the line offset to print this bar (starting from 0)
        Automatic if unspecified.
        Useful to manage multiple bars at once (eg, from threads).
    postfix  : dict or *, optional
        Specify additional stats to display at the end of the bar.
        Calls `set_postfix(**postfix)` if possible (dict).
    unit_divisor  : float, optional
        [default: 1000], ignored unless `unit_scale` is True.
    write_bytes  : bool, optional
        If (default: None) and `file` is unspecified,
        bytes will be written in Python 2. If `True` will also write
        bytes. In all other cases will default to unicode.
    gui  : bool, optional
        WARNING: internal parameter - do not use.
        Use tqdm_gui(...) instead. If set, will attempt to use
        matplotlib animations for a graphical output [default: False].

    Returns
    -------
    out  : decorated iterator.
    """
    if write_bytes is None:
        write_bytes = file is None and sys.version_info < (3,)

    if file is None:
        file = sys.stderr

    if write_bytes:
        # Despite coercing unicode into bytes, py2 sys.std* streams
        # should have bytes written to them.
        file = SimpleTextIOWrapper(file, encoding=getattr(file, "encoding", "utf-8"))

    if disable is None and hasattr(file, "isatty") and not file.isatty():
        disable = True

    if total is None and iterable is not None:
        try:
            total = len(iterable)
        except (TypeError, AttributeError):
            total = None
    if total == float("inf"):
        # Infinite iterations, behave same as unknown
        total = None

    if disable:
        self.iterable = iterable
        self.disable = disable
        self.pos = self._get_free_pos(self)
        self._instances.remove(self)
        self.n = initial
        self.total = total
        return

    if kwargs:
        self.disable = True
        self.pos = self._get_free_pos(self)
        self._instances.remove(self)
        from textwrap import dedent

        raise (
            TqdmDeprecationWarning(
                dedent("""\
                       `nested` is deprecated and automated.
                       Use `position` instead for manual control.
                       """),
                fp_write=getattr(file, "write", sys.stderr.write),
            )
            if "nested" in kwargs
            else TqdmKeyError("Unknown argument(s): " + str(kwargs))
        )

    # Preprocess the arguments
    if (
        (ncols is None) and (file in (sys.stderr, sys.stdout))
    ) or dynamic_ncols:  # pragma: no cover
        if dynamic_ncols:
            dynamic_ncols = _environ_cols_wrapper()
            if dynamic_ncols:
                ncols = dynamic_ncols(file)
            # elif ncols is not None:
            #     ncols = 79
        else:
            _dynamic_ncols = _environ_cols_wrapper()
            if _dynamic_ncols:
                ncols = _dynamic_ncols(file)
            # else:
            #     ncols = 79

    if miniters is None:
        miniters = 0
        dynamic_miniters = True
    else:
        dynamic_miniters = False

    if mininterval is None:
        mininterval = 0

    if maxinterval is None:
        maxinterval = 0

    if ascii is None:
        ascii = not _supports_unicode(file)

    if bar_format and not ((ascii is True) or _is_ascii(ascii)):
        # Convert bar format into unicode since terminal uses unicode
        bar_format = _unicode(bar_format)

    if smoothing is None:
        smoothing = 0

    # Store the arguments
    self.iterable = iterable
    self.desc = desc or ""
    self.total = total
    self.leave = leave
    self.fp = file
    self.ncols = ncols
    self.mininterval = mininterval
    self.maxinterval = maxinterval
    self.miniters = miniters
    self.dynamic_miniters = dynamic_miniters
    self.ascii = ascii
    self.disable = disable
    self.unit = unit
    self.unit_scale = unit_scale
    self.unit_divisor = unit_divisor
    self.gui = gui
    self.dynamic_ncols = dynamic_ncols
    self.smoothing = smoothing
    self.avg_time = None
    self._time = time
    self.bar_format = bar_format
    self.postfix = None
    if postfix:
        try:
            self.set_postfix(refresh=False, **postfix)
        except TypeError:
            self.postfix = postfix

    # Init the iterations counters
    self.last_print_n = initial
    self.n = initial

    # if nested, at initial sp() call we replace '\r' by '\n' to
    # not overwrite the outer progress bar
    with self._lock:
        if position is None:
            self.pos = self._get_free_pos(self)
        else:  # mark fixed positions as negative
            self.pos = -position

    if not gui:
        # Initialize the screen printer
        self.sp = self.status_printer(self.fp)
        with self._lock:
            self.display()

    # Init the time counter
    self.last_print_t = self._time()
    # NB: Avoid race conditions by setting start_t at the very end of init
    self.start_t = self.last_print_t


# Traceback (TypeError)
Traceback (most recent call last):
File "./patcher/env/bin/autopatch", line 11, in <module>
load_entry_point('PearPatcher==0.1', 'console_scripts', 'autopatch')()
File "build/bdist.macosx-10.13-x86_64/egg/pearpatcher/commandlineinterface.py", line 40, in main
File "build/bdist.macosx-10.13-x86_64/egg/pearpatcher/util.py", line 58, in tracedFunctionWrapper
File "build/bdist.macosx-10.13-x86_64/egg/pearpatcher/commandlineinterface.py", line 64, in run
File "build/bdist.macosx-10.13-x86_64/egg/pearpatcher/util.py", line 58, in tracedFunctionWrapper
File "build/bdist.macosx-10.13-x86_64/egg/pearpatcher/commandlineinterface.py", line 162, in patch_action
File "build/bdist.macosx-10.13-x86_64/egg/pearpatcher/swiftscanner.py", line 52, in create_patches
File "build/bdist.macosx-10.13-x86_64/egg/pearpatcher/util.py", line 58, in tracedFunctionWrapper
File "build/bdist.macosx-10.13-x86_64/egg/pearpatcher/swiftscanner.py", line 61, in scan
File "./patcher/env/lib/python2.7/site-packages/tqdm-4.31.0-py2.7.egg/tqdm/_tqdm.py", line 945, in __init__
self.display()
File "./patcher/env/lib/python2.7/site-packages/tqdm-4.31.0-py2.7.egg/tqdm/_tqdm.py", line 1315, in display
self.sp(self.__repr__() if msg is None else msg)
File "./patcher/env/lib/python2.7/site-packages/tqdm-4.31.0-py2.7.egg/tqdm/_tqdm.py", line 250, in print_status
fp_write('\r' + s + (' ' * max(last_len[0] - len_s, 0)))
File "./patcher/env/lib/python2.7/site-packages/tqdm-4.31.0-py2.7.egg/tqdm/_tqdm.py", line 243, in fp_write
fp.write(_unicode(s))
File "./patcher/env/lib/python2.7/site-packages/tqdm-4.31.0-py2.7.egg/tqdm/_utils.py", line 160, in write
self, 'encoding')))
TypeError: encode() argument 1 must be string, not None
Exception TypeError: TypeError('encode() argument 1 must be string, not None',) in <bound method tqdm.__del__ of   0%|          | 0/11698 [00:00<?, ?it/s]> ignored

# Fixed code
def __init__(
    self,
    iterable=None,
    desc=None,
    total=None,
    leave=True,
    file=None,
    ncols=None,
    mininterval=0.1,
    maxinterval=10.0,
    miniters=None,
    ascii=None,
    disable=False,
    unit="it",
    unit_scale=False,
    dynamic_ncols=False,
    smoothing=0.3,
    bar_format=None,
    initial=0,
    position=None,
    postfix=None,
    unit_divisor=1000,
    write_bytes=None,
    gui=False,
    **kwargs,
):
    """
    Parameters
    ----------
    iterable  : iterable, optional
        Iterable to decorate with a progressbar.
        Leave blank to manually manage the updates.
    desc  : str, optional
        Prefix for the progressbar.
    total  : int, optional
        The number of expected iterations. If unspecified,
        len(iterable) is used if possible. If float("inf") or as a last
        resort, only basic progress statistics are displayed
        (no ETA, no progressbar).
        If `gui` is True and this parameter needs subsequent updating,
        specify an initial arbitrary large positive integer,
        e.g. int(9e9).
    leave  : bool, optional
        If [default: True], keeps all traces of the progressbar
        upon termination of iteration.
    file  : `io.TextIOWrapper` or `io.StringIO`, optional
        Specifies where to output the progress messages
        (default: sys.stderr). Uses `file.write(str)` and `file.flush()`
        methods.  For encoding, see `write_bytes`.
    ncols  : int, optional
        The width of the entire output message. If specified,
        dynamically resizes the progressbar to stay within this bound.
        If unspecified, attempts to use environment width. The
        fallback is a meter width of 10 and no limit for the counter and
        statistics. If 0, will not print any meter (only stats).
    mininterval  : float, optional
        Minimum progress display update interval [default: 0.1] seconds.
    maxinterval  : float, optional
        Maximum progress display update interval [default: 10] seconds.
        Automatically adjusts `miniters` to correspond to `mininterval`
        after long display update lag. Only works if `dynamic_miniters`
        or monitor thread is enabled.
    miniters  : int, optional
        Minimum progress display update interval, in iterations.
        If 0 and `dynamic_miniters`, will automatically adjust to equal
        `mininterval` (more CPU efficient, good for tight loops).
        If > 0, will skip display of specified number of iterations.
        Tweak this and `mininterval` to get very efficient loops.
        If your progress is erratic with both fast and slow iterations
        (network, skipping items, etc) you should set miniters=1.
    ascii  : bool or str, optional
        If unspecified or False, use unicode (smooth blocks) to fill
        the meter. The fallback is to use ASCII characters " 123456789#".
    disable  : bool, optional
        Whether to disable the entire progressbar wrapper
        [default: False]. If set to None, disable on non-TTY.
    unit  : str, optional
        String that will be used to define the unit of each iteration
        [default: it].
    unit_scale  : bool or int or float, optional
        If 1 or True, the number of iterations will be reduced/scaled
        automatically and a metric prefix following the
        International System of Units standard will be added
        (kilo, mega, etc.) [default: False]. If any other non-zero
        number, will scale `total` and `n`.
    dynamic_ncols  : bool, optional
        If set, constantly alters `ncols` to the environment (allowing
        for window resizes) [default: False].
    smoothing  : float, optional
        Exponential moving average smoothing factor for speed estimates
        (ignored in GUI mode). Ranges from 0 (average speed) to 1
        (current/instantaneous speed) [default: 0.3].
    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, rate, rate_fmt, rate_noinv, rate_noinv_fmt,
          rate_inv, rate_inv_fmt, elapsed, elapsed_s, remaining,
          remaining_s, desc, postfix, unit.
        Note that a trailing ": " is automatically removed after {desc}
        if the latter is empty.
    initial  : int, optional
        The initial counter value. Useful when restarting a progress
        bar [default: 0].
    position  : int, optional
        Specify the line offset to print this bar (starting from 0)
        Automatic if unspecified.
        Useful to manage multiple bars at once (eg, from threads).
    postfix  : dict or *, optional
        Specify additional stats to display at the end of the bar.
        Calls `set_postfix(**postfix)` if possible (dict).
    unit_divisor  : float, optional
        [default: 1000], ignored unless `unit_scale` is True.
    write_bytes  : bool, optional
        If (default: None) and `file` is unspecified,
        bytes will be written in Python 2. If `True` will also write
        bytes. In all other cases will default to unicode.
    gui  : bool, optional
        WARNING: internal parameter - do not use.
        Use tqdm_gui(...) instead. If set, will attempt to use
        matplotlib animations for a graphical output [default: False].

    Returns
    -------
    out  : decorated iterator.
    """
    if write_bytes is None:
        write_bytes = file is None and sys.version_info < (3,)

    if file is None:
        file = sys.stderr

    if write_bytes:
        # Despite coercing unicode into bytes, py2 sys.std* streams
        # should have bytes written to them.
        file = SimpleTextIOWrapper(
            file, encoding=getattr(file, "encoding", None) or "utf-8"
        )

    if disable is None and hasattr(file, "isatty") and not file.isatty():
        disable = True

    if total is None and iterable is not None:
        try:
            total = len(iterable)
        except (TypeError, AttributeError):
            total = None
    if total == float("inf"):
        # Infinite iterations, behave same as unknown
        total = None

    if disable:
        self.iterable = iterable
        self.disable = disable
        self.pos = self._get_free_pos(self)
        self._instances.remove(self)
        self.n = initial
        self.total = total
        return

    if kwargs:
        self.disable = True
        self.pos = self._get_free_pos(self)
        self._instances.remove(self)
        from textwrap import dedent

        raise (
            TqdmDeprecationWarning(
                dedent("""\
                       `nested` is deprecated and automated.
                       Use `position` instead for manual control.
                       """),
                fp_write=getattr(file, "write", sys.stderr.write),
            )
            if "nested" in kwargs
            else TqdmKeyError("Unknown argument(s): " + str(kwargs))
        )

    # Preprocess the arguments
    if (
        (ncols is None) and (file in (sys.stderr, sys.stdout))
    ) or dynamic_ncols:  # pragma: no cover
        if dynamic_ncols:
            dynamic_ncols = _environ_cols_wrapper()
            if dynamic_ncols:
                ncols = dynamic_ncols(file)
            # elif ncols is not None:
            #     ncols = 79
        else:
            _dynamic_ncols = _environ_cols_wrapper()
            if _dynamic_ncols:
                ncols = _dynamic_ncols(file)
            # else:
            #     ncols = 79

    if miniters is None:
        miniters = 0
        dynamic_miniters = True
    else:
        dynamic_miniters = False

    if mininterval is None:
        mininterval = 0

    if maxinterval is None:
        maxinterval = 0

    if ascii is None:
        ascii = not _supports_unicode(file)

    if bar_format and not ((ascii is True) or _is_ascii(ascii)):
        # Convert bar format into unicode since terminal uses unicode
        bar_format = _unicode(bar_format)

    if smoothing is None:
        smoothing = 0

    # Store the arguments
    self.iterable = iterable
    self.desc = desc or ""
    self.total = total
    self.leave = leave
    self.fp = file
    self.ncols = ncols
    self.mininterval = mininterval
    self.maxinterval = maxinterval
    self.miniters = miniters
    self.dynamic_miniters = dynamic_miniters
    self.ascii = ascii
    self.disable = disable
    self.unit = unit
    self.unit_scale = unit_scale
    self.unit_divisor = unit_divisor
    self.gui = gui
    self.dynamic_ncols = dynamic_ncols
    self.smoothing = smoothing
    self.avg_time = None
    self._time = time
    self.bar_format = bar_format
    self.postfix = None
    if postfix:
        try:
            self.set_postfix(refresh=False, **postfix)
        except TypeError:
            self.postfix = postfix

    # Init the iterations counters
    self.last_print_n = initial
    self.n = initial

    # if nested, at initial sp() call we replace '\r' by '\n' to
    # not overwrite the outer progress bar
    with self._lock:
        if position is None:
            self.pos = self._get_free_pos(self)
        else:  # mark fixed positions as negative
            self.pos = -position

    if not gui:
        # Initialize the screen printer
        self.sp = self.status_printer(self.fp)
        with self._lock:
            self.display()

    # Init the time counter
    self.last_print_t = self._time()
    # NB: Avoid race conditions by setting start_t at the very end of init
    self.start_t = self.last_print_t