1 /*
2  * Python Bindings for LZMA
3  *
4  * Copyright (c) 2004-2015 by Joachim Bauch, mail@joachim-bauch.de
5  * 7-Zip Copyright (C) 1999-2010 Igor Pavlov
6  * LZMA SDK Copyright (C) 1999-2010 Igor Pavlov
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * $Id$
23  *
24  */
25 
26 #ifndef ___PYLZMA__H___
27 #define ___PYLZMA__H___
28 
29 #include <Python.h>
30 
31 #include "../sdk/C/7zTypes.h"
32 
33 #ifndef min
34 #define min(a, b) ((a) < (b) ? (a) : (b))
35 #endif
36 
37 #define BLOCK_SIZE (128*1024)
38 
39 #define CHECK_NULL(a) if ((a) == NULL) { PyErr_NoMemory(); goto exit; }
40 #define DEC_AND_NULL(a) { Py_XDECREF(a); a = NULL; }
41 #define DELETE_AND_NULL(a) if (a != NULL) { delete a; a = NULL; }
42 #define FREE_AND_NULL(a) if (a != NULL) { free(a); a = NULL; }
43 #define CHECK_RANGE(x, a, b, msg) if ((x) < (a) || (x) > (b)) { PyErr_SetString(PyExc_ValueError, msg); goto exit; }
44 
45 #if defined(WITH_THREAD)
46 /* For Python 2.3 and above, use the PyGILState_ calls */
47 #if (PY_VERSION_HEX >= 0x02030000)
48 #define PYLZMA_USE_GILSTATE
49 #endif
50 
51 #if defined(PYLZMA_USE_GILSTATE)
52 // Python 2.3 and above
53 #define START_BLOCK_THREADS \
54     PyGILState_STATE __savestate = PyGILState_Ensure();
55 #define END_BLOCK_THREADS \
56     PyGILState_Release(__savestate);
57 #else
58 // Before Python 2.3
59 PyInterpreterState* _pylzma_interpreterState;
60 #define START_BLOCK_THREADS {                                   \
61         PyThreadState* prevState;                               \
62         PyThreadState* newState;                                \
63         PyEval_AcquireLock();                                   \
64         newState  = PyThreadState_New(_pylzma_interpreterState);\
65         prevState = PyThreadState_Swap(newState);
66 
67 #define END_BLOCK_THREADS                                       \
68         newState = PyThreadState_Swap(prevState);               \
69         PyThreadState_Clear(newState);                          \
70         PyEval_ReleaseLock();                                   \
71         PyThreadState_Delete(newState);                         \
72         }
73 #endif
74 #else  // WITH_THREADS
75 #define START_BLOCK_THREADS
76 #define END_BLOCK_THREADS
77 #endif
78 
79 #if (PY_VERSION_HEX < 0x02060000)
80 #define PyBytes_Check(ob)                       PyString_Check(ob)
81 #define PyBytes_AS_STRING(ob)                   PyString_AS_STRING(ob)
82 #define PyBytes_GET_SIZE(ob)                    PyString_GET_SIZE(ob)
83 #define PyBytes_Size(ob)                        PyString_Size(ob)
84 #define PyBytes_FromString(data)                PyString_FromString(data)
85 #define PyBytes_FromStringAndSize(data, size)   PyString_FromStringAndSize(data, size)
86 #define _PyBytes_Resize(data, newsize)          _PyString_Resize(data, newsize)
87 #endif
88 
89 #if !defined(Py_TYPE)
90 #define Py_TYPE(ob)     ((ob)->ob_type)
91 #endif
92 
93 #if !defined(PyVarObject_HEAD_INIT)
94 #define PyVarObject_HEAD_INIT(ob, size)    \
95     PyObject_HEAD_INIT(NULL) \
96     0,
97 #endif
98 
99 #if defined(PY_SSIZE_T_CLEAN) && (PY_VERSION_HEX >= 0x02040000)
100 #define PARSE_LENGTH_TYPE   Py_ssize_t
101 #define PARSE_LENGTH_FORMAT "%zd"
102 #else
103 #define PARSE_LENGTH_TYPE   int
104 #define PARSE_LENGTH_FORMAT "%d"
105 #endif
106 
107 extern ISzAlloc allocator;
108 
109 #endif
110