Skip to content

Commit c6418b7

Browse files
committed
Nicer way of doing the get_atom alias (making the caching a little more flexible and smarter in the process)
1 parent 975f7e8 commit c6418b7

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

Xlib/display.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: display.py,v 1.21 2007-03-17 22:14:06 mggrant Exp $
1+
# $Id: display.py,v 1.22 2007-03-17 22:24:47 mggrant Exp $
22
#
33
# Xlib.display -- high level display object
44
#
@@ -64,17 +64,22 @@ class _BaseDisplay(protocol.display.Display):
6464
# dealing with some ICCCM properties not defined in Xlib.Xatom
6565

6666
def __init__(self, *args, **keys):
67-
apply(protocol.display.Display.__init__, (self, ) + args, keys)
68-
self._atom_cache = {}
67+
apply(protocol.display.Display.__init__, (self, ) + args, keys)
68+
self._atom_cache = {}
69+
70+
def get_atom(self, atomname, only_if_exists=0):
71+
if self._atom_cache.has_key(atomname):
72+
return self._atom_cache[atomname]
73+
74+
r = request.InternAtom(display = self, name = atomname, only_if_exists = only_if_exists)
75+
76+
# don't cache NONE responses in case someone creates this later
77+
if r.atom != X.NONE:
78+
self._atom_cache[atomname] = r.atom
79+
80+
return r.atom
6981

70-
def get_atom(self, atomname):
71-
if not self._atom_cache.has_key(atomname):
72-
r = request.InternAtom(display = self, name = atomname, only_if_exists = 0)
73-
self._atom_cache[atomname] = r.atom
74-
75-
return self._atom_cache[atomname]
7682

77-
7883
class Display:
7984
def __init__(self, display = None):
8085
self.display = _BaseDisplay(display)
@@ -444,15 +449,8 @@ def intern_atom(self, name, only_if_exists = 0):
444449
return r.atom
445450

446451
def get_atom(self, atom, only_if_exists = 0):
447-
"""Alias for intern_atom, using internal cache if safe to do"""
448-
if only_if_exists == 0:
449-
# cache lookup will create the atom, as the user wishes
450-
# safe to use the cache
451-
return self.display.get_atom(atom)
452-
else:
453-
# the user doesn't want the atom created if it doesn't
454-
# exist, so use intern_atom to avoid the cache creating it
455-
return self.intern_atom(atom, only_if_exists)
452+
"""Alias for intern_atom, using internal cache"""
453+
return self.display.get_atom(atom, only_if_exists)
456454

457455

458456
def get_atom_name(self, atom):

0 commit comments

Comments
 (0)