Commit 8c459a99 authored by Kirill Smelkov's avatar Kirill Smelkov

gcompat: Move qq into golang

This will allow to integrate qq with u in the next patch.

Moving to compiled code for string processing functions is also
generally better for performance.
parent bcb95cd5
...@@ -770,6 +770,7 @@ cdef DType parse_dtype(dtype) except <DType>-1: ...@@ -770,6 +770,7 @@ cdef DType parse_dtype(dtype) except <DType>-1:
# ---- strings ---- # ---- strings ----
from golang import strconv as pystrconv from golang import strconv as pystrconv
import six
def pyb(s): # -> bytes def pyb(s): # -> bytes
"""b converts str/unicode/bytes s to UTF-8 encoded bytestring. """b converts str/unicode/bytes s to UTF-8 encoded bytestring.
...@@ -810,3 +811,24 @@ def pyu(s): # -> unicode ...@@ -810,3 +811,24 @@ def pyu(s): # -> unicode
""" """
us, _ = pystrconv._ustr(s) us, _ = pystrconv._ustr(s)
return us return us
# qq is substitute for %q, which is missing in python.
#
# (python's automatic escape uses smartquotes quoting with either ' or ").
#
# like %s, %q automatically converts its argument to string.
def pyqq(obj):
# make sure obj is text | bytes
# py2: unicode | str
# py3: str | bytes
if not isinstance(obj, (six.text_type, six.binary_type)):
obj = str(obj)
qobj = pystrconv.quote(obj)
# `printf('%s', qq(obj))` should work. For this make sure qobj is always a
# str - not bytes under py3 (if it was bytes it will print e.g. as b'...')
if six.PY3 and isinstance(qobj, bytes):
qobj = qobj.decode('UTF-8') # TODO use u
return qobj
...@@ -21,26 +21,5 @@ ...@@ -21,26 +21,5 @@
from __future__ import print_function, absolute_import from __future__ import print_function, absolute_import
from golang import strconv # TODO deprecate qq in favour of -> fmt.Sprintf("%q") ?
import six from golang._golang import pyqq as qq
# qq is substitute for %q, which is missing in python.
#
# (python's automatic escape uses smartquotes quoting with either ' or ").
#
# like %s, %q automatically converts its argument to string.
def qq(obj):
# make sure obj is text | bytes
# py2: unicode | str
# py3: str | bytes
if not isinstance(obj, (six.text_type, six.binary_type)):
obj = str(obj)
qobj = strconv.quote(obj)
# `printf('%s', qq(obj))` should work. For this make sure qobj is always a
# str - not bytes under py3 (if it was bytes it will print e.g. as b'...')
if six.PY3 and isinstance(qobj, bytes):
qobj = qobj.decode('UTF-8') # TODO use u
return qobj
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