[gnumeric] PythonLoader: use python's boolean type instead of rolling our own.



commit e8b6405ecb70dd7917d7a09f0973a1c5294f064d
Author: Morten Welinder <terra gnome org>
Date:   Sun Mar 21 20:33:54 2021 -0400

    PythonLoader: use python's boolean type instead of rolling our own.

 NEWS                                |  1 +
 plugins/python-loader/ChangeLog     |  1 +
 plugins/python-loader/py-gnumeric.c | 68 ++++---------------------------------
 3 files changed, 9 insertions(+), 61 deletions(-)
---
diff --git a/NEWS b/NEWS
index a56330bd5..2490818f3 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ Gnumeric 1.12.50
 Morten:
        * Fix undo problem with sorting and cell comments.
        * Python loader cleanups.
+       * Use Python's bool type instead of rolling our own.
 
 --------------------------------------------------------------------------
 Gnumeric 1.12.49
diff --git a/plugins/python-loader/ChangeLog b/plugins/python-loader/ChangeLog
index abbe18227..9f720488e 100644
--- a/plugins/python-loader/ChangeLog
+++ b/plugins/python-loader/ChangeLog
@@ -2,6 +2,7 @@
 
        * py-gnumeric.c (py_obj_to_gnm_value): Handle python ints via
        gnm_float, not int, so we handle larger numbers.
+       (gnm_value_to_py_obj): Use python's boolean type.
 
        * python-loader.c (gplp_unload_service_function_group): Clear
        python_fn_info_dict after unref so we don't do it again.
diff --git a/plugins/python-loader/py-gnumeric.c b/plugins/python-loader/py-gnumeric.c
index 14e04eca8..e0892d3df 100644
--- a/plugins/python-loader/py-gnumeric.c
+++ b/plugins/python-loader/py-gnumeric.c
@@ -31,11 +31,6 @@
 
 static PyObject *GnmModule = NULL;
 
-static PyTypeObject py_Boolean_object_type;
-typedef struct _py_Boolean_object py_Boolean_object;
-static PyObject *py_new_Boolean_object (gboolean value);
-static gboolean py_Boolean_as_gboolean (py_Boolean_object *self);
-
 static PyTypeObject py_CellRef_object_type;
 typedef struct _py_CellRef_object py_CellRef_object;
 
@@ -117,8 +112,8 @@ py_obj_to_gnm_value (const GnmEvalPos *eval_pos, PyObject *py_val)
                ret_val = value_new_empty ();
        } else if (py_val == Py_None) {
                ret_val = value_new_empty ();
-       } else if (py_val_type == (PyObject *) &py_Boolean_object_type) {
-               ret_val = value_new_bool (py_Boolean_as_gboolean ((py_Boolean_object *) py_val));
+       } else if (PyBool_Check (py_val)) {
+               ret_val = value_new_bool (py_val == Py_True);
        } else if (PyLong_Check (py_val)) {
                ret_val = value_new_float ((gnm_float)PyLong_AsLong (py_val));
        } else if (PyFloat_Check (py_val)) {
@@ -239,7 +234,8 @@ gnm_value_to_py_obj (const GnmEvalPos *eval_pos, const GnmValue *val)
 
        switch (val->v_any.type) {
        case VALUE_BOOLEAN:
-               py_val = py_new_Boolean_object (value_get_as_checked_bool (val));
+               py_val = value_get_as_checked_bool (val) ? Py_True : Py_False;
+               Py_INCREF (py_val);
                break;
        case VALUE_FLOAT:
                py_val = PyFloat_FromDouble (value_get_as_float (val));
@@ -373,55 +369,6 @@ call_python_function (PyObject *python_fn, GnmEvalPos const *eval_pos, gint n_ar
 }
 
 
-/*
- * Boolean
- */
-
-struct _py_Boolean_object {
-       PyObject_HEAD
-       gboolean value;
-};
-
-static gboolean
-py_Boolean_as_gboolean (py_Boolean_object *self)
-{
-       return self->value;
-}
-
-static PyObject *
-py_Boolean_object_str (py_Boolean_object *self)
-{
-       return PyUnicode_FromString (self->value ? "True" : "False");
-}
-
-static void
-py_Boolean_object_dealloc (py_Boolean_object *self)
-{
-       PyObject_Del (self);
-}
-
-static PyObject *
-py_new_Boolean_object (gboolean value)
-{
-       py_Boolean_object *self;
-
-       self = PyObject_NEW (py_Boolean_object, &py_Boolean_object_type);
-       if (self == NULL) {
-               return NULL;
-       }
-       self->value = value;
-
-       return (PyObject *) self;
-}
-
-static PyTypeObject py_Boolean_object_type = {
-       PyVarObject_HEAD_INIT(NULL, 0)
-       .tp_name = "Boolean",
-       .tp_basicsize = sizeof (py_Boolean_object),
-       .tp_dealloc = (destructor) &py_Boolean_object_dealloc,
-       .tp_str = (reprfunc) py_Boolean_object_str
-};
-
 /*
  * CellRef
  */
@@ -875,10 +822,9 @@ py_initgnumeric (void)
        GnmModule = PyModule_Create (&GnmModuleDef);
        module_dict = PyModule_GetDict (GnmModule);
 
-       gnm_py_dict_store
-               (module_dict, "TRUE", py_new_Boolean_object (TRUE));
-       gnm_py_dict_store
-               (module_dict, "FALSE", py_new_Boolean_object (FALSE));
+       // For historical reasons.  New code use python True/False.
+       gnm_py_dict_store (module_dict, "TRUE", PyBool_FromLong (TRUE));
+       gnm_py_dict_store (module_dict, "FALSE", PyBool_FromLong (FALSE));
 
        gnm_py_dict_store
                (module_dict, "GnumericError",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]