Skip to content

Commit ae24d52

Browse files
Merge pull request #6417 from hexagonrecursion/bp11-unicode
1.1-maint: ‘PyUnicode_AsUnicode’ is deprecated
2 parents a146f11 + e5e0da0 commit ae24d52

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/borg/platform/posix.pyx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@ import errno
44
import os
55

66

7+
from cpython.mem cimport PyMem_Free
8+
from libc.stddef cimport wchar_t
9+
710
cdef extern from "wchar.h":
8-
cdef int wcswidth(const Py_UNICODE *str, size_t n)
11+
# https://www.man7.org/linux/man-pages/man3/wcswidth.3.html
12+
cdef int wcswidth(const wchar_t *s, size_t n)
13+
14+
15+
cdef extern from "Python.h":
16+
# https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_AsWideCharString
17+
wchar_t* PyUnicode_AsWideCharString(object, Py_ssize_t*) except NULL
918

1019

1120
def swidth(s):
12-
str_len = len(s)
13-
terminal_width = wcswidth(s, str_len)
21+
cdef Py_ssize_t size
22+
cdef wchar_t *as_wchar = PyUnicode_AsWideCharString(s, &size)
23+
terminal_width = wcswidth(as_wchar, <size_t>size)
24+
PyMem_Free(as_wchar)
1425
if terminal_width >= 0:
1526
return terminal_width
1627
else:
17-
return str_len
28+
return len(s)
1829

1930

2031
def process_alive(host, pid, thread):

0 commit comments

Comments
 (0)