<commit_before>from collections import deque
import sys, pyuv

class UvStream(object):
    """
    Blocking read/write stream implemented with libuv. Provides the read/write
    interface required by `Client`
    """
    def __init__(self, address=None, port=None):
        self._loop = pyuv.Loop()
        self._error = None
        self._data = deque()
        self._written = True
        self._connected = False
        self._timed_out = False
        self._timer = pyuv.Timer(self._loop)
        self._timeout_cb = self._on_timeout.__get__(self, UvStream)
        self._read_cb = self._on_read.__get__(self, UvStream)
        self._write_cb = self._on_write.__get__(self, UvStream)
        connect_cb = self._on_connect.__get__(self, UvStream)
        # Select the type of handle
        if port:
            # tcp
            self._stream = pyuv.TCP(self._loop)
            self._stream.connect((address, port), connect_cb)
        elif address:
            # named pipe or unix socket
            self._stream = pyuv.Pipe(self._loop)
            self._stream.connect(address, connect_cb)
        else:
            # stdin/stdout
            self._read_stream = pyuv.Pipe(self._loop) 
            self._read_stream.open(sys.stdin.fileno())
            self._write_stream = pyuv.Pipe(self._loop) 
            self._write_stream.open(sys.stdout.fileno())
        async_cb = self._on_async.__get__(self, UvStream)
        self._async = pyuv.Async(self._loop, async_cb)
        self._interrupted = False

    """
    Called when the libuv stream is connected
    """
    def _on_connect(self, stream, error):
        if error:
            self._error = IOError(pyuv.errno.strerror(error))
            return
        self._connected = True
        self._read_stream = self._write_stream = stream
        self._read_stream.start_read(self._read_cb)

    """
    Called when data is read from the libuv stream
    """
    def _on_read(self, handle, data, error):
        if error:
            self._error = IOError(pyuv.errno.strerror(error))
            return
        elif not data:
            self._error = IOError('EOF')
            return
        else:
            self._data.append(data)
        self._loop.stop()

    """
    Called when the async handle is fired
    """
    def _on_async(self, handle):
        self._interrupted = True
        self._loop.stop()

    """
    Called when a timeout occurs
    """
    def _on_timeout(self, handle):
        self._timed_out = True
        self._loop.stop()

    """
    Called when data is written to the libuv stream
    """
    def _on_write(self, handle, error):
        if error:
            self._error = IOError(pyuv.errno.strerror(error))
            return
        self._written = True
        self._loop.stop()
    
    """
    Runs the event loop until a certain condition
    """
    def _run(self, condition=lambda: True, timeout=None):
        if timeout == 0 and not condition():
            return

        if timeout:
            self._timer.start(self._timeout_cb, timeout, 0)

        while not (condition() or self._timed_out):
            if self._error:
                if timeout and not self._timed_out:
                    self._timer.stop()
                # Error occurred, throw it to the caller
                err = self._error
                self._error = None
                raise err<commit_msg>Make sure libuv event loop is paused on all events<commit_after>from collections import deque
import sys, pyuv

class UvStream(object):
    """
    Blocking read/write stream implemented with libuv. Provides the read/write
    interface required by `Client`
    """
    def __init__(self, address=None, port=None):
        self._loop = pyuv.Loop()
        self._error = None
        self._data = deque()
        self._written = True
        self._connected = False
        self._timed_out = False
        self._timer = pyuv.Timer(self._loop)
        self._timeout_cb = self._on_timeout.__get__(self, UvStream)
        self._read_cb = self._on_read.__get__(self, UvStream)
        self._write_cb = self._on_write.__get__(self, UvStream)
        connect_cb = self._on_connect.__get__(self, UvStream)
        # Select the type of handle
        if port:
            # tcp
            self._stream = pyuv.TCP(self._loop)
            self._stream.connect((address, port), connect_cb)
        elif address:
            # named pipe or unix socket
            self._stream = pyuv.Pipe(self._loop)
            self._stream.connect(address, connect_cb)
        else:
            # stdin/stdout
            self._read_stream = pyuv.Pipe(self._loop) 
            self._read_stream.open(sys.stdin.fileno())
            self._write_stream = pyuv.Pipe(self._loop) 
            self._write_stream.open(sys.stdout.fileno())
        async_cb = self._on_async.__get__(self, UvStream)
        self._async = pyuv.Async(self._loop, async_cb)
        self._interrupted = False

    """
    Called when the libuv stream is connected
    """
    def _on_connect(self, stream, error):
        self._loop.stop()
        if error:
            self._error = IOError(pyuv.errno.strerror(error))
            return
        self._connected = True
        self._read_stream = self._write_stream = stream
        self._read_stream.start_read(self._read_cb)

    """
    Called when data is read from the libuv stream
    """
    def _on_read(self, handle, data, error):
        self._loop.stop()
        if error:
            self._error = IOError(pyuv.errno.strerror(error))
            return
        elif not data:
            self._error = IOError('EOF')
            return
        else:
            self._data.append(data)

    """
    Called when the async handle is fired
    """
    def _on_async(self, handle):
        self._interrupted = True
        self._loop.stop()

    """
    Called when a timeout occurs
    """
    def _on_timeout(self, handle):
        self._timed_out = True
        self._loop.stop()

    """
    Called when data is written to the libuv stream
    """
    def _on_write(self, handle, error):
        self._loop.stop()
        if error:
            self._error = IOError(pyuv.errno.strerror(error))
            return
        self._written = True
    
    """
    Runs the event loop until a certain condition
    """
    def _run(self, condition=lambda: True, timeout=None):
        if timeout == 0 and not condition():
            return

        if timeout:
            self._timer.start(self._timeout_cb, timeout, 0)

        while not (condition() or self._timed_out):
            if self._error:
                if timeout and not self._timed_out:
                    self._timer.stop()
                # Error occurred, throw it to the caller
                err = self._error
                self._error = None
                raise err
{'fraction_non_alphanumeric': 0.09541928874579247, 'fraction_numerical': 0.0005853944094833894, 'mean_word_length': 1.8798988621997472, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7087673', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1938, 'n_words': 485}