def intern_atom(self, name, only_if_exists=True): """ Create a new X11 atom with the given ``name``. ``name`` is a byte or unicode string with the name of the atom. If ``only_if_exists`` is ``True``, the atom is only created, if it already exists. If it does not exist, ``None`` is returned. Return an :class:`Atom` with the given ``name``, or ``None``, if the ``only_if_exists`` was ``True`` and the atom did not exist. """ name = ensure_byte_string(name) atom = self._atom_cache.get(name) if atom: return atom atom = xlib.intern_atom(self, ensure_byte_string(name), only_if_exists) if atom == xlib.NONE: return None return self._atom_cache.setdefault(name, Atom(self, atom))
def from_name(cls, name=None): """ Connect to the display with the given ``name``. ``name`` is a byte or unicode string containing the name of a display (e.g. ``':0'``). If ``name`` is ``None``, the value of ``$DISPLAY`` is used. If ``name`` refers to a non-existing display, or if ``name`` is empty and ``$DISPLAY`` is empty or refers to a non-existing display, the display connection fails, and :exc:`DisplayError` is raised. Return a :class:`Display` object representing the connection to the display. Raise :exc:`DisplayError`, if no display could be opened. """ if name is not None: name = ensure_byte_string(name) return cls(xlib.open_display(name))
def __str__(self): return ensure_byte_string(self.name)
def test_ensure_byte_string(): assert isinstance(util.ensure_byte_string('foo'), bytes) assert util.ensure_byte_string('foo') == b'foo' s = b'foo' assert util.ensure_byte_string(s) is s