Commit ce507f4e authored by Kirill Smelkov's avatar Kirill Smelkov

golang: Provide __pystr internally

To convert an object to str of current python.
It will be handy to use __pystr when implementing __str__ methods.

/reviewed-on !17
parent 4690460b
# cython: language_level=2 # cython: language_level=2
# Copyright (C) 2019-2020 Nexedi SA and Contributors. # Copyright (C) 2019-2022 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com> # Kirill Smelkov <kirr@nexedi.com>
# #
# This program is free software: you can Use, Study, Modify and Redistribute # This program is free software: you can Use, Study, Modify and Redistribute
...@@ -256,3 +256,6 @@ cdef class pyerror(Exception): ...@@ -256,3 +256,6 @@ cdef class pyerror(Exception):
# from_error(nil) -> returns None. # from_error(nil) -> returns None.
@staticmethod @staticmethod
cdef object from_error (error err) # -> pyerror | None cdef object from_error (error err) # -> pyerror | None
cdef __pystr(object obj)
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# distutils: language = c++ # distutils: language = c++
# distutils: depends = libgolang.h # distutils: depends = libgolang.h
# #
# Copyright (C) 2018-2020 Nexedi SA and Contributors. # Copyright (C) 2018-2022 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com> # Kirill Smelkov <kirr@nexedi.com>
# #
# This program is free software: you can Use, Study, Modify and Redistribute # This program is free software: you can Use, Study, Modify and Redistribute
...@@ -878,6 +878,25 @@ cdef class _pyunicode(unicode): ...@@ -878,6 +878,25 @@ cdef class _pyunicode(unicode):
return pyb(self) return pyb(self)
# __pystr converts obj to str of current python:
#
# - to bytes, via b, if running on py2, or
# - to unicode, via u, if running on py3.
#
# It is handy to use __pystr when implementing __str__ methods.
#
# NOTE __pystr is currently considered to be internal function and should not
# be used by code outside of pygolang.
#
# XXX we should be able to use _pystr, but py3's str verify that it must have
# Py_TPFLAGS_UNICODE_SUBCLASS in its type flags.
cdef __pystr(object obj):
if PY_MAJOR_VERSION >= 3:
return pyu(obj)
else:
return pyb(obj)
# ---- error ---- # ---- error ----
from golang cimport errors from golang cimport errors
......
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