Skip to content

Commit

Permalink
Adjust code to deal with compile warnings
Browse files Browse the repository at this point in the history
Recent Python versions return 'const char *' to PyUnicode_asUTF8.  This
can give compile errors (I was using Ubuntu 20.04's default build env).
Some code changes had been made to case to 'char *', but it's dangerous
to discard the const and apparently insufficient to address the warning.

Update the code to make use 'const char *' variables where relevant.
The warnings go away.  Functionality is unaffected.
  • Loading branch information
iawells committed Jan 20, 2022
1 parent 0fb9055 commit 1005e07
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
12 changes: 6 additions & 6 deletions src/pyconstraint.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,25 @@ PyObject *_ped_Constraint_richcompare(_ped_Constraint *a, PyObject *b, int op) {

PyObject *_ped_Constraint_str(_ped_Constraint *self) {
char *ret = NULL;
char *start_align = NULL, *end_align = NULL;
char *start_range = NULL, *end_range = NULL;
const char *start_align = NULL, *end_align = NULL;
const char *start_range = NULL, *end_range = NULL;

start_align = (char *) PyUnicode_AsUTF8(_ped_Alignment_Type_obj.tp_repr(self->start_align));
start_align = PyUnicode_AsUTF8(_ped_Alignment_Type_obj.tp_repr(self->start_align));
if (start_align == NULL) {
return NULL;
}

end_align = (char *) PyUnicode_AsUTF8(_ped_Alignment_Type_obj.tp_repr(self->end_align));
end_align = PyUnicode_AsUTF8(_ped_Alignment_Type_obj.tp_repr(self->end_align));
if (end_align == NULL) {
return NULL;
}

start_range = (char *) PyUnicode_AsUTF8(_ped_Geometry_Type_obj.tp_repr(self->start_range));
start_range = PyUnicode_AsUTF8(_ped_Geometry_Type_obj.tp_repr(self->start_range));
if (start_range == NULL) {
return NULL;
}

end_range = (char *) PyUnicode_AsUTF8(_ped_Geometry_Type_obj.tp_repr(self->end_range));
end_range = PyUnicode_AsUTF8(_ped_Geometry_Type_obj.tp_repr(self->end_range));
if (end_range == NULL) {
return NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions src/pydevice.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ PyObject *_ped_Device_richcompare(_ped_Device *a, PyObject *b, int op) {

PyObject *_ped_Device_str(_ped_Device *self) {
char *ret = NULL;
char *hw_geom = NULL, *bios_geom = NULL;
const char *hw_geom = NULL, *bios_geom = NULL;

hw_geom = (char *) PyUnicode_AsUTF8(_ped_CHSGeometry_Type_obj.tp_repr(self->hw_geom));
hw_geom = PyUnicode_AsUTF8(_ped_CHSGeometry_Type_obj.tp_repr(self->hw_geom));
if (hw_geom == NULL) {
return NULL;
}

bios_geom = (char *) PyUnicode_AsUTF8(_ped_CHSGeometry_Type_obj.tp_repr(self->bios_geom));
bios_geom = PyUnicode_AsUTF8(_ped_CHSGeometry_Type_obj.tp_repr(self->bios_geom));
if (bios_geom == NULL) {
return NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions src/pydisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ PyObject *_ped_Partition_richcompare(_ped_Partition *a, PyObject *b, int op) {

PyObject *_ped_Partition_str(_ped_Partition *self) {
char *ret = NULL;
char *disk = NULL, *fs_type = NULL, *geom = NULL;
const char *disk = NULL, *fs_type = NULL, *geom = NULL;

disk = (char *) PyUnicode_AsUTF8(_ped_Disk_Type_obj.tp_repr(self->disk));
if (disk == NULL) {
Expand Down Expand Up @@ -346,7 +346,7 @@ PyObject *_ped_Disk_richcompare(_ped_Disk *a, PyObject *b, int op) {

PyObject *_ped_Disk_str(_ped_Disk *self) {
char *ret = NULL;
char *dev = NULL, *type = NULL;
const char *dev = NULL, *type = NULL;

dev = (char *) PyUnicode_AsUTF8(_ped_Device_Type_obj.tp_repr(self->dev));
if (dev == NULL) {
Expand Down
6 changes: 3 additions & 3 deletions src/pyfilesys.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ PyObject *_ped_FileSystem_richcompare(_ped_FileSystem *a, PyObject *b, int op) {

PyObject *_ped_FileSystem_str(_ped_FileSystem *self) {
char *ret = NULL;
char *type = NULL, *geom = NULL;
const char *type = NULL, *geom = NULL;

type = (char *) PyUnicode_AsUTF8(_ped_FileSystem_Type_obj.tp_repr(self->type));
type = PyUnicode_AsUTF8(_ped_FileSystem_Type_obj.tp_repr(self->type));
if (type == NULL) {
return NULL;
}

geom = (char *) PyUnicode_AsUTF8(_ped_Geometry_Type_obj.tp_repr(self->geom));
geom = PyUnicode_AsUTF8(_ped_Geometry_Type_obj.tp_repr(self->geom));
if (geom == NULL) {
return NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions src/pygeom.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ PyObject *_ped_Geometry_richcompare(_ped_Geometry *a, PyObject *b, int op) {

PyObject *_ped_Geometry_str(_ped_Geometry *self) {
char *ret = NULL;
char *dev = NULL;
const char *dev = NULL;

dev = (char *) PyUnicode_AsUTF8(_ped_Device_Type_obj.tp_repr(self->dev));
dev = PyUnicode_AsUTF8(_ped_Device_Type_obj.tp_repr(self->dev));
if (dev == NULL) {
return NULL;
}
Expand Down
8 changes: 5 additions & 3 deletions src/pytimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,21 @@ int _ped_Timer_set(_ped_Timer *self, PyObject *value, void *closure) {
return -1;
}
} else if (!strcmp(member, "state_name")) {
self->state_name = (char *) PyUnicode_AsUTF8(value);
const char *state_name = PyUnicode_AsUTF8(value);
if (PyErr_Occurred()) {
return -1;
}
/* self->state_name now points to the internal buffer of a PyUnicode obj
* which may be freed when its refcount drops to zero, so strdup it.
*/
if (self->state_name) {
self->state_name = strdup(self->state_name);
if (state_name) {
self->state_name = strdup(state_name);
if (!self->state_name) {
PyErr_NoMemory();
return -2;
}
} else {
self->state_name = NULL;
}
} else {
PyErr_Format(PyExc_AttributeError, "_ped.Timer object has no attribute %s", member);
Expand Down

0 comments on commit 1005e07

Please sign in to comment.