<commit_before># coding: UTF-8
from __future__ import unicode_literals
from ben10.filesystem import CreateFile
from ben10.foundation.print_detailed_traceback import PrintDetailedTraceback
from io import BytesIO
import pytest
import re


def testPrintDetailedTraceback(embed_data):

    def Pad(seq):
        """Pads a sequence of strings with up to 4 leading '0' chars"""
        result = []
        for value in seq:
            try:
                result.append('0' * (4 - len(value)) + value)
            except TypeError:
                # we raise our own exception because the message changes between python versions
                raise TypeError("object of type 'int' has no len()")
        return result

    data = map(unicode, xrange(100))
    data[3] = 3

    stream = BytesIO()
    try:
        Pad(data)
    except:
        PrintDetailedTraceback(max_levels=2, stream=stream, max_line_width=100)
    else:
        assert False, 'Pad() should fail with an exception!'

    ss = stream.getvalue()

    # >>> Remove parts of the string that are platform/file system dependent

    # remove filename
    filename_re = re.compile(re.escape(__file__) + '?', re.IGNORECASE)
    ss = re.sub(filename_re, '/path_to_file/file.py', ss)

    # remove address of objects
    address_re = re.compile('at 0x([a-f0-9]+)', re.IGNORECASE)
    ss = re.sub(address_re, 'at 0x0', ss)

    # "self" description because the name of the module changes if we run this test
    # from the command line vs running it with runtests
    self_re = re.compile('self = <.*>', re.IGNORECASE)
    ss = re.sub(self_re, 'self = <Test.testPrintDetailedTraceback>', ss)

    obtained_filename = embed_data['traceback.obtained.txt']
    CreateFile(obtained_filename, ss)

    # Changes
    # File "/path_to_file/file.py", line 51, in testPrintDetailedTraceback
    # to:
    # File "/path_to_file/file.py", line XX, in testPrintDetailedTraceback
    def FixIt(lines):
        import re
        return [re.sub('line (\\d)+', 'line XX', i) for i in lines]

    embed_data.AssertEqualFiles(
        obtained_filename,
        'traceback.expected.txt',
        fix_callback = FixIt
    )


def testNoException():
    '''
    Should not print anything in case there's no exception info (complies with the behavoir from
    traceback.print_exception)
    '''
    stream = BytesIO()
    PrintDetailedTraceback(exc_info=(None, None, None), stream=stream)
    assert stream.getvalue() == 'None\n'


@pytest.mark.parametrize(('exception_message',), [(u'fake unicode message',), (u'Сообщение об ошибке.',)])
def testPrintDetailedTracebackWithUnicode(exception_message):
    '''
    Test PrintDetailedTraceback with 'plain' unicode arguments and an unicode argument with cyrillic
    characters
    '''
    stream = BytesIO()
    try:
        raise Exception(exception_message)
    except:
        PrintDetailedTraceback(stream=stream)

    assert 'Exception: %s' % (exception_message) in stream.getvalue().decode('UTF-8')


@pytest.mark.parametrize(('exception_message',), [(u'fake unicode message',), (u'Сообщение об ошибке.',)])
def testPrintDetailedTracebackToStderr(exception_message):
    '''
    Test PrintDetailedTraceback with 'plain' unicode arguments and an unicode argument with cyrillic
    characters, written directly to PrintDetailedTraceback's default stream, sys.stderr.
    '''
    import sys

    # Since we want to check the values written, create a 'fake_stderr' that is simple a BytesIO
    # with the same expected encoding as sys.std_err's encoding.
    std_err_encoding = sys.stderr.encoding
    fake_stderr = BytesIO()
    try:
        raise Exception(exception_message)
    except:
        PrintDetailedTraceback(stream=fake_stderr, encoding=std_err_encoding)

    written_traceback = fake_stderr.getvalue()
    encoded_message = exception_message.encode(std_err_encoding, errors='replace')
    assert 'Exception: %s' % encoded_message in written_traceback


def testOmitLocals():
    '''
    Makes sure arguments and local variables are not present in traceback contents whenever
    omit locals option is enabled.
    '''
    stream = BytesIO()
    def Flawed(foo):
        arthur = 'dent'  # @UnusedVariable
        raise Exception('something')

    try:
        Flawed(foo='bar')
    except:
        PrintDetailedTraceback(stream=stream, omit_locals=True)

    stream_value = stream.getvalue()
    assert 'foo' not in stream_value
    assert 'bar' not in stream_value
    assert 'arthur' not in stream_value
    assert 'dent' not in stream_value<commit_msg>Add a smoke-test with the original sys.stderr<commit_after># coding: UTF-8
from __future__ import unicode_literals
from ben10.filesystem import CreateFile
from ben10.foundation.print_detailed_traceback import PrintDetailedTraceback
from io import BytesIO
import pytest
import re


def testPrintDetailedTraceback(embed_data):

    def Pad(seq):
        """Pads a sequence of strings with up to 4 leading '0' chars"""
        result = []
        for value in seq:
            try:
                result.append('0' * (4 - len(value)) + value)
            except TypeError:
                # we raise our own exception because the message changes between python versions
                raise TypeError("object of type 'int' has no len()")
        return result

    data = map(unicode, xrange(100))
    data[3] = 3

    stream = BytesIO()
    try:
        Pad(data)
    except:
        PrintDetailedTraceback(max_levels=2, stream=stream, max_line_width=100)
    else:
        assert False, 'Pad() should fail with an exception!'

    ss = stream.getvalue()

    # >>> Remove parts of the string that are platform/file system dependent

    # remove filename
    filename_re = re.compile(re.escape(__file__) + '?', re.IGNORECASE)
    ss = re.sub(filename_re, '/path_to_file/file.py', ss)

    # remove address of objects
    address_re = re.compile('at 0x([a-f0-9]+)', re.IGNORECASE)
    ss = re.sub(address_re, 'at 0x0', ss)

    # "self" description because the name of the module changes if we run this test
    # from the command line vs running it with runtests
    self_re = re.compile('self = <.*>', re.IGNORECASE)
    ss = re.sub(self_re, 'self = <Test.testPrintDetailedTraceback>', ss)

    obtained_filename = embed_data['traceback.obtained.txt']
    CreateFile(obtained_filename, ss)

    # Changes
    # File "/path_to_file/file.py", line 51, in testPrintDetailedTraceback
    # to:
    # File "/path_to_file/file.py", line XX, in testPrintDetailedTraceback
    def FixIt(lines):
        import re
        return [re.sub('line (\\d)+', 'line XX', i) for i in lines]

    embed_data.AssertEqualFiles(
        obtained_filename,
        'traceback.expected.txt',
        fix_callback = FixIt
    )


def testNoException():
    '''
    Should not print anything in case there's no exception info (complies with the behavoir from
    traceback.print_exception)
    '''
    stream = BytesIO()
    PrintDetailedTraceback(exc_info=(None, None, None), stream=stream)
    assert stream.getvalue() == 'None\n'


@pytest.mark.parametrize(('exception_message',), [(u'fake unicode message',), (u'Сообщение об ошибке.',)])
def testPrintDetailedTracebackWithUnicode(exception_message):
    '''
    Test PrintDetailedTraceback with 'plain' unicode arguments and an unicode argument with cyrillic
    characters
    '''
    stream = BytesIO()
    try:
        raise Exception(exception_message)
    except:
        PrintDetailedTraceback(stream=stream)

    assert 'Exception: %s' % (exception_message) in stream.getvalue().decode('UTF-8')


@pytest.mark.parametrize(('exception_message',), [(u'fake unicode message',), (u'Сообщение об ошибке.',)])
def testPrintDetailedTracebackToFakeStderr(exception_message):
    '''
    Test PrintDetailedTraceback with 'plain' unicode arguments and an unicode argument with cyrillic
    characters, written to a buffer similar to PrintDetailedTraceback()'s default stream, sys.std_err
    '''
    import sys

    # Since we want to check the values written, create a 'fake_stderr' that is simple a BytesIO
    # with the same expected encoding as sys.std_err's encoding.
    std_err_encoding = sys.stderr.encoding
    fake_stderr = BytesIO()
    try:
        raise Exception(exception_message)
    except:
        PrintDetailedTraceback(stream=fake_stderr, encoding=std_err_encoding)

    written_traceback = fake_stderr.getvalue()
    encoded_message = exception_message.encode(std_err_encoding, errors='replace')
    assert 'Exception: %s' % encoded_message in written_traceback


@pytest.mark.parametrize(('exception_message',), [(u'fake unicode message',), (u'Сообщение об ошибке.',)])
def testPrintDetailedTracebackToRealStderr(exception_message):
    '''
    The same as above, but writing to the real stderr. Just a smoke test to verify that no errors
    occur.
    '''
    try:
        raise Exception(exception_message)
    except:
        PrintDetailedTraceback()


def testOmitLocals():
    '''
    Makes sure arguments and local variables are not present in traceback contents whenever
    omit locals option is enabled.
    '''
    stream = BytesIO()
    def Flawed(foo):
        arthur = 'dent'  # @UnusedVariable
        raise Exception('something')

    try:
        Flawed(foo='bar')
    except:
        PrintDetailedTraceback(stream=stream, omit_locals=True)

    stream_value = stream.getvalue()
    assert 'foo' not in stream_value
    assert 'bar' not in stream_value
    assert 'arthur' not in stream_value
    assert 'dent' not in stream_value
{'fraction_non_alphanumeric': 0.09904722018636793, 'fraction_numerical': 0.005444456077897603, 'mean_word_length': 3.36563071297989, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2771235', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2830, 'n_words': 824}