mirror of
https://github.com/Gnucash/gnucash.git
synced 2026-09-03 20:53:02 -05:00
Use glib.h over custom typedefs in Python SWIG
Use the native glib.h (mainly gint, gfloat ...) over custom typedefs in SWIG type files. This is for Python only.
This commit is contained in:
+15
-6
@@ -22,18 +22,16 @@
|
||||
* *
|
||||
\********************************************************************/
|
||||
|
||||
typedef void * gpointer; // Not sure why SWIG doesn't figure this out.
|
||||
%typemap(newfree) gchar * "g_free($1);"
|
||||
|
||||
#if defined(SWIGGUILE)
|
||||
|
||||
/* Not sure why SWIG doesn't figure this out. */
|
||||
typedef int gint;
|
||||
typedef gint64 time64;
|
||||
typedef unsigned int guint;
|
||||
typedef double gdouble;
|
||||
typedef float gfloat;
|
||||
typedef void * gpointer;
|
||||
|
||||
%typemap(newfree) gchar * "g_free($1);"
|
||||
|
||||
#if defined(SWIGGUILE)
|
||||
typedef char gchar;
|
||||
|
||||
%typemap (out) char * {
|
||||
@@ -169,6 +167,9 @@ typedef char gchar;
|
||||
}
|
||||
%enddef
|
||||
#elif defined(SWIGPYTHON) /* Typemaps for Python */
|
||||
|
||||
%import "glib.h"
|
||||
|
||||
%typemap(in) gint8, gint16, gint32, gint64, gshort, glong {
|
||||
$1 = ($1_type)PyInt_AsLong($input);
|
||||
}
|
||||
@@ -185,6 +186,14 @@ typedef char gchar;
|
||||
$result = PyLong_FromUnsignedLong($1);
|
||||
}
|
||||
|
||||
%typemap(in) gdouble {
|
||||
$1 = ($1_type)PyFloat_AsDouble($input);
|
||||
}
|
||||
|
||||
%typemap(out) gdouble {
|
||||
$result = PyFloat_FromDouble($1);
|
||||
}
|
||||
|
||||
%typemap(in) gchar * {
|
||||
$1 = ($1_ltype)PyString_AsString($input);
|
||||
}
|
||||
|
||||
@@ -19,12 +19,26 @@ ENDMACRO (GNC_ADD_SWIG_COMMAND)
|
||||
|
||||
MACRO (GNC_ADD_SWIG_PYTHON_COMMAND _target _output _input)
|
||||
|
||||
ADD_CUSTOM_COMMAND(OUTPUT ${_output}
|
||||
set (DEFAULT_SWIG_PYTHON_FLAGS
|
||||
-python
|
||||
-Wall -Werror
|
||||
${SWIG_ARGS}
|
||||
)
|
||||
set (DEFAULT_SWIG_PYTHON_C_INCLUDES
|
||||
${GLIB2_INCLUDE_DIRS}
|
||||
${CMAKE_SOURCE_DIR}/src/libqof/qof
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
${CMAKE_SOURCE_DIR}/src/engine
|
||||
${CMAKE_SOURCE_DIR}/src/app-utils
|
||||
)
|
||||
|
||||
COMMAND ${SWIG_EXECUTABLE} -python -Wall -Werror ${SWIG_ARGS}
|
||||
-I${CMAKE_SOURCE_DIR}/common
|
||||
-I${CMAKE_SOURCE_DIR}/libgnucash/engine -I${CMAKE_SOURCE_DIR}/libgnucash/app-utils
|
||||
-o ${_output} ${_input}
|
||||
|
||||
set (PYTHON_SWIG_FLAGS ${DEFAULT_SWIG_PYTHON_FLAGS})
|
||||
foreach (dir ${DEFAULT_SWIG_PYTHON_C_INCLUDES})
|
||||
list (APPEND PYTHON_SWIG_FLAGS "-I${dir}")
|
||||
endforeach (dir)
|
||||
ADD_CUSTOM_COMMAND(OUTPUT ${_output}
|
||||
COMMAND ${SWIG_EXECUTABLE} ${PYTHON_SWIG_FLAGS} -o ${_output} ${_input}
|
||||
DEPENDS ${_input} ${CMAKE_SOURCE_DIR}/common/base-typemaps.i ${ARGN}
|
||||
)
|
||||
ADD_CUSTOM_TARGET(${_target} ALL DEPENDS ${_output} ${CMAKE_SOURCE_DIR}/common/base-typemaps.i ${_input} ${ARGN})
|
||||
|
||||
@@ -38,7 +38,7 @@ endif
|
||||
|
||||
swig-unittest-support-python.c: unittest-support.i $(top_srcdir)/common/base-typemaps.i
|
||||
$(SWIG) -python -Wall -Werror $(SWIG_ARGS) \
|
||||
-I${top_srcdir}/common \
|
||||
-I${GLIB_CFLAGS} -I${top_srcdir}/common \
|
||||
${AM_CPPFLAGS} -o $@ $<
|
||||
|
||||
unittest-support.py: swig-unittest-support-python.c ${SWIG_FILES}
|
||||
|
||||
@@ -114,7 +114,7 @@ endif
|
||||
endif
|
||||
swig-app-utils-python.c: app-utils.i ${top_srcdir}/common/base-typemaps.i
|
||||
$(SWIG) -python -Wall -Werror $(SWIG_ARGS) \
|
||||
-I${top_srcdir}/common -o $@ $<
|
||||
-I${GLIB_CFLAGS} -I${top_srcdir}/common -o $@ $<
|
||||
endif
|
||||
|
||||
if WITH_PYTHON
|
||||
|
||||
@@ -51,7 +51,7 @@ endif
|
||||
endif
|
||||
swig-core-utils-python.c: core-utils.i ${top_srcdir}/common/base-typemaps.i
|
||||
$(SWIG) -python -Wall -Werror $(SWIG_ARGS) \
|
||||
-I${top_srcdir}/common -o $@ $<
|
||||
-I${GLIB_CFLAGS} -I${top_srcdir}/common -o $@ $<
|
||||
endif
|
||||
|
||||
AM_CPPFLAGS = \
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
from unittest import TestCase, main
|
||||
|
||||
from gnucash import GncNumeric, GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED, \
|
||||
GNC_HOW_RND_NEVER, GNC_HOW_RND_FLOOR, GNC_HOW_RND_CEIL
|
||||
|
||||
class TestGncNumeric( TestCase ):
|
||||
def test_defaut(self):
|
||||
num = GncNumeric()
|
||||
self.assertEqual(str(num), "0/1")
|
||||
self.assertEqual(num.num(), 0)
|
||||
self.assertEqual(num.denom(), 1)
|
||||
|
||||
def test_from_num_denom(self):
|
||||
num = GncNumeric(1, 2)
|
||||
self.assertEqual(str(num), "1/2")
|
||||
self.assertEqual(num.num(), 1)
|
||||
self.assertEqual(num.denom(), 2)
|
||||
|
||||
def test_from_int(self):
|
||||
num = GncNumeric(3)
|
||||
self.assertEqual(str(num), "3/1")
|
||||
self.assertEqual(num.num(), 3)
|
||||
self.assertEqual(num.denom(), 1)
|
||||
|
||||
# Safest outcome at current. This can be fixed but correct bounds checks
|
||||
# are required to ensure gint64 is not overflowed.
|
||||
def test_from_long(self):
|
||||
with self.assertRaises(TypeError):
|
||||
GncNumeric(3L)
|
||||
|
||||
def test_from_float(self):
|
||||
num = GncNumeric(3.1, 20, GNC_HOW_DENOM_FIXED | GNC_HOW_RND_NEVER)
|
||||
self.assertEqual(str(num), "62/20")
|
||||
self.assertEqual(num.num(), 62)
|
||||
self.assertEqual(num.denom(), 20)
|
||||
|
||||
num = GncNumeric(1/3.0, 10000000000, GNC_HOW_RND_FLOOR)
|
||||
self.assertEqual(str(num), "3333333333/10000000000")
|
||||
self.assertEqual(num.num(), 3333333333)
|
||||
self.assertEqual(num.denom(), 10000000000)
|
||||
|
||||
num = GncNumeric(1/3.0, 10000000000, GNC_HOW_RND_CEIL)
|
||||
self.assertEqual(str(num), "3333333334/10000000000")
|
||||
self.assertEqual(num.num(), 3333333334)
|
||||
self.assertEqual(num.denom(), 10000000000)
|
||||
|
||||
def test_from_float_auto(self):
|
||||
num = GncNumeric(3.1)
|
||||
self.assertEqual(str(num), "31/10")
|
||||
self.assertEqual(num.num(), 31)
|
||||
self.assertEqual(num.denom(), 10)
|
||||
|
||||
def test_from_instance(self):
|
||||
orig = GncNumeric(3)
|
||||
num = GncNumeric(instance=orig.instance)
|
||||
self.assertEqual(str(num), "3/1")
|
||||
self.assertEqual(num.num(), 3)
|
||||
self.assertEqual(num.denom(), 1)
|
||||
|
||||
def test_from_str(self):
|
||||
num = GncNumeric("3.1")
|
||||
self.assertEqual(str(num), "31/10")
|
||||
self.assertEqual(num.num(), 31)
|
||||
self.assertEqual(num.denom(), 10)
|
||||
|
||||
num = GncNumeric("1/3")
|
||||
self.assertEqual(str(num), "1/3")
|
||||
self.assertEqual(num.num(), 1)
|
||||
self.assertEqual(num.denom(), 3)
|
||||
|
||||
def test_to_str(self):
|
||||
num = GncNumeric("1000/3")
|
||||
self.assertEqual(str(num), "1000/3")
|
||||
|
||||
num = GncNumeric(1, 0)
|
||||
self.assertEqual(str(num), "1/0")
|
||||
|
||||
def test_to_double(self):
|
||||
for test_num in [0.0, 1.1, -1.1, 1/3.0]:
|
||||
self.assertEqual(GncNumeric(test_num).to_double(), test_num)
|
||||
|
||||
def test_to_fraction(self):
|
||||
fraction = GncNumeric("1000/3").to_fraction()
|
||||
self.assertEqual(fraction.numerator, 1000)
|
||||
self.assertEqual(fraction.denominator, 3)
|
||||
|
||||
def test_incorect_args(self):
|
||||
with self.assertRaises(TypeError):
|
||||
GncNumeric(1, 2, 3)
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
GncNumeric("1", 2)
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
GncNumeric(1.1, "round")
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
GncNumeric(complex(1, 1))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user