# Buggy code
def _text_circuit_drawer(
    circuit,
    filename=None,
    reverse_bits=False,
    plot_barriers=True,
    justify=None,
    vertical_compression="high",
    idle_wires=True,
    with_layout=True,
    fold=None,
    initial_state=True,
    cregbundle=False,
):
    """Draws a circuit using ascii art.

    Args:
        circuit (QuantumCircuit): Input circuit
        filename (str): optional filename to write the result
        reverse_bits (bool): Rearrange the bits in reverse order.
        plot_barriers (bool): Draws the barriers when they are there.
        justify (str) : `left`, `right` or `none`. Defaults to `left`. Says how
                        the circuit should be justified.
        vertical_compression (string): `high`, `medium`, or `low`. It merges the
            lines so the drawing will take less vertical room. Default is `high`.
        idle_wires (bool): Include idle wires. Default is True.
        with_layout (bool): Include layout information, with labels on the physical
            layout. Default: True
        fold (int): Optional. Breaks the circuit drawing to this length. This
                    useful when the drawing does not fit in the console. If
                    None (default), it will try to guess the console width using
                    `shutil.get_terminal_size()`. If you don't want pagination
                   at all, set `fold=-1`.
        initial_state (bool): Optional. Adds |0> in the beginning of the line. Default: `True`.
        cregbundle (bool): Optional. If set True bundle classical registers. Only used by
            the ``text`` output. Default: ``False``.
    Returns:
        TextDrawing: An instances that, when printed, draws the circuit in ascii art.
    """
    qregs, cregs, ops = utils._get_layered_instructions(
        circuit, reverse_bits=reverse_bits, justify=justify, idle_wires=idle_wires
    )
    if with_layout:
        layout = circuit._layout
    else:
        layout = None
    global_phase = circuit.global_phase if hasattr(circuit, "global_phase") else None
    text_drawing = _text.TextDrawing(
        qregs,
        cregs,
        ops,
        layout=layout,
        initial_state=initial_state,
        cregbundle=cregbundle,
        global_phase=global_phase,
    )
    text_drawing.plotbarriers = plot_barriers
    text_drawing.line_length = fold
    text_drawing.vertical_compression = vertical_compression

    if filename:
        text_drawing.dump(filename)
    return text_drawing


# Traceback (AssertionError)
Traceback (most recent call last):'
File "C:\\projects\\qiskit-sdk-py\\test\\python\\test_examples.py", line 50, in test_all_examples'
self.assertEqual(run_example.returncode, 0, error_string)'
File "C:\\Python37\\lib\\unittest\\case.py", line 839, in assertEqual'
assertion_func(first, second, msg=msg)'
File "C:\\Python37\\lib\\unittest\\case.py", line 832, in _baseAssertEqual'
raise self.failureException(msg)'
AssertionError: 1 != 0 : Running example circuit_draw.py failed with return code 1'
'stdout:'
'stderr: Traceback (most recent call last):\r'
File "C:\\projects\\qiskit-sdk-py\\examples\\python\\circuit_draw.py", line 34, in <module>\r'
print(bell_circuit)\r'
File "C:\\Python37\\lib\\encodings\\cp1252.py", line 19, in encode\r'
return codecs.charmap_encode(input,self.errors,encoding_table)[0]\r'
UnicodeEncodeError: 'charmap' codec can't encode characters in position 8-12: character maps to <undefined>\r"

# Fixed code
def _text_circuit_drawer(
    circuit,
    filename=None,
    reverse_bits=False,
    plot_barriers=True,
    justify=None,
    vertical_compression="high",
    idle_wires=True,
    with_layout=True,
    fold=None,
    initial_state=True,
    cregbundle=False,
    encoding=None,
):
    """Draws a circuit using ascii art.

    Args:
        circuit (QuantumCircuit): Input circuit
        filename (str): optional filename to write the result
        reverse_bits (bool): Rearrange the bits in reverse order.
        plot_barriers (bool): Draws the barriers when they are there.
        justify (str) : `left`, `right` or `none`. Defaults to `left`. Says how
                        the circuit should be justified.
        vertical_compression (string): `high`, `medium`, or `low`. It merges the
            lines so the drawing will take less vertical room. Default is `high`.
        idle_wires (bool): Include idle wires. Default is True.
        with_layout (bool): Include layout information, with labels on the physical
            layout. Default: True
        fold (int): Optional. Breaks the circuit drawing to this length. This
                    useful when the drawing does not fit in the console. If
                    None (default), it will try to guess the console width using
                    `shutil.get_terminal_size()`. If you don't want pagination
                   at all, set `fold=-1`.
        initial_state (bool): Optional. Adds |0> in the beginning of the line. Default: `True`.
        cregbundle (bool): Optional. If set True bundle classical registers. Default: ``False``.
        encoding (str): Optional. Sets the encoding preference of the output.
                   Default: ``sys.stdout.encoding``.
    Returns:
        TextDrawing: An instances that, when printed, draws the circuit in ascii art.
    """
    qregs, cregs, ops = utils._get_layered_instructions(
        circuit, reverse_bits=reverse_bits, justify=justify, idle_wires=idle_wires
    )
    if with_layout:
        layout = circuit._layout
    else:
        layout = None
    global_phase = circuit.global_phase if hasattr(circuit, "global_phase") else None
    text_drawing = _text.TextDrawing(
        qregs,
        cregs,
        ops,
        layout=layout,
        initial_state=initial_state,
        cregbundle=cregbundle,
        global_phase=global_phase,
        encoding=encoding,
    )
    text_drawing.plotbarriers = plot_barriers
    text_drawing.line_length = fold
    text_drawing.vertical_compression = vertical_compression

    if filename:
        text_drawing.dump(filename, encoding=encoding)
    return text_drawing