Skip to content

Commit ac8874e

Browse files
committed
Remove support for Python 2.7 from C code
Most of this is removing code for PY_VERSION_MINOR < 3, removing the guards for PY_VERSION_MAJOR >= 3, and removing all unnecessary macros that would now have a single definition (e.g., PyText_Check -> PyUnicode_Check) in favour of using the direct Python C API for clarity. Only in minor circumstances some small logic needed to be changed. Signed-off-by: Rodrigo Tobar <[email protected]>
1 parent 519949a commit ac8874e

17 files changed

+136
-577
lines changed

src/c/_cffi_backend.c

Lines changed: 93 additions & 334 deletions
Large diffs are not rendered by default.

src/c/call_python.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static PyObject *_get_interpstate_dict(void)
4747
/* from there on, we know the (sub-)interpreter is still valid */
4848

4949
if (attr_name == NULL) {
50-
attr_name = PyText_InternFromString("__cffi_backend_extern_py");
50+
attr_name = PyUnicode_InternFromString("__cffi_backend_extern_py");
5151
if (attr_name == NULL)
5252
goto error;
5353
}
@@ -90,7 +90,7 @@ static PyObject *_ffi_def_extern_decorator(PyObject *outer_args, PyObject *fn)
9090
name = PyObject_GetAttrString(fn, "__name__");
9191
if (name == NULL)
9292
return NULL;
93-
s = PyText_AsUTF8(name);
93+
s = PyUnicode_AsUTF8(name);
9494
if (s == NULL) {
9595
Py_DECREF(name);
9696
return NULL;

src/c/cdlopen.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ static void *cdlopen_fetch(PyObject *libname, void *libhandle,
77

88
if (libhandle == NULL) {
99
PyErr_Format(FFIError, "library '%s' has been closed",
10-
PyText_AS_UTF8(libname));
10+
PyUnicode_AsUTF8(libname));
1111
return NULL;
1212
}
1313

@@ -16,7 +16,7 @@ static void *cdlopen_fetch(PyObject *libname, void *libhandle,
1616
if (address == NULL) {
1717
const char *error = dlerror();
1818
PyErr_Format(FFIError, "symbol '%s' not found in library '%s': %s",
19-
symbol, PyText_AS_UTF8(libname), error);
19+
symbol, PyUnicode_AsUTF8(libname), error);
2020
}
2121
return address;
2222
}
@@ -32,7 +32,7 @@ static int cdlopen_close(PyObject *libname, void *libhandle)
3232
if (libhandle != NULL && dlclose(libhandle) != 0) {
3333
const char *error = dlerror();
3434
PyErr_Format(FFIError, "closing library '%s': %s",
35-
PyText_AS_UTF8(libname), error);
35+
PyUnicode_AsUTF8(libname), error);
3636
return -1;
3737
}
3838
return 0;
@@ -195,13 +195,6 @@ static int ffiobj_init(PyObject *self, PyObject *args, PyObject *kwds)
195195
_CFFI_GETOP(nglobs[i].type_op) == _CFFI_OP_ENUM) {
196196
PyObject *o = PyTuple_GET_ITEM(globals, i * 2 + 1);
197197
nglobs[i].address = &_cdl_realize_global_int;
198-
#if PY_MAJOR_VERSION < 3
199-
if (PyInt_Check(o)) {
200-
nintconsts[i].neg = PyInt_AS_LONG(o) <= 0;
201-
nintconsts[i].value = (long long)PyInt_AS_LONG(o);
202-
}
203-
else
204-
#endif
205198
{
206199
nintconsts[i].neg = PyObject_RichCompareBool(o, Py_False,
207200
Py_LE);

src/c/cffi1_module.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static int init_ffi_lib(PyObject *m)
4646
return -1;
4747

4848
for (i = 0; all_dlopen_flags[i].name != NULL; i++) {
49-
x = PyInt_FromLong(all_dlopen_flags[i].value);
49+
x = PyLong_FromLong(all_dlopen_flags[i].value);
5050
if (x == NULL)
5151
return -1;
5252
res = PyDict_SetItemString(FFI_Type.tp_dict,
@@ -116,7 +116,6 @@ static int make_included_tuples(char *module_name,
116116

117117
static PyObject *_my_Py_InitModule(char *module_name)
118118
{
119-
#if PY_MAJOR_VERSION >= 3
120119
struct PyModuleDef *module_def, local_module_def = {
121120
PyModuleDef_HEAD_INIT,
122121
module_name,
@@ -137,9 +136,6 @@ static PyObject *_my_Py_InitModule(char *module_name)
137136
}
138137
# endif
139138
return m;
140-
#else
141-
return Py_InitModule(module_name, NULL);
142-
#endif
143139
}
144140

145141
static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg)
@@ -211,12 +207,10 @@ static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg)
211207
(PyObject *)lib) < 0)
212208
return NULL;
213209

214-
#if PY_MAJOR_VERSION >= 3
215210
/* add manually 'module_name' in sys.modules: it seems that
216211
Py_InitModule() is not enough to do that */
217212
if (PyDict_SetItemString(modules_dict, module_name, m) < 0)
218213
return NULL;
219-
#endif
220214

221215
return m;
222216
}

src/c/cglob.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static void *fetch_global_var_addr(GlobSupportObject *gs)
7474
}
7575
if (data == NULL) {
7676
PyErr_Format(FFIError, "global variable '%s' is at address NULL",
77-
PyText_AS_UTF8(gs->gs_name));
77+
PyUnicode_AsUTF8(gs->gs_name));
7878
return NULL;
7979
}
8080
return data;

src/c/commontypes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static PyObject *b__get_common_types(PyObject *self, PyObject *arg)
205205
size_t i;
206206
for (i = 0; i < num_common_simple_types; i++) {
207207
const char *s = common_simple_types[i];
208-
PyObject *o = PyText_FromString(s + strlen(s) + 1);
208+
PyObject *o = PyUnicode_FromString(s + strlen(s) + 1);
209209
if (o == NULL)
210210
return NULL;
211211
err = PyDict_SetItemString(arg, s, o);

src/c/ffi_obj.c

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ static CTypeDescrObject *_ffi_type(FFIObject *ffi, PyObject *arg,
185185
/* Returns the CTypeDescrObject from the user-supplied 'arg'.
186186
Does not return a new reference!
187187
*/
188-
if ((accept & ACCEPT_STRING) && PyText_Check(arg)) {
188+
if ((accept & ACCEPT_STRING) && PyUnicode_Check(arg)) {
189189
PyObject *types_dict = ffi->types_builder.types_dict;
190190
/* The types_dict keeps the reference alive. Items are never removed */
191191
PyObject *x = PyDict_GetItem(types_dict, arg);
192192

193193
if (x == NULL) {
194-
const char *input_text = PyText_AS_UTF8(arg);
194+
const char *input_text = PyUnicode_AsUTF8(arg);
195195
struct _cffi_parse_info_s info;
196196
info.ctx = &ffi->types_builder.ctx;
197197
info.output_size = FFI_COMPLEXITY_OUTPUT;
@@ -241,17 +241,6 @@ static CTypeDescrObject *_ffi_type(FFIObject *ffi, PyObject *arg,
241241
else if ((accept & ACCEPT_CDATA) && CData_Check(arg)) {
242242
return ((CDataObject *)arg)->c_type;
243243
}
244-
#if PY_MAJOR_VERSION < 3
245-
else if (PyUnicode_Check(arg)) {
246-
CTypeDescrObject *result;
247-
arg = PyUnicode_AsASCIIString(arg);
248-
if (arg == NULL)
249-
return NULL;
250-
result = _ffi_type(ffi, arg, accept);
251-
Py_DECREF(arg);
252-
return result;
253-
}
254-
#endif
255244
else {
256245
const char *m1 = (accept & ACCEPT_STRING) ? "string" : "";
257246
const char *m2 = (accept & ACCEPT_CTYPE) ? "ctype object" : "";
@@ -291,7 +280,7 @@ static PyObject *ffi_sizeof(FFIObject *self, PyObject *arg)
291280
return NULL;
292281
}
293282
}
294-
return PyInt_FromSsize_t(size);
283+
return PyLong_FromSsize_t(size);
295284
}
296285

297286
PyDoc_STRVAR(ffi_alignof_doc,
@@ -308,7 +297,7 @@ static PyObject *ffi_alignof(FFIObject *self, PyObject *arg)
308297
align = get_alignment(ct);
309298
if (align < 0)
310299
return NULL;
311-
return PyInt_FromLong(align);
300+
return PyLong_FromLong(align);
312301
}
313302

314303
PyDoc_STRVAR(ffi_typeof_doc,
@@ -526,7 +515,7 @@ static PyObject *ffi_offsetof(FFIObject *self, PyObject *args)
526515
return NULL;
527516
offset += ofs1;
528517
}
529-
return PyInt_FromSsize_t(offset);
518+
return PyLong_FromSsize_t(offset);
530519
}
531520

532521
PyDoc_STRVAR(ffi_addressof_doc,
@@ -639,9 +628,7 @@ static PyObject *ffi_getctype(FFIObject *self, PyObject *args, PyObject *kwds)
639628
CTypeDescrObject *ct;
640629
size_t replace_with_len;
641630
static char *keywords[] = {"cdecl", "replace_with", NULL};
642-
#if PY_MAJOR_VERSION >= 3
643631
PyObject *u;
644-
#endif
645632

646633
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:getctype", keywords,
647634
&c_decl, &replace_with))
@@ -675,14 +662,12 @@ static PyObject *ffi_getctype(FFIObject *self, PyObject *args, PyObject *kwds)
675662
if (add_paren)
676663
p[replace_with_len] = ')';
677664

678-
#if PY_MAJOR_VERSION >= 3
679665
/* bytes -> unicode string */
680666
u = PyUnicode_DecodeLatin1(PyBytes_AS_STRING(res),
681667
PyBytes_GET_SIZE(res),
682668
NULL);
683669
Py_DECREF(res);
684670
res = u;
685-
#endif
686671

687672
return res;
688673
}
@@ -931,7 +916,7 @@ static PyObject *ffi_list_types(FFIObject *self, PyObject *noargs)
931916
goto error;
932917

933918
for (i = 0; i < n1; i++) {
934-
o = PyText_FromString(self->types_builder.ctx.typenames[i].name);
919+
o = PyUnicode_FromString(self->types_builder.ctx.typenames[i].name);
935920
if (o == NULL)
936921
goto error;
937922
PyList_SET_ITEM(lst[0], i, o);
@@ -945,7 +930,7 @@ static PyObject *ffi_list_types(FFIObject *self, PyObject *noargs)
945930
if (s->name[0] == '$')
946931
continue;
947932

948-
o = PyText_FromString(s->name);
933+
o = PyUnicode_FromString(s->name);
949934
if (o == NULL)
950935
goto error;
951936
index = (s->flags & _CFFI_F_UNION) ? 2 : 1;
@@ -990,22 +975,13 @@ PyDoc_STRVAR(ffi_init_once_doc,
990975
"of function() is done. If function() raises an exception, it is\n"
991976
"propagated and nothing is cached.");
992977

993-
#if PY_MAJOR_VERSION < 3
994-
/* PyCapsule_New is redefined to be PyCObject_FromVoidPtr in _cffi_backend,
995-
which gives 2.6 compatibility; but the destructor signature is different */
996-
static void _free_init_once_lock(void *lock)
997-
{
998-
PyThread_free_lock((PyThread_type_lock)lock);
999-
}
1000-
#else
1001978
static void _free_init_once_lock(PyObject *capsule)
1002979
{
1003980
PyThread_type_lock lock;
1004981
lock = PyCapsule_GetPointer(capsule, "cffi_init_once_lock");
1005982
if (lock != NULL)
1006983
PyThread_free_lock(lock);
1007984
}
1008-
#endif
1009985

1010986
static PyObject *ffi_init_once(FFIObject *self, PyObject *args, PyObject *kwds)
1011987
{

src/c/file_emulator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static FILE *PyFile_AsFile(PyObject *ob_file)
5151
ob_mode = PyObject_GetAttrString(ob_file, "mode");
5252
if (ob_mode == NULL)
5353
goto fail;
54-
mode = PyText_AsUTF8(ob_mode);
54+
mode = PyUnicode_AsUTF8(ob_mode);
5555
if (mode == NULL)
5656
goto fail;
5757

src/c/lib_obj.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ static int lib_traverse(LibObject *lib, visitproc visit, void *arg)
111111

112112
static PyObject *lib_repr(LibObject *lib)
113113
{
114-
return PyText_FromFormat("<Lib object for '%.200s'>",
115-
PyText_AS_UTF8(lib->l_libname));
114+
return PyUnicode_FromFormat("<Lib object for '%.200s'>",
115+
PyUnicode_AsUTF8(lib->l_libname));
116116
}
117117

118118
static PyObject *lib_build_cpython_func(LibObject *lib,
@@ -131,7 +131,7 @@ static PyObject *lib_build_cpython_func(LibObject *lib,
131131
int i, type_index = _CFFI_GETARG(g->type_op);
132132
_cffi_opcode_t *opcodes = lib->l_types_builder->ctx.types;
133133
static const char *const format = ";\n\nCFFI C function from %s.lib";
134-
const char *libname = PyText_AS_UTF8(lib->l_libname);
134+
const char *libname = PyUnicode_AsUTF8(lib->l_libname);
135135
struct funcbuilder_s funcbuilder;
136136

137137
/* return type: */
@@ -214,7 +214,7 @@ static PyObject *lib_build_and_cache_attr(LibObject *lib, PyObject *name,
214214
const struct _cffi_global_s *g;
215215
CTypeDescrObject *ct;
216216
builder_c_t *types_builder = lib->l_types_builder;
217-
const char *s = PyText_AsUTF8(name);
217+
const char *s = PyUnicode_AsUTF8(name);
218218
if (s == NULL)
219219
return NULL;
220220

@@ -269,7 +269,7 @@ static PyObject *lib_build_and_cache_attr(LibObject *lib, PyObject *name,
269269
PyErr_Format(PyExc_AttributeError,
270270
"cffi library '%.200s' has no function, constant "
271271
"or global variable named '%.200s'",
272-
PyText_AS_UTF8(lib->l_libname), s);
272+
PyUnicode_AsUTF8(lib->l_libname), s);
273273
return NULL;
274274
}
275275

@@ -465,7 +465,7 @@ static PyObject *_lib_dir1(LibObject *lib, int ignore_global_vars)
465465
if (op == _CFFI_OP_GLOBAL_VAR || op == _CFFI_OP_GLOBAL_VAR_F)
466466
continue;
467467
}
468-
s = PyText_FromString(g[i].name);
468+
s = PyUnicode_FromString(g[i].name);
469469
if (s == NULL)
470470
goto error;
471471
PyList_SET_ITEM(lst, count, s);
@@ -489,7 +489,7 @@ static PyObject *_lib_dict(LibObject *lib)
489489
return NULL;
490490

491491
for (i = 0; i < total; i++) {
492-
name = PyText_FromString(g[i].name);
492+
name = PyUnicode_FromString(g[i].name);
493493
if (name == NULL)
494494
goto error;
495495

@@ -521,7 +521,7 @@ static PyObject *lib_getattr(LibObject *lib, PyObject *name)
521521

522522
missing:
523523
/*** ATTRIBUTEERROR IS SET HERE ***/
524-
p = PyText_AsUTF8(name);
524+
p = PyUnicode_AsUTF8(name);
525525
if (p == NULL)
526526
return NULL;
527527
if (strcmp(p, "__all__") == 0) {
@@ -545,16 +545,14 @@ static PyObject *lib_getattr(LibObject *lib, PyObject *name)
545545
module-like behavior */
546546
if (strcmp(p, "__name__") == 0) {
547547
PyErr_Clear();
548-
return PyText_FromFormat("%s.lib", PyText_AS_UTF8(lib->l_libname));
548+
return PyUnicode_FromFormat("%s.lib", PyUnicode_AsUTF8(lib->l_libname));
549549
}
550-
#if PY_MAJOR_VERSION >= 3
551550
if (strcmp(p, "__loader__") == 0 || strcmp(p, "__spec__") == 0) {
552551
/* some more module-like behavior hacks */
553552
PyErr_Clear();
554553
Py_INCREF(Py_None);
555554
return Py_None;
556555
}
557-
#endif
558556
return NULL;
559557
}
560558

@@ -574,7 +572,7 @@ static int lib_setattr(LibObject *lib, PyObject *name, PyObject *val)
574572

575573
PyErr_Format(PyExc_AttributeError,
576574
"cannot write to function or constant '%.200s'",
577-
PyText_Check(name) ? PyText_AS_UTF8(name) : "?");
575+
PyUnicode_Check(name) ? PyUnicode_AsUTF8(name) : "?");
578576
return -1;
579577
}
580578

@@ -632,7 +630,7 @@ static LibObject *lib_internal_new(FFIObject *ffi, const char *module_name,
632630
LibObject *lib;
633631
PyObject *libname, *dict;
634632

635-
libname = PyText_FromString(module_name);
633+
libname = PyUnicode_FromString(module_name);
636634
if (libname == NULL)
637635
goto err1;
638636

@@ -699,7 +697,7 @@ static PyObject *address_of_global_var(PyObject *args)
699697

700698
/* rebuild a string from 'varname', to do typechecks and to force
701699
a unicode back to a plain string (on python 2) */
702-
o_varname = PyText_FromString(varname);
700+
o_varname = PyUnicode_FromString(varname);
703701
if (o_varname == NULL)
704702
return NULL;
705703

0 commit comments

Comments
 (0)