本文整理汇总了C++中PyInt_AsLong函数的典型用法代码示例。如果您正苦于以下问题:C++ PyInt_AsLong函数的具体用法?C++ PyInt_AsLong怎么用?C++ PyInt_AsLong使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyInt_AsLong函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: AerospikeClient_Type_Init
//.........这里部分代码省略.........
if( PyTuple_Check(py_host) && PyTuple_Size(py_host) == 2) {
py_addr = PyTuple_GetItem(py_host, 0);
if(PyString_Check(py_addr)) {
addr = strdup(PyString_AsString(py_addr));
}
py_port = PyTuple_GetItem(py_host,1);
if( PyInt_Check(py_port) || PyLong_Check(py_port) ) {
port = (uint16_t) PyLong_AsLong(py_port);
}
else {
port = 0;
}
}
else if ( PyString_Check(py_host) ) {
addr = strdup( strtok( PyString_AsString(py_host), ":" ) );
addr = strtok(addr, ":");
char *temp = strtok(NULL, ":");
if(NULL != temp) {
port = (uint16_t)atoi(temp);
}
}
as_config_add_host(&config, addr, port);
}
}
PyObject * py_shm = PyDict_GetItemString(py_config, "shm");
if (py_shm && PyDict_Check(py_shm) ) {
config.use_shm = true;
PyObject * py_shm_max_nodes = PyDict_GetItemString( py_shm, "shm_max_nodes" );
if(py_shm_max_nodes && PyInt_Check(py_shm_max_nodes) ) {
config.shm_max_nodes = PyInt_AsLong(py_shm_max_nodes);
}
PyObject * py_shm_max_namespaces = PyDict_GetItemString(py_shm, "shm_max_namespaces");
if(py_shm_max_namespaces && PyInt_Check(py_shm_max_namespaces) ) {
config.shm_max_namespaces = PyInt_AsLong(py_shm_max_namespaces);
}
PyObject* py_shm_takeover_threshold_sec = PyDict_GetItemString(py_shm, "shm_takeover_threshold_sec");
if(py_shm_takeover_threshold_sec && PyInt_Check(py_shm_takeover_threshold_sec) ) {
config.shm_takeover_threshold_sec = PyInt_AsLong( py_shm_takeover_threshold_sec);
}
}
self->is_client_put_serializer = false;
self->user_serializer_call_info.callback = NULL;
self->user_deserializer_call_info.callback = NULL;
PyObject *py_serializer_option = PyDict_GetItemString(py_config, "serialization");
if (py_serializer_option && PyTuple_Check(py_serializer_option)) {
PyObject *py_serializer = PyTuple_GetItem(py_serializer_option, 0);
if (py_serializer && py_serializer != Py_None) {
if (!PyCallable_Check(py_serializer)) {
return -1;
}
memset(&self->user_serializer_call_info, 0, sizeof(self->user_serializer_call_info));
self->user_serializer_call_info.callback = py_serializer;
}
PyObject *py_deserializer = PyTuple_GetItem(py_serializer_option, 1);
if (py_deserializer && py_deserializer != Py_None) {
if (!PyCallable_Check(py_deserializer)) {
return -1;
}
memset(&self->user_deserializer_call_info, 0, sizeof(self->user_deserializer_call_info));
示例2: eFatal
int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePyObject additional)
{
int return_reason = 0;
/* get current time */
if (additional && !PyDict_Check(additional))
eFatal("additional, but it's not dict");
if (additional && !res)
eFatal("additional, but no res");
long poll_timeout = -1; /* infinite in case of empty timer list */
{
ePtrList<eTimer>::iterator it = m_timer_list.begin();
if (it != m_timer_list.end())
{
eTimer *tmr = *it;
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
/* process all timers which are ready. first remove them out of the list. */
while (tmr->needsActivation(now))
{
m_timer_list.erase(it);
tmr->AddRef();
tmr->activate();
tmr->Release();
it = m_timer_list.begin();
if (it == m_timer_list.end()) break;
tmr = *it;
}
it = m_timer_list.begin();
if (it != m_timer_list.end()) poll_timeout = timeout_usec((*it)->getNextActivation());
if (poll_timeout < 0)
poll_timeout = 0;
else /* convert us to ms */
poll_timeout /= 1000;
}
}
if ((twisted_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > twisted_timeout))
{
poll_timeout = twisted_timeout;
return_reason = 1;
}
int nativecount=notifiers.size(),
fdcount=nativecount,
ret=0;
if (additional)
fdcount += PyDict_Size(additional);
// build the poll aray
pollfd pfd[fdcount]; // make new pollfd array
std::map<int,eSocketNotifier*>::iterator it = notifiers.begin();
int i=0;
for (; i < nativecount; ++i, ++it)
{
it->second->state = 1; // running and in poll
pfd[i].fd = it->first;
pfd[i].events = it->second->getRequested();
}
if (additional)
{
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
# define PY_SSIZE_T_MAX INT_MAX
# define PY_SSIZE_T_MIN INT_MIN
#endif
PyObject *key, *val;
Py_ssize_t pos=0;
while (PyDict_Next(additional, &pos, &key, &val)) {
pfd[i].fd = PyObject_AsFileDescriptor(key);
pfd[i++].events = PyInt_AsLong(val);
}
}
m_is_idle = 1;
++m_idle_count;
if (this == eApp)
{
Py_BEGIN_ALLOW_THREADS
ret = ::poll(pfd, fdcount, poll_timeout);
Py_END_ALLOW_THREADS
} else
示例3: convertEpetraMultiVectorToDistAarray
PyObject *
convertEpetraMultiVectorToDistAarray(const Epetra_MultiVector & emv)
{
// Initialization
PyObject * dap = NULL;
PyObject * dim_data = NULL;
PyObject * dim_dict = NULL;
PyObject * size = NULL;
PyObject * buffer = NULL;
Py_ssize_t ndim = 1;
npy_intp dims[3];
// Get the underlying Epetra_BlockMap
const Epetra_BlockMap & ebm = emv.Map();
// Allocate the DistArray object and set the version key value
dap = PyDict_New();
if (!dap) goto fail;
if (PyDict_SetItemString(dap,
"__version__",
PyString_FromString("0.9.0")) == -1) goto fail;
// Get the Dimension Data and the number of dimensions. If the
// underlying Epetra_BlockMap has variable element sizes, an error
// will be detected here.
dim_data = convertEpetraBlockMapToDimData(ebm,
emv.NumVectors());
if (!dim_data) goto fail;
ndim = PyTuple_Size(dim_data);
// Assign the Dimension Data key value.
if (PyDict_SetItemString(dap,
"dim_data",
dim_data) == -1) goto fail;
// Extract the buffer dimensions from the Dimension Data, construct
// the buffer and assign the buffer key value
for (Py_ssize_t i = 0; i < ndim; ++i)
{
dim_dict = PyTuple_GetItem(dim_data, i);
if (!dim_dict) goto fail;
size = PyDict_GetItemString(dim_dict, "size");
if (!size) goto fail;
dims[i] = PyInt_AsLong(size);
if (PyErr_Occurred()) goto fail;
}
buffer = PyArray_SimpleNewFromData(ndim,
dims,
NPY_DOUBLE,
(void*)emv[0]);
if (!buffer) goto fail;
if (PyDict_SetItemString(dap,
"buffer",
buffer) == -1) goto fail;
// Return the DistArray Protocol object
return dap;
// Error handling
fail:
Py_XDECREF(dap);
Py_XDECREF(dim_data);
Py_XDECREF(dim_dict);
Py_XDECREF(buffer);
return NULL;
}
示例4: child_init
//.........这里部分代码省略.........
classname = get_instance_class_name(_sr_apy_handler_obj);
if (classname == NULL)
{
if (!PyErr_Occurred())
PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
pFunc = PyObject_GetAttrString(_sr_apy_handler_obj, child_init_mname.s);
if (pFunc == NULL) {
python_handle_exception("child_init");
Py_XDECREF(pFunc);
Py_DECREF(format_exc_obj);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
if (!PyCallable_Check(pFunc)) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pFunc);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
pArgs = PyTuple_New(1);
if (pArgs == NULL) {
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_DECREF(pFunc);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
pValue = PyInt_FromLong((long)rank);
if (pValue == NULL) {
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_DECREF(pArgs);
Py_DECREF(pFunc);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
PyTuple_SetItem(pArgs, 0, pValue);
/* pValue has been stolen */
pResult = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pFunc);
Py_DECREF(pArgs);
if (PyErr_Occurred()) {
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pResult);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
if (pResult == NULL) {
LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
if (!PyInt_Check(pResult))
{
if (!PyErr_Occurred())
PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pResult);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return -1;
}
rval = PyInt_AsLong(pResult);
Py_DECREF(pResult);
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return rval;
}
示例5: main
int
main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
if (argc < 3) {
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
return 1;
}
Py_Initialize();
pName = PyString_FromString(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(argc - 3);
for (i = 0; i < argc - 3; ++i) {
pValue = PyInt_FromLong(atoi(argv[i + 3]));
if (!pValue) {
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, i, pValue);
}
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
Py_Finalize();
return 0;
}
示例6: gevent_loop
static void gevent_loop() {
if (!uwsgi.has_threads && uwsgi.mywid == 1) {
uwsgi_log("!!! Running gevent without threads IS NOT recommended, enable them with --enable-threads !!!\n");
}
if (uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] < 30) {
uwsgi_log("!!! Running gevent with a socket-timeout lower than 30 seconds is not recommended, tune it with --socket-timeout !!!\n");
}
// get the GIL
UWSGI_GET_GIL
up.gil_get = gil_gevent_get;
up.gil_release = gil_gevent_release;
uwsgi.wait_write_hook = uwsgi_gevent_wait_write_hook;
uwsgi.wait_read_hook = uwsgi_gevent_wait_read_hook;
struct uwsgi_socket *uwsgi_sock = uwsgi.sockets;
if (uwsgi.async < 2) {
uwsgi_log("the gevent loop engine requires async mode (--async <n>)\n");
exit(1);
}
uwsgi.current_wsgi_req = uwsgi_gevent_current_wsgi_req;
PyObject *gevent_dict = get_uwsgi_pydict("gevent");
if (!gevent_dict) uwsgi_pyexit;
PyObject *gevent_version = PyDict_GetItemString(gevent_dict, "version_info");
if (!gevent_version) uwsgi_pyexit;
if (PyInt_AsLong(PyTuple_GetItem(gevent_version, 0)) < 1) {
uwsgi_log("uWSGI requires at least gevent 1.x version\n");
exit(1);
}
ugevent.spawn = PyDict_GetItemString(gevent_dict, "spawn");
if (!ugevent.spawn) uwsgi_pyexit;
ugevent.signal = PyDict_GetItemString(gevent_dict, "signal");
if (!ugevent.signal) uwsgi_pyexit;
ugevent.greenlet_switch = PyDict_GetItemString(gevent_dict, "sleep");
if (!ugevent.greenlet_switch) uwsgi_pyexit;
ugevent.greenlet_switch_args = PyTuple_New(0);
Py_INCREF(ugevent.greenlet_switch_args);
PyObject *gevent_get_hub = PyDict_GetItemString(gevent_dict, "get_hub");
ugevent.hub = python_call(gevent_get_hub, PyTuple_New(0), 0, NULL);
if (!ugevent.hub) uwsgi_pyexit;
ugevent.get_current = PyDict_GetItemString(gevent_dict, "getcurrent");
if (!ugevent.get_current) uwsgi_pyexit;
ugevent.get_current_args = PyTuple_New(0);
Py_INCREF(ugevent.get_current_args);
ugevent.hub_loop = PyObject_GetAttrString(ugevent.hub, "loop");
if (!ugevent.hub_loop) uwsgi_pyexit;
// main greenlet waiting for connection (one greenlet per-socket)
PyObject *uwsgi_gevent_main = PyCFunction_New(uwsgi_gevent_main_def, NULL);
Py_INCREF(uwsgi_gevent_main);
// greenlet to run at each request
PyObject *uwsgi_request_greenlet = PyCFunction_New(uwsgi_gevent_request_def, NULL);
Py_INCREF(uwsgi_request_greenlet);
// pre-fill the greenlet args
ugevent.greenlet_args = PyTuple_New(2);
PyTuple_SetItem(ugevent.greenlet_args, 0, uwsgi_request_greenlet);
if (uwsgi.signal_socket > -1) {
// and these are the watcher for signal sockets
ugevent.signal_watcher = PyObject_CallMethod(ugevent.hub_loop, "io", "ii", uwsgi.signal_socket, 1);
if (!ugevent.signal_watcher) uwsgi_pyexit;
ugevent.my_signal_watcher = PyObject_CallMethod(ugevent.hub_loop, "io", "ii", uwsgi.my_signal_socket, 1);
if (!ugevent.my_signal_watcher) uwsgi_pyexit;
PyObject *uwsgi_greenlet_signal = PyCFunction_New(uwsgi_gevent_signal_def, NULL);
Py_INCREF(uwsgi_greenlet_signal);
PyObject *uwsgi_greenlet_my_signal = PyCFunction_New(uwsgi_gevent_my_signal_def, NULL);
Py_INCREF(uwsgi_greenlet_my_signal);
PyObject *uwsgi_greenlet_signal_handler = PyCFunction_New(uwsgi_gevent_signal_handler_def, NULL);
Py_INCREF(uwsgi_greenlet_signal_handler);
ugevent.signal_args = PyTuple_New(2);
PyTuple_SetItem(ugevent.signal_args, 0, uwsgi_greenlet_signal_handler);
//.........这里部分代码省略.........
示例7: uwsgi_python_spooler
int uwsgi_python_spooler(char *filename, char *buf, uint16_t len, char *body, size_t body_len) {
static int random_seed_reset = 0;
UWSGI_GET_GIL;
PyObject *spool_dict = PyDict_New();
PyObject *spool_func, *pyargs, *ret;
if (!random_seed_reset) {
uwsgi_python_reset_random_seed();
random_seed_reset = 1;
}
if (!up.embedded_dict) {
// ignore
UWSGI_RELEASE_GIL;
return 0;
}
spool_func = PyDict_GetItemString(up.embedded_dict, "spooler");
if (!spool_func) {
// ignore
UWSGI_RELEASE_GIL;
return 0;
}
if (uwsgi_hooked_parse(buf, len, uwsgi_python_add_item, spool_dict)) {
// malformed packet, destroy it
UWSGI_RELEASE_GIL;
return -2;
}
pyargs = PyTuple_New(1);
PyDict_SetItemString(spool_dict, "spooler_task_name", PyString_FromString(filename));
if (body && body_len > 0) {
PyDict_SetItemString(spool_dict, "body", PyString_FromStringAndSize(body, body_len));
}
PyTuple_SetItem(pyargs, 0, spool_dict);
ret = python_call(spool_func, pyargs, 0, NULL);
if (ret) {
if (!PyInt_Check(ret)) {
// error, retry
UWSGI_RELEASE_GIL;
return -1;
}
int retval = (int) PyInt_AsLong(ret);
UWSGI_RELEASE_GIL;
return retval;
}
if (PyErr_Occurred())
PyErr_Print();
// error, retry
UWSGI_RELEASE_GIL;
return -1;
}
示例8: PyErr_SetString
it is in 0.3 of the list\n\
\n\
";
static PyObject *clistfns_contents(PyObject *self, PyObject *args)
{
int i;
PyObject *items, *counts, *percentages;
PyObject *countitems, *countitem;
PyObject *key, *count, *perc;
long c;
double total;
if(!PyArg_ParseTuple(args, "O", &items))
return NULL;
if(!PySequence_Check(items)) {
PyErr_SetString(PyExc_TypeError, "expected mapping type");
return NULL;
}
if((total = PySequence_Length(items)) == -1) {
PyErr_SetString(PyExc_ValueError, "I couldn't get length of item.");
return NULL;
}
counts = clistfns_count(self, args);
if(!counts || PyErr_Occurred())
return NULL;
if(!(percentages = PyDict_New())) {
Py_DECREF(counts);
return NULL;
}
/* Loop through every element in counts, calculating the probabilities. */
if(!(countitems = PyMapping_Items(counts))) {
Py_DECREF(counts);
Py_DECREF(percentages);
return NULL;
}
/* Go through the loop, counting how often each item appears. */
i = 0;
while(1) {
if(!(countitem = PyList_GetItem(countitems, i))) {
PyErr_Clear(); /* clear the exception set by PyList_GetItem */
break; /* no more numbers */
}
key = PyTuple_GetItem(countitem, 0);
count = PyTuple_GetItem(countitem, 1);
c = PyInt_AsLong(count);
perc = PyFloat_FromDouble((double)c / total);
PyDict_SetItem(percentages, key, perc);
Py_DECREF(perc);
if(PyErr_Occurred()) /* PyDict_SetItem failed */
break;
i++;
}
if(PyErr_Occurred()) {
Py_DECREF(percentages);
percentages = NULL;
}
Py_DECREF(countitems);
Py_DECREF(counts);
return percentages;
}
示例9: parse_syntax_error
static int
parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
int *lineno, int *offset, const char **text)
{
long hold;
PyObject *v;
/* old style errors */
if (PyTuple_Check(err))
return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
lineno, offset, text);
/* new style errors. `err' is an instance */
if (! (v = PyObject_GetAttrString(err, "msg")))
goto finally;
*message = v;
if (!(v = PyObject_GetAttrString(err, "filename")))
goto finally;
if (v == Py_None)
*filename = NULL;
else if (! (*filename = PyString_AsString(v)))
goto finally;
Py_DECREF(v);
if (!(v = PyObject_GetAttrString(err, "lineno")))
goto finally;
hold = PyInt_AsLong(v);
Py_DECREF(v);
v = NULL;
if (hold < 0 && PyErr_Occurred())
goto finally;
*lineno = (int)hold;
if (!(v = PyObject_GetAttrString(err, "offset")))
goto finally;
if (v == Py_None) {
*offset = -1;
Py_DECREF(v);
v = NULL;
} else {
hold = PyInt_AsLong(v);
Py_DECREF(v);
v = NULL;
if (hold < 0 && PyErr_Occurred())
goto finally;
*offset = (int)hold;
}
if (!(v = PyObject_GetAttrString(err, "text")))
goto finally;
if (v == Py_None)
*text = NULL;
else if (! (*text = PyString_AsString(v)))
goto finally;
Py_DECREF(v);
return 1;
finally:
Py_XDECREF(v);
return 0;
}
示例10: BoolFromPython
gboolean BoolFromPython(PyObject * o, const char *key)
{
char *s;
int i;
PyObject *o2;
if (o == Py_None) {
return FALSE;
}
if (!PyBool_Check(o)) {
if (PyLong_Check(o)) {
i = PyLong_AsLong(o);
if (i == 0)
return FALSE;
else
return TRUE;
}
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(o)) {
i = PyInt_AsLong(o);
if (i == 0)
return FALSE;
else
return TRUE;
}
if (PyString_Check(o)) {
s = PyString_AsString(o);
if (isdigit((int)s[0])) {
i = atoi(s);
if (i == 0)
return FALSE;
else
return TRUE;
} else if (strcasecmp(s, "yes") == 0) {
return TRUE;
} else if (strcasecmp(s, "true") == 0) {
return TRUE;
} else if (strcasecmp(s, "no") == 0) {
return FALSE;
} else if (strcasecmp(s, "false") == 0) {
return FALSE;
} else {
PyErr_Format(PyExc_ValueError,
"String value of '%s' doesn't seem to be boolean",
key);
return BOOL_INVALID;
}
}
#endif
if (PyUnicode_Check(o)) {
o2 = PyUnicode_AsASCIIString(o);
if (o2 == NULL) {
return BOOL_INVALID;
}
s = PyBytes_AsString(o2);
if (isdigit((int)s[0])) {
i = atoi(s);
Py_DECREF(o2);
if (i == 0)
return FALSE;
else
return TRUE;
} else if (strcasecmp(s, "yes") == 0) {
Py_DECREF(o2);
return TRUE;
} else if (strcasecmp(s, "true") == 0) {
Py_DECREF(o2);
return TRUE;
} else if (strcasecmp(s, "no") == 0) {
Py_DECREF(o2);
return FALSE;
} else if (strcasecmp(s, "false") == 0) {
Py_DECREF(o2);
return FALSE;
} else {
Py_DECREF(o2);
PyErr_Format(PyExc_ValueError,
"String value of '%s' doesn't seem to be boolean",
key);
return BOOL_INVALID;
}
}
PyErr_Format(PyExc_ValueError,
"Value of '%s' doesn't seem to be boolean", key);
return BOOL_INVALID;
}
if (Py_False == o)
return FALSE;
else if (Py_True == o)
return TRUE;
PyErr_Format(PyExc_ValueError,
"Bool value of '%s' doesn't seem to be boolean", key);
return BOOL_INVALID;
}
示例11: PyTuple_GetItem
PyObject *py_uwsgi_gevent_request(PyObject * self, PyObject * args) {
PyObject *py_wsgi_req = PyTuple_GetItem(args, 0);
struct wsgi_request *wsgi_req = (struct wsgi_request *) PyLong_AsLong(py_wsgi_req);
PyObject *greenlet_switch = NULL;
PyObject *current_greenlet = GET_CURRENT_GREENLET;
// another hack to retrieve the current wsgi_req;
PyObject_SetAttrString(current_greenlet, "uwsgi_wsgi_req", py_wsgi_req);
// if in edge-triggered mode read from socket now !!!
if (wsgi_req->socket->edge_trigger) {
int status = wsgi_req->socket->proto(wsgi_req);
if (status < 0) {
goto end;
}
goto request;
}
greenlet_switch = PyObject_GetAttrString(current_greenlet, "switch");
for(;;) {
int ret = uwsgi.wait_read_hook(wsgi_req->fd, uwsgi.socket_timeout);
wsgi_req->switches++;
if (ret <= 0) {
goto end;
}
int status = wsgi_req->socket->proto(wsgi_req);
if (status < 0) {
goto end;
}
else if (status == 0) {
break;
}
}
request:
#ifdef UWSGI_ROUTING
if (uwsgi_apply_routes(wsgi_req) == UWSGI_ROUTE_BREAK) {
goto end;
}
#endif
for(;;) {
if (uwsgi.p[wsgi_req->uh->modifier1]->request(wsgi_req) <= UWSGI_OK) {
goto end;
}
wsgi_req->switches++;
// switch after each yield
GEVENT_SWITCH;
}
end:
if (greenlet_switch) {
Py_DECREF(greenlet_switch);
}
Py_DECREF(current_greenlet);
uwsgi_close_request(wsgi_req);
free_req_queue;
if (uwsgi.workers[uwsgi.mywid].manage_next_request == 0) {
int running_cores = 0;
int i;
for(i=0;i<uwsgi.async;i++) {
if (uwsgi.workers[uwsgi.mywid].cores[i].in_request) {
running_cores++;
}
}
if (running_cores == 0) {
// no need to worry about freeing memory
PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi");
if (uwsgi_dict) {
PyObject *ae = PyDict_GetItemString(uwsgi_dict, "atexit");
if (ae) {
python_call(ae, PyTuple_New(0), 0, NULL);
}
}
}
} else {
// If we stopped any watcher due to being out of async workers, restart it.
int i = 0;
struct uwsgi_socket *uwsgi_sock = uwsgi.sockets;
for (; uwsgi_sock; uwsgi_sock = uwsgi_sock->next, ++i) {
PyObject *py_watcher_active = PyObject_GetAttrString(ugevent.watchers[i], "active");
if (py_watcher_active && PyBool_Check(py_watcher_active) &&
!PyInt_AsLong(py_watcher_active)) {
start_watcher(i, uwsgi_sock);
}
Py_XDECREF(py_watcher_active);
}
}
//.........这里部分代码省略.........
示例12: GetIntFromDict
int GetIntFromDict(PyObject * dict, const char *key)
{
PyObject *o;
PyObject *o2;
char *s;
int i;
o = PyDict_GetItemString(dict, key);
if (o == NULL) {
PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s",
key);
return INT_INVALID;
}
if (PyLong_Check(o)) {
/* Well we loose here something, but it is intentional :-) */
return PyLong_AsLongLong(o);
}
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(o)) {
return PyInt_AsLong(o);
}
if (PyString_Check(o)) {
s = PyString_AsString(o);
if (isdigit((int)s[0])) {
i = atoi(s);
return i;
} else {
PyErr_Format(PyExc_ValueError,
"Value of '%s' doesn't seem to be integer",
key);
return INT_INVALID;
}
}
#endif
if (PyUnicode_Check(o)) {
o2 = PyUnicode_AsASCIIString(o);
if (o2 == NULL) {
return INT_INVALID;
}
s = PyBytes_AsString(o2);
if (isdigit((int)s[0])) {
i = atoi(s);
Py_DECREF(o2);
return i;
} else {
Py_DECREF(o2);
PyErr_Format(PyExc_ValueError,
"Value of '%s' doesn't seem to be integer",
key);
return INT_INVALID;
}
}
PyErr_Format(PyExc_ValueError,
"Value of '%s' doesn't seem to be integer", key);
return INT_INVALID;
}
示例13: Level2User_Join
static PyObject* Level2User_Join(PyObject * self, PyObject * args){
CThostFtdcLevel2UserApi * user = (CThostFtdcLevel2UserApi *) PyInt_AsLong(PyTuple_GET_ITEM(args, 0));
PyObject * ret = Py_BuildValue("i", user->Join());
return ret;
}
示例14: RM_cmd
int RM_cmd(int args, char** argv) {
printf("Ross-MacDonald\n");
size_t const NB_ARGS = 12;
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue;
if (args < NB_ARGS) {
fprintf(stderr, "Not enough arguments\n");
return -1;
} else if (strcmp("rm", argv[0]) != 0) {
fprintf(stderr, "Incorrect call\n");
return -1;
}
Py_Initialize();
PyRun_SimpleString("import sys, os");
PyRun_SimpleString("sys.path.append('models')");
pName = PyString_FromString("RM");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, "main");
if (pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(NB_ARGS - 1);
for (int i = 0; i < NB_ARGS; ++i) {
pValue = PyFloat_FromDouble(atof(argv[i]));
if (!pValue) {
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Error arguments\n");
return -1;
}
PyTuple_SetItem(pArgs, i-1, pValue);
}
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
if (PyInt_AsLong(pValue) != 0) {
printf("Call error\n");
} else {
printf("Call success\n");
}
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return -1;
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
Py_Finalize();
return 0;
}
示例15: bignumpy
static PyObject* bignumpy(PyObject* self, PyObject* args) {
const char* filename = NULL;
PyObject* dtype = NULL;
PyObject* shape = Py_None;
int fd;
PyArrayObject* z;
PyArray_Descr* descr;
int nd = 0;
npy_intp dims[BIGNUMPY_MAXDIMS];
npy_intp strides[BIGNUMPY_MAXDIMS];
size_t nelm;
int i;
npy_intp stride;
PyObject* obj;
struct BignumpyObject* bno;
if (!PyArg_ParseTuple(args,"sO|O",&filename,&dtype,&shape)) return NULL;
// We actually start with the type. If we can't figure out the size
// it is useless to open the file. dtype can be almost anything, so
// here, we create an array of zeros to get information about the
// type using the actual numpy interface
z = /*owned*/ (PyArrayObject*)PyObject_CallFunctionObjArgs(ZEROS,EMPTY,dtype,NULL);
if (!z) return NULL;
Py_INCREF(descr = PyArray_DESCR(z));
Py_DECREF(z); z = NULL;
// OK, we can open the file. If it does not exist, we may have to create it
fd = open(filename,O_RDWR|O_CREAT,0666);
if (fd < 0) {
Py_DECREF(descr);
return PyErr_SetFromErrnoWithFilename(PyExc_OSError,filename);
}
// Figure out the current size of the file.
struct stat status;
if (fstat(fd,&status) < 0) {
Py_DECREF(descr);
close(fd);
return PyErr_SetFromErrnoWithFilename(PyExc_OSError,filename);
}
//If the size is zero and we have a shape,
// then we'll use ftruncate to change the size. If we have no shape,
// assume shape is (size(file)/elsize,)
if (shape == Py_None) {
strides[nd] = descr->elsize;
dims[nd++] = status.st_size/descr->elsize;
} else {
PyObject* iterator = PyObject_GetIter(shape);
if (!iterator) {
long v = PyInt_AsLong(shape);
if (v == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError,"invalid shape");
Py_DECREF(descr);
close(fd);
return NULL;
}
dims[nd++] = v;
} else {
PyObject* item;
while((item = PyIter_Next(iterator))) {
if (nd >= BIGNUMPY_MAXDIMS) {
Py_DECREF(iterator);
Py_DECREF(item);
Py_DECREF(descr);
close(fd);
PyErr_SetString(PyExc_RuntimeError,"shape has too many dimensions");
return NULL;
}
long v = PyInt_AsLong(item);
if (v == -1 && PyErr_Occurred()) {
Py_DECREF(iterator);
Py_DECREF(item);
Py_DECREF(descr);
close(fd);
PyErr_SetString(PyExc_RuntimeError,"invalid shape");
return NULL;
}
strides[nd] = 1;
dims[nd++] = v;
Py_DECREF(item);
}
Py_DECREF(iterator);
}
}
// ----------------------------------------------------------------------
// Compute the number of required elements
// ----------------------------------------------------------------------
nelm = 0;
if (nd > 0) {
nelm = 1;
for(i=0;i<nd;++i) nelm *= dims[i];
}
// ----------------------------------------------------------------------
// The strides include the element size. We compute from back to front
// ----------------------------------------------------------------------
//.........这里部分代码省略.........