Пример #1
0
static void
getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* res)
{
    PyGILState_STATE gstate = PyGILState_Ensure();
    struct addrinfo *ptr;
    uv_err_t err;
    getaddrinfo_cb_data_t *cb_data;
    Loop *loop;
    PyObject *callback, *addr, *item, *errorno, *dns_result, *result;

    ASSERT(req);
    cb_data = (getaddrinfo_cb_data_t *)req->data;
    ASSERT(cb_data);
    loop = cb_data->loop;
    callback = cb_data->cb;
    ASSERT(loop);
    ASSERT(callback);

    if (status != 0) {
        err = uv_last_error(loop->uv_loop);
        errorno = PyInt_FromLong((long)err.code);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    dns_result = PyList_New(0);
    if (!dns_result) {
        PyErr_NoMemory();
        PyErr_WriteUnraisable(Py_None);
        errorno = PyInt_FromLong((long)UV_ENOMEM);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    for (ptr = res; ptr; ptr = ptr->ai_next) {
        addr = makesockaddr(ptr->ai_addr, ptr->ai_addrlen);
        if (!addr) {
            PyErr_NoMemory();
            PyErr_WriteUnraisable(callback);
            break;
        }

        item = PyStructSequence_New(&DNSAddrinfoResultType);
        if (!item) {
            PyErr_NoMemory();
            PyErr_WriteUnraisable(callback);
            break;
        }
        PyStructSequence_SET_ITEM(item, 0, PyInt_FromLong((long)ptr->ai_family));
        PyStructSequence_SET_ITEM(item, 1, PyInt_FromLong((long)ptr->ai_socktype));
        PyStructSequence_SET_ITEM(item, 2, PyInt_FromLong((long)ptr->ai_protocol));
        PyStructSequence_SET_ITEM(item, 3, PYUVString_FromString(ptr->ai_canonname ? ptr->ai_canonname : ""));
        PyStructSequence_SET_ITEM(item, 4, addr);

        PyList_Append(dns_result, item);
        Py_DECREF(item);
    }
    errorno = Py_None;
    Py_INCREF(Py_None);

callback:
    result = PyObject_CallFunctionObjArgs(callback, dns_result, errorno, NULL);
    if (result == NULL) {
        PyErr_WriteUnraisable(callback);
    }
    Py_XDECREF(result);
    Py_DECREF(dns_result);
    Py_DECREF(errorno);

    Py_DECREF(loop);
    Py_DECREF(callback);
    uv_freeaddrinfo(res);
    PyMem_Free(req);
    PyMem_Free(cb_data);

    PyGILState_Release(gstate);
}
Пример #2
0
static int
set_nameservers(Channel *self, PyObject *value)
{
    char *server;
    int i, r, length, ret;
    struct ares_addr_node *servers;
    Py_buffer pbuf;
    PyObject *server_list, *item, *data_fast;

    servers = NULL;
    server_list = value;
    ret = 0;

    if ((data_fast = PySequence_Fast(server_list, "argument 1 must be an iterable")) == NULL) {
        return -1;
    }

    length = PySequence_Fast_GET_SIZE(data_fast);
    if (length > INT_MAX) {
        PyErr_SetString(PyExc_ValueError, "argument 1 is too long");
        Py_DECREF(data_fast);
        return -1;
    }

    if (length == 0) {
        /* c-ares doesn't do anything */
        return 0;
    }

    servers = PyMem_Malloc(sizeof(struct ares_addr_node) * length);
    if (!servers) {
        PyErr_NoMemory();
        ret = -1;
        goto end;
    }

    for (i = 0; i < length; i++) {
        item = PySequence_Fast_GET_ITEM(data_fast, i);
        if (!item || !PyArg_Parse(item, "s*;args contains a non-string value", &pbuf)) {
            Py_XDECREF(item);
            goto end;
        }
        server = pbuf.buf;

        if (ares_inet_pton(AF_INET, server, &servers[i].addr.addr4) == 1) {
            servers[i].family = AF_INET;
        } else if (ares_inet_pton(AF_INET6, server, &servers[i].addr.addr6) == 1) {
            servers[i].family = AF_INET6;
        } else {
            PyErr_SetString(PyExc_ValueError, "invalid IP address");
            PyBuffer_Release(&pbuf);
            ret = -1;
            goto end;
        }

        PyBuffer_Release(&pbuf);

        if (i > 0) {
            servers[i-1].next = &servers[i];
        }

    }

    if (servers) {
        servers[length-1].next = NULL;
    }

    r = ares_set_servers(self->channel, servers);
    if (r != ARES_SUCCESS) {
        RAISE_ARES_EXCEPTION(r);
        ret = -1;
    }

end:
    PyMem_Free(servers);
    return ret;
}
Пример #3
0
static void
query_mx_cb(void *arg, int status,int timeouts, unsigned char *answer_buf, int answer_len)
{
    PyGILState_STATE gstate = PyGILState_Ensure();
    int parse_status;
    struct ares_mx_reply *mx_reply, *mx_ptr;
    PyObject *dns_result, *errorno, *tmp, *result, *callback;

    mx_reply = NULL;
    callback = (PyObject *)arg;
    ASSERT(callback);

    if (status != ARES_SUCCESS) {
        errorno = PyInt_FromLong((long)status);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    parse_status = ares_parse_mx_reply(answer_buf, answer_len, &mx_reply);
    if (parse_status != ARES_SUCCESS) {
        errorno = PyInt_FromLong((long)parse_status);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    dns_result = PyList_New(0);
    if (!dns_result) {
        PyErr_NoMemory();
        PyErr_WriteUnraisable(Py_None);
        errorno = PyInt_FromLong((long)ARES_ENOMEM);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    for (mx_ptr = mx_reply; mx_ptr != NULL; mx_ptr = mx_ptr->next) {
        tmp = PyStructSequence_New(&AresQueryMXResultType);
        if (tmp == NULL) {
            break;
        }
        PyStructSequence_SET_ITEM(tmp, 0, Py_BuildValue("s", mx_ptr->host));
        PyStructSequence_SET_ITEM(tmp, 1, PyInt_FromLong((long)mx_ptr->priority));
        PyList_Append(dns_result, tmp);
        Py_DECREF(tmp);
    }
    errorno = Py_None;
    Py_INCREF(Py_None);

callback:
    result = PyObject_CallFunctionObjArgs(callback, dns_result, errorno, NULL);
    if (result == NULL) {
        PyErr_WriteUnraisable(callback);
    }
    Py_XDECREF(result);
    Py_DECREF(dns_result);
    Py_DECREF(errorno);
    if (mx_reply) {
        ares_free_data(mx_reply);
    }
    Py_DECREF(callback);
    PyGILState_Release(gstate);
}
Пример #4
0
int NI_MinOrMaxFilter(PyArrayObject* input, PyArrayObject* footprint,
        PyArrayObject* structure, PyArrayObject* output,
        NI_ExtendMode mode, double cvalue, maybelong *origins, int minimum)
{
  Bool *pf = NULL;
  maybelong fsize, jj, kk, filter_size = 0, border_flag_value;
  maybelong *offsets = NULL, *oo, size;
  NI_FilterIterator fi;
  NI_Iterator ii, io;
  char *pi, *po;
  int ll;
  double *ss = NULL;
  Float64 *ps;

  /* get the the footprint: */
  fsize = 1;
  for(ll = 0; ll < footprint->nd; ll++)
    fsize *= footprint->dimensions[ll];
  pf = (Bool*)PyArray_DATA(footprint);
  for(jj = 0; jj < fsize; jj++) {
    if (pf[jj]) {
      ++filter_size;
    }
  }
  /* get the structure: */
  if (structure) {
    ss = (double*)malloc(filter_size * sizeof(double));
    if (!ss) {
      PyErr_NoMemory();
      goto exit;
    }
    /* copy the weights to contiguous memory: */
    ps = (Float64*)PyArray_DATA(structure);
    jj = 0;
    for(kk = 0; kk < fsize; kk++)
      if (pf[kk])
        ss[jj++] = minimum ? -ps[kk] : ps[kk];
  }
  /* initialize filter offsets: */
  if (!NI_InitFilterOffsets(input, pf, footprint->dimensions, origins,
                            mode, &offsets, &border_flag_value, NULL))
    goto exit;
  /* initialize filter iterator: */
  if (!NI_InitFilterIterator(input->nd, footprint->dimensions,
                             filter_size, input->dimensions, origins, &fi))
    goto exit;
  /* initialize input element iterator: */
  if (!NI_InitPointIterator(input, &ii))
    goto exit;
  /* initialize output element iterator: */
  if (!NI_InitPointIterator(output, &io))
    goto exit;
  /* get data pointers an array size: */
  pi = (void *)PyArray_DATA(input);
  po = (void *)PyArray_DATA(output);
  size = 1;
  for(ll = 0; ll < input->nd; ll++)
    size *= input->dimensions[ll];
  /* iterator over the elements: */
  oo = offsets;
  for(jj = 0; jj < size; jj++) {
    double tmp = 0.0;
    switch (input->descr->type_num) {
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, Bool,
                            minimum, tmp, border_flag_value, ss);
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, UInt8,
                            minimum, tmp, border_flag_value, ss);
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, UInt16,
                            minimum, tmp, border_flag_value, ss);
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, UInt32,
                            minimum, tmp, border_flag_value, ss);
#if HAS_UINT64
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, UInt64,
                            minimum, tmp, border_flag_value, ss);
#endif
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, Int8,
                            minimum, tmp, border_flag_value, ss);
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, Int16,
                            minimum, tmp, border_flag_value, ss);
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, Int32,
                            minimum, tmp, border_flag_value, ss);
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, Int64,
                            minimum, tmp, border_flag_value, ss);
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, Float32,
                            minimum, tmp, border_flag_value, ss);
      CASE_MIN_OR_MAX_POINT(pi, oo, filter_size, cvalue, Float64,
                            minimum, tmp, border_flag_value, ss);
    default:
      PyErr_SetString(PyExc_RuntimeError, "array type not supported");
      goto exit;
    }
    switch (output->descr->type_num) {
      CASE_FILTER_OUT(po, tmp, Bool);
      CASE_FILTER_OUT(po, tmp, UInt8);
      CASE_FILTER_OUT(po, tmp, UInt16);
      CASE_FILTER_OUT(po, tmp, UInt32);
#if HAS_UINT64
      CASE_FILTER_OUT(po, tmp, UInt64);
#endif
      CASE_FILTER_OUT(po, tmp, Int8);
      CASE_FILTER_OUT(po, tmp, Int16);
      CASE_FILTER_OUT(po, tmp, Int32);
      CASE_FILTER_OUT(po, tmp, Int64);
      CASE_FILTER_OUT(po, tmp, Float32);
      CASE_FILTER_OUT(po, tmp, Float64);
    default:
      PyErr_SetString(PyExc_RuntimeError, "array type not supported");
      goto exit;
    }
    NI_FILTER_NEXT2(fi, ii, io, oo, pi, po);
  }
exit:
  if (offsets) free(offsets);
  if (ss) free(ss);
  return PyErr_Occurred() ? 0 : 1;
}
Пример #5
0
static PyObject *Py_FindObjects(PyObject *obj, PyObject *args)
{
    PyArrayObject *input = NULL;
    PyObject *result = NULL, *tuple = NULL, *start = NULL, *end = NULL;
    PyObject *slc = NULL;
    int jj;
    npy_intp max_label;
    npy_intp ii, *regions = NULL;

    if (!PyArg_ParseTuple(args, "O&n",
                          NI_ObjectToInputArray, &input, &max_label))
        goto exit;

    if (max_label < 0)
        max_label = 0;
    if (max_label > 0) {
        if (PyArray_NDIM(input) > 0) {
            regions = (npy_intp*)malloc(2 * max_label * PyArray_NDIM(input) *
                                        sizeof(npy_intp));
        } else {
            regions = (npy_intp*)malloc(max_label * sizeof(npy_intp));
        }
        if (!regions) {
            PyErr_NoMemory();
            goto exit;
        }
    }

    if (!NI_FindObjects(input, max_label, regions))
        goto exit;

    result = PyList_New(max_label);
    if (!result) {
        PyErr_NoMemory();
        goto exit;
    }

    for(ii = 0; ii < max_label; ii++) {
        npy_intp idx =
                PyArray_NDIM(input) > 0 ? 2 * PyArray_NDIM(input) * ii : ii;
        if (regions[idx] >= 0) {
            PyObject *tuple = PyTuple_New(PyArray_NDIM(input));
            if (!tuple) {
                PyErr_NoMemory();
                goto exit;
            }
            for(jj = 0; jj < PyArray_NDIM(input); jj++) {
                start = PyLong_FromSsize_t(regions[idx + jj]);
                end = PyLong_FromSsize_t(regions[idx + jj +
                                             PyArray_NDIM(input)]);
                if (!start || !end) {
                    PyErr_NoMemory();
                    goto exit;
                }
                slc = PySlice_New(start, end, NULL);
                if (!slc) {
                    PyErr_NoMemory();
                    goto exit;
                }
                Py_XDECREF(start);
                Py_XDECREF(end);
                start = end = NULL;
                PyTuple_SetItem(tuple, jj, slc);
                slc = NULL;
            }
            PyList_SetItem(result, ii, tuple);
            tuple = NULL;
        } else {
            Py_INCREF(Py_None);
            PyList_SetItem(result, ii, Py_None);
        }
    }

    Py_INCREF(result);

 exit:
    Py_XDECREF(input);
    Py_XDECREF(result);
    Py_XDECREF(tuple);
    Py_XDECREF(start);
    Py_XDECREF(end);
    Py_XDECREF(slc);
    free(regions);
    if (PyErr_Occurred()) {
        Py_XDECREF(result);
        return NULL;
    } else {
        return result;
    }
}
Пример #6
0
PyObject *
psutil_disk_io_counters(PyObject *self, PyObject *args) {
    int i, dk_ndrive, mib[3];
    size_t len;
    struct diskstats *stats;

    PyObject *py_retdict = PyDict_New();
    PyObject *py_disk_info = NULL;
    if (py_retdict == NULL)
        return NULL;

    mib[0] = CTL_HW;
    mib[1] = HW_DISKSTATS;
    len = 0;
    if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) {
        warn("can't get hw.diskstats size");
        PyErr_SetFromErrno(PyExc_OSError);
        goto error;
    }
    dk_ndrive = (int)(len / sizeof(struct diskstats));

    stats = malloc(len);
    if (stats == NULL) {
        warn("can't malloc");
        PyErr_NoMemory();
        goto error;
    }
    if (sysctl(mib, 2, stats, &len, NULL, 0) < 0 ) {
        warn("could not read hw.diskstats");
        PyErr_SetFromErrno(PyExc_OSError);
        goto error;
    }

    for (i = 0; i < dk_ndrive; i++) {
        py_disk_info = Py_BuildValue(
            "(KKKKLL)",
            stats[i].ds_rxfer,
            stats[i].ds_wxfer,
            stats[i].ds_rbytes,
            stats[i].ds_wbytes,
            // assume half read - half writes.
            // TODO: why?
            (long long) PSUTIL_TV2DOUBLE(stats[i].ds_time) / 2,
            (long long) PSUTIL_TV2DOUBLE(stats[i].ds_time) / 2);
        if (!py_disk_info)
            goto error;
        if (PyDict_SetItemString(py_retdict, stats[i].ds_name, py_disk_info))
            goto error;
        Py_DECREF(py_disk_info);
    }

    free(stats);
    return py_retdict;

error:
    Py_XDECREF(py_disk_info);
    Py_DECREF(py_retdict);
    if (stats != NULL)
        free(stats);
    return NULL;
}
Пример #7
0
static PyObject* GetDataString(Cursor* cur, Py_ssize_t iCol)
{
    // Returns a string, unicode, or bytearray object for character and binary data.
    //
    // In Python 2.6+, binary data is returned as a byte array.  Earlier versions will return an ASCII str object here
    // which will be wrapped in a buffer object by the caller.
    //
    // NULL terminator notes:
    //
    //  * pinfo->column_size, from SQLDescribeCol, does not include a NULL terminator.  For example, column_size for a
    //    char(10) column would be 10.  (Also, when dealing with SQLWCHAR, it is the number of *characters*, not bytes.)
    //
    //  * When passing a length to PyString_FromStringAndSize and similar Unicode functions, do not add the NULL
    //    terminator -- it will be added automatically.  See objects/stringobject.c
    //
    //  * SQLGetData does not return the NULL terminator in the length indicator.  (Therefore, you can pass this value
    //    directly to the Python string functions.)
    //
    //  * SQLGetData will write a NULL terminator in the output buffer, so you must leave room for it.  You must also
    //    include the NULL terminator in the buffer length passed to SQLGetData.
    //
    // ODBC generalization:
    //  1) Include NULL terminators in input buffer lengths.
    //  2) NULL terminators are not used in data lengths.

    ColumnInfo* pinfo = &cur->colinfos[iCol];

    // Some Unix ODBC drivers do not return the correct length.
    if (pinfo->sql_type == SQL_GUID)
        pinfo->column_size = 36;

    SQLSMALLINT nTargetType;

    switch (pinfo->sql_type)
    {
    case SQL_CHAR:
    case SQL_VARCHAR:
    case SQL_LONGVARCHAR:
    case SQL_GUID:
    case SQL_SS_XML:
#if PY_MAJOR_VERSION < 3
        if (cur->cnxn->unicode_results)
            nTargetType  = SQL_C_WCHAR;
        else
            nTargetType  = SQL_C_CHAR;
#else
        nTargetType  = SQL_C_WCHAR;
#endif

        break;

    case SQL_WCHAR:
    case SQL_WVARCHAR:
    case SQL_WLONGVARCHAR:
        nTargetType  = SQL_C_WCHAR;
        break;

    default:
        nTargetType  = SQL_C_BINARY;
        break;
    }

    char tempBuffer[1026]; // Pad with 2 bytes for driver bugs
    DataBuffer buffer(nTargetType, tempBuffer, sizeof(tempBuffer)-2);

    for (int iDbg = 0; iDbg < 10; iDbg++) // failsafe
    {
        SQLRETURN ret;
        SQLLEN cbData = 0;

        Py_BEGIN_ALLOW_THREADS
        ret = SQLGetData(cur->hstmt, (SQLUSMALLINT)(iCol+1), nTargetType, buffer.GetBuffer(), buffer.GetRemaining(), &cbData);
        Py_END_ALLOW_THREADS;

        if (cbData == SQL_NULL_DATA || (ret == SQL_SUCCESS && cbData < 0))
        {
            // HACK: FreeTDS 0.91 on OS/X returns -4 for NULL data instead of SQL_NULL_DATA (-1).  I've traced into the
            // code and it appears to be the result of assigning -1 to a SQLLEN:
            //
            //   if (colinfo->column_cur_size < 0) {
            //       /* TODO check what should happen if pcbValue was NULL */
            //       *pcbValue = SQL_NULL_DATA;
            //
            // I believe it will be fine to treat all negative values as NULL for now.

            Py_RETURN_NONE;
        }

        if (!SQL_SUCCEEDED(ret) && ret != SQL_NO_DATA)
            return RaiseErrorFromHandle("SQLGetData", cur->cnxn->hdbc, cur->hstmt);

        // The SQLGetData behavior is incredibly quirky.  It doesn't tell us the total, the total we've read, or even
        // the amount just read.  It returns the amount just read, plus any remaining.  Unfortunately, the only way to
        // pick them apart is to subtract out the amount of buffer we supplied.

        SQLLEN cbBuffer = buffer.GetRemaining(); // how much we gave SQLGetData

        if (ret == SQL_SUCCESS_WITH_INFO)
        {
            // There is more data than fits in the buffer.  The amount of data equals the amount of data in the buffer
            // minus a NULL terminator.

            SQLLEN cbRead;
            SQLLEN cbMore;

            if (cbData == SQL_NO_TOTAL)
            {
                // We don't know how much more, so just guess.
                cbRead = cbBuffer - buffer.null_size;
                cbMore = 2048;
            }
            else if (cbData >= cbBuffer)
            {
                // There is more data.  We supplied cbBuffer, but there was cbData (more).  We received cbBuffer, so we
                // need to subtract that, allocate enough to read the rest (cbData-cbBuffer).

                cbRead = cbBuffer - buffer.null_size;
                cbMore = cbData - cbRead;
            }
            else
            {
                // I'm not really sure why I would be here ... I would have expected SQL_SUCCESS
                cbRead = cbData - buffer.null_size;
                cbMore = 0;
            }

            buffer.AddUsed(cbRead);
            if (!buffer.AllocateMore(cbMore))
                return PyErr_NoMemory();
        }
        else if (ret == SQL_SUCCESS)
        {
            // For some reason, the NULL terminator is used in intermediate buffers but not in this final one.
            buffer.AddUsed(cbData);
        }

        if (ret == SQL_SUCCESS || ret == SQL_NO_DATA)
            return buffer.DetachValue();
    }

    // REVIEW: Add an error message.
    return 0;
}
Пример #8
0
static PyObject *msameff(PyObject *self, PyObject *args, PyObject *kwargs) {

    PyArrayObject *msa,*pythonw;
    double theta = 0.0;
    int meff_only = 1, refine = 0;
    int alignlist[26] = {1, 0, 2, 3, 4, 5, 6, 7, 8, 0, 9, 10, 11, 12,
             0, 13, 14, 15, 16, 17, 0, 18, 19, 0, 20, 0};
    static char *kwlist[] = {"msa", "theta", "meff_only", "refine", "w", NULL};
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Odii|O", kwlist,
                                     &msa, &theta, &meff_only, &refine,
                                     &pythonw))
        return NULL;
    /* make sure to have a contiguous and well-behaved array */
    msa = PyArray_GETCONTIGUOUS(msa);
    /* check dimensions */
    long number = PyArray_DIMS(msa)[0], length = PyArray_DIMS(msa)[1];
    long i, j, k, l = 0;
    /* get pointers to data */
    char *seq = (char *) PyArray_DATA(msa); /*size: number x length */

    /*Set ind and get l first.*/
    int *ind = malloc(length * sizeof(int));
    if (!ind) {
        return PyErr_NoMemory();
    }

    if (!refine){
        for (i = 0; i < length; i++){
            l += 1;
            ind[i] = l;
        }
    }
    else{
        for (i = 0; i < length; i++){
            if (seq[i] <= 90 && seq[i] >= 65){
                l += 1;
                ind[i] = l;
            }
            else
                ind[i] = 0;
        }
    }

    /*Use l to set align and w size.*/
    int *align = malloc(number * l * sizeof(int));
    if (!align) {
        free(ind);
        return PyErr_NoMemory();
    }
    for (i = 0; i < number * l; i++){
        align[i] = 0;
    }
    double *w = malloc(number * sizeof(double));
    if (!w) {
        free(ind);
        free(align);
        return PyErr_NoMemory();
    }

    #define align(x,y) align[(x)*l+(y)]

    /*Set align matrix*/
    for (i = 0; i < number; i++){
        for (j = 0; j < length; j++){
            if (ind[j] != 0){
                if (seq[i*length+j] >= 65 && seq[i*length+j] <= 90)
                    align(i,ind[j]-1) = alignlist[seq[i*length+j] - 65];
                else
                    align(i,ind[j]-1) = 0;
            }
        }
    }

    /*Calculate weight(w) for each sequence, sum of w is Meff*/
    for (i = 0; i < number; i++)
        w[i] = 1.;
    for (i = 0; i < number; i++)
        for (j = i+1; j < number; j++){
            double temp = 0.;
            for (k = 0; k < l; k++){
                if (align(i,k) != align(j,k))
                    temp += 1.;
            }
            temp /= l;
            if (temp < theta){
                w[i] += 1.;
                w[j] += 1.;
            }
        }
    double meff = 0.0;
    for (i = 0; i < number; i++){
        w[i] = 1./ w[i];
        meff += w[i];
    }

    #undef align

    /*Clean up memory.*/
    free(ind);
    if (meff_only == 1){
        free(align);
        free(w);
        return Py_BuildValue("d", meff);
    }
    else if (meff_only == 2){
        for (i = 0; i < number; i++)
            w[i] /= meff;
        return Py_BuildValue("dllll", meff, number, l , w, align);
    }
    else {
        free(align);
        pythonw = PyArray_GETCONTIGUOUS(pythonw);
        double *pw = (double *) PyArray_DATA(pythonw);
        for (i = 0; i < number; i++){
            pw[i]=w[i];
        }
        free(w);
        return Py_BuildValue("dO",meff,pythonw);
    }
}
Пример #9
0
static PyObject *msadirectinfo1(PyObject *self, PyObject *args, PyObject *kwargs) {

    PyArrayObject *msa, *cinfo, *pinfo;
    double theta = 0.2, pseudocount_weight = 0.5;
    int refine = 0, q = 0;
    static char *kwlist[] = {"msa", "c", "prob", "theta", "pseudocount_weight",
                             "refine", "q", NULL};
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOOddi|i", kwlist,
                                     &msa, &cinfo, &pinfo, &theta,
                                     &pseudocount_weight, &refine, &q))
        return NULL;
    long i, j, k, k1, k2;
    cinfo = PyArray_GETCONTIGUOUS(cinfo);
    pinfo = PyArray_GETCONTIGUOUS(pinfo);
    double *c = (double *) PyArray_DATA(cinfo);
    double *prob = (double *) PyArray_DATA(pinfo);

    /*Calculate meff, w and align.*/
    double meff = -1.;
    long number = 0, l = 0;
    int *align = NULL;
    double *w = NULL;
    PyObject *meffinfo;
    meffinfo = msameff(NULL, Py_BuildValue("(O)", msa),
             Py_BuildValue("{s:d,s:i,s:i}", "theta", theta, "meff_only", 2,
                 "refine", refine));
    if (!PyArg_ParseTuple(meffinfo, "dllll", &meff, &number, &l, &w, &align))
        return NULL;

    /*Build single probablity. use pseudocount_weight to weight it.*/
    double pse_weight_val = pseudocount_weight / q;
    double pro_weight = 1. - pseudocount_weight;
    for (i = 0; i < q*l; i++)
        prob[i] = pse_weight_val;
    #define prob(x,y) prob[(x)*q + (y)]
    #define align(x,y) align[(x)*l + (y)]
    for (i = 0; i < number; i++)
        for (j = 0; j < l; j++)
            prob(j, align(i,j)) += pro_weight * w[i];

    /*Calculate C matrix.*/
    double *joint = malloc(q*q*sizeof(double));
    if (!joint){
        free(w);
        free(align);
        return PyErr_NoMemory();
    }
    #define joint(x,y) joint[(x)*q + (y)]
    #define c(x,y) c[(x)*l*(q-1) + (y)]
    for (i = 0; i < l; i++){
        for (j = i; j < l; j++){

            if (i==j){
                for (k = 0; k < q*q; k++)
                    joint[k] = 0.;
                pse_weight_val = pseudocount_weight / q;
                for (k = 0; k < q; k++)
                    joint(k,k) = pse_weight_val;
            }
            else{
                pse_weight_val = pseudocount_weight / q / q;
                for (k = 0; k < q*q; k++)
                    joint[k] = pse_weight_val;
            }

            for (k = 0; k < number; k++){
                joint(align(k,i), align(k,j)) += pro_weight * w[k];
            }

            for (k1 = 0; k1 < q-1; k1++){
                for(k2 = 0; k2 < q-1; k2++){
                    c((q-1)*j+k2, (q-1)*i+k1) = c((q-1)*i+k1, (q-1)*j+k2) = joint(k1,k2) - prob(i,k1) * prob(j,k2);
                    // c((q-1)*j+k2, (q-1)*i+k1) = c((q-1)*i+k1, (q-1)*j+k2);
                }
            }
        }
    }

    free(w);
    free(align);
    free(joint);
    #undef prob
    #undef align
    #undef joint
    #undef c
    return Py_BuildValue("dllOO", meff, number, l, cinfo, pinfo);
}
Пример #10
0
static PyObject *
select_select(PyObject *self, PyObject *args)
{
#ifdef SELECT_USES_HEAP
	pylist *rfd2obj, *wfd2obj, *efd2obj;
#else  /* !SELECT_USES_HEAP */
	/* XXX: All this should probably be implemented as follows:
	 * - find the highest descriptor we're interested in
	 * - add one
	 * - that's the size
	 * See: Stevens, APitUE, $12.5.1
	 */
	pylist rfd2obj[FD_SETSIZE + 1];
	pylist wfd2obj[FD_SETSIZE + 1];
	pylist efd2obj[FD_SETSIZE + 1];
#endif /* SELECT_USES_HEAP */
	PyObject *ifdlist, *ofdlist, *efdlist;
	PyObject *ret = NULL;
	PyObject *tout = Py_None;
	fd_set ifdset, ofdset, efdset;
	double timeout;
	struct timeval tv, *tvp;
	long seconds;
	int imax, omax, emax, max;
	int n;

	/* convert arguments */
	if (!PyArg_ParseTuple(args, "OOO|O:select",
			      &ifdlist, &ofdlist, &efdlist, &tout))
		return NULL;

	if (tout == Py_None)
		tvp = (struct timeval *)0;
	else if (!PyNumber_Check(tout)) {
		PyErr_SetString(PyExc_TypeError,
				"timeout must be a float or None");
		return NULL;
	}
	else {
		timeout = PyFloat_AsDouble(tout);
		if (timeout == -1 && PyErr_Occurred())
			return NULL;
		if (timeout > (double)LONG_MAX) {
			PyErr_SetString(PyExc_OverflowError,
					"timeout period too long");
			return NULL;
		}
		seconds = (long)timeout;
		timeout = timeout - (double)seconds;
		tv.tv_sec = seconds;
		tv.tv_usec = (long)(timeout*1000000.0);
		tvp = &tv;
	}


#ifdef SELECT_USES_HEAP
	/* Allocate memory for the lists */
	rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
	wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
	efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
	if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
		if (rfd2obj) PyMem_DEL(rfd2obj);
		if (wfd2obj) PyMem_DEL(wfd2obj);
		if (efd2obj) PyMem_DEL(efd2obj);
		return PyErr_NoMemory();
	}
#endif /* SELECT_USES_HEAP */
	/* Convert sequences to fd_sets, and get maximum fd number
	 * propagates the Python exception set in seq2set()
	 */
	rfd2obj[0].sentinel = -1;
	wfd2obj[0].sentinel = -1;
	efd2obj[0].sentinel = -1;
	if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0) 
		goto finally;
	if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0) 
		goto finally;
	if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0) 
		goto finally;
	max = imax;
	if (omax > max) max = omax;
	if (emax > max) max = emax;

	Py_BEGIN_ALLOW_THREADS
	n = select(max, &ifdset, &ofdset, &efdset, tvp);
	Py_END_ALLOW_THREADS

#ifdef MS_WINDOWS
	if (n == SOCKET_ERROR) {
		PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError());
	}
#else
	if (n < 0) {
		PyErr_SetFromErrno(SelectError);
	}
#endif
	else if (n == 0) {
                /* optimization */
		ifdlist = PyList_New(0);
		if (ifdlist) {
			ret = PyTuple_Pack(3, ifdlist, ifdlist, ifdlist);
			Py_DECREF(ifdlist);
		}
	}
	else {
		/* any of these three calls can raise an exception.  it's more
		   convenient to test for this after all three calls... but
		   is that acceptable?
		*/
		ifdlist = set2list(&ifdset, rfd2obj);
		ofdlist = set2list(&ofdset, wfd2obj);
		efdlist = set2list(&efdset, efd2obj);
		if (PyErr_Occurred())
			ret = NULL;
		else
			ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);

		Py_DECREF(ifdlist);
		Py_DECREF(ofdlist);
		Py_DECREF(efdlist);
	}
	
  finally:
	reap_obj(rfd2obj);
	reap_obj(wfd2obj);
	reap_obj(efd2obj);
#ifdef SELECT_USES_HEAP
	PyMem_DEL(rfd2obj);
	PyMem_DEL(wfd2obj);
	PyMem_DEL(efd2obj);
#endif /* SELECT_USES_HEAP */
	return ret;
}
Пример #11
0
static PyObject *msasca(PyObject *self, PyObject *args, PyObject *kwargs) {

    PyArrayObject *msa, *scainfo;
    int turbo = 1;
    static char *kwlist[] = {"msa", "sca", "turbo", NULL};
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i", kwlist,
                                     &msa, &scainfo, &turbo))
        return NULL;
    /* make sure to have a contiguous and well-behaved array */
    msa = PyArray_GETCONTIGUOUS(msa);
    /* check dimensions */
    long number = PyArray_DIMS(msa)[0], length = PyArray_DIMS(msa)[1];
    /* get pointers to data */
    char *seq = (char *) PyArray_DATA(msa); /*size: number x length */
    double *sca = (double *) PyArray_DATA(scainfo);

    long i, j, k;
    double q[NUMCHARS] = {0., 0.073, 0., 0.025, 0.05, 0.061, 0.042, 0.072,
        0.023, 0.053, 0., 0.064, 0.089, 0.023, 0.043, 0., 0.052, 0.04, 0.052,
        0.073, 0.056, 0., 0.063, 0.013, 0., 0.033, 0.};
    int qlist[21] = {0, 1, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13,
                        14, 16, 17, 18, 19, 20, 22, 23, 25};

    /* weighted probability matrix length*27 */
    double **wprob = malloc(length * sizeof(double *));
    if (!wprob)
        return PyErr_NoMemory();

    /* each row of weighted probability */
    for (i = 0; i < length; i++) {
        wprob[i] = malloc(NUMCHARS * sizeof(double));
        if (!wprob[i]) {
            for (j = 0; j < i; j++)
                free(wprob[j]);
            free(wprob);
            return PyErr_NoMemory();
        }
        for (j = 0; j < NUMCHARS; j++)
            wprob[i][j] = 0;
    }

    /* single column probability */
    double *prob;

    /* weighted x~ matrix array */
    double **wx = malloc(length * sizeof(double *));
    if (!turbo)
        free(wx);
    if (!wx)
        turbo = 0;
    if (turbo) {
        for (i = 0; i < length; i++) {
            wx[i] = malloc(number * sizeof(double));
            if (!wx[i]) {
                for (j = 0; j < i; j++)
                    free(wx[j]);
                free(wx);
                turbo = 0;
            }
        }
    }

    /* build weighted probability prob */
    for (i = 0; i < length; i++){
        prob = wprob[i];
        double phi[NUMCHARS];
        for (j = 0; j < NUMCHARS; j++){
            prob[j] = 0.0;
            phi[i] = 0.0;
        }
        for (j=0; j<number; j++){
            int temp = seq[j * length + i];
            temp = (temp > 96) ?  temp - 97 : temp - 65;
            if ((temp >= 0) && (temp <= 25))
                prob[temp + 1] += 1.0 ;
        }
        for (j=0; j<NUMCHARS; j++){
            prob[j] = prob[j] / number;
        }
        if (prob[2] > 0){ /* B -> D, N  */
            prob[4] += prob[2] / 2.;
            prob[14] += prob[2] / 2.;
            prob[2] = 0.;
        }
        if (prob[10] > 0){ /* J -> I, L  */
            prob[9] += prob[10] / 2.;
            prob[12] += prob[10] / 2.;
            prob[10] = 0.;
        }
        if (prob[26] > 0){ /* Z -> E, Q  */
            prob[4] += prob[26] / 2.;
            prob[17] += prob[26] / 2.;
            prob[26] = 0.;
        }
        if (prob[24] > 0) { /* X -> 20 AA */
            for (k = 0; k < 20; k++)
                prob[twenty[k]] += prob[24] / 20.;
            prob[24] = 0.;
        }
        double sum=0.0;
        for (j = 0; j < 21; j++){
            phi[qlist[j]] = (prob[qlist[j]] == 0.0 || q[qlist[j]] == 0.0
                            || prob[qlist[j]] == 1.0 || q[qlist[j]] == 1.0)
                    ? 0.0
                    : log(prob[qlist[j]] * (1 - q[qlist[j]]) /
                        (1 - prob[qlist[j]]) / q[qlist[j]]);
            phi[qlist[j]] = (phi[qlist[j]] >= 0.) ?
                phi[qlist[j]] : -phi[qlist[j]];
            prob[qlist[j]] = prob[qlist[j]] * phi[qlist[j]];
            sum += prob[qlist[j]] * prob[qlist[j]];
            prob[qlist[j]] = prob[qlist[j]] * phi[qlist[j]];
        }
        sum = sqrt(sum);
        if (sum == 0.)
            for (j = 0; j < 21; j++){
                prob[qlist[j]] = 0.;
            }
        else
            for (j = 0; j < 21; j++){
                prob[qlist[j]] = prob[qlist[j]] / sum;
            }
        prob[2] = (prob[4] + prob[14]) /2.0;
        prob[10] = (prob[9] + prob[12]) /2.0;
        prob[26] = (prob[4] + prob[17]) /2.0;
        sum =0.0;
        for (k = 0; k < 20; k++)
            sum += prob[twenty[k]];
        sum = sum / 20.0;
        prob[24] = sum;
        if (turbo){
            for (j = 0; j < number; j++){
                int temp = seq[j * length + i];
                temp = (temp > 96) ? temp - 97 : temp - 65;
                if (temp >= 0 && temp <= 25)
                    wx[i][j] = prob[temp + 1];
                else
                    wx[i][j] = 0.0;
            }
        }
    }

    /* Calculate SCA Matrix*/
    for (i=0;i<length;i++){
        for (j = i;j<length;j++){
            double *icol, *jcol, sumi=0.0, sumj=0.0, sum=0.0;
            if (turbo){
                icol=wx[i];
                jcol=wx[j];
                for (k=0; k< number; k++){
                    sumi += icol[k];
                    sumj += jcol[k];
                    sum += icol[k]*jcol[k];
                }
            }
            else{
                for (k = 0; k < number; k++){
                    int tempi = (seq[k*length + i] > 96) ?
                    seq[k * length + i] - 97 : seq[k * length + i] - 65;
                    double xi = (tempi >= 0 && tempi <= 25) ?
                        wprob[i][tempi + 1] : wprob[i][0];
                    int tempj = (seq[k * length + j] > 96) ?
                        seq[k * length + j] - 97 : seq[k * length + j] - 65;
                    double xj = (tempj >= 0 && tempj <= 25) ?
                        wprob[j][tempj + 1] : wprob[j][0];
                    sumi += xi;
                    sumj += xj;
                    sum += xi * xj;
                }
            }
            sum /= number;
            sumj /= number;
            sumi /= number;
            sum = sum - sumi * sumj;
            sum = sum >= 0 ? sum : -sum ;
            sca[i * length + j] = sca[j * length + i] = sum;
        }
    }

    /* free memory */
    for (j = 1; j < length; j++)
        free(wprob[j]);
    free(wprob);
    if (turbo){
        for (j = 1; j < length; j++)
            free(wx[j]);
        free(wx);
    }

    return Py_BuildValue("O", scainfo);
}
Пример #12
0
PyObject*
PyImaging_JpegEncoderNew(PyObject* self, PyObject* args)
{
    ImagingEncoderObject* encoder;

    char *mode;
    char *rawmode;
    int quality = 0;
    int progressive = 0;
    int smooth = 0;
    int optimize = 0;
    int streamtype = 0; /* 0=interchange, 1=tables only, 2=image only */
    int xdpi = 0, ydpi = 0;
    int subsampling = -1; /* -1=default, 0=none, 1=medium, 2=high */
    PyObject* qtables=NULL;
    unsigned int **qarrays = NULL;
    char* extra = NULL;
    int extra_size;
    char* rawExif = NULL;
    int rawExifLen = 0;

    if (!PyArg_ParseTuple(args, "ss|iiiiiiiiO"PY_ARG_BYTES_LENGTH""PY_ARG_BYTES_LENGTH,
                          &mode, &rawmode, &quality,
                          &progressive, &smooth, &optimize, &streamtype,
                          &xdpi, &ydpi, &subsampling, &qtables, &extra, &extra_size,
                          &rawExif, &rawExifLen))
	return NULL;

    encoder = PyImaging_EncoderNew(sizeof(JPEGENCODERSTATE));
    if (encoder == NULL)
	return NULL;

    if (get_packer(encoder, mode, rawmode) < 0)
	return NULL;

    qarrays = get_qtables_arrays(qtables);

    if (extra && extra_size > 0) {
        char* p = malloc(extra_size);
        if (!p)
            return PyErr_NoMemory();
        memcpy(p, extra, extra_size);
        extra = p;
    } else
        extra = NULL;

    if (rawExif && rawExifLen > 0) {
        char* pp = malloc(rawExifLen);
        if (!pp)
            return PyErr_NoMemory();
        memcpy(pp, rawExif, rawExifLen);
        rawExif = pp;
    } else
        rawExif = NULL;

    encoder->encode = ImagingJpegEncode;

    ((JPEGENCODERSTATE*)encoder->state.context)->quality = quality;
    ((JPEGENCODERSTATE*)encoder->state.context)->qtables = qarrays;
    ((JPEGENCODERSTATE*)encoder->state.context)->subsampling = subsampling;
    ((JPEGENCODERSTATE*)encoder->state.context)->progressive = progressive;
    ((JPEGENCODERSTATE*)encoder->state.context)->smooth = smooth;
    ((JPEGENCODERSTATE*)encoder->state.context)->optimize = optimize;
    ((JPEGENCODERSTATE*)encoder->state.context)->streamtype = streamtype;
    ((JPEGENCODERSTATE*)encoder->state.context)->xdpi = xdpi;
    ((JPEGENCODERSTATE*)encoder->state.context)->ydpi = ydpi;
    ((JPEGENCODERSTATE*)encoder->state.context)->extra = extra;
    ((JPEGENCODERSTATE*)encoder->state.context)->extra_size = extra_size;
    ((JPEGENCODERSTATE*)encoder->state.context)->rawExif = rawExif;
    ((JPEGENCODERSTATE*)encoder->state.context)->rawExifLen = rawExifLen;

    return (PyObject*) encoder;
}
Пример #13
0
/*
 * Converts a Python input into a non-normalized list of holidays.
 *
 * IMPORTANT: This function can't do the normalization, because it doesn't
 *            know the weekmask. You must call 'normalize_holiday_list'
 *            on the result before using it.
 */
NPY_NO_EXPORT int
PyArray_HolidaysConverter(PyObject *dates_in, npy_holidayslist *holidays)
{
    PyArrayObject *dates = NULL;
    PyArray_Descr *date_dtype = NULL;
    npy_intp count;

    /* Make 'dates' into an array */
    if (PyArray_Check(dates_in)) {
        dates = (PyArrayObject *)dates_in;
        Py_INCREF(dates);
    }
    else {
        PyArray_Descr *datetime_dtype;

        /* Use the datetime dtype with generic units so it fills it in */
        datetime_dtype = PyArray_DescrFromType(NPY_DATETIME);
        if (datetime_dtype == NULL) {
            goto fail;
        }

        /* This steals the datetime_dtype reference */
        dates = (PyArrayObject *)PyArray_FromAny(dates_in, datetime_dtype,
                                                0, 0, 0, dates_in);
        if (dates == NULL) {
            goto fail;
        }
    }

    date_dtype = create_datetime_dtype_with_unit(NPY_DATETIME, NPY_FR_D);
    if (date_dtype == NULL) {
        goto fail;
    }

    if (!PyArray_CanCastTypeTo(PyArray_DESCR(dates),
                                    date_dtype, NPY_SAFE_CASTING)) {
        PyErr_SetString(PyExc_ValueError, "Cannot safely convert "
                        "provided holidays input into an array of dates");
        goto fail;
    }
    if (PyArray_NDIM(dates) != 1) {
        PyErr_SetString(PyExc_ValueError, "holidays must be a provided "
                        "as a one-dimensional array");
        goto fail;
    }

    /* Allocate the memory for the dates */
    count = PyArray_DIM(dates, 0);
    holidays->begin = PyArray_malloc(sizeof(npy_datetime) * count);
    if (holidays->begin == NULL) {
        PyErr_NoMemory();
        goto fail;
    }
    holidays->end = holidays->begin + count;

    /* Cast the data into a raw date array */
    if (PyArray_CastRawArrays(count,
                            PyArray_BYTES(dates), (char *)holidays->begin,
                            PyArray_STRIDE(dates, 0), sizeof(npy_datetime),
                            PyArray_DESCR(dates), date_dtype,
                            0) != NPY_SUCCEED) {
        goto fail;
    }

    Py_DECREF(dates);
    Py_DECREF(date_dtype);

    return 1;

fail:
    Py_XDECREF(dates);
    Py_XDECREF(date_dtype);
    return 0;
}
Пример #14
0
static PyObject *
Util_func_getaddrinfo(PyObject *obj, PyObject *args, PyObject *kwargs)
{
    char *name;
    char port_str[6];
    int port, family, socktype, protocol, flags, r;
    struct addrinfo hints;
    getaddrinfo_cb_data_t *cb_data = NULL;
    uv_getaddrinfo_t* req = NULL;
    Loop *loop;
    PyObject *callback;

    UNUSED_ARG(obj);

    static char *kwlist[] = {"loop", "callback", "name", "port", "family", "socktype", "protocol", "flags", NULL};

    port = socktype = protocol = flags = 0;
    family = AF_UNSPEC;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!sO|iiiii:getaddrinfo", kwlist, &LoopType, &loop, &name, &callback, &port, &family, &socktype, &protocol, &flags)) {
        return NULL;
    }

    if (!PyCallable_Check(callback)) {
        PyErr_SetString(PyExc_TypeError, "a callable is required");
        return NULL;
    }

    if (port < 0 || port > 65536) {
        PyErr_SetString(PyExc_ValueError, "port must be between 0 and 65536");
        return NULL;
    }
    snprintf(port_str, sizeof(port_str), "%d", port);

    req = PyMem_Malloc(sizeof(uv_getaddrinfo_t));
    if (!req) {
        PyErr_NoMemory();
        goto error;
    }

    cb_data = PyMem_Malloc(sizeof(getaddrinfo_cb_data_t));
    if (!cb_data) {
        PyErr_NoMemory();
        goto error;
    }

    Py_INCREF(loop);
    Py_INCREF(callback);
    cb_data->loop = loop;
    cb_data->cb = callback;
    req->data = (void *)cb_data;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = family;
    hints.ai_socktype = socktype;
    hints.ai_protocol = protocol;
    hints.ai_flags = flags;

    r = uv_getaddrinfo(loop->uv_loop, req, &getaddrinfo_cb, name, port_str, &hints);
    if (r != 0) {
        RAISE_UV_EXCEPTION(loop->uv_loop, PyExc_UVError);
        goto error;
    }

    Py_RETURN_NONE;

error:
    if (req) {
        PyMem_Free(req);
    }
    return NULL;
}
Пример #15
0
static PyObject *create(PyObject * obj, PyObject * args)
{
	PyObject *f = NULL;
	PyObject *gradf = NULL;
	PyObject *g = NULL;
	PyObject *jacg = NULL;
	PyObject *h = NULL;
	PyObject *applynew = NULL;

	DispatchData myowndata;

	/*
	 * I have to create a new python object here, return this python object
	 */

	int n;			/* Number of variables */
	PyArrayObject *xL = NULL;
	PyArrayObject *xU = NULL;
	int m;			/* Number of constraints */
	PyArrayObject *gL = NULL;
	PyArrayObject *gU = NULL;

	problem *object = NULL;

	int nele_jac;
	int nele_hess;

	Number *x_L = NULL;	/* lower bounds on x */
	Number *x_U = NULL;	/* upper bounds on x */
	Number *g_L = NULL;	/* lower bounds on g */
	Number *g_U = NULL;	/* upper bounds on g */

	double *xldata, *xudata;
	double *gldata, *gudata;

	int i;

	DispatchData *dp = NULL;

	PyObject *retval = NULL;

	/* Init the myowndata field */
	myowndata.eval_f_python = NULL;
	myowndata.eval_grad_f_python = NULL;
	myowndata.eval_g_python = NULL;
	myowndata.eval_jac_g_python = NULL;
	myowndata.eval_h_python = NULL;
	myowndata.apply_new_python = NULL;
	myowndata.userdata = NULL;

	/* "O!", &PyArray_Type &a_x  */
	if (!PyArg_ParseTuple(args, "iO!O!iO!O!iiOOOO|OO:pyipoptcreate",
			      &n, &PyArray_Type, &xL,
			      &PyArray_Type, &xU,
			      &m,
			      &PyArray_Type, &gL,
			      &PyArray_Type, &gU,
			      &nele_jac, &nele_hess,
			      &f, &gradf, &g, &jacg, &h, &applynew)) {
		retval = NULL;
		SAFE_FREE(x_L);
		SAFE_FREE(x_U);
		SAFE_FREE(g_L);
		SAFE_FREE(g_U);
		return retval;
	}
	if (!PyCallable_Check(f) ||
	    !PyCallable_Check(gradf) ||
	    !PyCallable_Check(g) || !PyCallable_Check(jacg)) {
		PyErr_SetString(PyExc_TypeError,
				"Need a callable object for callback functions");
		retval = NULL;
		SAFE_FREE(x_L);
		SAFE_FREE(x_U);
		SAFE_FREE(g_L);
		SAFE_FREE(g_U);
		return retval;
	}
	myowndata.eval_f_python = f;
	myowndata.eval_grad_f_python = gradf;
	myowndata.eval_g_python = g;
	myowndata.eval_jac_g_python = jacg;

	if (h != NULL) {
		if (PyCallable_Check(h)) {
			myowndata.eval_h_python = h;
		} else {
			PyErr_SetString(PyExc_TypeError,
					"Need a callable object for function h.");
			retval = NULL;
			SAFE_FREE(x_L);
			SAFE_FREE(x_U);
			SAFE_FREE(g_L);
			SAFE_FREE(g_U);
			return retval;
		}
	} else {
		logger("[PyIPOPT] Ipopt will use Hessian approximation.\n");
	}

	if (applynew != NULL) {
		if (PyCallable_Check(applynew)) {
			myowndata.apply_new_python = applynew;
		} else {
			PyErr_SetString(PyExc_TypeError,
					"Need a callable object for function applynew.");
			retval = NULL;
			SAFE_FREE(x_L);
			SAFE_FREE(x_U);
			SAFE_FREE(g_L);
			SAFE_FREE(g_U);
			return retval;
		}
	}
	if (m < 0 || n < 0) {
		PyErr_SetString(PyExc_TypeError, "m or n can't be negative");
		retval = NULL;
		SAFE_FREE(x_L);
		SAFE_FREE(x_U);
		SAFE_FREE(g_L);
		SAFE_FREE(g_U);
		return retval;
	}
	x_L = (Number *) malloc(sizeof(Number) * n);
	x_U = (Number *) malloc(sizeof(Number) * n);
	if (!x_L || !x_U) {
		retval = PyErr_NoMemory();
		SAFE_FREE(x_L);
		SAFE_FREE(x_U);
		SAFE_FREE(g_L);
		SAFE_FREE(g_U);
		return retval;
	}
	xldata = (double *)xL->data;
	xudata = (double *)xU->data;
	for (i = 0; i < n; i++) {
		x_L[i] = xldata[i];
		x_U[i] = xudata[i];
	}

	g_L = (Number *) malloc(sizeof(Number) * m);
	g_U = (Number *) malloc(sizeof(Number) * m);
	if (!g_L || !g_U)
		PyErr_NoMemory();

	gldata = (double *)gL->data;
	gudata = (double *)gU->data;

	for (i = 0; i < m; i++) {
		g_L[i] = gldata[i];
		g_U[i] = gudata[i];
	}

  /* Grab the callback objects because we want to use them later. */
  Py_XINCREF(f);
  Py_XINCREF(gradf);
  Py_XINCREF(g);
  Py_XINCREF(jacg);
  Py_XINCREF(h);
  Py_XINCREF(applynew);

	/* create the Ipopt Problem */

	int C_indexstyle = 0;
	IpoptProblem thisnlp = CreateIpoptProblem(n,
						  x_L, x_U, m, g_L, g_U,
						  nele_jac, nele_hess,
						  C_indexstyle,
						  &eval_f, &eval_g,
						  &eval_grad_f,
						  &eval_jac_g, &eval_h);
	logger("[PyIPOPT] Problem created");
	if (!thisnlp) {
		PyErr_SetString(PyExc_MemoryError, "Cannot create IpoptProblem instance");
		retval = NULL;
		SAFE_FREE(x_L);
		SAFE_FREE(x_U);
		SAFE_FREE(g_L);
		SAFE_FREE(g_U);
		return retval;
	}
	object = PyObject_NEW(problem, &IpoptProblemType);

	if (object != NULL) {
		object->n_variables = n;
		object->m_constraints = m;
		object->nlp = thisnlp;
		dp = (DispatchData *) malloc(sizeof(DispatchData));
		if (!dp) {
			retval = PyErr_NoMemory();
			SAFE_FREE(x_L);
			SAFE_FREE(x_U);
			SAFE_FREE(g_L);
			SAFE_FREE(g_U);
			return retval;
		}
		memcpy((void *)dp, (void *)&myowndata, sizeof(DispatchData));
		object->data = dp;
		retval = (PyObject *) object;
		SAFE_FREE(x_L);
		SAFE_FREE(x_U);
		SAFE_FREE(g_L);
		SAFE_FREE(g_U);
		return retval;
	} else {
		PyErr_SetString(PyExc_MemoryError, "Can't create a new Problem instance");
		retval = NULL;
		SAFE_FREE(x_L);
		SAFE_FREE(x_U);
		SAFE_FREE(g_L);
		SAFE_FREE(g_U);
		return retval;
	}
}
Пример #16
0
static PyObject *msaomes(PyObject *self, PyObject *args, PyObject *kwargs) {

    PyArrayObject *msa, *omes;
    int ambiguity = 1, turbo = 1, debug = 0;

    static char *kwlist[] = {"msa", "omes",
                             "ambiguity", "turbo", "debug", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iii", kwlist,
                                     &msa, &omes,
                                     &ambiguity, &turbo, &debug))
        return NULL;

    /* make sure to have a contiguous and well-behaved array */
    msa = PyArray_GETCONTIGUOUS(msa);

    /* check dimensions */
    long number = PyArray_DIMS(msa)[0], length = PyArray_DIMS(msa)[1];

    /* get pointers to data */
    char *seq = (char *) PyArray_DATA(msa); /*size: number x length */
    double *data = (double *) PyArray_DATA(omes);

    long i, j;
    /* allocate memory */
    unsigned char *iseq = malloc(number * sizeof(unsigned char));
    if (!iseq)
        return PyErr_NoMemory();

    /* hold transpose of the sorted character array */
    unsigned char **trans = malloc(length * sizeof(unsigned char *));
    if (!trans) {
        turbo = 0;
    }

    if (turbo) {
        /* allocate rows that will store columns of MSA */
        trans[0] = iseq;
        for (i = 1; i < length; i++) {
            trans[i] = malloc(number * sizeof(unsigned char));
            if (!trans[i]) {
                for (j = 1; j < i; j++)
                    free(trans[j]);
                free(trans);
                turbo = 0;
            }
        }
    }
    unsigned char *jseq = iseq; /* so that we don't get uninitialized warning*/

    /* length*27, a row for each column in the MSA */
    double **probs = malloc(length * sizeof(double *)), *prow;
    if (!probs) {
        if (turbo)
            for (j = 1; j < length; j++)
                free(trans[j]);
        free(trans);
        free(iseq);
        return PyErr_NoMemory();
    }

    /* 27x27, alphabet characters and a gap*/
    double **joint = malloc(NUMCHARS * sizeof(double *)), *jrow;
    if (!joint) {
        if (turbo)
            for (j = 1; j < length; j++)
                free(trans[j]);
        free(trans);
        free(iseq);
        free(probs);
        return PyErr_NoMemory();
    }

    for (i = 0; i < length; i++) {
        prow = malloc(NUMCHARS * sizeof(double));
        if (!prow) {
            for (j = 0; j < i; j++)
                free(probs[j]);
            free(probs);
            free(joint);
            if (turbo)
                for (j = 1; j < length; j++)
                    free(trans[j]);
            free(trans);
            free(iseq);
            return PyErr_NoMemory();
        }
        probs[i] = prow;
        for (j = 0; j < NUMCHARS; j++)
            prow[j] = 0;
    }

    for (i = 0; i < NUMCHARS; i++)  {
        joint[i] = malloc(NUMCHARS * sizeof(double));
        if (!joint[i]) {
            for (j = 0; j < i; j++)
                free(joint[j]);
            free(joint);
            for (j = 0; j < length; j++)
                free(probs[j]);
            free(probs);
            if (turbo)
                for (j = 1; j < length; j++)
                    free(trans[j]);
            free(trans);
            free(iseq);
            return PyErr_NoMemory();
        }
    }

    if (debug)
        printProbs(probs, length);

    unsigned char a, b;
    long k, l, diff, offset;
    double p_incr = 1. / number;
    double prb = 0;
    prow = probs[0];

    /* START OMES calculation */
    /* calculate first row of OMES matrix and all column probabilities */
    i = 0;
    data[0] = 0;
    for (j = 1; j < length; j++) {
        data[j * length + j] = 0; /* using empty, so needed for diagonal */
        jrow = probs[j];
        zeroJoint(joint);
        diff = j - 1;
        if (turbo) /* in turbo mode, there is a row for refined sequences */
            jseq = trans[j];
        for (k = 0; k < number; k++) {
            offset = k * length;
            if (diff) {
                a = iseq[k];
            } else {
                a = (unsigned char) seq[offset + i];
                if (a > 90)
                    a -= 96;
                else
                    a -= 64;
                if (a < 1 || a > 26)
                    a = 0; /* gap character */
                iseq[k] = a;
                prow[a] += p_incr;
            }

            b = (unsigned char) seq[offset + j];
            if (b > 90)
                b -= 96;
            else
                b -= 64;
            if (b < 1 || b > 26)
                b = 0; /* gap character */
            if (turbo)  /* we keep the refined chars for all sequences*/
                jseq[k] = b;
            joint[a][b] += p_incr;
            jrow[b] += p_incr;
        }

        if (ambiguity) {

            if (debug)
                printProbs(probs, length);
            if (diff)
                k = j;
            else
                k = 0;
            for (; k <= j; k++) {
                prow = probs[k];
                prb = prow[2];
                if (prb > 0) { /* B -> D, N  */
                    prb = prb / 2.;
                    prow[4] += prb;
                    prow[14] += prb;
                    prow[2] = 0;
                }
                prb = prow[10];
                if (prb > 0) { /* J -> I, L  */
                    prb = prb / 2.;
                    prow[9] += prb;
                    prow[12] += prb;
                    prow[10] = 0;
                }
                prb = prow[26];
                if (prb > 0) { /* Z -> E, Q  */
                    prb = prb / 2.;
                    prow[5] += prb;
                    prow[17] += prb;
                    prow[26] = 0;
                }
                if (prow[24] > 0) { /* X -> 20 AA */
                    prb = prow[24] / 20.;
                    for (l = 0; l < 20; l++)
                        prow[twenty[l]] += prb;
                    prow[24] = 0;
                }
            }

            if (debug)
                printProbs(probs, length);
            if (debug)
                printJoint(joint, i, j);
            sortJoint(joint);
            if (debug)
                printJoint(joint, i, j);
        }
        data[j] = data[length * j] = calcOMES(joint, probs, i, j, number);
    }
    if (debug)
        printProbs(probs, length);
    if (turbo)
        free(iseq);

    /* calculate rest of OMES matrix */
    long ioffset;
    for (i = 1; i < length; i++) {
        ioffset = i * length;
        if (turbo)
            iseq = trans[i];

        for (j = i + 1; j < length; j++) {
            zeroJoint(joint);

            if (turbo) {
                jseq = trans[j];
                for (k = 0; k < number; k++)
                    joint[iseq[k]][jseq[k]] += p_incr;

            } else {
                diff = j - i - 1;
                for (k = 0; k < number; k++) {
                    offset = k * length;
                    if (diff) {
                        a = iseq[k];
                    } else {
                        a = (unsigned char) seq[offset + i];
                        if (a > 90)
                            a -= 96;
                        else
                            a -= 64;
                        if (a < 1 || a > 26)
                            a = 0; /* gap character */
                        iseq[k] = a;
                    }

                    b = (unsigned char) seq[offset + j];
                    if (b > 90)
                        b -= 96;
                    else
                        b -= 64;
                    if (b < 1 || b > 26)
                        b = 0; /* gap character */
                    joint[a][b] += p_incr;
                }
            }
            if (ambiguity)
                sortJoint(joint);
        data[ioffset + j] = data[i + length * j] =
        calcOMES(joint, probs, i, j, number);
        }
    }

    /* free memory */
    for (i = 0; i < length; i++){
        free(probs[i]);
    }
    free(probs);
    for (i = 0; i < NUMCHARS; i++){
        free(joint[i]);
    }
    free(joint);
    if (turbo)
        for (j = 1; j < length; j++)
            free(trans[j]);
    free(trans);

    return Py_BuildValue("O", omes);
}
Пример #17
0
PyObject *solve(PyObject * self, PyObject * args)
{
	enum ApplicationReturnStatus status;	/* Solve return code */
	int i;
	int n;

	/* Return values */
	problem *temp = (problem *) self;

	IpoptProblem nlp = (IpoptProblem) (temp->nlp);
	DispatchData *bigfield = (DispatchData *) (temp->data);
	int m = temp->m_constraints;

	/* int dX[1]; */
	npy_intp dX[1];
	npy_intp dlambda[1];

	PyArrayObject *x = NULL, *mL = NULL, *mU = NULL, *lambda = NULL;
	Number obj;		/* objective value */

	PyObject *retval = NULL;
	PyArrayObject *x0 = NULL;

	PyObject *myuserdata = NULL;

	Number *newx0 = NULL;

	if (!PyArg_ParseTuple(args, "O!|O", &PyArray_Type, &x0, &myuserdata)) {
		retval = NULL;
		/* clean up and return */
		if (retval == NULL) {
			Py_XDECREF(x);
			Py_XDECREF(mL);
			Py_XDECREF(mU);
			Py_XDECREF(lambda);
		}
		SAFE_FREE(newx0);
		return retval;
	}
	if (x0->nd != 1){ //If x0 is not 1-dimensional then solve will fail and cause a segmentation fault.
		logger("[ERROR] x0 must be a 1-dimensional array");
		Py_XDECREF(x);
		Py_XDECREF(mL);
		Py_XDECREF(mU);
		Py_XDECREF(lambda);
		PyErr_SetString(PyExc_TypeError,
				"x0 passed to solve is not 1-dimensional.");
		return NULL;
	}

	if (myuserdata != NULL) {
		bigfield->userdata = myuserdata;
		/*
		 * logger("[PyIPOPT] User specified data field to callback
		 * function.\n");
		 */
	}
	if (nlp == NULL) {
		PyErr_SetString(PyExc_TypeError,
				"nlp objective passed to solve is NULL\n Problem created?\n");
		retval = NULL;
		/* clean up and return */
		if (retval == NULL) {
			Py_XDECREF(x);
			Py_XDECREF(mL);
			Py_XDECREF(mU);
			Py_XDECREF(lambda);
		}
		SAFE_FREE(newx0);
		return retval;
	}
	if (bigfield->eval_h_python == NULL) {
		AddIpoptStrOption(nlp, "hessian_approximation", "limited-memory");
		/* logger("Can't find eval_h callback function\n"); */
	}
	/* allocate space for the initial point and set the values */
	npy_intp *dim = ((PyArrayObject *) x0)->dimensions;
	n = dim[0];
	dX[0] = n;

	x = (PyArrayObject *) PyArray_SimpleNew(1, dX, PyArray_DOUBLE);
	if (!x) {
		retval = PyErr_NoMemory();
		/* clean up and return */
		if (retval == NULL) {
			Py_XDECREF(x);
			Py_XDECREF(mL);
			Py_XDECREF(mU);
			Py_XDECREF(lambda);
		}
		SAFE_FREE(newx0);
		return retval;
	}
	newx0 = (Number *) malloc(sizeof(Number) * n);
	if (!newx0) {
		retval = PyErr_NoMemory();
		/* clean up and return */
		if (retval == NULL) {
			Py_XDECREF(x);
			Py_XDECREF(mL);
			Py_XDECREF(mU);
			Py_XDECREF(lambda);
		}
		SAFE_FREE(newx0);
		return retval;
	}
	double *xdata = (double *)x0->data;
	for (i = 0; i < n; i++)
		newx0[i] = xdata[i];

	/* Allocate multiplier arrays */ 

	mL = (PyArrayObject *) PyArray_SimpleNew(1, dX, PyArray_DOUBLE);
	mU = (PyArrayObject *) PyArray_SimpleNew(1, dX, PyArray_DOUBLE);
	dlambda[0] = m;
	lambda = (PyArrayObject *) PyArray_SimpleNew(1, dlambda, 
						     PyArray_DOUBLE);

	/* For status code, see IpReturnCodes_inc.h in Ipopt */

	status =
	  IpoptSolve(nlp, newx0, NULL, &obj, (double *)lambda->data, 
		     (double *)mL->data, (double *)mU->data, 
		     (UserDataPtr) bigfield);
	double *return_x_data = (double *)x->data;
	for (i = 0; i < n; i++) {
		return_x_data[i] = newx0[i];
	}
	retval = Py_BuildValue("OOOOdi", 
			       PyArray_Return(x),
			       PyArray_Return(mL),
			       PyArray_Return(mU),
			       PyArray_Return(lambda),
			       obj, status
	    );
	/* clean up and return */

	Py_XDECREF(x);
	Py_XDECREF(mL);
	Py_XDECREF(mU);
	Py_XDECREF(lambda);

	SAFE_FREE(newx0);
	return retval;
}
Пример #18
0
/* Function of no arguments returning new Data object */
static PyObject *
open_file(PyObject *self, PyObject *args)
{
	char *filename;
	int fd;
	DataObject *data_obj;
	unsigned char file_hdr[2], jpeg_hdr[2] = {0xff,0xd8};

	if (!PyArg_ParseTuple(args, "s:new", &filename))
		return NULL;

	/* check if the file exists first.  We close this fd just
	 * because, but maybe in the future there is a case for
	 * keeping it around. */
	fd = open(filename, O_RDONLY);
	if (fd < 0)
		return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);

	/* read the first 2 bytes, and check it looks like a jpeg */
	if (read(fd, file_hdr, 2) < 2)	{
		close(fd);
		return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
	}
	if (memcmp(jpeg_hdr, file_hdr, 2) != 0)	{
		close(fd);
		PyErr_SetString(PyExc_ValueError,
				"This file does not appear to be a JPEG file\n");
		return NULL;
	}
	close(fd);

	data_obj = newDataObject(args);
	if (data_obj == NULL)
		return PyErr_NoMemory();

	/* save the filename for later */
	data_obj->filename = PyString_FromString(filename);
	if (!data_obj->filename) {
		Py_DECREF(data_obj);
		return PyErr_NoMemory();
	}

	/* firstly, try and get the existing data */
	data_obj->d = iptc_data_new_from_jpeg(filename);
	if (data_obj->d) {
		/* read the existing iptc data into the dataset objects */
		int i;
		for (i=0; i < data_obj->d->count; i++) {
			IptcDataSet *e = data_obj->d->datasets[i];
			DataSetObject *ds = newDataSetObject(e);
			/* XXX bail out? */

			/* dataset objects hold a reference to their
			 * parent dataobject */
			ds->parent = data_obj;
			Py_INCREF(data_obj);

			ds->state = VALID;

			PyList_Append(data_obj->DataSet_list, (PyObject *)ds);
		}
	} else {
		/* create a new, empty data object */
		data_obj->d = iptc_data_new();
		if (!data_obj->d)
			return PyErr_NoMemory();
	}

	data_obj->state = OPEN;

	return (PyObject*)data_obj;
}
Пример #19
0
int
exceptionlist__initmodule(PyObject* module, struct exceptionlist* exclist) {
	const struct exceptionlist* exc;
	char* longname = NULL;
	PyObject* name;
	Py_ssize_t size;
	Py_ssize_t maxsize;

	if ((name = PyObject_GetAttrString(module, "__name__")) == NULL)
		return -1;
	if (!PyString_Check(name)) {
		PyErr_SetString(PyExc_TypeError, "__name__ required an str()");
		goto failed;
	}
	size = PyString_GET_SIZE(name);
	for (maxsize = 0, exc = exclist; exc->exc_name != NULL; exc++) {
		register int i = strlen(exc->exc_name);
		if (i > maxsize)
			maxsize = i;
	}
	if ((longname = PyMem_MALLOC(size + sizeof(".") + maxsize + sizeof("\0"))) == NULL) {
		PyErr_NoMemory();
		goto failed;
	}
	memcpy(longname, PyString_AS_STRING(name), size);
	Py_DECREF(name);
	longname[size++] = '.';
	for (exc = exclist; exc->exc_name != NULL; exc++) {
		PyObject* dict;
		PyObject* s;
		
		if ((dict = PyDict_New()) == NULL)
			goto failed;
		if ((s = PyString_FromString(exc->exc_doc)) == NULL) {
			Py_DECREF(dict);
			goto failed;
		}
		if (PyDict_SetItemString(dict, "__doc__", s) < 0) {
			Py_DECREF(dict);
			Py_DECREF(s);
			goto failed;
		}
		Py_DECREF(s);
		strcpy(&longname[size], exc->exc_name);
		if (*exc->exc_this == NULL &&
		   (*exc->exc_this = PyErr_NewException(longname, *exc->exc_base, dict)) == NULL) {
			Py_DECREF(dict);
			goto failed;
		}
		Py_DECREF(dict);
	}
	PyMem_FREE(longname);
	for (exc = exclist; exc->exc_name != NULL; exc++) {
		Py_INCREF(*exc->exc_this);
		if (PyModule_AddObject(module, exc->exc_name, *exc->exc_this) < 0)
			return -1;
	}
	return 0;
failed:
	if (longname != NULL)
		PyMem_FREE(longname);
	Py_DECREF(name);
	return -1;
}
Пример #20
0
static PyObject *
Util_func_getaddrinfo(PyObject *obj, PyObject *args, PyObject *kwargs)
{
    char *host_str;
    char port_str[6];
    int port, family, socktype, protocol, flags, r;
    struct addrinfo hints;
    uv_getaddrinfo_t* req = NULL;
    Loop *loop;
    PyObject *callback, *host, *idna;

    static char *kwlist[] = {"loop", "callback", "host", "port", "family", "socktype", "protocol", "flags", NULL};

    UNUSED_ARG(obj);
    port = socktype = protocol = flags = 0;
    family = AF_UNSPEC;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!OO|iiiii:getaddrinfo", kwlist, &LoopType, &loop, &host, &callback, &port, &family, &socktype, &protocol, &flags)) {
        return NULL;
    }

    if (host == Py_None) {
        host_str = NULL;
    } else if (PyUnicode_Check(host)) {
        idna = PyObject_CallMethod(host, "encode", "s", "idna");
        if (!idna)
            return NULL;
        host_str = PyBytes_AS_STRING(idna);
    } else if (PyBytes_Check(host)) {
        host_str = PyBytes_AsString(host);
    } else {
        PyErr_SetString(PyExc_TypeError, "getaddrinfo() argument 2 must be string or None");
        return NULL;
    }

    if (!PyCallable_Check(callback)) {
        PyErr_SetString(PyExc_TypeError, "a callable is required");
        return NULL;
    }

    if (port < 0 || port > 65535) {
        PyErr_SetString(PyExc_ValueError, "port must be between 0 and 65535");
        return NULL;
    }
    snprintf(port_str, sizeof(port_str), "%d", port);

    req = PyMem_Malloc(sizeof(uv_getaddrinfo_t));
    if (!req) {
        PyErr_NoMemory();
        goto error;
    }

    Py_INCREF(loop);
    Py_INCREF(callback);
    req->data = (void *)callback;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = family;
    hints.ai_socktype = socktype;
    hints.ai_protocol = protocol;
    hints.ai_flags = flags;

    r = uv_getaddrinfo(loop->uv_loop, req, &getaddrinfo_cb, host_str, port_str, &hints);
    if (r != 0) {
        RAISE_UV_EXCEPTION(loop->uv_loop, PyExc_UVError);
        goto error;
    }

    Py_RETURN_NONE;

error:
    if (req) {
        PyMem_Free(req);
    }
    return NULL;
}
Пример #21
0
int NI_Correlate(PyArrayObject* input, PyArrayObject* weights,
                        PyArrayObject* output, NI_ExtendMode mode,
                        double cvalue, maybelong *origins)
{
  Bool *pf = NULL;
  maybelong fsize, jj, kk, filter_size = 0, border_flag_value;
  maybelong *offsets = NULL, *oo, size;
  NI_FilterIterator fi;
  NI_Iterator ii, io;
  char *pi, *po;
  Float64 *pw;
  Float64 *ww = NULL;
  int ll;

  /* get the the footprint: */
  fsize = 1;
  for(ll = 0; ll < weights->nd; ll++)
    fsize *= weights->dimensions[ll];
  pw = (Float64*)PyArray_DATA(weights);
  pf = (Bool*)malloc(fsize * sizeof(Bool));
  if (!pf) {
    PyErr_NoMemory();
    goto exit;
  }
  for(jj = 0; jj < fsize; jj++) {
    if (fabs(pw[jj]) > DBL_EPSILON) {
      pf[jj] = 1;
      ++filter_size;
    } else {
      pf[jj] = 0;
    }
  }
  /* copy the weights to contiguous memory: */
  ww = (Float64*)malloc(filter_size * sizeof(Float64));
  if (!ww) {
    PyErr_NoMemory();
    goto exit;
  }
  jj = 0;
  for(kk = 0; kk < fsize; kk++) {
    if (pf[kk]) {
      ww[jj++] = pw[kk];
    }
  }
  /* initialize filter offsets: */
  if (!NI_InitFilterOffsets(input, pf, weights->dimensions, origins,
                            mode, &offsets, &border_flag_value, NULL))
    goto exit;
  /* initialize filter iterator: */
  if (!NI_InitFilterIterator(input->nd, weights->dimensions, filter_size,
                             input->dimensions, origins, &fi))
    goto exit;
  /* initialize input element iterator: */
  if (!NI_InitPointIterator(input, &ii))
    goto exit;
  /* initialize output element iterator: */
  if (!NI_InitPointIterator(output, &io))
    goto exit;
  /* get data pointers an array size: */
  pi = (void *)PyArray_DATA(input);
  po = (void *)PyArray_DATA(output);
  size = 1;
  for(ll = 0; ll < input->nd; ll++)
    size *= input->dimensions[ll];
  /* iterator over the elements: */
  oo = offsets;
  for(jj = 0; jj < size; jj++) {
    double tmp = 0.0;
    switch (input->descr->type_num) {
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, Bool,
                           tmp, border_flag_value);
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, UInt8,
                           tmp, border_flag_value);
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, UInt16,
                           tmp, border_flag_value);
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, UInt32,
                           tmp, border_flag_value);
#if HAS_UINT64
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, UInt64,
                           tmp, border_flag_value);
#endif
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, Int8,
                           tmp, border_flag_value);
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, Int16,
                           tmp, border_flag_value);
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, Int32,
                           tmp, border_flag_value);
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, Int64,
                           tmp, border_flag_value);
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, Float32,
                           tmp, border_flag_value);
      CASE_CORRELATE_POINT(pi, ww, oo, filter_size, cvalue, Float64,
                           tmp, border_flag_value);
    default:
      PyErr_SetString(PyExc_RuntimeError, "array type not supported");
      goto exit;
    }
    switch (output->descr->type_num) {
      CASE_FILTER_OUT(po, tmp, Bool);
      CASE_FILTER_OUT(po, tmp, UInt8);
      CASE_FILTER_OUT(po, tmp, UInt16);
      CASE_FILTER_OUT(po, tmp, UInt32);
#if HAS_UINT64
      CASE_FILTER_OUT(po, tmp, UInt64);
#endif
      CASE_FILTER_OUT(po, tmp, Int8);
      CASE_FILTER_OUT(po, tmp, Int16);
      CASE_FILTER_OUT(po, tmp, Int32);
      CASE_FILTER_OUT(po, tmp, Int64);
      CASE_FILTER_OUT(po, tmp, Float32);
      CASE_FILTER_OUT(po, tmp, Float64);
    default:
      PyErr_SetString(PyExc_RuntimeError, "array type not supported");
      goto exit;
    }
    NI_FILTER_NEXT2(fi, ii, io, oo, pi, po);
  }
exit:
  if (offsets) free(offsets);
  if (ww) free(ww);
  if (pf) free(pf);
  return PyErr_Occurred() ? 0 : 1;
}
Пример #22
0
//----------------------------------------------------------------------------------------
//
static int
do_set_servers(CmemcacheObject* self, PyObject* servers)
{
    debug(("do_set_servers\n"));
    
    if (!PySequence_Check(servers))
    {
        PyErr_BadArgument();
        return -1;
    }

    int error = 0;
    
    /* there seems to be no way to remove servers, so get rid of memcache all together */
    if (self->mc)
    {
        mcm_free(self->mc_ctxt, self->mc);
        self->mc = NULL;
    }
    assert(self->mc == NULL);

    /* create new instance */
    self->mc = mcm_new(self->mc_ctxt);
    debug(("new mc %p\n", self->mc));
    if (self->mc == NULL)
    {
        PyErr_NoMemory();
        return -1;
    }

    /* add servers, allow any sequence of strings */
    const int size = PySequence_Size(servers);
    int i;
    for (i = 0; i < size && error == 0; ++i)
    {
        PyObject* item = PySequence_GetItem(servers, i);
        if (item)
        {
            PyObject* name = NULL;
            int weight = 1;
            
            if (PyString_Check(item))
            {
                name = item;
            }
            else if (PyTuple_Check(item))
            {
                error = ! PyArg_ParseTuple(item, "Oi", &name, &weight);
            }
            if (name)    
            {
                const char* cserver = PyString_AsString(name);
                assert(cserver);
                debug(("cserver %s weight %d\n", cserver, weight));
            
                /* mc_server_add4 is not happy without ':' (it segfaults!) so check */
                if (strstr(cserver, ":") == NULL)
                {
                    PyErr_Format(PyExc_TypeError,
                                 "expected \"server:port\" but \"%s\" found", cserver);
                    error = 1;
                }
                else
                {
                    int i;
                    if (weight>15)
                    {
                        weight = 15;
                    }
                    Py_BEGIN_ALLOW_THREADS;
                    for (i = 0; i < weight; ++i)
                    {
                        debug_def(int retval =)
                            mcm_server_add4(self->mc_ctxt, self->mc, cserver);
                        debug(("retval %d\n", retval));
                    }
                    Py_END_ALLOW_THREADS;
                }
            }
            else
            {
                PyErr_BadArgument();
                error = 1;
            }
            Py_DECREF(item);
        }
    }
Пример #23
0
int NI_GenericFilter(PyArrayObject* input,
      int (*function)(double*, maybelong, double*, void*), void *data,
      PyArrayObject* footprint, PyArrayObject* output,
      NI_ExtendMode mode, double cvalue, maybelong *origins)
{
  Bool *pf = NULL;
  maybelong fsize, jj, filter_size = 0, border_flag_value;
  maybelong *offsets = NULL, *oo, size;
  NI_FilterIterator fi;
  NI_Iterator ii, io;
  char *pi, *po;
  double *buffer = NULL;
  int ll;

  /* get the the footprint: */
  fsize = 1;
  for(ll = 0; ll < footprint->nd; ll++)
    fsize *= footprint->dimensions[ll];
  pf = (Bool*)PyArray_DATA(footprint);
  for(jj = 0; jj < fsize; jj++) {
    if (pf[jj])
      ++filter_size;
  }
  /* initialize filter offsets: */
  if (!NI_InitFilterOffsets(input, pf, footprint->dimensions, origins,
                            mode, &offsets, &border_flag_value, NULL))
    goto exit;
  /* initialize filter iterator: */
  if (!NI_InitFilterIterator(input->nd, footprint->dimensions,
                             filter_size, input->dimensions, origins, &fi))
    goto exit;
  /* initialize input element iterator: */
  if (!NI_InitPointIterator(input, &ii))
    goto exit;
  /* initialize output element iterator: */
  if (!NI_InitPointIterator(output, &io))
    goto exit;
  /* get data pointers an array size: */
  pi = (void *)PyArray_DATA(input);
  po = (void *)PyArray_DATA(output);
  size = 1;
  for(ll = 0; ll < input->nd; ll++)
    size *= input->dimensions[ll];
  /* buffer for filter calculation: */
  buffer = (double*)malloc(filter_size * sizeof(double));
  if (!buffer) {
    PyErr_NoMemory();
    goto exit;
  }
  /* iterate over the elements: */
  oo = offsets;
  for(jj = 0; jj < size; jj++) {
    double tmp = 0.0;
    switch (input->descr->type_num) {
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, Bool,
                        tmp, border_flag_value, function, data, buffer);
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, UInt8,
                        tmp, border_flag_value, function, data, buffer);
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, UInt16,
                        tmp, border_flag_value, function, data, buffer);
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, UInt32,
                        tmp, border_flag_value, function, data, buffer);
#if HAS_UINT64
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, UInt64,
                        tmp, border_flag_value, function, data, buffer);
#endif
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, Int8,
                        tmp, border_flag_value, function, data, buffer);
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, Int16,
                        tmp, border_flag_value, function, data, buffer);
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, Int32,
                        tmp, border_flag_value, function, data, buffer);
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, Int64,
                        tmp, border_flag_value, function, data, buffer);
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, Float32,
                        tmp, border_flag_value, function, data, buffer);
      CASE_FILTER_POINT(pi, oo, filter_size, cvalue, Float64,
                        tmp, border_flag_value, function, data, buffer);
    default:
      PyErr_SetString(PyExc_RuntimeError, "array type not supported");
      goto exit;
    }
    switch (output->descr->type_num) {
      CASE_FILTER_OUT(po, tmp, Bool);
      CASE_FILTER_OUT(po, tmp, UInt8);
      CASE_FILTER_OUT(po, tmp, UInt16);
      CASE_FILTER_OUT(po, tmp, UInt32);
#if HAS_UINT64
      CASE_FILTER_OUT(po, tmp, UInt64);
#endif
      CASE_FILTER_OUT(po, tmp, Int8);
      CASE_FILTER_OUT(po, tmp, Int16);
      CASE_FILTER_OUT(po, tmp, Int32);
      CASE_FILTER_OUT(po, tmp, Int64);
      CASE_FILTER_OUT(po, tmp, Float32);
      CASE_FILTER_OUT(po, tmp, Float64);
    default:
      PyErr_SetString(PyExc_RuntimeError, "array type not supported");
      goto exit;
    }
    NI_FILTER_NEXT2(fi, ii, io, oo, pi, po);
  }
exit:
  if (offsets) free(offsets);
  if (buffer) free(buffer);
  return PyErr_Occurred() ? 0 : 1;
}
Пример #24
0
static PyObject *
complex_format(PyComplexObject *v, int precision, char format_code)
{
    PyObject *result = NULL;
    Py_ssize_t len;

    /* If these are non-NULL, they'll need to be freed. */
    char *pre = NULL;
    char *im = NULL;
    char *buf = NULL;

    /* These do not need to be freed. re is either an alias
       for pre or a pointer to a constant.  lead and tail
       are pointers to constants. */
    char *re = NULL;
    char *lead = "";
    char *tail = "";

    if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
        re = "";
        im = PyOS_double_to_string(v->cval.imag, format_code,
                                   precision, 0, NULL);
        if (!im) {
            PyErr_NoMemory();
            goto done;
        }
    } else {
        /* Format imaginary part with sign, real part without */
        pre = PyOS_double_to_string(v->cval.real, format_code,
                                    precision, 0, NULL);
        if (!pre) {
            PyErr_NoMemory();
            goto done;
        }
        re = pre;

        im = PyOS_double_to_string(v->cval.imag, format_code,
                                   precision, Py_DTSF_SIGN, NULL);
        if (!im) {
            PyErr_NoMemory();
            goto done;
        }
        lead = "(";
        tail = ")";
    }
    /* Alloc the final buffer. Add one for the "j" in the format string,
       and one for the trailing zero. */
    len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
    buf = PyMem_Malloc(len);
    if (!buf) {
        PyErr_NoMemory();
        goto done;
    }
    PyOS_snprintf(buf, len, "%s%s%sj%s", lead, re, im, tail);
    result = PyString_FromString(buf);
done:
    PyMem_Free(im);
    PyMem_Free(pre);
    PyMem_Free(buf);

    return result;
}
Пример #25
0
static PyObject *
winutil_strftime(PyObject *self, PyObject *args)
{
    PyObject *tup = NULL;
    struct tm buf;
    size_t buflen;
    wchar_t *outbuf = NULL;
    Py_UNICODE *fmt = NULL;
    int fmtlen;
    size_t i;
    memset((void *) &buf, 0, sizeof(buf));

    if (!PyArg_ParseTuple(args, "u#|O:strftime", &fmt, &fmtlen, &tup)) return NULL;

    if (tup == NULL) {
        time_t tt = time(NULL);
        if(localtime_s(&buf, &tt) != 0) {
            PyErr_SetString(PyExc_ValueError, "Failed to get localtime()");
            return NULL;
        }
    } else if (!gettmarg(tup, &buf)) return NULL;

    if (buf.tm_mon == -1) buf.tm_mon = 0;
    else if (buf.tm_mon < 0 || buf.tm_mon > 11) {
        PyErr_SetString(PyExc_ValueError, "month out of range");
        return NULL;
    }
    if (buf.tm_mday == 0) buf.tm_mday = 1;
    else if (buf.tm_mday < 0 || buf.tm_mday > 31) {
        PyErr_SetString(PyExc_ValueError, "day of month out of range");
        return NULL;
    }
    if (buf.tm_hour < 0 || buf.tm_hour > 23) {
        PyErr_SetString(PyExc_ValueError, "hour out of range");
        return NULL;
    }
    if (buf.tm_min < 0 || buf.tm_min > 59) {
        PyErr_SetString(PyExc_ValueError, "minute out of range");
        return NULL;
    }
    if (buf.tm_sec < 0 || buf.tm_sec > 61) {
        PyErr_SetString(PyExc_ValueError, "seconds out of range");
        return NULL;
    }
    /* tm_wday does not need checking of its upper-bound since taking
       ``% 7`` in gettmarg() automatically restricts the range. */
    if (buf.tm_wday < 0) {
        PyErr_SetString(PyExc_ValueError, "day of week out of range");
        return NULL;
    }
    if (buf.tm_yday == -1) buf.tm_yday = 0;
    else if (buf.tm_yday < 0 || buf.tm_yday > 365) {
        PyErr_SetString(PyExc_ValueError, "day of year out of range");
        return NULL;
    }
    if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
        PyErr_SetString(PyExc_ValueError,
                "daylight savings flag out of range");
        return NULL;
    }

    for (i = 5*(unsigned int)fmtlen; ; i += i) {
        outbuf = (wchar_t *)PyMem_Malloc(i*sizeof(wchar_t));
        if (outbuf == NULL) {
            PyErr_NoMemory(); return NULL;
        }
        buflen = wcsftime(outbuf, i, fmt, &buf);
        if (buflen > 0 || i >= 256 * (unsigned int)fmtlen) {
            /* If the buffer is 256 times as long as the format,
               it's probably not failing for lack of room!
               More likely, the format yields an empty result,
               e.g. an empty format, or %Z when the timezone
               is unknown. */
            PyObject *ret;
            ret = PyUnicode_FromWideChar(outbuf, buflen);
            PyMem_Free(outbuf);
            return ret;
        }
        PyMem_Free(outbuf);
        /* VisualStudio .NET 2005 does this properly */
        if (buflen == 0 && errno == EINVAL) {
            PyErr_SetString(PyExc_ValueError, "Invalid format string");
            return NULL;
        }
    }
    return NULL;
}
Пример #26
0
static PyObject *
complex_subtype_from_string(PyTypeObject *type, PyObject *v)
{
    const char *s, *start;
    char *end;
    double x=0.0, y=0.0, z;
    int got_bracket=0;
#ifdef Py_USING_UNICODE
    char *s_buffer = NULL;
#endif
    Py_ssize_t len;

    if (PyString_Check(v)) {
        s = PyString_AS_STRING(v);
        len = PyString_GET_SIZE(v);
    }
#ifdef Py_USING_UNICODE
    else if (PyUnicode_Check(v)) {
        s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
        if (s_buffer == NULL)
            return PyErr_NoMemory();
        if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
                                    PyUnicode_GET_SIZE(v),
                                    s_buffer,
                                    NULL))
            goto error;
        s = s_buffer;
        len = strlen(s);
    }
#endif
    else if (PyObject_AsCharBuffer(v, &s, &len)) {
        PyErr_SetString(PyExc_TypeError,
                        "complex() arg is not a string");
        return NULL;
    }

    /* position on first nonblank */
    start = s;
    while (Py_ISSPACE(*s))
        s++;
    if (*s == '(') {
        /* Skip over possible bracket from repr(). */
        got_bracket = 1;
        s++;
        while (Py_ISSPACE(*s))
            s++;
    }

    /* a valid complex string usually takes one of the three forms:

         <float>                  - real part only
         <float>j                 - imaginary part only
         <float><signed-float>j   - real and imaginary parts

       where <float> represents any numeric string that's accepted by the
       float constructor (including 'nan', 'inf', 'infinity', etc.), and
       <signed-float> is any string of the form <float> whose first
       character is '+' or '-'.

       For backwards compatibility, the extra forms

         <float><sign>j
         <sign>j
         j

       are also accepted, though support for these forms may be removed from
       a future version of Python.
    */

    /* first look for forms starting with <float> */
    z = PyOS_string_to_double(s, &end, NULL);
    if (z == -1.0 && PyErr_Occurred()) {
        if (PyErr_ExceptionMatches(PyExc_ValueError))
            PyErr_Clear();
        else
            goto error;
    }
    if (end != s) {
        /* all 4 forms starting with <float> land here */
        s = end;
        if (*s == '+' || *s == '-') {
            /* <float><signed-float>j | <float><sign>j */
            x = z;
            y = PyOS_string_to_double(s, &end, NULL);
            if (y == -1.0 && PyErr_Occurred()) {
                if (PyErr_ExceptionMatches(PyExc_ValueError))
                    PyErr_Clear();
                else
                    goto error;
            }
            if (end != s)
                /* <float><signed-float>j */
                s = end;
            else {
                /* <float><sign>j */
                y = *s == '+' ? 1.0 : -1.0;
                s++;
            }
            if (!(*s == 'j' || *s == 'J'))
                goto parse_error;
            s++;
        }
        else if (*s == 'j' || *s == 'J') {
            /* <float>j */
            s++;
            y = z;
        }
        else
            /* <float> */
            x = z;
    }
    else {
        /* not starting with <float>; must be <sign>j or j */
        if (*s == '+' || *s == '-') {
            /* <sign>j */
            y = *s == '+' ? 1.0 : -1.0;
            s++;
        }
        else
            /* j */
            y = 1.0;
        if (!(*s == 'j' || *s == 'J'))
            goto parse_error;
        s++;
    }

    /* trailing whitespace and closing bracket */
    while (Py_ISSPACE(*s))
        s++;
    if (got_bracket) {
        /* if there was an opening parenthesis, then the corresponding
           closing parenthesis should be right here */
        if (*s != ')')
            goto parse_error;
        s++;
        while (Py_ISSPACE(*s))
            s++;
    }

    /* we should now be at the end of the string */
    if (s-start != len)
        goto parse_error;


#ifdef Py_USING_UNICODE
    if (s_buffer)
        PyMem_FREE(s_buffer);
#endif
    return complex_subtype_from_doubles(type, x, y);

parse_error:
    PyErr_SetString(PyExc_ValueError,
                    "complex() arg is a malformed string");
error:
#ifdef Py_USING_UNICODE
    if (s_buffer)
        PyMem_FREE(s_buffer);
#endif
    return NULL;
}
Пример #27
0
static void
query_aaaa_cb(void *arg, int status,int timeouts, unsigned char *answer_buf, int answer_len)
{
    PyGILState_STATE gstate = PyGILState_Ensure();
    int parse_status;
    char ip[INET6_ADDRSTRLEN];
    char **ptr;
    struct hostent *hostent = NULL;
    PyObject *dns_result, *errorno, *tmp, *result, *callback;

    callback = (PyObject *)arg;
    ASSERT(callback);

    if (status != ARES_SUCCESS) {
        errorno = PyInt_FromLong((long)status);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    parse_status = ares_parse_aaaa_reply(answer_buf, answer_len, &hostent, NULL, NULL);
    if (parse_status != ARES_SUCCESS) {
        errorno = PyInt_FromLong((long)parse_status);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    dns_result = PyList_New(0);
    if (!dns_result) {
        PyErr_NoMemory();
        PyErr_WriteUnraisable(Py_None);
        errorno = PyInt_FromLong((long)ARES_ENOMEM);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    for (ptr = hostent->h_addr_list; *ptr != NULL; ptr++) {
        ares_inet_ntop(hostent->h_addrtype, *ptr, ip, sizeof(ip));
        tmp = Py_BuildValue("s", ip);
        if (tmp == NULL) {
            break;
        }
        PyList_Append(dns_result, tmp);
        Py_DECREF(tmp);
    }
    errorno = Py_None;
    Py_INCREF(Py_None);

callback:
    result = PyObject_CallFunctionObjArgs(callback, dns_result, errorno, NULL);
    if (result == NULL) {
        PyErr_WriteUnraisable(callback);
    }
    Py_XDECREF(result);
    Py_DECREF(dns_result);
    Py_DECREF(errorno);
    if (hostent) {
        ares_free_hostent(hostent);
    }
    Py_DECREF(callback);
    PyGILState_Release(gstate);
}
Пример #28
0
static PyObject *
time_strftime(PyObject *self, PyObject *args)
{
    PyObject *tup = NULL;
    struct tm buf;
    const time_char *fmt;
#ifdef HAVE_WCSFTIME
    wchar_t *format;
#else
    PyObject *format;
#endif
    PyObject *format_arg;
    size_t fmtlen, buflen;
    time_char *outbuf = NULL;
    size_t i;
    PyObject *ret = NULL;

    memset((void *) &buf, '\0', sizeof(buf));

    /* Will always expect a unicode string to be passed as format.
       Given that there's no str type anymore in py3k this seems safe.
    */
    if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup))
        return NULL;

    if (tup == NULL) {
        time_t tt = time(NULL);
        if (pylocaltime(&tt, &buf) == -1)
            return NULL;
    }
    else if (!gettmarg(tup, &buf) || !checktm(&buf))
        return NULL;

#if defined(_MSC_VER) || defined(sun)
    if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) {
        PyErr_SetString(PyExc_ValueError,
                        "strftime() requires year in [1; 9999]");
        return NULL;
    }
#endif

    /* Normalize tm_isdst just in case someone foolishly implements %Z
       based on the assumption that tm_isdst falls within the range of
       [-1, 1] */
    if (buf.tm_isdst < -1)
        buf.tm_isdst = -1;
    else if (buf.tm_isdst > 1)
        buf.tm_isdst = 1;

#ifdef HAVE_WCSFTIME
    format = PyUnicode_AsWideCharString(format_arg, NULL);
    if (format == NULL)
        return NULL;
    fmt = format;
#else
    /* Convert the unicode string to an ascii one */
    format = PyUnicode_EncodeLocale(format_arg, "surrogateescape");
    if (format == NULL)
        return NULL;
    fmt = PyBytes_AS_STRING(format);
#endif

#if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME)
    /* check that the format string contains only valid directives */
    for(outbuf = strchr(fmt, '%');
        outbuf != NULL;
        outbuf = strchr(outbuf+2, '%'))
    {
        if (outbuf[1]=='#')
            ++outbuf; /* not documented by python, */
        if (outbuf[1]=='\0' ||
            !strchr("aAbBcdHIjmMpSUwWxXyYzZ%", outbuf[1]))
        {
            PyErr_SetString(PyExc_ValueError, "Invalid format string");
            Py_DECREF(format);
            return NULL;
        }
    }
#endif

    fmtlen = time_strlen(fmt);

    /* I hate these functions that presume you know how big the output
     * will be ahead of time...
     */
    for (i = 1024; ; i += i) {
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
        int err;
#endif
        outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
        if (outbuf == NULL) {
            PyErr_NoMemory();
            break;
        }
        buflen = format_time(outbuf, i, fmt, &buf);
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
        err = errno;
#endif
        if (buflen > 0 || i >= 256 * fmtlen) {
            /* If the buffer is 256 times as long as the format,
               it's probably not failing for lack of room!
               More likely, the format yields an empty result,
               e.g. an empty format, or %Z when the timezone
               is unknown. */
#ifdef HAVE_WCSFTIME
            ret = PyUnicode_FromWideChar(outbuf, buflen);
#else
            ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen,
                                                "surrogateescape");
#endif
            PyMem_Free(outbuf);
            break;
        }
        PyMem_Free(outbuf);
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
        /* VisualStudio .NET 2005 does this properly */
        if (buflen == 0 && err == EINVAL) {
            PyErr_SetString(PyExc_ValueError, "Invalid format string");
            break;
        }
#endif
    }
#ifdef HAVE_WCSFTIME
    PyMem_Free(format);
#else
    Py_DECREF(format);
#endif
    return ret;
}
Пример #29
0
static void
query_ptr_cb(void *arg, int status,int timeouts, unsigned char *answer_buf, int answer_len)
{
    PyGILState_STATE gstate = PyGILState_Ensure();
    int parse_status;
    char *addr = "0.0.0.0";
    char **ptr;
    struct hostent *hostent = NULL;
    PyObject *dns_result, *errorno, *tmp, *result, *callback;

    callback = (PyObject *)arg;
    ASSERT(callback);

    if (status != ARES_SUCCESS) {
        errorno = PyInt_FromLong((long)status);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    /* addr is only used to populate the hostent struct, it's not used to validate the response */
    parse_status = ares_parse_ptr_reply(answer_buf, answer_len, addr, sizeof(addr)-1, AF_INET, &hostent);
    if (parse_status != ARES_SUCCESS) {
        errorno = PyInt_FromLong((long)parse_status);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    dns_result = PyList_New(0);
    if (!dns_result) {
        PyErr_NoMemory();
        PyErr_WriteUnraisable(Py_None);
        errorno = PyInt_FromLong((long)ARES_ENOMEM);
        dns_result = Py_None;
        Py_INCREF(Py_None);
        goto callback;
    }

    for (ptr = hostent->h_aliases; *ptr != NULL; ptr++) {
        tmp = Py_BuildValue("s", *ptr);
        if (tmp == NULL) {
            break;
        }
        PyList_Append(dns_result, tmp);
        Py_DECREF(tmp);
    }
    errorno = Py_None;
    Py_INCREF(Py_None);

callback:
    result = PyObject_CallFunctionObjArgs(callback, dns_result, errorno, NULL);
    if (result == NULL) {
        PyErr_WriteUnraisable(callback);
    }
    Py_XDECREF(result);
    Py_DECREF(dns_result);
    Py_DECREF(errorno);
    if (hostent) {
        ares_free_hostent(hostent);
    }
    Py_DECREF(callback);
    PyGILState_Release(gstate);
}
Пример #30
0
static PyObject*
automaton_make_automaton(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
	if (automaton->kind != TRIE)
		Py_RETURN_FALSE;

	AutomatonQueueItem* item;
	List queue;
	int i;

	list_init(&queue);

	// 1. setup nodes at 1-st level
	ASSERT(automaton->root);

	for (i=0; i < 256; i++) {
		TrieNode* child = trienode_get_next(automaton->root, i);
		if (child) {
			// fail edges go to root
			child->fail = automaton->root;

			item = (AutomatonQueueItem*)list_item_new(sizeof(AutomatonQueueItem));
			if (item) {
				item->node = child;
				list_append(&queue, (ListItem*)item);
			}
			else
				goto no_mem;
		}
		else
			// loop on root - implicit (see automaton_next)
			;
	}

	// 2. make links
	TrieNode* node;
	TrieNode* child;
	TrieNode* state;
	while (true) {
		AutomatonQueueItem* item = (AutomatonQueueItem*)list_pop_first(&queue);
		if (item == NULL)
			break;
		else {
			node = item->node;
			memfree(item);
		}

		const size_t n = node->n;
		for (i=0; i < n; i++) {
			child = node->next[i];
			ASSERT(child);

			item = (AutomatonQueueItem*)list_item_new(sizeof(AutomatonQueueItem));
			item->node = child;
			if (item)
				list_append(&queue, (ListItem*)item);
			else
				goto no_mem;

			state = node->fail;
			ASSERT(state);
			ASSERT(child);
			while (state != automaton->root and\
				   not trienode_get_next(state, child->letter)) {

				state = state->fail;
				ASSERT(state);
			}

			child->fail = trienode_get_next(state, child->letter);
			if (child->fail == NULL)
				child->fail = automaton->root;
			
			ASSERT(child->fail);
		}
	}

	automaton->kind = AHOCORASICK;
	automaton->version += 1;
	list_delete(&queue);
	Py_RETURN_NONE;
#undef automaton

no_mem:
	list_delete(&queue);
	PyErr_NoMemory();
	return NULL;
}