HL-HDF
pyhlcompat.h
Go to the documentation of this file.
1/* --------------------------------------------------------------------
2Copyright (C) 2009 Swedish Meteorological and Hydrological Institute, SMHI,
3
4This file is part of HLHDF.
5
6HLHDF is free software: you can redistribute it and/or modify
7it under the terms of the GNU Lesser General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11HLHDF is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU Lesser General Public License for more details.
15
16You should have received a copy of the GNU Lesser General Public License
17along with HLHDF. If not, see <http://www.gnu.org/licenses/>.
18------------------------------------------------------------------------*/
19
27/* Define this to ensure that we are not using old API:s from Numpy. However, for now we are more interested in getting the
28 * code to build on both 2.7 and 3.x
29 */
30/*
31 * #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
32 */
33#include "Python.h"
34
35#ifndef PYHLHDFCOMPAT_H
36#define PYHLHDFCOMPAT_H
37
38
45int PyHlhdfAPI_CompareWithASCIIString(PyObject* ptr, const char* name);
46
51PyObject* PyHlhdf_StringOrUnicode_FromASCII(const char *buffer, Py_ssize_t size);
52
53#if PY_MAJOR_VERSION >= 3
54#define PyString_FromStringAndSize(str, len) PyUnicode_FromStringAndSize(str, len)
55#define PyInt_Check PyLong_Check
56#define PyInt_FromLong PyLong_FromLong
57#define PyInt_AsLong PyLong_AsLong
58#define PyInt_Type PyLong_Type
59#define PyString_Check PyUnicode_Check
60#define PyString_AsString PyUnicode_AsUTF8
61#define PyString_FromString PyUnicode_FromString
62#define PyString_FromFormat PyUnicode_FromFormat
63#endif
64
65#if PY_MAJOR_VERSION >= 3
66#define PY_ATTRO_NAME_TO_STRING PyUnicode_AsUTF8
67#else
68#define PY_ATTRO_NAME_TO_STRING PyString_AsString
69#endif
70
71#define PY_COMPARE_ATTRO_NAME_WITH_STRING(ptr, name) PyHlhdfAPI_CompareWithASCIIString(ptr, name)
72
73#define PY_COMPARE_STRING_WITH_ATTRO_NAME(name, ptr) PyHlhdfAPI_CompareWithASCIIString(ptr, name)
74
75#if PY_MAJOR_VERSION >= 3
76#define MOD_INIT_ERROR NULL
77#define MOD_INIT_SUCCESS(val) val
78#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
79
80#define MOD_INIT_DEF(ob, name, doc, methods) \
81 static struct PyModuleDef moduledef = { \
82 PyModuleDef_HEAD_INIT, name, doc, -1, methods, NULL, NULL, NULL, NULL }; \
83 ob = PyModule_Create(&moduledef);
84
85#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
86static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
87{ ob->ob_type = type; }
88#define Py_SET_TYPE(ob, type) _Py_SET_TYPE((PyObject*)(ob), type)
89#endif
90
91#define MOD_INIT_SETUP_TYPE(itype, otype) Py_SET_TYPE(&itype, otype)
92#define MOD_INIT_VERIFY_TYPE_READY(type) if (PyType_Ready(type) < 0) return MOD_INIT_ERROR
93
94#else
95#define MOD_INIT_ERROR
96#define MOD_INIT_SUCCESS(val)
97#define MOD_INIT(name) void init##name(void)
98#define MOD_INIT_DEF(ob, name, doc, methods) \
99 ob = Py_InitModule3(name, methods, doc);
100#define MOD_INIT_SETUP_TYPE(itype, otype) itype.ob_type = otype
101#define MOD_INIT_VERIFY_TYPE_READY(type)
102#endif
103
104
108#define MOD_DIR_FORWARD_DECLARE(name) static PyObject* _##name##__dir__(name *self)
109
110#define MOD_DIR_REFERENCE(name) _##name##__dir__
111
112#define MOD_DIR_APPEND(list, str) \
113 do { \
114 PyObject *o = PyUnicode_FromString(str); \
115 if (o != NULL) \
116 PyList_Append(list, o); \
117 Py_XDECREF(o); \
118 } while (0)
119
120#define MOD_DIR_FUNCTION(name, method_listing) static PyObject * _##name##__dir__(name *self) { \
121 int i=0; \
122 PyObject* rc = PyList_New(0); \
123 if (!rc) \
124 return NULL; \
125\
126 while (method_listing[i].ml_name != NULL) { \
127 MOD_DIR_APPEND(rc, method_listing[i++].ml_name); \
128 } \
129\
130 return rc; \
131}
132
133
134#endif
PyObject * PyHlhdf_StringOrUnicode_FromASCII(const char *buffer, Py_ssize_t size)
Tries to get same behaviour for the result returned when getting a string from the data node.
Definition pyhlcompat.c:48
int PyHlhdfAPI_CompareWithASCIIString(PyObject *ptr, const char *name)
Tests if a python object string (or unicode) is equal to name.
Definition pyhlcompat.c:29