Commit 85a1765d authored by Kirill Smelkov's avatar Kirill Smelkov

golang: Make qq to always return str type of current python

i.e. unicode on py3 and bytes on py2. This makes it more likely to work
for

	"format string %s ..." % qq(object)

with format string being str and object of arbitrary type.

However more on this topic in the next patch.
parent 5251a74c
...@@ -827,10 +827,12 @@ def pyqq(obj): ...@@ -827,10 +827,12 @@ def pyqq(obj):
qobj = pystrconv.quote(obj) qobj = pystrconv.quote(obj)
# `printf('%s', qq(obj))` should work. For this make sure qobj is always a # `printf('%s', qq(obj))` should work. For this make sure qobj is always
# str - not bytes under py3 (if it was bytes it will print e.g. as b'...') # of str type (unicode on py3, bytes on py2).
if PY_MAJOR_VERSION >= 3: if PY_MAJOR_VERSION >= 3:
qobj = pyu(qobj) qobj = pyu(qobj)
else:
qobj = pyb(qobj)
return qobj return qobj
......
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2019 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
# qq is tested as part of strconv.
from __future__ import print_function, absolute_import
...@@ -22,6 +22,7 @@ from __future__ import print_function, absolute_import ...@@ -22,6 +22,7 @@ from __future__ import print_function, absolute_import
from golang import go, chan, select, default, nilchan, _PanicError, func, panic, \ from golang import go, chan, select, default, nilchan, _PanicError, func, panic, \
defer, recover, u, b defer, recover, u, b
from golang.gcompat import qq
from golang import sync from golang import sync
from golang.strconv_test import byterange from golang.strconv_test import byterange
from pytest import raises, mark, fail from pytest import raises, mark, fail
...@@ -1652,6 +1653,14 @@ def test_strings(): ...@@ -1652,6 +1653,14 @@ def test_strings():
assert u(_) is _ assert u(_) is _
def test_qq():
# NOTE qq is also tested as part of strconv.quote
# qq(any) returns string type
assert isinstance(qq(b('мир')), str) # qq(b) -> str (bytes·py2, unicode·py3)
assert isinstance(qq( u'мир'), str) # qq(u) -> str (bytes·py2, unicode·py3)
# ---- misc ---- # ---- misc ----
# _pyrun runs `sys.executable argv... <stdin`. # _pyrun runs `sys.executable argv... <stdin`.
......
...@@ -34,9 +34,15 @@ def byterange(start, stop): ...@@ -34,9 +34,15 @@ def byterange(start, stop):
return b return b
# asstr converts unicode|bytes to str type of current python.
def asstr(s): def asstr(s):
if PY3 and isinstance(s, bytes): if PY3:
if isinstance(s, bytes):
s = s.decode('utf-8') s = s.decode('utf-8')
# PY2
else:
if isinstance(s, unicode):
s = s.encode('utf-8')
return s return s
def test_quote(): def test_quote():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment