Skip to content

Commit 1b806f5

Browse files
wschuelljdavid
authored andcommitted
maybe_string dealing with non-unicode strings
1 parent d224940 commit 1b806f5

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

pygit2/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def check_error(err, io=False):
4242
# Error message
4343
giterr = C.git_error_last()
4444
if giterr != ffi.NULL:
45-
message = ffi.string(giterr.message).decode('utf8')
45+
message = ffi.string(giterr.message).decode('utf8', errors='surrogateescape')
4646
else:
4747
message = f'err {err} (no message provided)'
4848

pygit2/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def maybe_string(ptr):
3434
if not ptr:
3535
return None
3636

37-
return ffi.string(ptr).decode('utf8')
37+
return ffi.string(ptr).decode('utf8', errors='surrogateescape')
3838

3939

4040
def to_bytes(s, encoding='utf-8', errors='strict'):

test/test_nonunicode.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2010-2024 The pygit2 contributors
2+
#
3+
# This file is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License, version 2,
5+
# as published by the Free Software Foundation.
6+
#
7+
# In addition to the permissions in the GNU General Public License,
8+
# the authors give you unlimited permission to link the compiled
9+
# version of this file into combinations with other programs,
10+
# and to distribute those combinations without any restriction
11+
# coming from the use of this file. (The General Public License
12+
# restrictions do apply in other respects; for example, they cover
13+
# modification of the file, and distribution when not linked into
14+
# a combined executable.)
15+
#
16+
# This file is distributed in the hope that it will be useful, but
17+
# WITHOUT ANY WARRANTY; without even the implied warranty of
18+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
# General Public License for more details.
20+
#
21+
# You should have received a copy of the GNU General Public License
22+
# along with this program; see the file COPYING. If not, write to
23+
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
24+
# Boston, MA 02110-1301, USA.
25+
26+
"""Tests for non unicode byte strings"""
27+
28+
import pygit2
29+
import os
30+
import shutil
31+
32+
33+
bstring = b'\xc3master'
34+
35+
def test_nonunicode_branchname(testrepo):
36+
folderpath = 'temp_repo_nonutf'
37+
if os.path.exists(folderpath):
38+
shutil.rmtree(folderpath)
39+
newrepo = pygit2.clone_repository(
40+
path=folderpath, url='https://github.com/pygit2/test_branch_notutf.git'
41+
)
42+
assert bstring in [
43+
(ref.split('/')[-1]).encode('utf8', 'surrogateescape')
44+
for ref in newrepo.listall_references()
45+
] # Remote branch among references: 'refs/remotes/origin/\udcc3master'

0 commit comments

Comments
 (0)