[pygobject/more-build-warnings] pyg_pyobj_to_unichar_conv: port to new converters and add some tests
- From: Christoph Reiter <creiter src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject/more-build-warnings] pyg_pyobj_to_unichar_conv: port to new converters and add some tests
- Date: Thu, 12 Apr 2018 10:49:42 +0000 (UTC)
commit 1b670da89091041a6ec96baff02925f6398de024
Author: Christoph Reiter <reiter christoph gmail com>
Date: Thu Apr 12 12:47:47 2018 +0200
pyg_pyobj_to_unichar_conv: port to new converters and add some tests
This allows us to get rid of the deprecated Py_UNICODE API and the
deprecation warnings with Python 3.7.
gi/pygi-basictype.c | 2 +-
gi/pygi-basictype.h | 1 +
gi/pygi-type.c | 30 ++----------------------------
tests/test_internal_api.py | 22 ++++++++++++++++++++++
tests/testhelpermodule.c | 15 +++++++++++++++
5 files changed, 41 insertions(+), 29 deletions(-)
---
diff --git a/gi/pygi-basictype.c b/gi/pygi-basictype.c
index 21dcbe60..75db0b26 100644
--- a/gi/pygi-basictype.c
+++ b/gi/pygi-basictype.c
@@ -159,7 +159,7 @@ pygi_gfloat_to_py (gfloat value)
return PyFloat_FromDouble (value);
}
-static gboolean
+gboolean
pygi_gunichar_from_py (PyObject *py_arg, gunichar *result)
{
Py_ssize_t size;
diff --git a/gi/pygi-basictype.h b/gi/pygi-basictype.h
index 281eba6e..396cedc9 100644
--- a/gi/pygi-basictype.h
+++ b/gi/pygi-basictype.h
@@ -77,6 +77,7 @@ gboolean pygi_glong_from_py (PyObject *object, glong *result);
gboolean pygi_gulong_from_py (PyObject *object, gulong *result);
gboolean pygi_gint_from_py (PyObject *object, gint *result);
gboolean pygi_guint_from_py (PyObject *object, guint *result);
+gboolean pygi_gunichar_from_py (PyObject *py_arg, gunichar *result);
G_END_DECLS
diff --git a/gi/pygi-type.c b/gi/pygi-type.c
index 5ff5184a..0c5a428f 100644
--- a/gi/pygi-type.c
+++ b/gi/pygi-type.c
@@ -1297,35 +1297,9 @@ pyg_object_descr_doc_get(void)
*/
int pyg_pyobj_to_unichar_conv(PyObject* py_obj, void* ptr)
{
- gunichar* u = ptr;
- const Py_UNICODE* uni_buffer;
- PyObject* tmp_uni = NULL;
-
- if (PyUnicode_Check(py_obj)) {
- tmp_uni = py_obj;
- Py_INCREF(tmp_uni);
- }
- else {
- tmp_uni = PyUnicode_FromObject(py_obj);
- if (tmp_uni == NULL)
- goto failure;
- }
-
- if ( PyUnicode_GetSize(tmp_uni) != 1) {
- PyErr_SetString(PyExc_ValueError, "unicode character value must be 1 character uniode string");
- goto failure;
- }
- uni_buffer = PyUnicode_AsUnicode(tmp_uni);
- if ( uni_buffer == NULL)
- goto failure;
- *u = uni_buffer[0];
-
- Py_DECREF(tmp_uni);
+ if (!pygi_gunichar_from_py (py_obj, ptr))
+ return 0;
return 1;
-
- failure:
- Py_XDECREF(tmp_uni);
- return 0;
}
gboolean
diff --git a/tests/test_internal_api.py b/tests/test_internal_api.py
index 5f3f9b42..7b2b8ea5 100644
--- a/tests/test_internal_api.py
+++ b/tests/test_internal_api.py
@@ -1,10 +1,13 @@
+# -*- coding: utf-8 -*-
# -*- Mode: Python -*-
from __future__ import absolute_import
import unittest
+import pytest
from gi.repository import GLib, GObject
+from gi._compat import PY3
from . import testhelper
@@ -94,3 +97,22 @@ class TestErrors(unittest.TestCase):
def test_no_gerror(self):
callable_ = lambda: GLib.file_get_contents(__file__)
self.assertEqual(testhelper.test_gerror_exception(callable_), None)
+
+
+def test_to_unichar_conv():
+ assert testhelper.test_to_unichar_conv(u"A") == 65
+ assert testhelper.test_to_unichar_conv(u"Ä") == 196
+
+ if PY3:
+ with pytest.raises(TypeError):
+ assert testhelper.test_to_unichar_conv(b"\x65")
+ else:
+ assert testhelper.test_to_unichar_conv(b"\x65") == 0x65
+ with pytest.raises(ValueError):
+ assert testhelper.test_to_unichar_conv(b"\xff")
+
+ with pytest.raises(TypeError):
+ testhelper.test_to_unichar_conv(object())
+
+ with pytest.raises(TypeError):
+ testhelper.test_to_unichar_conv(u"AA")
diff --git a/tests/testhelpermodule.c b/tests/testhelpermodule.c
index 1c8f03ed..9fc4fc6f 100644
--- a/tests/testhelpermodule.c
+++ b/tests/testhelpermodule.c
@@ -617,10 +617,25 @@ _wrap_test_parse_constructor_args (PyObject *self, PyObject *args)
return PYGLIB_PyLong_FromLong (nparams);
}
+static PyObject *
+_wrap_test_to_unichar_conv (PyObject * self, PyObject *args)
+{
+ PyObject *obj;
+ gunichar result;
+
+ if (!PyArg_ParseTuple(args, "O", &obj))
+ return NULL;
+
+ if (!pyg_pyobj_to_unichar_conv (obj, &result))
+ return NULL;
+
+ return PYGLIB_PyLong_FromLong (result);
+}
static PyMethodDef testhelper_functions[] = {
{ "test_parse_constructor_args", (PyCFunction)_wrap_test_parse_constructor_args, METH_VARARGS },
{ "get_test_thread", (PyCFunction)_wrap_get_test_thread, METH_NOARGS },
+ { "test_to_unichar_conv", (PyCFunction)_wrap_test_to_unichar_conv, METH_VARARGS },
{ "get_unknown", (PyCFunction)_wrap_get_unknown, METH_NOARGS },
{ "create_test_type", (PyCFunction)_wrap_create_test_type, METH_NOARGS },
{ "test_g_object_new", (PyCFunction)_wrap_test_g_object_new, METH_NOARGS },
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]