Merge lp:~gz/brz/py3_static_tuple_import into lp:brz

Proposed by Martin Packman
Status: Merged
Approved by: Martin Packman
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~gz/brz/py3_static_tuple_import
Merge into: lp:brz
Diff against target: 113 lines (+17/-27)
3 files modified
breezy/_export_c_api.h (+5/-7)
breezy/_import_c_api.h (+9/-17)
setup.py (+3/-3)
To merge this branch: bzr merge lp:~gz/brz/py3_static_tuple_import
Reviewer Review Type Date Requested Status
Jelmer Vernooij Approve
Review via email: mp+326329@code.launchpad.net

Commit message

Switch _*_c_api.h helpers from PyCObject to PyCapsule

Description of the change

#3 of the options on making _static_tuple_c imports work for Python 3, and the wimp-out-est method.

Just switches from CObject to Capsule leaving all the existing semantics alone. This is safe(*), but there's still a huge pile of logic to support imports for just one c module.

Drive-by moves the order in setup.py around as a bunch of stuff depends on StaticTuple anyway so it's best to fail early when changing things.

(*) I think.

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'breezy/_export_c_api.h'
--- breezy/_export_c_api.h 2017-06-22 00:47:16 +0000
+++ breezy/_export_c_api.h 2017-06-27 00:16:51 +0000
@@ -15,8 +15,6 @@
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */16 */
1717
18/* GZ 2017-06-22: This header needs updating to use Capsules over CObjects */
19
20/* This file contains helper functions for exporting a C API for a CPython18/* This file contains helper functions for exporting a C API for a CPython
21 * extension module.19 * extension module.
22 */20 */
@@ -44,7 +42,7 @@
44_export_function(PyObject *module, char *funcname, void *func, char *signature)42_export_function(PyObject *module, char *funcname, void *func, char *signature)
45{43{
46 PyObject *d = NULL;44 PyObject *d = NULL;
47 PyObject *c_obj = NULL;45 PyObject *capsule = NULL;
4846
49 d = PyObject_GetAttrString(module, _C_API_NAME);47 d = PyObject_GetAttrString(module, _C_API_NAME);
50 if (!d) {48 if (!d) {
@@ -56,15 +54,15 @@
56 if (PyModule_AddObject(module, _C_API_NAME, d) < 0)54 if (PyModule_AddObject(module, _C_API_NAME, d) < 0)
57 goto bad;55 goto bad;
58 }56 }
59 c_obj = PyCObject_FromVoidPtrAndDesc(func, signature, 0);57 capsule = PyCapsule_New(func, signature, 0);
60 if (!c_obj)58 if (!capsule)
61 goto bad;59 goto bad;
62 if (PyDict_SetItemString(d, funcname, c_obj) < 0)60 if (PyDict_SetItemString(d, funcname, capsule) < 0)
63 goto bad;61 goto bad;
64 Py_DECREF(d);62 Py_DECREF(d);
65 return 0;63 return 0;
66bad:64bad:
67 Py_XDECREF(c_obj);65 Py_XDECREF(capsule);
68 Py_XDECREF(d);66 Py_XDECREF(d);
69 return -1;67 return -1;
70}68}
7169
=== modified file 'breezy/_import_c_api.h'
--- breezy/_import_c_api.h 2009-10-12 21:44:27 +0000
+++ breezy/_import_c_api.h 2017-06-27 00:16:51 +0000
@@ -44,36 +44,28 @@
44 void **func, const char *signature)44 void **func, const char *signature)
45{45{
46 PyObject *d = NULL;46 PyObject *d = NULL;
47 PyObject *c_obj = NULL;47 PyObject *capsule = NULL;
48 const char *desc = NULL;48 void *pointer;
4949
50 /* (char *) because Python2.4 defines this as (char *) rather than50 d = PyObject_GetAttrString(module, _C_API_NAME);
51 * (const char *)
52 */
53 d = PyObject_GetAttrString(module, (char *)_C_API_NAME);
54 if (!d) {51 if (!d) {
55 // PyObject_GetAttrString sets an appropriate exception52 // PyObject_GetAttrString sets an appropriate exception
56 goto bad;53 goto bad;
57 }54 }
58 c_obj = PyDict_GetItemString(d, funcname);55 capsule = PyDict_GetItemString(d, funcname);
59 if (!c_obj) {56 if (!capsule) {
60 // PyDict_GetItemString does not set an exception57 // PyDict_GetItemString does not set an exception
61 PyErr_Format(PyExc_AttributeError,58 PyErr_Format(PyExc_AttributeError,
62 "Module %s did not export a function named %s\n",59 "Module %s did not export a function named %s\n",
63 PyModule_GetName(module), funcname);60 PyModule_GetName(module), funcname);
64 goto bad;61 goto bad;
65 }62 }
66 desc = (char *)PyCObject_GetDesc(c_obj);63 pointer = PyCapsule_GetPointer(capsule, signature);
67 if (!desc || strcmp(desc, signature) != 0) {64 if (!pointer) {
68 if (desc == NULL) {65 // PyCapsule_GetPointer sets an error with a little context
69 desc = "<null>";
70 }
71 PyErr_Format(PyExc_TypeError,
72 "C function %s.%s has wrong signature (expected %s, got %s)",
73 PyModule_GetName(module), funcname, signature, desc);
74 goto bad;66 goto bad;
75 }67 }
76 *func = PyCObject_AsVoidPtr(c_obj);68 *func = pointer;
77 Py_DECREF(d);69 Py_DECREF(d);
78 return 0;70 return 0;
79bad:71bad:
8072
=== modified file 'setup.py'
--- setup.py 2017-06-13 02:58:33 +0000
+++ setup.py 2017-06-27 00:16:51 +0000
@@ -285,6 +285,9 @@
285 include_dirs=include_dirs))285 include_dirs=include_dirs))
286286
287287
288add_cython_extension('breezy._simple_set_pyx')
289ext_modules.append(Extension('breezy._static_tuple_c',
290 ['breezy/_static_tuple_c.c']))
288add_cython_extension('breezy._annotator_pyx')291add_cython_extension('breezy._annotator_pyx')
289add_cython_extension('breezy._bencode_pyx')292add_cython_extension('breezy._bencode_pyx')
290add_cython_extension('breezy._chunks_to_lines_pyx')293add_cython_extension('breezy._chunks_to_lines_pyx')
@@ -303,9 +306,6 @@
303add_cython_extension('breezy.bzr._chk_map_pyx')306add_cython_extension('breezy.bzr._chk_map_pyx')
304ext_modules.append(Extension('breezy._patiencediff_c',307ext_modules.append(Extension('breezy._patiencediff_c',
305 ['breezy/_patiencediff_c.c']))308 ['breezy/_patiencediff_c.c']))
306add_cython_extension('breezy._simple_set_pyx')
307ext_modules.append(Extension('breezy._static_tuple_c',
308 ['breezy/_static_tuple_c.c']))
309add_cython_extension('breezy.bzr._btree_serializer_pyx')309add_cython_extension('breezy.bzr._btree_serializer_pyx')
310310
311311

Subscribers

People subscribed via source and target branches