# Path: botoflow/core/async_context.py
# @classmethod
# def get_async_context(cls):
#     context = cls.thread_local.async_current_context
#     if DEBUG:
#         log.debug("Current async context: %r", context)
#     return context
# 
# Path: botoflow/core/utils.py
# def get_context_with_traceback(context):
#     """
#     Returns the very first context that contains traceback information
#     """
#     if context.tb_list:
#         return context
#     if context.parent:
#         return get_context_with_traceback(context.parent)


import sys
import traceback
from .async_context import get_async_context
from .utils import get_context_with_traceback
# permissions and limitations under the License.

"""
This module simulates some functions from the original traceback module
specifically for printing tracebacks. It tidies up the traceback, hiding the
underlying asynchronous framework, so the tracebacks are much more readable.
"""



def print_exc(limit=None, file=None):
    """
    Print exception information and up to limit stack trace entries to file
    """
    if file is None:
        file = sys.stderr

    formatted_exc = format_exc(limit)
    if formatted_exc is not None:
        for line in formatted_exc:
            file.write(line)


def format_exc(limit=None, exception=None, tb_list=None):
    """
    This is like print_exc(limit) but returns a string instead of printing to a
    file.
    """
    result = ["Traceback (most recent call last):\n"]
    if exception is None:

# Path: botoflow/core/async_context.py
# @classmethod
# def get_async_context(cls):
#     context = cls.thread_local.async_current_context
#     if DEBUG:
#         log.debug("Current async context: %r", context)
#     return context
# 
# Path: botoflow/core/utils.py
# def get_context_with_traceback(context):
#     """
#     Returns the very first context that contains traceback information
#     """
#     if context.tb_list:
#         return context
#     if context.parent:
#         return get_context_with_traceback(context.parent)


# Path: botoflow/core/async_traceback.py
import sys
import traceback
from .async_context import get_async_context
from .utils import get_context_with_traceback

# permissions and limitations under the License.

"""
This module simulates some functions from the original traceback module
specifically for printing tracebacks. It tidies up the traceback, hiding the
underlying asynchronous framework, so the tracebacks are much more readable.
"""



def print_exc(limit=None, file=None):
    """
    Print exception information and up to limit stack trace entries to file
    """
    if file is None:
        file = sys.stderr

    formatted_exc = format_exc(limit)
    if formatted_exc is not None:
        for line in formatted_exc:
            file.write(line)


def format_exc(limit=None, exception=None, tb_list=None):
    """
    This is like print_exc(limit) but returns a string instead of printing to a
    file.
    """
    result = ["Traceback (most recent call last):\n"]
    if exception is None:

        exception = get_context_with_traceback(get_async_context()).exception