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
1=== modified file 'breezy/_export_c_api.h'
2--- breezy/_export_c_api.h 2017-06-22 00:47:16 +0000
3+++ breezy/_export_c_api.h 2017-06-27 00:16:51 +0000
4@@ -15,8 +15,6 @@
5 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 */
7
8-/* GZ 2017-06-22: This header needs updating to use Capsules over CObjects */
9-
10 /* This file contains helper functions for exporting a C API for a CPython
11 * extension module.
12 */
13@@ -44,7 +42,7 @@
14 _export_function(PyObject *module, char *funcname, void *func, char *signature)
15 {
16 PyObject *d = NULL;
17- PyObject *c_obj = NULL;
18+ PyObject *capsule = NULL;
19
20 d = PyObject_GetAttrString(module, _C_API_NAME);
21 if (!d) {
22@@ -56,15 +54,15 @@
23 if (PyModule_AddObject(module, _C_API_NAME, d) < 0)
24 goto bad;
25 }
26- c_obj = PyCObject_FromVoidPtrAndDesc(func, signature, 0);
27- if (!c_obj)
28+ capsule = PyCapsule_New(func, signature, 0);
29+ if (!capsule)
30 goto bad;
31- if (PyDict_SetItemString(d, funcname, c_obj) < 0)
32+ if (PyDict_SetItemString(d, funcname, capsule) < 0)
33 goto bad;
34 Py_DECREF(d);
35 return 0;
36 bad:
37- Py_XDECREF(c_obj);
38+ Py_XDECREF(capsule);
39 Py_XDECREF(d);
40 return -1;
41 }
42
43=== modified file 'breezy/_import_c_api.h'
44--- breezy/_import_c_api.h 2009-10-12 21:44:27 +0000
45+++ breezy/_import_c_api.h 2017-06-27 00:16:51 +0000
46@@ -44,36 +44,28 @@
47 void **func, const char *signature)
48 {
49 PyObject *d = NULL;
50- PyObject *c_obj = NULL;
51- const char *desc = NULL;
52+ PyObject *capsule = NULL;
53+ void *pointer;
54
55- /* (char *) because Python2.4 defines this as (char *) rather than
56- * (const char *)
57- */
58- d = PyObject_GetAttrString(module, (char *)_C_API_NAME);
59+ d = PyObject_GetAttrString(module, _C_API_NAME);
60 if (!d) {
61 // PyObject_GetAttrString sets an appropriate exception
62 goto bad;
63 }
64- c_obj = PyDict_GetItemString(d, funcname);
65- if (!c_obj) {
66+ capsule = PyDict_GetItemString(d, funcname);
67+ if (!capsule) {
68 // PyDict_GetItemString does not set an exception
69 PyErr_Format(PyExc_AttributeError,
70 "Module %s did not export a function named %s\n",
71 PyModule_GetName(module), funcname);
72 goto bad;
73 }
74- desc = (char *)PyCObject_GetDesc(c_obj);
75- if (!desc || strcmp(desc, signature) != 0) {
76- if (desc == NULL) {
77- desc = "<null>";
78- }
79- PyErr_Format(PyExc_TypeError,
80- "C function %s.%s has wrong signature (expected %s, got %s)",
81- PyModule_GetName(module), funcname, signature, desc);
82+ pointer = PyCapsule_GetPointer(capsule, signature);
83+ if (!pointer) {
84+ // PyCapsule_GetPointer sets an error with a little context
85 goto bad;
86 }
87- *func = PyCObject_AsVoidPtr(c_obj);
88+ *func = pointer;
89 Py_DECREF(d);
90 return 0;
91 bad:
92
93=== modified file 'setup.py'
94--- setup.py 2017-06-13 02:58:33 +0000
95+++ setup.py 2017-06-27 00:16:51 +0000
96@@ -285,6 +285,9 @@
97 include_dirs=include_dirs))
98
99
100+add_cython_extension('breezy._simple_set_pyx')
101+ext_modules.append(Extension('breezy._static_tuple_c',
102+ ['breezy/_static_tuple_c.c']))
103 add_cython_extension('breezy._annotator_pyx')
104 add_cython_extension('breezy._bencode_pyx')
105 add_cython_extension('breezy._chunks_to_lines_pyx')
106@@ -303,9 +306,6 @@
107 add_cython_extension('breezy.bzr._chk_map_pyx')
108 ext_modules.append(Extension('breezy._patiencediff_c',
109 ['breezy/_patiencediff_c.c']))
110-add_cython_extension('breezy._simple_set_pyx')
111-ext_modules.append(Extension('breezy._static_tuple_c',
112- ['breezy/_static_tuple_c.c']))
113 add_cython_extension('breezy.bzr._btree_serializer_pyx')
114
115

Subscribers

People subscribed via source and target branches