Skip to content

Commit 88ef0b5

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 b57a92c commit 88ef0b5

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,
@@ -131,9 +130,6 @@ static PyObject *_my_Py_InitModule(char *module_name)
131130
return PyErr_NoMemory();
132131
*module_def = local_module_def;
133132
return PyModule_Create(module_def);
134-
#else
135-
return Py_InitModule(module_name, NULL);
136-
#endif
137133
}
138134

139135
static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg)
@@ -205,12 +201,10 @@ static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg)
205201
(PyObject *)lib) < 0)
206202
return NULL;
207203

208-
#if PY_MAJOR_VERSION >= 3
209204
/* add manually 'module_name' in sys.modules: it seems that
210205
Py_InitModule() is not enough to do that */
211206
if (PyDict_SetItemString(modules_dict, module_name, m) < 0)
212207
return NULL;
213-
#endif
214208

215209
return m;
216210
}

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
@@ -183,12 +183,12 @@ static CTypeDescrObject *_ffi_type(FFIObject *ffi, PyObject *arg,
183183
/* Returns the CTypeDescrObject from the user-supplied 'arg'.
184184
Does not return a new reference!
185185
*/
186-
if ((accept & ACCEPT_STRING) && PyText_Check(arg)) {
186+
if ((accept & ACCEPT_STRING) && PyUnicode_Check(arg)) {
187187
PyObject *types_dict = ffi->types_builder.types_dict;
188188
PyObject *x = PyDict_GetItem(types_dict, arg);
189189

190190
if (x == NULL) {
191-
const char *input_text = PyText_AS_UTF8(arg);
191+
const char *input_text = PyUnicode_AsUTF8(arg);
192192
int err, index = parse_c_type(&ffi->info, input_text);
193193
if (index < 0)
194194
return _ffi_bad_type(ffi, input_text);
@@ -226,17 +226,6 @@ static CTypeDescrObject *_ffi_type(FFIObject *ffi, PyObject *arg,
226226
else if ((accept & ACCEPT_CDATA) && CData_Check(arg)) {
227227
return ((CDataObject *)arg)->c_type;
228228
}
229-
#if PY_MAJOR_VERSION < 3
230-
else if (PyUnicode_Check(arg)) {
231-
CTypeDescrObject *result;
232-
arg = PyUnicode_AsASCIIString(arg);
233-
if (arg == NULL)
234-
return NULL;
235-
result = _ffi_type(ffi, arg, accept);
236-
Py_DECREF(arg);
237-
return result;
238-
}
239-
#endif
240229
else {
241230
const char *m1 = (accept & ACCEPT_STRING) ? "string" : "";
242231
const char *m2 = (accept & ACCEPT_CTYPE) ? "ctype object" : "";
@@ -272,7 +261,7 @@ static PyObject *ffi_sizeof(FFIObject *self, PyObject *arg)
272261
return NULL;
273262
}
274263
}
275-
return PyInt_FromSsize_t(size);
264+
return PyLong_FromSsize_t(size);
276265
}
277266

278267
PyDoc_STRVAR(ffi_alignof_doc,
@@ -289,7 +278,7 @@ static PyObject *ffi_alignof(FFIObject *self, PyObject *arg)
289278
align = get_alignment(ct);
290279
if (align < 0)
291280
return NULL;
292-
return PyInt_FromLong(align);
281+
return PyLong_FromLong(align);
293282
}
294283

295284
PyDoc_STRVAR(ffi_typeof_doc,
@@ -507,7 +496,7 @@ static PyObject *ffi_offsetof(FFIObject *self, PyObject *args)
507496
return NULL;
508497
offset += ofs1;
509498
}
510-
return PyInt_FromSsize_t(offset);
499+
return PyLong_FromSsize_t(offset);
511500
}
512501

513502
PyDoc_STRVAR(ffi_addressof_doc,
@@ -620,9 +609,7 @@ static PyObject *ffi_getctype(FFIObject *self, PyObject *args, PyObject *kwds)
620609
CTypeDescrObject *ct;
621610
size_t replace_with_len;
622611
static char *keywords[] = {"cdecl", "replace_with", NULL};
623-
#if PY_MAJOR_VERSION >= 3
624612
PyObject *u;
625-
#endif
626613

627614
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:getctype", keywords,
628615
&c_decl, &replace_with))
@@ -656,14 +643,12 @@ static PyObject *ffi_getctype(FFIObject *self, PyObject *args, PyObject *kwds)
656643
if (add_paren)
657644
p[replace_with_len] = ')';
658645

659-
#if PY_MAJOR_VERSION >= 3
660646
/* bytes -> unicode string */
661647
u = PyUnicode_DecodeLatin1(PyBytes_AS_STRING(res),
662648
PyBytes_GET_SIZE(res),
663649
NULL);
664650
Py_DECREF(res);
665651
res = u;
666-
#endif
667652

668653
return res;
669654
}
@@ -912,7 +897,7 @@ static PyObject *ffi_list_types(FFIObject *self, PyObject *noargs)
912897
goto error;
913898

914899
for (i = 0; i < n1; i++) {
915-
o = PyText_FromString(self->types_builder.ctx.typenames[i].name);
900+
o = PyUnicode_FromString(self->types_builder.ctx.typenames[i].name);
916901
if (o == NULL)
917902
goto error;
918903
PyList_SET_ITEM(lst[0], i, o);
@@ -926,7 +911,7 @@ static PyObject *ffi_list_types(FFIObject *self, PyObject *noargs)
926911
if (s->name[0] == '$')
927912
continue;
928913

929-
o = PyText_FromString(s->name);
914+
o = PyUnicode_FromString(s->name);
930915
if (o == NULL)
931916
goto error;
932917
index = (s->flags & _CFFI_F_UNION) ? 2 : 1;
@@ -971,22 +956,13 @@ PyDoc_STRVAR(ffi_init_once_doc,
971956
"of function() is done. If function() raises an exception, it is\n"
972957
"propagated and nothing is cached.");
973958

974-
#if PY_MAJOR_VERSION < 3
975-
/* PyCapsule_New is redefined to be PyCObject_FromVoidPtr in _cffi_backend,
976-
which gives 2.6 compatibility; but the destructor signature is different */
977-
static void _free_init_once_lock(void *lock)
978-
{
979-
PyThread_free_lock((PyThread_type_lock)lock);
980-
}
981-
#else
982959
static void _free_init_once_lock(PyObject *capsule)
983960
{
984961
PyThread_type_lock lock;
985962
lock = PyCapsule_GetPointer(capsule, "cffi_init_once_lock");
986963
if (lock != NULL)
987964
PyThread_free_lock(lock);
988965
}
989-
#endif
990966

991967
static PyObject *ffi_init_once(FFIObject *self, PyObject *args, PyObject *kwds)
992968
{

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)