Skip to content

Commit 4ebb091

Browse files
Andy Groverstratakis
authored andcommitted
python3: Use bytesobject.h and PyBytes_* instead of PyString_*
As described here: https://docs.python.org/3/howto/cporting.html#str-unicode-unification Signed-off-by: Andy Grover <agrover@redhat.com>
1 parent b0d16d2 commit 4ebb091

File tree

4 files changed

+67
-63
lines changed

4 files changed

+67
-63
lines changed

python-ethtool/etherinfo.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
#include <Python.h>
19+
#include <bytesobject.h>
1920
#include <bits/sockaddr.h>
2021
#include <stdio.h>
2122
#include <string.h>
@@ -65,7 +66,7 @@ static void callback_nl_link(struct nl_object *obj, void *arg)
6566
if( ethi->hwaddress ) {
6667
Py_XDECREF(ethi->hwaddress);
6768
}
68-
ethi->hwaddress = PyString_FromFormat("%s", hwaddr);
69+
ethi->hwaddress = PyBytes_FromFormat("%s", hwaddr);
6970
}
7071

7172

@@ -129,7 +130,7 @@ static int _set_device_index(PyEtherInfo *self)
129130
return 0;
130131
}
131132

132-
link = rtnl_link_get_by_name(link_cache, PyString_AsString(self->device));
133+
link = rtnl_link_get_by_name(link_cache, PyBytes_AsString(self->device));
133134
if( !link ) {
134135
errno = ENODEV;
135136
PyErr_SetFromErrno(PyExc_IOError);
@@ -179,7 +180,7 @@ int get_etherinfo_link(PyEtherInfo *self)
179180
if( !open_netlink(self) ) {
180181
PyErr_Format(PyExc_RuntimeError,
181182
"Could not open a NETLINK connection for %s",
182-
PyString_AsString(self->device));
183+
PyBytes_AsString(self->device));
183184
return 0;
184185
}
185186

@@ -233,7 +234,7 @@ PyObject * get_etherinfo_address(PyEtherInfo *self, nlQuery query)
233234
if( !open_netlink(self) ) {
234235
PyErr_Format(PyExc_RuntimeError,
235236
"Could not open a NETLINK connection for %s",
236-
PyString_AsString(self->device));
237+
PyBytes_AsString(self->device));
237238
return NULL;
238239
}
239240

python-ethtool/etherinfo_obj.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
#include <Python.h>
27+
#include <bytesobject.h>
2728
#include "structmember.h"
2829

2930
#include <netlink/route/rtnl.h>
@@ -86,7 +87,7 @@ static PyNetlinkIPaddress * get_last_ipv4_address(PyObject *addrlist)
8687
*/
8788
PyObject *_ethtool_etherinfo_getter(PyEtherInfo *self, PyObject *attr_o)
8889
{
89-
char *attr = PyString_AsString(attr_o);
90+
char *attr = PyBytes_AsString(attr_o);
9091
PyNetlinkIPaddress *py_addr;
9192
PyObject *addrlist = NULL;
9293

@@ -177,31 +178,31 @@ PyObject *_ethtool_etherinfo_str(PyEtherInfo *self)
177178

178179
get_etherinfo_link(self);
179180

180-
ret = PyString_FromFormat("Device ");
181-
PyString_Concat(&ret, self->device);
182-
PyString_ConcatAndDel(&ret, PyString_FromString(":\n"));
181+
ret = PyBytes_FromFormat("Device ");
182+
PyBytes_Concat(&ret, self->device);
183+
PyBytes_ConcatAndDel(&ret, PyBytes_FromString(":\n"));
183184

184185
if( self->hwaddress ) {
185-
PyString_ConcatAndDel(&ret, PyString_FromString("\tMAC address: "));
186-
PyString_Concat(&ret, self->hwaddress);
187-
PyString_ConcatAndDel(&ret, PyString_FromString("\n"));
186+
PyBytes_ConcatAndDel(&ret, PyBytes_FromString("\tMAC address: "));
187+
PyBytes_Concat(&ret, self->hwaddress);
188+
PyBytes_ConcatAndDel(&ret, PyBytes_FromString("\n"));
188189
}
189190

190191
ipv4addrs = get_etherinfo_address(self, NLQRY_ADDR4);
191192
if( ipv4addrs ) {
192193
Py_ssize_t i;
193194
for (i = 0; i < PyList_Size(ipv4addrs); i++) {
194195
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv4addrs, i);
195-
PyObject *tmp = PyString_FromFormat("\tIPv4 address: ");
196-
PyString_Concat(&tmp, py_addr->local);
197-
PyString_ConcatAndDel(&tmp, PyString_FromFormat("/%d", py_addr->prefixlen));
196+
PyObject *tmp = PyBytes_FromFormat("\tIPv4 address: ");
197+
PyBytes_Concat(&tmp, py_addr->local);
198+
PyBytes_ConcatAndDel(&tmp, PyBytes_FromFormat("/%d", py_addr->prefixlen));
198199
if (py_addr->ipv4_broadcast ) {
199-
PyString_ConcatAndDel(&tmp,
200-
PyString_FromString(" Broadcast: "));
201-
PyString_Concat(&tmp, py_addr->ipv4_broadcast);
200+
PyBytes_ConcatAndDel(&tmp,
201+
PyBytes_FromString(" Broadcast: "));
202+
PyBytes_Concat(&tmp, py_addr->ipv4_broadcast);
202203
}
203-
PyString_ConcatAndDel(&tmp, PyString_FromString("\n"));
204-
PyString_ConcatAndDel(&ret, tmp);
204+
PyBytes_ConcatAndDel(&tmp, PyBytes_FromString("\n"));
205+
PyBytes_ConcatAndDel(&ret, tmp);
205206
}
206207
}
207208

@@ -210,13 +211,13 @@ PyObject *_ethtool_etherinfo_str(PyEtherInfo *self)
210211
Py_ssize_t i;
211212
for (i = 0; i < PyList_Size(ipv6addrs); i++) {
212213
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv6addrs, i);
213-
PyObject *tmp = PyString_FromFormat("\tIPv6 address: [");
214-
PyString_Concat(&tmp, py_addr->scope);
215-
PyString_ConcatAndDel(&tmp, PyString_FromString("] "));
216-
PyString_Concat(&tmp, py_addr->local);
217-
PyString_ConcatAndDel(&tmp, PyString_FromFormat("/%d", py_addr->prefixlen));
218-
PyString_ConcatAndDel(&tmp, PyString_FromString("\n"));
219-
PyString_ConcatAndDel(&ret, tmp);
214+
PyObject *tmp = PyBytes_FromFormat("\tIPv6 address: [");
215+
PyBytes_Concat(&tmp, py_addr->scope);
216+
PyBytes_ConcatAndDel(&tmp, PyBytes_FromString("] "));
217+
PyBytes_Concat(&tmp, py_addr->local);
218+
PyBytes_ConcatAndDel(&tmp, PyBytes_FromFormat("/%d", py_addr->prefixlen));
219+
PyBytes_ConcatAndDel(&tmp, PyBytes_FromString("\n"));
220+
PyBytes_ConcatAndDel(&ret, tmp);
220221
}
221222
}
222223

python-ethtool/ethtool.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* General Public License for more details.
1717
*/
1818
#include <Python.h>
19+
#include <bytesobject.h>
1920

2021
#include <errno.h>
2122
#include <stddef.h>
@@ -60,7 +61,7 @@ static PyObject *get_active_devices(PyObject *self __unused, PyObject *args __un
6061

6162
list = PyList_New(0);
6263
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
63-
PyObject *str = PyString_FromString(ifa->ifa_name);
64+
PyObject *str = PyBytes_FromString(ifa->ifa_name);
6465
/* names are not unique (listed for both ipv4 and ipv6) */
6566
if (!PySequence_Contains(list, str) && (ifa->ifa_flags & (IFF_UP))) {
6667
PyList_Append(list, str);
@@ -104,7 +105,7 @@ static PyObject *get_devices(PyObject *self __unused, PyObject *args __unused)
104105
while (*name == ' ')
105106
name++; /* skip over leading whitespace if any */
106107

107-
str = PyString_FromString(name);
108+
str = PyBytes_FromString(name);
108109
PyList_Append(list, str);
109110
Py_DECREF(str);
110111
}
@@ -151,7 +152,7 @@ static PyObject *get_hwaddress(PyObject *self __unused, PyObject *args)
151152
(unsigned int)ifr.ifr_hwaddr.sa_data[4] % 256,
152153
(unsigned int)ifr.ifr_hwaddr.sa_data[5] % 256);
153154

154-
return PyString_FromString(hwaddr);
155+
return PyBytes_FromString(hwaddr);
155156
}
156157

157158
static PyObject *get_ipaddress(PyObject *self __unused, PyObject *args)
@@ -191,7 +192,7 @@ static PyObject *get_ipaddress(PyObject *self __unused, PyObject *args)
191192
(unsigned int)ifr.ifr_addr.sa_data[4] % 256,
192193
(unsigned int)ifr.ifr_addr.sa_data[5] % 256);
193194

194-
return PyString_FromString(ipaddr);
195+
return PyBytes_FromString(ipaddr);
195196
}
196197

197198

@@ -218,19 +219,19 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) {
218219

219220
/* Parse input arguments if we got them */
220221
if( inargs != NULL ) {
221-
if( PyString_Check(inargs) ) { /* Input argument is just a string */
222+
if( PyBytes_Check(inargs) ) { /* Input argument is just a string */
222223
fetch_devs_len = 1;
223224
fetch_devs = calloc(1, sizeof(char *));
224-
fetch_devs[0] = PyString_AsString(inargs);
225+
fetch_devs[0] = PyBytes_AsString(inargs);
225226
} else if( PyTuple_Check(inargs) ) { /* Input argument is a tuple list with devices */
226227
int j = 0;
227228

228229
fetch_devs_len = PyTuple_Size(inargs);
229230
fetch_devs = calloc(fetch_devs_len+1, sizeof(char *));
230231
for( i = 0; i < fetch_devs_len; i++ ) {
231232
PyObject *elmt = PyTuple_GetItem(inargs, i);
232-
if( elmt && PyString_Check(elmt) ) {
233-
fetch_devs[j++] = PyString_AsString(elmt);
233+
if( elmt && PyBytes_Check(elmt) ) {
234+
fetch_devs[j++] = PyBytes_AsString(elmt);
234235
}
235236
}
236237
fetch_devs_len = j;
@@ -241,8 +242,8 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) {
241242
fetch_devs = calloc(fetch_devs_len+1, sizeof(char *));
242243
for( i = 0; i < fetch_devs_len; i++ ) {
243244
PyObject *elmt = PyList_GetItem(inargs, i);
244-
if( elmt && PyString_Check(elmt) ) {
245-
fetch_devs[j++] = PyString_AsString(elmt);
245+
if( elmt && PyBytes_Check(elmt) ) {
246+
fetch_devs[j++] = PyBytes_AsString(elmt);
246247
}
247248
}
248249
fetch_devs_len = j;
@@ -267,7 +268,7 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) {
267268
free(fetch_devs);
268269
return NULL;
269270
}
270-
dev->device = PyString_FromString(fetch_devs[i]);
271+
dev->device = PyBytes_FromString(fetch_devs[i]);
271272
dev->hwaddress = NULL;
272273
dev->index = -1;
273274

@@ -350,7 +351,7 @@ static PyObject *get_netmask (PyObject *self __unused, PyObject *args)
350351
(unsigned int)ifr.ifr_netmask.sa_data[4] % 256,
351352
(unsigned int)ifr.ifr_netmask.sa_data[5] % 256);
352353

353-
return PyString_FromString(netmask);
354+
return PyBytes_FromString(netmask);
354355
}
355356

356357
static PyObject *get_broadcast(PyObject *self __unused, PyObject *args)
@@ -390,7 +391,7 @@ static PyObject *get_broadcast(PyObject *self __unused, PyObject *args)
390391
(unsigned int)ifr.ifr_broadaddr.sa_data[4] % 256,
391392
(unsigned int)ifr.ifr_broadaddr.sa_data[5] % 256);
392393

393-
return PyString_FromString(broadcast);
394+
return PyBytes_FromString(broadcast);
394395
}
395396

396397
static PyObject *get_module(PyObject *self __unused, PyObject *args)
@@ -455,12 +456,12 @@ static PyObject *get_module(PyObject *self __unused, PyObject *args)
455456
return NULL;
456457
} else {
457458
PyErr_Clear();
458-
return PyString_FromString(driver);
459+
return PyBytes_FromString(driver);
459460
}
460461
}
461462

462463
close(fd);
463-
return PyString_FromString(((struct ethtool_drvinfo *)buf)->driver);
464+
return PyBytes_FromString(((struct ethtool_drvinfo *)buf)->driver);
464465
}
465466

466467
static PyObject *get_businfo(PyObject *self __unused, PyObject *args)
@@ -499,7 +500,7 @@ static PyObject *get_businfo(PyObject *self __unused, PyObject *args)
499500
}
500501

501502
close(fd);
502-
return PyString_FromString(((struct ethtool_drvinfo *)buf)->bus_info);
503+
return PyBytes_FromString(((struct ethtool_drvinfo *)buf)->bus_info);
503504
}
504505

505506
static int send_command(int cmd, const char *devname, void *value)

python-ethtool/netlink-address.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
/* Python object corresponding to a (struct rtnl_addr) */
1717
#include <Python.h>
18+
#include <bytesobject.h>
1819
#include "structmember.h"
1920

2021
#include <arpa/inet.h>
@@ -54,7 +55,7 @@ PyNetlinkIPaddress_from_rtnl_addr(struct rtnl_addr *addr)
5455
PyErr_SetFromErrno(PyExc_RuntimeError);
5556
goto error;
5657
}
57-
py_obj->local = PyString_FromString(buf);
58+
py_obj->local = PyBytes_FromString(buf);
5859
if (!py_obj->local) {
5960
goto error;
6061
}
@@ -63,7 +64,7 @@ PyNetlinkIPaddress_from_rtnl_addr(struct rtnl_addr *addr)
6364
memset(&buf, 0, sizeof(buf));
6465
if ((peer_addr = rtnl_addr_get_peer(addr))) {
6566
nl_addr2str(peer_addr, buf, sizeof(buf));
66-
py_obj->peer = PyString_FromString(buf);
67+
py_obj->peer = PyBytes_FromString(buf);
6768
if (!py_obj->local) {
6869
goto error;
6970
}
@@ -84,7 +85,7 @@ PyNetlinkIPaddress_from_rtnl_addr(struct rtnl_addr *addr)
8485
PyErr_SetFromErrno(PyExc_RuntimeError);
8586
goto error;
8687
}
87-
py_obj->ipv4_broadcast = PyString_FromString(buf);
88+
py_obj->ipv4_broadcast = PyBytes_FromString(buf);
8889
if (!py_obj->ipv4_broadcast) {
8990
goto error;
9091
}
@@ -93,7 +94,7 @@ PyNetlinkIPaddress_from_rtnl_addr(struct rtnl_addr *addr)
9394
/* Set IP address scope: */
9495
memset(&buf, 0, sizeof(buf));
9596
rtnl_scope2str(rtnl_addr_get_scope(addr), buf, sizeof(buf));
96-
py_obj->scope = PyString_FromString(buf);
97+
py_obj->scope = PyBytes_FromString(buf);
9798

9899
return (PyObject*)py_obj;
99100

@@ -119,41 +120,41 @@ netlink_ip_address_dealloc(PyNetlinkIPaddress *obj)
119120
static PyObject*
120121
netlink_ip_address_repr(PyNetlinkIPaddress *obj)
121122
{
122-
PyObject *result = PyString_FromString("ethtool.NetlinkIPaddress(family=");
123+
PyObject *result = PyBytes_FromString("ethtool.NetlinkIPaddress(family=");
123124
char buf[256];
124125

125126
memset(&buf, 0, sizeof(buf));
126127
nl_af2str(obj->family, buf, sizeof(buf));
127-
PyString_ConcatAndDel(&result,
128-
PyString_FromFormat("%s, address='", buf));
129-
PyString_Concat(&result, obj->local);
128+
PyBytes_ConcatAndDel(&result,
129+
PyBytes_FromFormat("%s, address='", buf));
130+
PyBytes_Concat(&result, obj->local);
130131

131132
if (obj->family == AF_INET) {
132-
PyString_ConcatAndDel(&result,
133-
PyString_FromFormat("', netmask=%d",
133+
PyBytes_ConcatAndDel(&result,
134+
PyBytes_FromFormat("', netmask=%d",
134135
obj->prefixlen));
135136
} else if (obj->family == AF_INET6) {
136-
PyString_ConcatAndDel(&result,
137-
PyString_FromFormat("/%d'",
137+
PyBytes_ConcatAndDel(&result,
138+
PyBytes_FromFormat("/%d'",
138139
obj->prefixlen));
139140
}
140141

141142
if (obj->peer) {
142-
PyString_ConcatAndDel(&result, PyString_FromString(", peer_address='"));
143-
PyString_Concat(&result, obj->peer);
144-
PyString_ConcatAndDel(&result, PyString_FromString("'"));
143+
PyBytes_ConcatAndDel(&result, PyBytes_FromString(", peer_address='"));
144+
PyBytes_Concat(&result, obj->peer);
145+
PyBytes_ConcatAndDel(&result, PyBytes_FromString("'"));
145146
}
146147

147148
if (obj->family == AF_INET && obj->ipv4_broadcast) {
148-
PyString_ConcatAndDel(&result, PyString_FromString(", broadcast='"));
149-
PyString_Concat(&result, obj->ipv4_broadcast);
150-
PyString_ConcatAndDel(&result, PyString_FromString("'"));
149+
PyBytes_ConcatAndDel(&result, PyBytes_FromString(", broadcast='"));
150+
PyBytes_Concat(&result, obj->ipv4_broadcast);
151+
PyBytes_ConcatAndDel(&result, PyBytes_FromString("'"));
151152
}
152153

153-
PyString_ConcatAndDel(&result, PyString_FromString(", scope="));
154-
PyString_Concat(&result, obj->scope);
154+
PyBytes_ConcatAndDel(&result, PyBytes_FromString(", scope="));
155+
PyBytes_Concat(&result, obj->scope);
155156

156-
PyString_ConcatAndDel(&result, PyString_FromString(")"));
157+
PyBytes_ConcatAndDel(&result, PyBytes_FromString(")"));
157158

158159
return result;
159160
}

0 commit comments

Comments
 (0)