Skip to content

Commit

Permalink
move string gymnastics to a re-usable function (#3229)
Browse files Browse the repository at this point in the history
  • Loading branch information
totaam committed Aug 31, 2021
1 parent 889e6b7 commit 0c7d238
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
15 changes: 5 additions & 10 deletions xpra/client/client_window_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

from xpra.client.client_widget_base import ClientWidgetBase
from xpra.client.window_backing_base import fire_paint_callbacks
from xpra.os_util import bytestostr, strtobytes, OSX, WIN32, is_Wayland
from xpra.os_util import bytestostr, OSX, WIN32, is_Wayland
from xpra.common import GRAVITY_STR
from xpra.util import u, typedict, envbool, envint, WORKSPACE_UNSET, WORKSPACE_NAMES
from xpra.util import net_utf8, typedict, envbool, envint, WORKSPACE_UNSET, WORKSPACE_NAMES
from xpra.log import Logger

log = Logger("window")
Expand Down Expand Up @@ -301,12 +301,7 @@ def getvar(var):
value = metadata.get(var) or self._metadata.get(var)
if value is None:
return default_values.get(var, "<unknown %s>" % var)
#with 'rencodeplus', we just get the unicode string:
if isinstance(value, str):
return value
#with rencode or bencode, we have to decode the value:
#(after converting it to 'bytes' if necessary)
return u(strtobytes(value))
return net_utf8(value)
def metadata_replace(match):
atvar = match.group(0) #ie: '@title@'
var = atvar[1:len(atvar)-1] #ie: 'title'
Expand Down Expand Up @@ -339,8 +334,8 @@ def set_metadata(self, metadata):
self.set_title(title)

if "icon-title" in metadata:
icon_title = metadata.strget("icon-title")
self.set_icon_name(icon_title)
icon_title = metadata.strget("icon-title", "")
self.set_icon_name(net_utf8(icon_title))
#the DE may have reset the icon now,
#force it to use the one we really want:
self.reset_icon()
Expand Down
15 changes: 15 additions & 0 deletions xpra/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ def noerr(fn, *args):
return None


def net_utf8(value):
"""
Given a value received by the network layer,
convert it to a string.
Gymnastics are involved if the rencode packet encoder is used
as it ends up giving us a string which is actually utf8 bytes.
"""
#with 'rencodeplus' or 'bencode', we just get the unicode string directly:
if isinstance(value, str):
return value
#with rencode v1, we have to decode the value:
#(after converting it to 'bytes' if necessary)
return u(strtobytes(value))


def u(v):
if isinstance(v, str):
return v
Expand Down

0 comments on commit 0c7d238

Please sign in to comment.