<commit_before>from __future__ import absolute_import
from . import MetaKernel
from .pexpect import EOF
from .replwrap import REPLWrapper, u, PY3
from subprocess import check_output
import os
import re

__version__ = '0.0'

version_pat = re.compile(r'version (\d+(\.\d+)+)')


class ProcessMetaKernel(MetaKernel):
    implementation = 'process_kernel'
    implementation_version = __version__
    language = 'process'
    language_info = {
        # 'mimetype': 'text/x-python',
        # 'language': 'python',
        # ------ If different from 'language':
        # 'codemirror_mode': 'language',
        # 'pygments_lexer': 'language',
        # 'file_extension': 'py',
    }

    @property
    def language_version(self):
        m = version_pat.search(self.banner)
        return m.group(1)

    _banner = "Process"

    @property
    def banner(self):
        return self._banner

    def __init__(self, **kwargs):
        MetaKernel.__init__(self, **kwargs)
        self.wrapper = None
        self.repr = str if PY3 else lambda x: x.encode('utf-8')
        self._start()

    def _start(self):
        if not self.wrapper is None:
            self.wrapper.child.terminate()
        self.wrapper = self.makeWrapper()

    def do_execute_direct(self, code, stream_handler=None):
        """Execute the code in the subprocess.
        Use the stream_handler to process lines as they are emitted
        by the subprocess.
        """
        self.payload = []

        if not code.strip():
            self.kernel_resp = {'status': 'ok',
                            'execution_count': self.execution_count,
                            'payload': [], 'user_expressions': {}}
            return

        interrupted = False
        output = ''
        try:
            self.wrapper.run_command(code.rstrip(), timeout=None,
                                     stream_handler=stream_handler)
        except KeyboardInterrupt as e:
            interrupted = True
            output = self.wrapper.child.before
            if 'REPL not responding to interrupt' in str(e):
                output += '\n%s' % e
        except EOF:
            output = self.wrapper.child.before + 'Restarting'
            self._start()

        if interrupted:
            self.kernel_resp = {'status': 'abort',
                            'execution_count': self.execution_count}

        exitcode, trace = self.check_exitcode()<commit_msg>Fix handling of output without stream handler<commit_after>from __future__ import absolute_import
from . import MetaKernel
from .pexpect import EOF
from .replwrap import REPLWrapper, u, PY3
from subprocess import check_output
import os
import re

__version__ = '0.0'

version_pat = re.compile(r'version (\d+(\.\d+)+)')


class ProcessMetaKernel(MetaKernel):
    implementation = 'process_kernel'
    implementation_version = __version__
    language = 'process'
    language_info = {
        # 'mimetype': 'text/x-python',
        # 'language': 'python',
        # ------ If different from 'language':
        # 'codemirror_mode': 'language',
        # 'pygments_lexer': 'language',
        # 'file_extension': 'py',
    }

    @property
    def language_version(self):
        m = version_pat.search(self.banner)
        return m.group(1)

    _banner = "Process"

    @property
    def banner(self):
        return self._banner

    def __init__(self, **kwargs):
        MetaKernel.__init__(self, **kwargs)
        self.wrapper = None
        self.repr = str if PY3 else lambda x: x.encode('utf-8')
        self._start()

    def _start(self):
        if not self.wrapper is None:
            self.wrapper.child.terminate()
        self.wrapper = self.makeWrapper()

    def do_execute_direct(self, code, stream_handler=None):
        """Execute the code in the subprocess.
        Use the stream_handler to process lines as they are emitted
        by the subprocess.
        """
        self.payload = []

        if not code.strip():
            self.kernel_resp = {'status': 'ok',
                            'execution_count': self.execution_count,
                            'payload': [], 'user_expressions': {}}
            return

        interrupted = False
        output = ''
        try:
            output = self.wrapper.run_command(code.rstrip(), timeout=None,
                                     stream_handler=stream_handler)
            if stream_handler is not None:
                output = ''
        except KeyboardInterrupt as e:
            interrupted = True
            output = self.wrapper.child.before
            if 'REPL not responding to interrupt' in str(e):
                output += '\n%s' % e
        except EOF:
            output = self.wrapper.child.before + 'Restarting'
            self._start()

        if interrupted:
            self.kernel_resp = {'status': 'abort',
                            'execution_count': self.execution_count}

        exitcode, trace = self.check_exitcode()
{'fraction_non_alphanumeric': 0.10714285714285714, 'fraction_numerical': 0.002421307506053269, 'mean_word_length': 1.9879445449065702, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3723861', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1357, 'n_words': 352}