/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <Python.h>
#include "cStringIO.h"
#include <stdint.h>
#ifndef _WIN32
# include <stdbool.h>
# include <netinet/in.h>
#else
# include <WinSock2.h>
# pragma comment (lib, "ws2_32.lib")
# define BIG_ENDIAN (4321)
# define LITTLE_ENDIAN (1234)
# define BYTE_ORDER LITTLE_ENDIAN
# if defined(_MSC_VER) && _MSC_VER < 1600
typedef int _Bool;
# define bool _Bool
# define false 0
# define true 1
# endif
# define inline __inline
#endif
/* Fix endianness issues on Solaris */
#if defined (__SVR4) && defined (__sun)
#if defined(__i386) && !defined(__i386__)
#define __i386__
#endif
#ifndef BIG_ENDIAN
#define BIG_ENDIAN (4321)
#endif
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN (1234)
#endif
/* I386 is LE, even on Solaris */
#if !defined(BYTE_ORDER) && defined(__i386__)
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#endif
// TODO(dreiss): defval appears to be unused. Look into removing it.
// TODO(dreiss): Make parse_spec_args recursive, and cache the output
// permanently in the object. (Malloc and orphan.)
// TODO(dreiss): Why do we need cStringIO for reading, why not just char*?
// Can cStringIO let us work with a BufferedTransport?
// TODO(dreiss): Don't ignore the rv from cwrite (maybe).
/* ====== BEGIN UTILITIES ====== */
#define INIT_OUTBUF_SIZE 128
// Stolen out of TProtocol.h.
// It would be a huge pain to have both get this from one place.
typedef enum TType {
T_STOP = 0,
T_VOID = 1,
T_BOOL = 2,
T_BYTE = 3,
T_I08 = 3,
T_I16 = 6,
T_I32 = 8,
T_U64 = 9,
T_I64 = 10,
T_DOUBLE = 4,
T_STRING = 11,
T_UTF7 = 11,
T_STRUCT = 12,
T_MAP = 13,
T_SET = 14,
T_LIST = 15,
T_UTF8 = 16,
T_UTF16 = 17
} TType;
#ifndef __BYTE_ORDER
# if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
# define __BYTE_ORDER BYTE_ORDER
# define __LITTLE_ENDIAN LITTLE_ENDIAN
# define __BIG_ENDIAN BIG_ENDIAN
# else
# error "Cannot determine endianness"
# endif
#endif
// Same comment as the enum. Sorry.
#if __BYTE_ORDER == __BIG_ENDIAN
# define ntohll(n) (n)
# define htonll(n) (n)
#elif __BYTE_ORDER == __LITTLE_ENDIAN
# if defined(__GNUC__) && defined(__GLIBC__)
# include <byteswap.h>
# define ntohll(n) bswap_64(n)
# define htonll(n) bswap_64(n)
# else /* GNUC & GLIBC */
# define ntohll(n) ( (((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32) )
# define htonll(n) ( (((unsigned long long)htonl(n)) << 32) + htonl(n >> 32) )
# endif /* GNUC & GLIBC */
#else /* __BYTE_ORDER */
# error "Can't define htonll or ntohll!"
#endif
// Doing a benchmark shows that interning actually makes a difference, amazingly.
#define INTERN_STRING(value) _intern_ ## value
#define INT_CONV_ERROR_OCCURRED(v) ( ((v) == -1) && PyErr_Occurred() )
#define CHECK_RANGE(v, min, max) ( ((v) <= (max)) && ((v) >= (min)) )
// Py_ssize_t was not defined before Python 2.5
#if (PY_VERSION_HEX < 0x02050000)
typedef int Py_ssize_t;
#endif
/**
* A cache of the spec_args for a set or list,
* so we don't have to keep calling PyTuple_GET_ITEM.
*/
typedef struct {
TType element_type;
PyObject* typeargs;
} SetListTypeArgs;
/**
* A cache of the spec_args for a map,
* so we don't have to keep calling PyTuple_GET_ITEM.
*/
typedef struct {
TType ktag;
TType vtag;
PyObject* ktypeargs;
PyObject* vtypeargs;
} MapTypeArgs;
/**
* A cache of the spec_args for a struct,
* so we don't have to keep calling PyTuple_GET_ITEM.
*/
typedef struct {
PyObject* klass;
PyObject* spec;
} StructTypeArgs;
/**
* A cache of the item spec from a struct specification,
* so we don't have to keep calling PyTuple_GET_ITEM.
*/
typedef struct {
int tag;
TType type;
PyObject* attrname;
PyObject* typeargs;
PyObject* defval;
} StructItemSpec;
/**
* A cache of the two key attributes of a CReadableTransport,
* so we don't have to keep calling PyObject_GetAttr.
*/
typedef struct {
PyObject* stringiobuf;
PyObject* refill_callable;
} DecodeBuffer;
/** Pointer to interned string to speed up attribute lookup. */
static PyObject* INTERN_STRING(cstringio_buf);
/** Pointer to interned string to speed up attribute lookup. */
static PyObject* INTERN_STRING(cstringio_refill);
static inline bool
check_ssize_t_32(Py_ssize_t len) {
// error from getting the int
if (INT_CONV_ERROR_OCCURRED(len)) {
return false;
}
if (!CHECK_RANGE(len, 0, INT32_MAX)) {
PyErr_SetString(PyExc_OverflowError, "string size out of range");
return false;
}
return true;
}
static inline bool
parse_pyint(PyObject* o, int32_t* ret, int32_t min, int32_t max) {
long val = PyInt_AsLong(o);
if (INT_CONV_ERROR_OCCURRED(val)) {
return false;
}
if (!CHECK_RANGE(val, min, max)) {
PyErr_SetString(PyExc_OverflowError, "int out of range");
return false;
}
*ret = (int32_t) val;
return true;
}
/* --- FUNCTIONS TO PARSE STRUCT SPECIFICATOINS --- */
static bool
parse_set_list_args(SetListTypeArgs* dest, PyObject* typeargs) {
if (PyTuple_Size(typeargs) != 2) {
PyErr_SetString(PyExc_TypeError, "expecting tuple of size 2 for list/set type args");
return false;
}
dest->element_type = PyInt_AsLong(PyTuple_GET_ITEM(typeargs, 0));
if (INT_CONV_ERROR_OCCURRED(dest->element_type)) {
return false;
}
dest->typeargs = PyTuple_GET_ITEM(typeargs, 1);
return true;
}
static bool
parse_map_args(MapTypeArgs* dest, PyObject* typeargs) {
if (PyTuple_Size(typeargs) != 4) {
PyErr_SetString(PyExc_TypeError, "expecting 4 arguments for typeargs to map");
return false;
}
dest->ktag = PyInt_AsLong(PyTuple_GET_ITEM(typeargs, 0));
if (INT_CONV_ERROR_OCCURRED(dest->ktag)) {
return false;
}
dest->vtag = PyInt_AsLong(PyTuple_GET_ITEM(typeargs, 2));
if (INT_CONV_ERROR_OCCURRED(dest->vtag)) {
return false;
}
dest->ktypeargs = PyTuple_GET_ITEM(typeargs, 1);
dest->vtypeargs = PyTuple_GET_ITEM(typeargs, 3);
return true;
}
static bool
parse_struct_args(StructTypeArgs* dest, PyObject* typeargs) {
if (PyTuple_Size(typeargs) != 2) {
PyErr_SetString(PyExc_TypeError, "expecting tuple of size 2 for struct args");
return false;
}
dest->klass = PyTuple_GET_ITEM(typeargs, 0);
dest->spec = PyTuple_GET_ITEM(typeargs, 1);
return true;
}
static int
parse_struct_item_spec(StructItemSpec* dest, PyObject* spec_tuple) {
// i'd like to use ParseArgs here, but it seems to be a bottleneck.
if (PyTuple_Size(spec_tuple) != 5) {
PyErr_SetString(PyExc_TypeError, "expecting 5 arguments for spec tuple");
return false;
}
dest->tag = PyInt_AsLong(PyTuple_GET_ITEM(spec_tuple, 0));
if (INT_CONV_ERROR_OCCURRED(dest->tag)) {
return false;
}
dest->type = PyInt_AsLong(PyTuple_GET_ITEM(spec_tuple, 1));
if (INT_CONV_ERROR_OCCURRED(dest->type)) {
return false;
}
dest->attrname = PyTuple_GET_ITEM(spec_tuple, 2);
dest->typeargs = PyTuple_GET_ITEM(spec_tuple, 3);
dest->defval = PyTuple_GET_ITEM(spec_tuple, 4);
return true;
}
/* ====== END UTILITIES ====== */
/* ====== BEGIN WRITING FUNCTIONS ====== */
/* --- LOW-LEVEL WRITING FUNCTIONS --- */
s
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
thrift2.zip (36个子文件)
thrift2
test.py 2KB
__init__.py 0B
thrift
server
__init__.py 830B
TNonblockingServer.py 12KB
TProcessPoolServer.py 4KB
TServer.py 8KB
THttpServer.py 3KB
TSerialization.py 1KB
TSCons.py 1KB
protocol
TBase.py 3KB
TMultiplexedProtocol.py 1KB
TBinaryProtocol.py 6KB
__init__.py 889B
TJSONProtocol.py 14KB
TProtocolDecorator.py 2KB
TCompactProtocol.py 11KB
TProtocol.py 11KB
fastbinary.c 27KB
__init__.py 817B
Thrift.py 4KB
transport
TSocket.py 6KB
TTwisted.py 10KB
TZlibTransport.py 8KB
__init__.py 855B
TSSLSocket.py 8KB
TTransport.py 11KB
THttpClient.py 4KB
TMultiplexedProcessor.py 2KB
TTornado.py 6KB
packages
hbase
constants.py 255B
THBaseService.py 109KB
ttypes.py 52KB
__init__.py 52B
THBaseService-remote 5KB
__init__.py 0B
hbase.thrift 13KB
共 36 条
- 1
资源评论
- weixin_387872282018-02-07不错可以使用
- mayong492772019-09-17不错,挺好的资源
- logjiang2017-08-29不错可以使用
莱登堡
- 粉丝: 29
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功