Skip to content

Commit ee4bf04

Browse files
jstasiakdcantrell
authored andcommitted
Handle non-UTF-8 device models
Took me a while to figure out why Fedora 40 installer was dying on me on a Wyse 3040 thin client and I finally narrowed it down to pyparted and found the associated issue (linked). The fix proposed in the issue comments worked for me so I figured I'd send a PR. Before: >>> import parted >>> parted.getDevice('/dev/mmcblk0').model Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/root/pyparted/build/lib.linux-x86_64-cpython-312/parted/device.py", line 69, in model return self.__device.model ^^^^^^^^^^^^^^^^^^^ UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 9: invalid start byte After: >>> import parted >>> parted.getDevice('/dev/mmcblk0').model 'MMC H8G4a�' Resolves: #76
1 parent d5870bb commit ee4bf04

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/pydevice.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,15 @@ PyObject *_ped_Device_get(_ped_Device *self, void *closure)
256256

257257
if (!strcmp(member, "model")) {
258258
if (self->model != NULL) {
259-
return PyUnicode_FromString(self->model);
259+
// There's at least one case of a non-UTF-8 model in the wild where
260+
// using PyUnicode_FromString would crash (model b"MMC H8G4a\x92"
261+
// on Wyse 3040 thin clients).
262+
//
263+
// Using PyUnicode_FromFormat instead will convert the 0x92 byte to
264+
// the replacement character.
265+
//
266+
// https://github.com/dcantrell/pyparted/issues/76
267+
return PyUnicode_FromFormat("%s", self->model)
260268
} else {
261269
return PyUnicode_FromString("");
262270
}

0 commit comments

Comments
 (0)