python: use libvirt_util to avoid raw free

This patch starts the process of elevating the python binding code
to be on the same level as the rest of libvirt when it comes to
requiring good coding styles.  Statically linking against the
libvirt_util library makes it much easier to write good code,
rather than having to open-code and reinvent things locally.

Done by global search and replace of s/free(/VIR_FREE(/, followed
by hand-inspection of remaining malloc and redundant memset.

* cfg.mk (exclude_file_name_regexp--sc_prohibit_raw_allocation):
Remove python from exemption.
* python/Makefile.am (INCLUDES): Add gnulib and src/util.  Drop
$(top_builddir)/$(subdir), as automake already guarantees that.
(mylibs, myqemulibs): Pull in libvirt_util and gnulib.
(libvirtmod_la_CFLAGS): Catch compiler warnings if configured to
use -Werror.
* python/typewrappers.c (libvirt_charPtrSizeWrap)
(libvirt_charPtrWrap): Convert free to VIR_FREE.
* python/generator.py (print_function_wrapper): Likewise.
* python/libvirt-override.c: Likewise.
This commit is contained in:
Eric Blake 2012-02-02 15:45:54 -07:00
parent 8fe454ce90
commit c700613b8d
5 changed files with 194 additions and 200 deletions

2
cfg.mk
View File

@ -744,7 +744,7 @@ exclude_file_name_regexp--sc_prohibit_nonreentrant = \
^((po|tests)/|docs/.*py$$|tools/(virsh|console)\.c$$)
exclude_file_name_regexp--sc_prohibit_raw_allocation = \
^(src/util/memory\.[ch]|(examples|python)/.*)$$
^(src/util/memory\.[ch]|examples/.*)$$
exclude_file_name_regexp--sc_prohibit_readlink = ^src/util/util\.c$$

View File

@ -7,9 +7,13 @@ SUBDIRS= . tests
INCLUDES = \
$(PYTHON_INCLUDES) \
-I$(top_builddir)/gnulib/lib \
-I$(top_srcdir)/gnulib/lib \
-I$(top_builddir)/src \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/util \
-I$(top_builddir)/include \
-I$(top_srcdir)/include \
-I$(top_builddir)/$(subdir) \
$(GETTEXT_CPPFLAGS)
AM_CFLAGS = $(WARN_CFLAGS)
@ -35,8 +39,14 @@ EXTRA_DIST = \
$(DOCS)
if WITH_PYTHON
mylibs = $(top_builddir)/src/libvirt.la
myqemulibs = $(top_builddir)/src/libvirt-qemu.la
mylibs = \
$(top_builddir)/src/libvirt.la \
$(top_builddir)/src/libvirt_util.la \
$(top_builddir)/gnulib/lib/libgnu.la
myqemulibs = \
$(top_builddir)/src/libvirt-qemu.la \
$(top_builddir)/src/libvirt_util.la \
$(top_builddir)/gnulib/lib/libgnu.la
all-local: libvirt.py libvirt_qemu.py
@ -46,7 +56,7 @@ libvirtmod_la_SOURCES = libvirt-override.c typewrappers.c
nodist_libvirtmod_la_SOURCES = libvirt.c libvirt.h
# Python <= 2.4 header files contain a redundant decl, hence we
# need extra flags here
libvirtmod_la_CFLAGS = $(WARN_PYTHON_CFLAGS)
libvirtmod_la_CFLAGS = $(WARN_CFLAGS) $(WARN_PYTHON_CFLAGS)
libvirtmod_la_LDFLAGS = -module -avoid-version -shared -L$(top_builddir)/src/.libs \
$(CYGWIN_EXTRA_LDFLAGS)

View File

@ -580,7 +580,7 @@ def print_function_wrapper(module, name, output, export, include):
if ret[0] == 'void':
if file == "python_accessor":
if args[1][1] == "char *":
c_call = "\n free(%s->%s);\n" % (
c_call = "\n VIR_FREE(%s->%s);\n" % (
args[0][0], args[1][0], args[0][0], args[1][0])
c_call = c_call + " %s->%s = (%s)strdup((const xmlChar *)%s);\n" % (args[0][0],
args[1][0], args[1][1], args[1][0])

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
* types.c: converter functions between the internal representation
* and the Python objects
*
* Copyright (C) 2005, 2007 Red Hat, Inc.
* Copyright (C) 2005, 2007, 2012 Red Hat, Inc.
*
* Daniel Veillard <veillard@redhat.com>
*/
@ -16,6 +16,8 @@
#include "typewrappers.h"
#include "memory.h"
#ifndef Py_CAPSULE_H
typedef void(*PyCapsule_Destructor)(void *, void *);
#endif
@ -86,7 +88,7 @@ libvirt_charPtrSizeWrap(char *str, Py_ssize_t size)
return (Py_None);
}
ret = PyString_FromStringAndSize(str, size);
free(str);
VIR_FREE(str);
return (ret);
}
@ -100,7 +102,7 @@ libvirt_charPtrWrap(char *str)
return (Py_None);
}
ret = PyString_FromString(str);
free(str);
VIR_FREE(str);
return (ret);
}