2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 #ifndef THRIFT_PY_TYPES_H
21 #define THRIFT_PY_TYPES_H
26 #define __STDC_FORMAT_MACROS
27 #define __STDC_LIMIT_MACROS
31 #if PY_MAJOR_VERSION >= 3
35 // TODO: better macros
36 #define PyInt_AsLong(v) PyLong_AsLong(v)
37 #define PyInt_FromLong(v) PyLong_FromLong(v)
39 #define PyString_InternFromString(v) PyUnicode_InternFromString(v)
43 #define INTERN_STRING(value) _intern_##value
45 #define INT_CONV_ERROR_OCCURRED(v) (((v) == -1) && PyErr_Occurred())
48 extern PyObject
* INTERN_STRING(TFrozenDict
);
49 extern PyObject
* INTERN_STRING(cstringio_buf
);
50 extern PyObject
* INTERN_STRING(cstringio_refill
);
57 extern PyObject
* ThriftModule
;
59 // Stolen out of TProtocol.h.
60 // It would be a huge pain to have both get this from one place.
83 // replace with unique_ptr when we're OK with C++11
84 class ScopedPyObject
{
86 ScopedPyObject() : obj_(NULL
) {}
87 explicit ScopedPyObject(PyObject
* py_object
) : obj_(py_object
) {}
92 PyObject
* get() throw() { return obj_
; }
93 operator bool() { return obj_
; }
94 void reset(PyObject
* py_object
) throw() {
99 PyObject
* release() throw() {
100 PyObject
* tmp
= obj_
;
104 void swap(ScopedPyObject
& other
) throw() {
105 ScopedPyObject
tmp(other
.release());
106 other
.reset(release());
107 reset(tmp
.release());
111 ScopedPyObject(const ScopedPyObject
&) {}
112 ScopedPyObject
& operator=(const ScopedPyObject
&) { return *this; }
118 * A cache of the two key attributes of a CReadableTransport,
119 * so we don't have to keep calling PyObject_GetAttr.
121 struct DecodeBuffer
{
122 ScopedPyObject stringiobuf
;
123 ScopedPyObject refill_callable
;
126 #if PY_MAJOR_VERSION < 3
127 extern char refill_signature
[3];
128 typedef PyObject EncodeBuffer
;
130 extern const char* refill_signature
;
131 struct EncodeBuffer
{
132 std::vector
<char> buf
;
138 * A cache of the spec_args for a set or list,
139 * so we don't have to keep calling PyTuple_GET_ITEM.
141 struct SetListTypeArgs
{
148 * A cache of the spec_args for a map,
149 * so we don't have to keep calling PyTuple_GET_ITEM.
160 * A cache of the spec_args for a struct,
161 * so we don't have to keep calling PyTuple_GET_ITEM.
163 struct StructTypeArgs
{
170 * A cache of the item spec from a struct specification,
171 * so we don't have to keep calling PyTuple_GET_ITEM.
173 struct StructItemSpec
{
181 bool parse_set_list_args(SetListTypeArgs
* dest
, PyObject
* typeargs
);
183 bool parse_map_args(MapTypeArgs
* dest
, PyObject
* typeargs
);
185 bool parse_struct_args(StructTypeArgs
* dest
, PyObject
* typeargs
);
187 bool parse_struct_item_spec(StructItemSpec
* dest
, PyObject
* spec_tuple
);
192 #endif // THRIFT_PY_TYPES_H