<commit_before>"""
abstraction layer over OS-depenedent byte streams
"""
import sys
import os
import socket
import time
import errno
from rpyc.lib import safe_import
from rpyc.lib.compat import select
win32file = safe_import("win32file")
win32pipe = safe_import("win32pipe")
msvcrt = safe_import("msvcrt")
ssl = safe_import("ssl")
tlsapi = safe_import("tlslite.api")


retry_errnos = set([errno.EAGAIN])
if hasattr(errno, "WSAEWOULDBLOCK"):
    retry_errnos.add(errno.WSAEWOULDBLOCK)


class Stream(object):
    __slots__ = ()
    def close(self):
        raise NotImplementedError()
    @property
    def closed(self):
        raise NotImplementedError()
    def fileno(self):
        raise NotImplementedError()
    def poll(self, timeout):
        """indicate whether the stream has data to read"""
        rl, wl, xl = select([self], [], [], timeout)
        return bool(rl)
    def read(self, count):
        """read exactly `count` bytes, or raise EOFError"""
        raise NotImplementedError()
    def write(self, data):
        """write the entire `data`, or raise EOFError"""
        raise NotImplementedError()


class ClosedFile(object):
    """represents a closed file object (singleton)"""
    __slots__ = ()
    def __getattr__(self, name):
        raise EOFError("stream has been closed")
    def close(self):
        pass
    @property
    def closed(self):
        return True
    def fileno(self):
        raise EOFError("stream has been closed")
ClosedFile = ClosedFile()


class SocketStream(Stream):
    __slots__ = ("sock",)
    MAX_IO_CHUNK = 8000
    def __init__(self, sock):
        self.sock = sock
    @classmethod
    def _connect(cls, host, port, family = socket.AF_INET, type = socket.SOCK_STREAM, 
            proto = 0, timeout = 3):
        s = socket.socket(family, type, proto)
        s.settimeout(timeout)
        s.connect((host, port))
        return s
    @classmethod
    def connect(cls, host, port, **kwargs):
        return cls(cls._connect(host, port, **kwargs))
    @classmethod
    def tlslite_connect(cls, host, port, username, password, **kwargs):
        s = cls._connect(host, port, **kwargs)
        s2 = tlsapi.TLSConnection(s)
        s2.fileno = lambda fd=s.fileno(): fd
        s2.handshakeClientSRP(username, password)
        return cls(s2)
    @classmethod
    def ssl_connect(cls, host, port, ssl_kwargs):
        s = cls._connect(host, port)<commit_msg>added TCP_NODELAY option, closed #6<commit_after>"""
abstraction layer over OS-depenedent byte streams
"""
import sys
import os
import socket
import time
import errno
from rpyc.lib import safe_import
from rpyc.lib.compat import select
win32file = safe_import("win32file")
win32pipe = safe_import("win32pipe")
msvcrt = safe_import("msvcrt")
ssl = safe_import("ssl")
tlsapi = safe_import("tlslite.api")


retry_errnos = set([errno.EAGAIN])
if hasattr(errno, "WSAEWOULDBLOCK"):
    retry_errnos.add(errno.WSAEWOULDBLOCK)


class Stream(object):
    __slots__ = ()
    def close(self):
        raise NotImplementedError()
    @property
    def closed(self):
        raise NotImplementedError()
    def fileno(self):
        raise NotImplementedError()
    def poll(self, timeout):
        """indicate whether the stream has data to read"""
        rl, wl, xl = select([self], [], [], timeout)
        return bool(rl)
    def read(self, count):
        """read exactly `count` bytes, or raise EOFError"""
        raise NotImplementedError()
    def write(self, data):
        """write the entire `data`, or raise EOFError"""
        raise NotImplementedError()


class ClosedFile(object):
    """represents a closed file object (singleton)"""
    __slots__ = ()
    def __getattr__(self, name):
        raise EOFError("stream has been closed")
    def close(self):
        pass
    @property
    def closed(self):
        return True
    def fileno(self):
        raise EOFError("stream has been closed")
ClosedFile = ClosedFile()


class SocketStream(Stream):
    __slots__ = ("sock",)
    MAX_IO_CHUNK = 8000
    def __init__(self, sock):
        self.sock = sock
    @classmethod
    def _connect(cls, host, port, family = socket.AF_INET, type = socket.SOCK_STREAM, 
            proto = 0, timeout = 3, nodelay = False):
        s = socket.socket(family, type, proto)
        s.settimeout(timeout)
        s.connect((host, port))
        if nodelay:
            s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        return s
    @classmethod
    def connect(cls, host, port, **kwargs):
        return cls(cls._connect(host, port, **kwargs))
    @classmethod
    def tlslite_connect(cls, host, port, username, password, **kwargs):
        s = cls._connect(host, port, **kwargs)
        s2 = tlsapi.TLSConnection(s)
        s2.fileno = lambda fd=s.fileno(): fd
        s2.handshakeClientSRP(username, password)
        return cls(s2)
    @classmethod
    def ssl_connect(cls, host, port, ssl_kwargs, **kwargs):
        s = cls._connect(host, port, **kwargs)
{'fraction_non_alphanumeric': 0.11809600321349668, 'fraction_numerical': 0.007632054629443663, 'mean_word_length': 3.081967213114754, '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': '3905992', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1564, 'n_words': 426}