<commit_before>import wayland.protocol
import wayland.utils
import os
import socket
import select
import struct
import array
import io
import logging

log = logging.getLogger(__name__)

class ServerDisconnected(Exception):
    """The server disconnected unexpectedly"""
    pass

class ProtocolError(Exception):
    """The server sent data that could not be decoded"""
    pass

class UnknownObjectError(Exception):
    """The server sent an event for an object we don't know about"""
    def __init__(self, oid):
        self.oid = oid
    def __str__(self):
        return "UnknownObjectError({})".format(self.oid)

class DisplayError(Exception):
    """The server sent a fatal error event

    This error can be raised during dispatching of the default queue.
    """
    def __init__(self, obj, code, codestr, message):
        self.obj = obj
        self.code = code
        self.codestr = codestr
        self.message = message
    def __str__(self):
        return "DisplayError({}, {} (\"{}\"), {})".format(
            obj, code, codestr, message)

class Display(wayland.protocol.wayland.interfaces['wl_display'].proxy_class):
    def __init__(self, name_or_fd=None):
        self._f = None
        self._oids = iter(range(1, 0xff000000))
        self._reusable_oids = []
        self._default_queue = []
        super(Display, self).__init__(self, self.get_new_oid(),
                                      self._default_queue)
        if hasattr(name_or_fd, 'fileno'):
            self._f = name_or_fd
            log.info("connected to existing fd %d", self._f)
        else:
            xdg_runtime_dir = os.getenv('XDG_RUNTIME_DIR')
            if not xdg_runtime_dir:
                raise wayland.utils.NoXDGRuntimeDir()
            if not name_or_fd:
                display = os.getenv('WAYLAND_DISPLAY')
                if not display:
                    display = "wayland-0"
            path = os.path.join(xdg_runtime_dir, display)
            self._f = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
            self._f.connect(path)
            log.info("connected to %s", path)

        self._f.setblocking(0)

        # Partial event left from last read
        self._read_partial_event = b''
        self._incoming_fds = []

        self.objects = {1: self}
        self._send_queue = []

        self.dispatcher['delete_id'] = self._delete_id
        self.silence['delete_id'] = True
        self.dispatcher['error'] = self._error_event

    def __del__(self):
        self.disconnect()

    def connect(self):
        pass # For compatibility with pywayland

    def disconnect(self):
        if self._f:
            self._f.close()
            self._f = None

    def get_fd(self):
        return self._f.fileno()

    def get_new_oid(self):
        if self._reusable_oids:
            return self._reusable_oids.pop()
        return next(self._oids)

    def _delete_id(self, display, id_):
        log.info("deleting %s", self.objects[id_])
        self.objects[id_]._oid = None
        del self.objects[id_]
        self._reusable_oids.append(id_)

    def _error_event(self, obj, code, message):
        # XXX look up string for error code in enum
        raise DisplayError(obj, code, "", message)

    def queue_request(self, r, fds=[]):
        log.debug("queueing to send: %s with fds %s", r, fds)
        self._send_queue.append((r,fds))

    def flush(self):
        """Send buffered requests to the display server.

        Will send as many requests as possible to the display server.
        Will not block; if sendmsg() would block, will leave events in
        the queue.

        Returns True if the queue was emptied.
        """
        while self._send_queue:
            b, fds = self._send_queue.pop(0)
            try:<commit_msg>Don't re-use server-allocated oids<commit_after>import wayland.protocol
import wayland.utils
import os
import socket
import select
import struct
import array
import io
import logging

log = logging.getLogger(__name__)

class ServerDisconnected(Exception):
    """The server disconnected unexpectedly"""
    pass

class ProtocolError(Exception):
    """The server sent data that could not be decoded"""
    pass

class UnknownObjectError(Exception):
    """The server sent an event for an object we don't know about"""
    def __init__(self, oid):
        self.oid = oid
    def __str__(self):
        return "UnknownObjectError({})".format(self.oid)

class DisplayError(Exception):
    """The server sent a fatal error event

    This error can be raised during dispatching of the default queue.
    """
    def __init__(self, obj, code, codestr, message):
        self.obj = obj
        self.code = code
        self.codestr = codestr
        self.message = message
    def __str__(self):
        return "DisplayError({}, {} (\"{}\"), {})".format(
            obj, code, codestr, message)

class Display(wayland.protocol.wayland.interfaces['wl_display'].proxy_class):
    def __init__(self, name_or_fd=None):
        self._f = None
        self._oids = iter(range(1, 0xff000000))
        self._reusable_oids = []
        self._default_queue = []
        super(Display, self).__init__(self, self.get_new_oid(),
                                      self._default_queue)
        if hasattr(name_or_fd, 'fileno'):
            self._f = name_or_fd
            log.info("connected to existing fd %d", self._f)
        else:
            xdg_runtime_dir = os.getenv('XDG_RUNTIME_DIR')
            if not xdg_runtime_dir:
                raise wayland.utils.NoXDGRuntimeDir()
            if not name_or_fd:
                display = os.getenv('WAYLAND_DISPLAY')
                if not display:
                    display = "wayland-0"
            path = os.path.join(xdg_runtime_dir, display)
            self._f = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
            self._f.connect(path)
            log.info("connected to %s", path)

        self._f.setblocking(0)

        # Partial event left from last read
        self._read_partial_event = b''
        self._incoming_fds = []

        self.objects = {1: self}
        self._send_queue = []

        self.dispatcher['delete_id'] = self._delete_id
        self.silence['delete_id'] = True
        self.dispatcher['error'] = self._error_event

    def __del__(self):
        self.disconnect()

    def connect(self):
        pass # For compatibility with pywayland

    def disconnect(self):
        if self._f:
            self._f.close()
            self._f = None

    def get_fd(self):
        return self._f.fileno()

    def get_new_oid(self):
        if self._reusable_oids:
            return self._reusable_oids.pop()
        return next(self._oids)

    def _delete_id(self, display, id_):
        log.info("deleting %s", self.objects[id_])
        self.objects[id_]._oid = None
        del self.objects[id_]
        if id_ < 0xff000000:
            self._reusable_oids.append(id_)

    def _error_event(self, obj, code, message):
        # XXX look up string for error code in enum
        raise DisplayError(obj, code, "", message)

    def queue_request(self, r, fds=[]):
        log.debug("queueing to send: %s with fds %s", r, fds)
        self._send_queue.append((r,fds))

    def flush(self):
        """Send buffered requests to the display server.

        Will send as many requests as possible to the display server.
        Will not block; if sendmsg() would block, will leave events in
        the queue.

        Returns True if the queue was emptied.
        """
        while self._send_queue:
            b, fds = self._send_queue.pop(0)
            try:
{'fraction_non_alphanumeric': 0.10419397116644823, 'fraction_numerical': 0.004325032765399738, 'mean_word_length': 2.2389643463497455, 'pattern_counts': {'":': 0, '<': 4, '<?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': '1155446', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2218, 'n_words': 607}