diff options
author | Fred Wright <fw@fwright.net> | 2016-04-08 10:07:42 -0700 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2016-04-09 04:39:15 -0400 |
commit | 97be9754e1eb6fb83e114af1b2286e287793f693 (patch) | |
tree | ddf288e79d4208498b7aaa039b230bdbfe292be9 /python_compatibility.h | |
parent | 90413f5f16d66b69da2f8d5cd8d989aabb12514b (diff) | |
download | gpsd-97be9754e1eb6fb83e114af1b2286e287793f693.tar.gz |
Fixes Python C extensions for Python 3 compatibility.
These are necessary, but not sufficient, changes to make the C
extensions work "polyglot". These are believed to be complete as far
as the C code is concerned, and don't break Python 2 compatibility.
This puts all the stuff that needs to differ between Python 2 and
Python 3 into conditionally-defined macros in a new header file
python_compatibility.h. The definitions assume Python 2.6 or later.
In addition to the things requiring conditionals, the Lexer object was
using the deprecated tp_getattr entry, with a function based on
Py_FindMethod, which is gone in Python 3. However, the newer
tp_getattro entry can be pointed directly to PyObject_GenericGetAttr,
which works in Python 2 and 3.
Packet data returned by the Lexer is now 'bytes' in Python 3, which is
appropriate given that it may contain binary data. However, it means
that packet data can't be passed directly to anything expecting a
'str'. In Python 2, the data is just a 'str' as usual.
TESTED:
Ran "scons build-all check" and xgps (using Python 2.7). Also ran the
daemon and maidenhead-locator tests with Python 2.6.
Diffstat (limited to 'python_compatibility.h')
-rw-r--r-- | python_compatibility.h | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/python_compatibility.h b/python_compatibility.h new file mode 100644 index 00000000..635cd06c --- /dev/null +++ b/python_compatibility.h @@ -0,0 +1,44 @@ +/* + * python_compatibility.h -- macros for Python 2/3 compatibility + * + * This file is Copyright (c) 2016 by the GPSD project + * BSD terms apply: see the file COPYING in the distribution root for details. + * + * Definitions based on examples in "Supporting Python 3 - The Book Site" + * http://python3porting.com/cextensions.html + */ + +#ifndef _PYTHON_COMPATIBILITY_H_ +#define _PYTHON_COMPATIBILITY_H_ + +#include <Python.h> + +#if PY_MAJOR_VERSION >= 3 + +#define GPSD_PY_MODULE_INIT(name) PyMODINIT_FUNC PyInit_##name(void) + +#define GPSD_PY_MODULE_DEF(mod, name, doc, methods) \ + static struct PyModuleDef moduledef = { \ + PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \ + mod = PyModule_Create(&moduledef); + +#define GPSD_PY_MODULE_ERROR_VAL NULL +#define GPSD_PY_MODULE_SUCCESS_VAL(val) val + +#define GPSD_PY_BYTE_FORMAT "y#" + +#else /* !Python 3 */ + +#define GPSD_PY_MODULE_INIT(name) PyMODINIT_FUNC init##name(void) + +#define GPSD_PY_MODULE_DEF(mod, name, doc, methods) \ + mod = Py_InitModule3(name, methods, doc); + +#define GPSD_PY_MODULE_ERROR_VAL +#define GPSD_PY_MODULE_SUCCESS_VAL(val) + +#define GPSD_PY_BYTE_FORMAT "s#" + +#endif /* !Python 3 */ + +#endif /* _PYTHON_COMPATIBILITY_H_ */ |