Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/borg/platform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from .base import ENOATTR, API_VERSION
from .base import SaveFile, sync_dir, fdatasync, safe_fadvise
from .base import get_process_id, fqdn, hostname, hostid
from .base import get_process_id, fqdn, hostname, hostid, swidth

# work around pyinstaller "forgetting" to include the xattr module
from . import xattr # noqa: F401
Expand All @@ -24,7 +24,6 @@
from .linux import set_flags, get_flags
from .linux import SyncFile
from .posix import process_alive, local_pid_alive
from .posix import swidth
from .posix import get_errno
from .posix import getosusername
from . import posix_ug as platform_ug
Expand All @@ -36,7 +35,6 @@
from .base import get_flags
from .base import SyncFile
from .posix import process_alive, local_pid_alive
from .posix import swidth
from .posix import get_errno
from .posix import getosusername
from . import posix_ug as platform_ug
Expand All @@ -47,7 +45,6 @@
from .base import set_flags, get_flags
from .base import SyncFile
from .posix import process_alive, local_pid_alive
from .posix import swidth
from .posix import get_errno
from .posix import getosusername
from . import posix_ug as platform_ug
Expand All @@ -60,7 +57,6 @@
from .base import get_flags
from .base import SyncFile
from .posix import process_alive, local_pid_alive
from .posix import swidth
from .posix import get_errno
from .posix import getosusername
from . import posix_ug as platform_ug
Expand All @@ -72,7 +68,6 @@
from .base import set_flags, get_flags
from .base import SyncFile
from .posix import process_alive, local_pid_alive
from .posix import swidth
from .posix import get_errno
from .posix import getosusername
from . import posix_ug as platform_ug
Expand All @@ -84,7 +79,6 @@
from .base import set_flags, get_flags
from .base import SyncFile
from .windows import process_alive, local_pid_alive
from .base import swidth
from .windows import getosusername
from . import windows_ug as platform_ug

Expand Down
16 changes: 15 additions & 1 deletion src/borg/platform/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import errno
import os
import socket
import unicodedata
import uuid
from pathlib import Path

Expand Down Expand Up @@ -266,7 +267,20 @@ def swidth(s):

For western scripts, this is just len(s), but for cjk glyphs, 2 cells are used.
"""
return len(s)
width = 0
for char in s:
# Get the East Asian Width property
ea_width = unicodedata.east_asian_width(char)

# Wide (W) and Fullwidth (F) characters take 2 cells
if ea_width in ("W", "F"):
width += 2
# Not a zero-width characters (combining marks, format characters)
elif unicodedata.category(char) not in ("Mn", "Me", "Cf"):
# Normal characters take 1 cell
width += 1

return width


# patched socket.getfqdn() - see https://bugs.python.org/issue5004
Expand Down
23 changes: 0 additions & 23 deletions src/borg/platform/posix.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,11 @@ from . import posix_ug

from libc.errno cimport errno as c_errno

from cpython.mem cimport PyMem_Free
from libc.stddef cimport wchar_t

cdef extern from "wchar.h":
# https://www.man7.org/linux/man-pages/man3/wcswidth.3.html
cdef int wcswidth(const wchar_t *s, size_t n)


cdef extern from "Python.h":
# https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_AsWideCharString
wchar_t* PyUnicode_AsWideCharString(object, Py_ssize_t*) except NULL


def get_errno():
return c_errno


def swidth(s):
cdef Py_ssize_t size
cdef wchar_t *as_wchar = PyUnicode_AsWideCharString(s, &size)
terminal_width = wcswidth(as_wchar, <size_t>size)
PyMem_Free(as_wchar)
if terminal_width >= 0:
return terminal_width
else:
return len(s)


def process_alive(host, pid, thread):
"""
Check whether the (host, pid, thread_id) combination corresponds to a process potentially alive.
Expand Down
13 changes: 13 additions & 0 deletions src/borg/testsuite/platform/all_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from ...platform import swidth


def test_swidth_ascii():
assert swidth("borg") == 4


def test_swidth_cjk():
assert swidth("バックアップ") == 6 * 2


def test_swidth_mixed():
assert swidth("borgバックアップ") == 4 + 6 * 2
18 changes: 0 additions & 18 deletions src/borg/testsuite/platform/posix_test.py

This file was deleted.

Loading