// construct and fill a CPT CPT* _buildcpt(PyArrayObject *obs, PyListObject *arities, int num_parents) { register int i,j; register int qi,ri; register int nx,ny; CPT *cpt; // child arity ri = PyInt_AsSsize_t(PyList_GET_ITEM(arities, 0)); // parent configurations qi = 1; for (i=0; i < num_parents; i++) { qi *= PyInt_AsSsize_t(PyList_GET_ITEM(arities, i+1)); } int len_offsets = (num_parents==0)?1:num_parents; // use existing cpt? if (_oldcpt != NULL) { cpt = _oldcpt; _oldcpt = NULL; // reallocate mem for new offsets and counts cpt->offsets = PyMem_Realloc(cpt->offsets, sizeof(int) * len_offsets); // qi > max_qi, so allocate more arrays if (qi > cpt->max_qi) { cpt->counts = PyMem_Realloc(cpt->counts, sizeof(int *) * qi); for (i=cpt->max_qi; i < qi; i++) { cpt->counts[i] = PyMem_Malloc(sizeof(int) * (ri+1)); } cpt->max_qi = qi; } // reallocate and initialize arrays we need for (i=0; i < qi; i++) { cpt->counts[i] = PyMem_Realloc(cpt->counts[i], sizeof(int) * (ri+1)); for (j=0; j < ri+1; j++) cpt->counts[i][j] = 0; } } else { // create new cpt cpt = (CPT*) PyMem_Malloc(sizeof(CPT)); cpt->max_qi = qi; cpt->offsets = PyMem_Malloc(sizeof(int) * len_offsets); cpt->counts = PyMem_Malloc(sizeof(int *) * qi); for (i=0; i<qi; i++) { cpt->counts[i] = PyMem_Malloc(sizeof(int) * (ri+1)); for (j=0; j < ri+1; j++) cpt->counts[i][j] = 0; } } // update cpt cpt->ri = ri; cpt->qi = qi; cpt->num_parents = num_parents; // create offsets cpt->offsets[0] = 1; for (i=1; i<num_parents; i++) cpt->offsets[i] = cpt->offsets[i-1]*PyInt_AsSsize_t(PyList_GET_ITEM(arities, i)); // adding to nij and nijk nx = PyArray_DIM(obs, 0); ny = PyArray_DIM(obs, 1); for (i=0; i<nx; i++) { j = cptindex(obs, i, cpt->offsets, num_parents); cpt->counts[j][0]++; cpt->counts[j][*(int*)PyArray_GETPTR2(obs,i,0) + 1]++; } // return the cpt return cpt; }
static PyObject *sendmsg_sendmsg(PyObject *self, PyObject *args, PyObject *keywds) { int fd; int flags = 0; Py_ssize_t sendmsg_result, iovec_length; struct msghdr message_header; struct iovec iov[1]; PyObject *ancillary = NULL; PyObject *iterator = NULL; PyObject *item = NULL; PyObject *result_object = NULL; static char *kwlist[] = {"fd", "data", "flags", "ancillary", NULL}; if (!PyArg_ParseTupleAndKeywords( args, keywds, "it#|iO:sendmsg", kwlist, &fd, &iov[0].iov_base, &iovec_length, &flags, &ancillary)) { return NULL; } iov[0].iov_len = iovec_length; message_header.msg_name = NULL; message_header.msg_namelen = 0; message_header.msg_iov = iov; message_header.msg_iovlen = 1; message_header.msg_control = NULL; message_header.msg_controllen = 0; message_header.msg_flags = 0; if (ancillary) { if (!PyList_Check(ancillary)) { PyErr_Format(PyExc_TypeError, "send1msg argument 3 expected list, got %s", ancillary->ob_type->tp_name); goto finished; } iterator = PyObject_GetIter(ancillary); if (iterator == NULL) { goto finished; } size_t all_data_len = 0; /* First we need to know how big the buffer needs to be in order to have enough space for all of the messages. */ while ( (item = PyIter_Next(iterator)) ) { int type, level; Py_ssize_t data_len; size_t prev_all_data_len; char *data; if (!PyTuple_Check(item)) { PyErr_Format(PyExc_TypeError, "send1msg argument 3 expected list of tuple, " "got list containing %s", item->ob_type->tp_name); goto finished; } if (!PyArg_ParseTuple( item, "iit#:sendmsg ancillary data (level, type, data)", &level, &type, &data, &data_len)) { goto finished; } prev_all_data_len = all_data_len; all_data_len += CMSG_SPACE(data_len); Py_DECREF(item); item = NULL; if (all_data_len < prev_all_data_len) { PyErr_Format(PyExc_OverflowError, "Too much msg_control to fit in a size_t: %zu", prev_all_data_len); goto finished; } } Py_DECREF(iterator); iterator = NULL; /* Allocate the buffer for all of the ancillary elements, if we have * any. */ if (all_data_len) { if (all_data_len > SOCKLEN_MAX) { PyErr_Format(PyExc_OverflowError, "Too much msg_control to fit in a socklen_t: %zu", all_data_len); goto finished; } message_header.msg_control = PyMem_Malloc(all_data_len); if (!message_header.msg_control) { PyErr_NoMemory(); goto finished; } } else { message_header.msg_control = NULL; } message_header.msg_controllen = (socklen_t) all_data_len; iterator = PyObject_GetIter(ancillary); /* again */ if (!iterator) { goto finished; } /* Unpack the tuples into the control message. */ struct cmsghdr *control_message = CMSG_FIRSTHDR(&message_header); while ( (item = PyIter_Next(iterator)) ) { int type, level; Py_ssize_t data_len; size_t data_size; unsigned char *data, *cmsg_data; /* We explicitly allocated enough space for all ancillary data above; if there isn't enough room, all bets are off. */ assert(control_message); if (!PyArg_ParseTuple(item, "iit#:sendmsg ancillary data (level, type, data)", &level, &type, &data, &data_len)) { goto finished; } control_message->cmsg_level = level; control_message->cmsg_type = type; data_size = CMSG_LEN(data_len); if (data_size > SOCKLEN_MAX) { PyErr_Format(PyExc_OverflowError, "CMSG_LEN(%zd) > SOCKLEN_MAX", data_len); goto finished; } control_message->cmsg_len = (socklen_t) data_size; cmsg_data = CMSG_DATA(control_message); memcpy(cmsg_data, data, data_len); Py_DECREF(item); item = NULL; control_message = CMSG_NXTHDR(&message_header, control_message); } Py_DECREF(iterator); iterator = NULL; if (PyErr_Occurred()) { goto finished; } } sendmsg_result = sendmsg(fd, &message_header, flags); if (sendmsg_result < 0) { PyErr_SetFromErrno(sendmsg_socket_error); goto finished; } result_object = Py_BuildValue("n", sendmsg_result); finished: if (item) { Py_DECREF(item); item = NULL; } if (iterator) { Py_DECREF(iterator); iterator = NULL; } if (message_header.msg_control) { PyMem_Free(message_header.msg_control); message_header.msg_control = NULL; } return result_object; }
static PyObject * ResponseObject_call(PyObject *obj, PyObject *args, PyObject *kw) { PyObject *status = NULL, *headers = NULL, *exc_info = NULL, *bytes = NULL; char *status_code = NULL; char *status_line = NULL; int bytelen = 0, int_code; ResponseObject *self = NULL; char *buf = NULL; self = (ResponseObject *)obj; #ifdef PY3 if (!PyArg_ParseTuple(args, "UO|O:start_response", &status, &headers, &exc_info)){ return NULL; } #else if (!PyArg_ParseTuple(args, "SO|O:start_response", &status, &headers, &exc_info)){ return NULL; } #endif if (self->cli->headers != NULL && exc_info && exc_info != Py_None) { // Re-raise original exception if headers sent PyObject *type = NULL; PyObject *value = NULL; PyObject *traceback = NULL; if (!PyArg_ParseTuple(exc_info, "OOO", &type, &value, &traceback)) { return NULL; } Py_INCREF(type); Py_INCREF(value); Py_INCREF(traceback); //raise PyErr_Restore(type, value, traceback); return NULL; } if(self->cli->headers != NULL){ PyErr_SetString(PyExc_TypeError, "headers already set"); return NULL; } if (!PyList_Check(headers)) { PyErr_SetString(PyExc_TypeError, "response headers must be a list"); return NULL; } bytes = wsgi_to_bytes(status); bytelen = PyBytes_GET_SIZE(bytes); buf = PyMem_Malloc(sizeof(char*) * bytelen); if (!buf) { return NULL; } status_line = buf; strcpy(status_line, PyBytes_AS_STRING(bytes)); /* DEBUG("%s :%d", (char*)status_line, bytelen); */ if (!*status_line) { PyErr_SetString(PyExc_ValueError, "status message was not supplied"); Py_XDECREF(bytes); if (buf) { PyMem_Free(buf); } return NULL; } status_code = strsep((char **)&status_line, " "); errno = 0; int_code = strtol(status_code, &status_code, 10); if (*status_code || errno == ERANGE) { PyErr_SetString(PyExc_TypeError, "status value is not an integer"); Py_XDECREF(bytes); if (buf) { PyMem_Free(buf); } return NULL; } if (int_code < 100 || int_code > 999) { PyErr_SetString(PyExc_ValueError, "status code is invalid"); Py_XDECREF(bytes); if (buf) { PyMem_Free(buf); } return NULL; } self->cli->status_code = int_code; Py_XDECREF(self->cli->headers); self->cli->headers = headers; Py_INCREF(self->cli->headers); Py_XDECREF(self->cli->http_status); self->cli->http_status = create_status(bytes, bytelen, self->cli->http_parser->http_minor); /* if(self->cli->http_parser->http_minor == 1){ */ /* self->cli->http_status = PyBytes_FromFormat("HTTP/1.1 %s\r\n", PyBytes_AS_STRING(bytes)); */ /* }else{ */ /* self->cli->http_status = PyBytes_FromFormat("HTTP/1.0 %s\r\n", PyBytes_AS_STRING(bytes)); */ /* } */ /* DEBUG("set http_status %p", self->cli); */ Py_XDECREF(bytes); if (buf) { PyMem_Free(buf); } Py_RETURN_NONE; }
static void calculate_path (void) { extern char *Py_GetProgramName(void); static char delimiter[2] = {DELIM, '\0'}; char *rtpypath = Py_GETENV("PYTHONPATH"); char *path = getenv("PATH"); char *prog = Py_GetProgramName(); static char proc_exe_path[] = "/proc/self/exe"; char *xzip_path; char *buf; /* If there is no slash in the argv0 path, then we have to * assume python is on the user's $PATH, since there's no * other way to find a directory to start the search from. If * $PATH isn't exported, you lose. */ if (strchr(prog, SEP)) strncpy(progpath, prog, MAXPATHLEN); else if (path) { while (1) { char *delim = strchr(path, DELIM); if (delim) { size_t len = delim - path; if (len > MAXPATHLEN) len = MAXPATHLEN; strncpy(progpath, path, len); *(progpath + len) = '\0'; } else strncpy(progpath, path, MAXPATHLEN); joinpath(progpath, prog); if (isxfile(progpath)) break; if (!delim) { progpath[0] = '\0'; break; } path = delim + 1; } } else progpath[0] = '\0'; if (progpath[0] != SEP && progpath[0] != '\0') absolutize(progpath); /**** pts ****/ { int fd = open(proc_exe_path, O_RDONLY); /* fprintf(stderr, "progpath=(%s)\n", progpath); */ if (fd < 0) { /* If /proc is not avaialbe, e.g. in chroot */ xzip_path = progpath; /* Use argv[0] for the .zip filename */ } else { xzip_path = proc_exe_path; close(fd); } } /**** pts ****/ if (rtpypath == NULL || rtpypath[0] == '\0') { module_search_path = xzip_path; } else if (NULL == (buf = (char *)PyMem_Malloc( 2 + strlen(xzip_path) + strlen(rtpypath)))) { /* We can't exit, so print a warning and limp along */ fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n"); fprintf(stderr, "Using default static PYTHONPATH.\n"); module_search_path = xzip_path; } else { strcpy(buf, rtpypath); strcat(buf, delimiter); strcat(buf, xzip_path); module_search_path = buf; } }
/* Encode a (wide) character string to the locale encoding with the surrogateescape error handler (characters in range U+DC80..U+DCFF are converted to bytes 0x80..0xFF). This function is the reverse of _Py_char2wchar(). Return a pointer to a newly allocated byte string (use PyMem_Free() to free the memory), or NULL on encoding or memory allocation error. If error_pos is not NULL: *error_pos is the index of the invalid character on encoding error, or (size_t)-1 otherwise. */ char* _Py_wchar2char(const wchar_t *text, size_t *error_pos) { #ifdef __APPLE__ Py_ssize_t len; PyObject *unicode, *bytes = NULL; char *cpath; unicode = PyUnicode_FromWideChar(text, wcslen(text)); if (unicode == NULL) return NULL; bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape"); Py_DECREF(unicode); if (bytes == NULL) { PyErr_Clear(); if (error_pos != NULL) *error_pos = (size_t)-1; return NULL; } len = PyBytes_GET_SIZE(bytes); cpath = PyMem_Malloc(len+1); if (cpath == NULL) { PyErr_Clear(); Py_DECREF(bytes); if (error_pos != NULL) *error_pos = (size_t)-1; return NULL; } memcpy(cpath, PyBytes_AsString(bytes), len + 1); Py_DECREF(bytes); return cpath; #else /* __APPLE__ */ const size_t len = wcslen(text); char *result = NULL, *bytes = NULL; size_t i, size, converted; wchar_t c, buf[2]; #ifndef MS_WINDOWS if (force_ascii == -1) force_ascii = check_force_ascii(); if (force_ascii) return encode_ascii_surrogateescape(text, error_pos); #endif /* The function works in two steps: 1. compute the length of the output buffer in bytes (size) 2. outputs the bytes */ size = 0; buf[1] = 0; while (1) { for (i=0; i < len; i++) { c = text[i]; if (c >= 0xdc80 && c <= 0xdcff) { /* UTF-8b surrogate */ if (bytes != NULL) { *bytes++ = c - 0xdc00; size--; } else size++; continue; } else { buf[0] = c; if (bytes != NULL) converted = wcstombs(bytes, buf, size); else converted = wcstombs(NULL, buf, 0); if (converted == (size_t)-1) { if (result != NULL) PyMem_Free(result); if (error_pos != NULL) *error_pos = i; return NULL; } if (bytes != NULL) { bytes += converted; size -= converted; } else size += converted; } } if (result != NULL) { *bytes = '\0'; break; } size += 1; /* nul byte at the end */ result = PyMem_Malloc(size); if (result == NULL) { if (error_pos != NULL) *error_pos = (size_t)-1; return NULL; } bytes = result; } return result; #endif /* __APPLE__ */ }
/* on error, -1 is returned and no allocation is made */ int mathutils_array_parse_alloc(float **array, int array_min, PyObject *value, const char *error_prefix) { int size; #if 1 /* approx 6x speedup for mathutils types */ if ((size = VectorObject_Check(value) ? ((VectorObject *)value)->size : 0) || (size = EulerObject_Check(value) ? 3 : 0) || (size = QuaternionObject_Check(value) ? 4 : 0) || (size = ColorObject_Check(value) ? 3 : 0)) { if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) { return -1; } if (size < array_min) { PyErr_Format(PyExc_ValueError, "%.200s: sequence size is %d, expected > %d", error_prefix, size, array_min); return -1; } *array = PyMem_Malloc(size * sizeof(float)); memcpy(*array, ((BaseMathObject *)value)->data, size * sizeof(float)); return size; } else #endif { PyObject *value_fast = NULL; // *array = NULL; int ret; /* non list/tuple cases */ if (!(value_fast = PySequence_Fast(value, error_prefix))) { /* PySequence_Fast sets the error */ return -1; } size = PySequence_Fast_GET_SIZE(value_fast); if (size < array_min) { Py_DECREF(value_fast); PyErr_Format(PyExc_ValueError, "%.200s: sequence size is %d, expected > %d", error_prefix, size, array_min); return -1; } *array = PyMem_Malloc(size * sizeof(float)); ret = mathutils_array_parse_fast(*array, size, value_fast, error_prefix); Py_DECREF(value_fast); if (ret == -1) { PyMem_Free(*array); } return ret; } }
static PyObject * process_task(PyObject *self, PyObject *args) { //printf ("####C#### PROCESS TASK\n"); long app_id = PyInt_AsLong(PyTuple_GetItem(args, 0)); //printf ("####C####App id: %ld\n", app_id); char *path = PyString_AsString(PyTuple_GetItem(args, 1)); //printf ("####C####Path: %s\n", path); char *func_name = PyString_AsString(PyTuple_GetItem(args, 2)); //printf ("####C####Function name: %s\n", func_name); int priority = (int)PyInt_AsLong(PyTuple_GetItem(args, 3)); //printf ("####C####Priority: %d\n", priority); int has_target = (int)PyInt_AsLong(PyTuple_GetItem(args, 4)); //printf ("####C####Has target: %d\n", has_target); PyObject *values = PyList_AsTuple(PyTuple_GetItem(args, 5)); PyObject *compss_types = PyList_AsTuple(PyTuple_GetItem(args, 6)); PyObject *compss_directions = PyList_AsTuple(PyTuple_GetItem(args, 7)); Py_ssize_t num_pars = PyTuple_Size(values); //printf ("####C####Num pars: %d\n", num_pars); PyObject *type, *val, *direction; Py_ssize_t j, pj; long l; int i; double d; char *s; void **params[num_pars * 3]; int c_types[num_pars], c_directions[num_pars]; char *c_values, *ini_c_values; int val_size = 0; // Get C types and directions for (j = 0; j < num_pars; j++) { type = PyTuple_GetItem(compss_types, j); // this does not increment reference (we don't own it) so no need for decref direction = PyTuple_GetItem(compss_directions, j); c_types[j] = (int)PyInt_AsLong(type); c_directions[j] = (int)PyInt_AsLong(direction); switch ((enum datatype) c_types[j]) { case file_dt: val_size += sizeof(char*); break; case string_dt: val_size += sizeof(char*); break; case int_dt: val_size += sizeof(int); break; case long_dt: val_size += sizeof(long); break; case double_dt: val_size += sizeof(double); break; case boolean_dt: val_size += sizeof(int); break; default: break; } } //printf ("####C####Size of values: %d\n", val_size); // Build the C values //c_values = (char *)malloc(val_size); c_values = (char *)PyMem_Malloc(val_size); // allocate the memory in the Python heap ini_c_values = c_values; for (j = 0; j < num_pars; j++) { pj = j * 3; val = PyTuple_GetItem(values, j); // this does not increment reference (we don't own it) so no need for decref params[pj] = (void *)c_values; switch ((enum datatype) c_types[j]) { case file_dt: s = PyString_AsString(val); *(char**)c_values = s; //printf ("####C#### \t Arg %d (FILE): %s, add %ld\n", j, *(char**)c_values, c_values); c_values += sizeof(char*); break; case string_dt: s = PyString_AsString(val); *(char**)c_values = s; //printf ("####C#### \t Arg %d (STRING): %s, add %ld\n", j, *(char**)c_values, c_values); c_values += sizeof(char*); break; case int_dt: i = (int)PyInt_AsLong(val); *(int*)c_values = i; //printf ("####C#### \t Arg %d (INT): %d, add %ld\n", j, *(int*)c_values, c_values); c_values += sizeof(int); break; case long_dt: l = PyLong_AsLong(val); *(long*)c_values = l; //printf ("####C#### \t Arg %d (LONG): %ld, add %ld\n", j, *(long*)c_values, c_values); c_values += sizeof(long); break; case double_dt: d = PyFloat_AsDouble(val); *(double*)c_values = d; //printf ("####C#### \t Arg %d (FLOAT): %f, add %ld\n", j, *(double *)c_values, c_values); c_values += sizeof(double); break; case boolean_dt: i = (int)PyInt_AsLong(val); *(int*)c_values = i; //printf ("####C#### \t Arg %d (BOOL): %d, add %ld\n", j, *(int*)c_values, c_values); c_values += sizeof(int); break; default: break; } params[pj+1] = (void *)&c_types[j]; params[pj+2] = (void *)&c_directions[j]; } // Invoke the C library GS_ExecuteTask(app_id, path, // class_name func_name, // method_name priority, has_target, (int)num_pars, params); //free(c_values); PyMem_Free(ini_c_values); Py_DECREF(values); Py_DECREF(compss_types); Py_DECREF(compss_directions); return Py_BuildValue("i", 0); }
Py::Object _image_module::pcolor2(const Py::Tuple& args) { _VERBOSE("_image_module::pcolor2"); if (args.length() != 7) throw Py::TypeError("Incorrect number of arguments (6 expected)"); Py::Object xp = args[0]; Py::Object yp = args[1]; Py::Object dp = args[2]; int rows = Py::Int(args[3]); int cols = Py::Int(args[4]); Py::Tuple bounds = args[5]; Py::Object bgp = args[6]; if (bounds.length() !=4) throw Py::TypeError("Incorrect number of bounds (4 expected)"); double x_left = Py::Float(bounds[0]); double x_right = Py::Float(bounds[1]); double y_bot = Py::Float(bounds[2]); double y_top = Py::Float(bounds[3]); // Check we have something to output to if (rows == 0 || cols ==0) throw Py::ValueError("rows or cols is zero; there are no pixels"); // Get numpy arrays PyArrayObject *x = (PyArrayObject *) PyArray_ContiguousFromObject(xp.ptr(), PyArray_DOUBLE, 1, 1); if (x == NULL) throw Py::ValueError("x is of incorrect type (wanted 1D double)"); PyArrayObject *y = (PyArrayObject *) PyArray_ContiguousFromObject(yp.ptr(), PyArray_DOUBLE, 1, 1); if (y == NULL) { Py_XDECREF(x); throw Py::ValueError("y is of incorrect type (wanted 1D double)"); } PyArrayObject *d = (PyArrayObject *) PyArray_ContiguousFromObject(dp.ptr(), PyArray_UBYTE, 3, 3); if (d == NULL) { Py_XDECREF(x); Py_XDECREF(y); throw Py::ValueError("data is of incorrect type (wanted 3D uint8)"); } if (d->dimensions[2] != 4) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); throw Py::ValueError("data must be in RGBA format"); } // Check dimensions match int nx = x->dimensions[0]; int ny = y->dimensions[0]; if (nx != d->dimensions[1]+1 || ny != d->dimensions[0]+1) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); throw Py::ValueError("data and axis bin boundary dimensions are incompatible"); } PyArrayObject *bg = (PyArrayObject *) PyArray_ContiguousFromObject(bgp.ptr(), PyArray_UBYTE, 1, 1); if (bg == NULL) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); throw Py::ValueError("bg is of incorrect type (wanted 1D uint8)"); } if (bg->dimensions[0] != 4) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); Py_XDECREF(bg); throw Py::ValueError("bg must be in RGBA format"); } // Allocate memory for pointer arrays int * irows = reinterpret_cast<int*>(PyMem_Malloc(sizeof(int)*rows)); if (irows == NULL) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); Py_XDECREF(bg); throw Py::MemoryError("Cannot allocate memory for lookup table"); } int * jcols = reinterpret_cast<int*>(PyMem_Malloc(sizeof(int*)*cols)); if (jcols == NULL) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); Py_XDECREF(bg); PyMem_Free(irows); throw Py::MemoryError("Cannot allocate memory for lookup table"); } // Create output Image* imo = new Image; imo->rowsIn = rows; imo->rowsOut = rows; imo->colsIn = cols; imo->colsOut = cols; size_t NUMBYTES(rows * cols * 4); agg::int8u *buffer = new agg::int8u[NUMBYTES]; if (buffer == NULL) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); Py_XDECREF(bg); PyMem_Free(irows); PyMem_Free(jcols); throw Py::MemoryError("Could not allocate memory for image"); } // Calculate the pointer arrays to map input x to output x int i, j; double *x0 = reinterpret_cast<double*>(x->data); double *y0 = reinterpret_cast<double*>(y->data); double sx = cols/(x_right - x_left); double sy = rows/(y_top - y_bot); _bin_indices(jcols, cols, x0, nx, sx, x_left); _bin_indices(irows, rows, y0, ny, sy, y_bot); // Copy data to output buffer agg::int8u * position = buffer; unsigned char *start = reinterpret_cast<unsigned char*>(d->data); unsigned char *bgptr = reinterpret_cast<unsigned char*>(bg->data); int s0 = d->strides[0]; int s1 = d->strides[1]; for (i=0; i<rows; i++) { for (j=0; j<cols; j++) { if (irows[i] == -1 || jcols[j] == -1) { memcpy(position, bgptr, 4*sizeof(agg::int8u)); } else { memcpy(position, (start + s0*irows[i] + s1*jcols[j]), 4*sizeof(agg::int8u)); } position += 4; } } // Attach output buffer to output buffer imo->rbufOut = new agg::rendering_buffer; imo->bufferOut = buffer; imo->rbufOut->attach(imo->bufferOut, imo->colsOut, imo->rowsOut, imo->colsOut * imo->BPP); Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); Py_XDECREF(bg); PyMem_Free(irows); PyMem_Free(jcols); return Py::asObject(imo); }
static PyObject * py_decodelzw(PyObject *obj, PyObject *args) { PyThreadState *_save = NULL; PyObject *byteobj = NULL; PyObject *result = NULL; int i, j; unsigned int encoded_len = 0; unsigned int decoded_len = 0; unsigned int result_len = 0; unsigned int table_len = 0; unsigned int len; unsigned int code, c, oldcode, mask, bitw, shr, bitcount; char *encoded = NULL; char *result_ptr = NULL; char *table2 = NULL; char *cptr; struct BYTE_STRING *decoded = NULL; struct BYTE_STRING *table[4096]; struct BYTE_STRING *decoded_ptr, *newentry, *newresult, *t; int little_endian = 0; if (!PyArg_ParseTuple(args, "O", &byteobj)) return NULL; if (!PyBytes_Check(byteobj)) { PyErr_Format(PyExc_TypeError, "expected byte string as input"); goto _fail; } Py_INCREF(byteobj); encoded = PyBytes_AS_STRING(byteobj); encoded_len = (unsigned int)PyBytes_GET_SIZE(byteobj); /* release GIL: byte/string objects are immutable */ _save = PyEval_SaveThread(); if ((*encoded != -128) || ((*(encoded+1) & 128))) { PyEval_RestoreThread(_save); PyErr_Format(PyExc_ValueError, "strip must begin with CLEAR code"); goto _fail; } little_endian = (*(unsigned short *)encoded) & 128; /* allocate buffer for codes and pointers */ decoded_len = 0; len = (encoded_len + encoded_len/9) * sizeof(decoded); decoded = PyMem_Malloc(len * sizeof(void *)); if (decoded == NULL) { PyEval_RestoreThread(_save); PyErr_Format(PyExc_MemoryError, "failed to allocate decoded"); goto _fail; } memset((void *)decoded, 0, len * sizeof(void *)); decoded_ptr = decoded; /* cache strings of length 2 */ cptr = table2 = PyMem_Malloc(256*256*2 * sizeof(char)); if (table2 == NULL) { PyEval_RestoreThread(_save); PyErr_Format(PyExc_MemoryError, "failed to allocate table2"); goto _fail; } for (i = 0; i < 256; i++) { for (j = 0; j < 256; j++) { *cptr++ = (char)i; *cptr++ = (char)j; } } memset(table, 0, sizeof(table)); table_len = 258; bitw = 9; shr = 23; mask = 4286578688; bitcount = 0; result_len = 0; code = 0; oldcode = 0; while ((unsigned int)((bitcount + bitw) >> 3) <= encoded_len) { /* read next code */ code = *((unsigned int *)((void *)(encoded + (bitcount / 8)))); if (little_endian) code = SWAP4BYTES(code); code <<= bitcount % 8; code &= mask; code >>= shr; bitcount += bitw; if (code == 257) /* end of information */ break; if (code == 256) { /* clearcode */ /* initialize table and switch to 9 bit */ while (table_len > 258) { t = table[--table_len]; t->ref--; if (t->ref == 0) { if (t->len > 2) PyMem_Free(t->str); PyMem_Free(t); } } bitw = 9; shr = 23; mask = 4286578688; /* read next code */ code = *((unsigned int *)((void *)(encoded + (bitcount / 8)))); if (little_endian) code = SWAP4BYTES(code); code <<= bitcount % 8; code &= mask; code >>= shr; bitcount += bitw; if (code == 257) /* end of information */ break; /* decoded.append(table[code]) */ if (code < 256) { result_len++; *((int *)decoded_ptr++) = code; } else { newresult = table[code]; newresult->ref++; result_len += newresult->len; *(struct BYTE_STRING **)decoded_ptr++ = newresult; } } else { if (code < table_len) { /* code is in table */ /* newresult = table[code]; */ /* newentry = table[oldcode] + table[code][0] */ /* decoded.append(newresult); table.append(newentry) */ if (code < 256) { c = code; *((unsigned int *)decoded_ptr++) = code; result_len++; } else { newresult = table[code]; newresult->ref++; c = (unsigned int) *newresult->str; *(struct BYTE_STRING **)decoded_ptr++ = newresult; result_len += newresult->len; } newentry = PyMem_Malloc(sizeof(struct BYTE_STRING)); newentry->ref = 1; if (oldcode < 256) { newentry->len = 2; newentry->str = table2 + (oldcode << 9) + ((unsigned char)c << 1); } else { len = table[oldcode]->len; newentry->len = len + 1; newentry->str = PyMem_Malloc(newentry->len); if (newentry->str == NULL) break; memmove(newentry->str, table[oldcode]->str, len); newentry->str[len] = c; } table[table_len++] = newentry; } else { /* code is not in table */ /* newentry = newresult = table[oldcode] + table[oldcode][0] */ /* decoded.append(newresult); table.append(newentry) */ newresult = PyMem_Malloc(sizeof(struct BYTE_STRING)); newentry = newresult; newentry->ref = 2; if (oldcode < 256) { newentry->len = 2; newentry->str = table2 + 514*oldcode; } else { len = table[oldcode]->len; newentry->len = len + 1; newentry->str = PyMem_Malloc(newentry->len); if (newentry->str == NULL) break; memmove(newentry->str, table[oldcode]->str, len); newentry->str[len] = *table[oldcode]->str; } table[table_len++] = newentry; *(struct BYTE_STRING **)decoded_ptr++ = newresult; result_len += newresult->len; } } oldcode = code; /* increase bit-width if necessary */ switch (table_len) { case 511: bitw = 10; shr = 22; mask = 4290772992; break; case 1023: bitw = 11; shr = 21; mask = 4292870144; break; case 2047: bitw = 12; shr = 20; mask = 4293918720; } }
static PyObject * Process_func_spawn(PyObject *cls, PyObject *args, PyObject *kwargs) { int err, flags, stdio_count; unsigned int uid, gid; Py_ssize_t i, n, pos, size; PyObject *key, *value, *item, *tmp, *callback, *arguments, *env, *stdio, *ret, *executable, *cwd; Process *self; Loop *loop; uv_process_options_t options; uv_stdio_container_t *stdio_container; static char *kwlist[] = {"loop", "args", "executable", "env", "cwd", "uid", "gid", "flags", "stdio", "exit_callback", NULL}; cwd = executable = Py_None; tmp = arguments = env = stdio = NULL; stdio_container = NULL; flags = uid = gid = stdio_count = 0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!O|OO!OIIiOO:__init__", kwlist, &LoopType, &loop, &arguments, &executable, &PyDict_Type, &env, &cwd, &uid, &gid, &flags, &stdio, &callback)) { return NULL; } if (callback != Py_None && !PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "a callable is required"); return NULL; } if (!PyBytes_Check(arguments) && !PyUnicode_Check(arguments) && !PySequence_Check(arguments)) { PyErr_SetString(PyExc_TypeError, "only string or iterable objects are supported for 'args'"); return NULL; } if (stdio && !PySequence_Check(stdio)) { PyErr_SetString(PyExc_TypeError, "only iterable objects are supported for 'stdio'"); return NULL; } self = (Process *)Process_tp_new((PyTypeObject *) cls, args, kwargs); if (!self) { return NULL; } initialize_handle(HANDLE(self), loop); /* Don't consider the handle initialized until uv_spawn is called. Unline other handles, * there is no uv_process_init, it's called at the begining of uv_spawn, so don't * consider the handle initialized until then, or we'd call uv_close when the Python * object is deallocated, which is not A Good Thing (TM). */ HANDLE(self)->initialized = False; memset(&options, 0, sizeof(uv_process_options_t)); options.uid = uid; options.gid = gid; options.flags = flags; options.exit_cb = pyuv__process_exit_cb; /* process args */ if (PyBytes_Check(arguments) || PyUnicode_Check(arguments)) { options.args = PyMem_Malloc(sizeof *(options.args) * 2); if (!options.args) { PyErr_NoMemory(); goto error; } options.args[0] = pyuv_dup_strobj(arguments); if (!options.args[0]) { goto error; } options.args[1] = NULL; } else { /* it's a sequence object */ n = PySequence_Length(arguments); if (n < 1) { PyErr_SetString(PyExc_ValueError, "'args' must contain at least one element"); goto error; } options.args = PyMem_Malloc(sizeof *(options.args) * (n + 1)); if (!options.args) { PyErr_NoMemory(); goto error; } for (i = 0; i < n; i++) { item = PySequence_GetItem(arguments, i); if (!item) { options.args[i] = NULL; goto error; } options.args[i] = pyuv_dup_strobj(item); if (!options.args[i]) { Py_DECREF(item); goto error; } Py_DECREF(item); } options.args[n] = NULL; } /* process file */ if (executable != Py_None) { options.file = pyuv_dup_strobj(executable); if (!options.file) { goto error; } } else { size = strlen(options.args[0]) + 1; options.file = PyMem_Malloc(size); if (!options.file) { PyErr_NoMemory(); goto error; } memcpy((void*)options.file, options.args[0], size); } /* process cwd */ if (cwd != Py_None) { options.cwd = pyuv_dup_strobj(cwd); if (!options.cwd) { goto error; } } /* process env */ if (env) { char *key_str, *value_str; PyObject *key_bytes, *value_bytes; n = PyDict_Size(env); if (n > 0) { options.env = PyMem_Malloc(sizeof *(options.env) * (n + 1)); if (!options.env) { PyErr_NoMemory(); goto error; } i = 0; pos = 0; while (PyDict_Next(env, &pos, &key, &value)) { key_bytes = value_bytes = NULL; if (!pyuv_PyUnicode_FSConverter(key, &key_bytes)) { options.env[i] = NULL; goto error; } if (!pyuv_PyUnicode_FSConverter(value, &value_bytes)) { Py_DECREF(key_bytes); options.env[i] = NULL; goto error; } key_str = PyBytes_AS_STRING(key_bytes); value_str = PyBytes_AS_STRING(value_bytes); size = PyBytes_GET_SIZE(key_bytes) + PyBytes_GET_SIZE(value_bytes) + 2; options.env[i] = PyMem_Malloc(size); if (!options.env[i]) { options.env[i] = NULL; PyErr_NoMemory(); Py_DECREF(key_bytes); Py_DECREF(value_bytes); goto error; } PyOS_snprintf(options.env[i], size, "%s=%s", key_str, value_str); Py_DECREF(key_bytes); Py_DECREF(value_bytes); i++; } options.env[i] = NULL; } } /* process stdio container */ if (stdio) { n = PySequence_Length(stdio); stdio_container = PyMem_Malloc(sizeof *stdio_container * n); if (!stdio_container) { PyErr_NoMemory(); goto error; } item = NULL; for (i = 0;i < n; i++) { item = PySequence_GetItem(stdio, i); if (!item || !PyObject_TypeCheck(item, &StdIOType)) { Py_XDECREF(item); PyErr_SetString(PyExc_TypeError, "a StdIO instance is required"); goto error; } stdio_count++; stdio_container[i].flags = ((StdIO *)item)->flags; if (((StdIO *)item)->flags & (UV_CREATE_PIPE | UV_INHERIT_STREAM)) { stdio_container[i].data.stream = (uv_stream_t *)(UV_HANDLE(((StdIO *)item)->stream)); } else if (((StdIO *)item)->flags & UV_INHERIT_FD) { stdio_container[i].data.fd = ((StdIO *)item)->fd; } Py_DECREF(item); } } options.stdio = stdio_container; options.stdio_count = stdio_count; HANDLE(self)->initialized = True; err = uv_spawn(UV_HANDLE_LOOP(self), &self->process_h, &options); if (err < 0) { RAISE_UV_EXCEPTION(err, PyExc_ProcessError); goto error; } ret = (PyObject *) self; tmp = (PyObject *)self->on_exit_cb; Py_INCREF(callback); self->on_exit_cb = callback; Py_XDECREF(tmp); tmp = self->stdio; Py_XINCREF(stdio); self->stdio = stdio; Py_XDECREF(tmp); /* Increase refcount so that object is not removed before the exit callback is called */ Py_INCREF(self); goto cleanup; error: ret = NULL; Py_DECREF(self); cleanup: if (options.args) { for (i = 0; options.args[i] != NULL; ++i) { PyMem_Free(options.args[i]); } PyMem_Free(options.args); } if (options.env) { for (i = 0; options.env[i] != NULL; ++i) { PyMem_Free(options.env[i]); } PyMem_Free(options.env); } PyMem_Free((void*)options.cwd); PyMem_Free((void*)options.file); PyMem_Free(options.stdio); return ret; }
Py::Object _image_module::pcolor(const Py::Tuple& args) { _VERBOSE("_image_module::pcolor"); if (args.length() != 6) throw Py::TypeError("Incorrect number of arguments (6 expected)"); Py::Object xp = args[0]; Py::Object yp = args[1]; Py::Object dp = args[2]; unsigned int rows = Py::Int(args[3]); unsigned int cols = Py::Int(args[4]); Py::Tuple bounds = args[5]; if (bounds.length() !=4) throw Py::TypeError("Incorrect number of bounds (4 expected)"); float x_min = Py::Float(bounds[0]); float x_max = Py::Float(bounds[1]); float y_min = Py::Float(bounds[2]); float y_max = Py::Float(bounds[3]); float width = x_max - x_min; float height = y_max - y_min; float dx = width / ((float) cols); float dy = height / ((float) rows); // Check we have something to output to if (rows == 0 || cols ==0) throw Py::ValueError("Cannot scale to zero size"); // Get numpy arrays PyArrayObject *x = (PyArrayObject *) PyArray_ContiguousFromObject(xp.ptr(), PyArray_FLOAT, 1, 1); if (x == NULL) throw Py::ValueError("x is of incorrect type (wanted 1D float)"); PyArrayObject *y = (PyArrayObject *) PyArray_ContiguousFromObject(yp.ptr(), PyArray_FLOAT, 1, 1); if (y == NULL) { Py_XDECREF(x); throw Py::ValueError("y is of incorrect type (wanted 1D float)"); } PyArrayObject *d = (PyArrayObject *) PyArray_ContiguousFromObject(dp.ptr(), PyArray_UBYTE, 3, 3); if (d == NULL) { Py_XDECREF(x); Py_XDECREF(y); throw Py::ValueError("data is of incorrect type (wanted 3D UInt8)"); } if (d->dimensions[2] != 4) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); throw Py::ValueError("data must be in RGBA format"); } // Check dimensions match int nx = x->dimensions[0]; int ny = y->dimensions[0]; if (nx != d->dimensions[1] || ny != d->dimensions[0]) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); throw Py::ValueError("data and axis dimensions do not match"); } // Allocate memory for pointer arrays unsigned int * rowstarts = reinterpret_cast<unsigned int*>(PyMem_Malloc(sizeof(unsigned int)*rows)); if (rowstarts == NULL) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); throw Py::MemoryError("Cannot allocate memory for lookup table"); } unsigned int * colstarts = reinterpret_cast<unsigned int*>(PyMem_Malloc(sizeof(unsigned int*)*cols)); if (colstarts == NULL) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); PyMem_Free(rowstarts); throw Py::MemoryError("Cannot allocate memory for lookup table"); } // Create output Image* imo = new Image; imo->rowsIn = rows; imo->colsIn = cols; imo->rowsOut = rows; imo->colsOut = cols; size_t NUMBYTES(rows * cols * 4); agg::int8u *buffer = new agg::int8u[NUMBYTES]; if (buffer == NULL) { Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); PyMem_Free(rowstarts); PyMem_Free(colstarts); throw Py::MemoryError("Could not allocate memory for image"); } // Calculate the pointer arrays to map input x to output x unsigned int i, j, j_last; unsigned int * colstart = colstarts; unsigned int * rowstart = rowstarts; float *xs1 = reinterpret_cast<float*>(x->data); float *ys1 = reinterpret_cast<float*>(y->data); float *xs2 = xs1+1; float *ys2 = ys1+1; float *xl = xs1 + nx - 1; float *yl = ys1 + ny - 1; float xo = x_min + dx/2.0; float yo = y_min + dy/2.0; float xm = 0.5*(*xs1 + *xs2); float ym = 0.5*(*ys1 + *ys2); // x/cols j = 0; j_last = j; for (i=0;i<cols;i++,xo+=dx,colstart++) { while(xs2 != xl && xo > xm) { xs1 = xs2; xs2 = xs1+1; xm = 0.5*(*xs1 + *xs2); j++; } *colstart = j - j_last; j_last = j; } // y/rows j = 0; j_last = j; for (i=0;i<rows;i++,yo+=dy,rowstart++) { while(ys2 != yl && yo > ym) { ys1 = ys2; ys2 = ys1+1; ym = 0.5*(*ys1 + *ys2); j++; } *rowstart = j - j_last; j_last = j; } // Copy data to output buffer unsigned char *start; unsigned char *inposition; size_t inrowsize(nx*4); size_t rowsize(cols*4); rowstart = rowstarts; agg::int8u * position = buffer; agg::int8u * oldposition = NULL; start = reinterpret_cast<unsigned char*>(d->data); for(i=0;i<rows;i++,rowstart++) { if (i > 0 && *rowstart == 0) { memcpy(position, oldposition, rowsize*sizeof(agg::int8u)); oldposition = position; position += rowsize; } else { oldposition = position; start += *rowstart * inrowsize; inposition = start; for(j=0,colstart=colstarts;j<cols;j++,position+=4,colstart++) { inposition += *colstart * 4; memcpy(position, inposition, 4*sizeof(agg::int8u)); } } } // Attatch output buffer to output buffer imo->rbufOut = new agg::rendering_buffer; imo->bufferOut = buffer; imo->rbufOut->attach(imo->bufferOut, imo->colsOut, imo->rowsOut, imo->colsOut * imo->BPP); Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(d); PyMem_Free(rowstarts); PyMem_Free(colstarts); return Py::asObject(imo); }
void initialize(int argc, char *argv[], bool setup_default_environment){ wchar_t **argv_copy; char exec_path[2048]; char home_path[2048]; wchar_t *wide_exec_path; wchar_t *wide_home_path; argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc); for (int i = 0; i < argc - 1; i++) { argv_copy[i] = char2wchar(argv[i + 1]); // todo: handle the case where argv_copy is NULL } Mango::initialize(false); if (setup_default_environment) { Mango::GlobalFrame = new Mango::Core::Frame(true); Mango::Engine = new MangoPy::PyEngine(); Mango::Keyboard = new Mango::Core::CoreKeyboard(); Mango::Mouse = new Mango::Core::CoreMouse(); Mango::Camera = new Mango::Core::CoreCamera(); Mango::View = new Mango::Core::CoreCamera(); Mango::Camera->set(STEP); Mango::Camera->lookAt(Mango::Vector(0, 0, 0), 5); Mango::View->set(STEP); Mango::View->setMode(RMB_CYLINDRICAL_ROTATE | LMB_DRAG_FOCUS | ZOOM_SCALES); Mango::Engine->setCameraObject(Mango::Camera); Mango::Engine->setViewObject(Mango::View); } // Add modules - phase one PyImport_AppendInittab("_mpygen", PyInit__mpygen); PyImport_AppendInittab("Core", PyInit_Core); PyImport_AppendInittab("OpenGL", PyInit_OpenGL); PyImport_AppendInittab("Draw", PyInit_Draw); // Initialize Python if (!executable_path(exec_path, 2048)){ std::cout << "Warning: could not determine executable path." << std::endl; std::cout << " using argv[0], but this value is not reliable" << std::endl; std::cout << " and Mango may not be able to find or execute " << std::endl; std::cout << " files outside of its own directory" << std::endl; strncpy(exec_path, argv[0], 2047); exec_path[2047] = NULL; } wide_exec_path = char2wchar(exec_path); // Py_SetProgramName(wide_exec_path); #if !defined(WIN32) // Set Home Path // Windows seems to set exec_prefix just fine without this, so // it is skipped on windows until needed python_home_path(exec_path, home_path, 2048); wide_home_path = char2wchar(home_path); // Py_SetPythonHome(wide_home_path); #endif Py_Initialize(); PySys_SetArgv(argc - 1, argv_copy); // Add modules - phase two PyObject* main_module = PyImport_AddModule("__main__"); PyObject *module_mpygen = PyImport_ImportModule("_mpygen"); Py_INCREF(module_mpygen); PyModule_AddObject(main_module, "_mpygen", module_mpygen); if (module_mpygen == NULL){ std::cout << "MangoPy: Error creating module _mpygen" << std::endl; exit(1); } PyObject *module_core = PyImport_ImportModule("Core"); Py_INCREF(module_core); PyModule_AddObject(main_module, "Core", module_core); if (module_core == NULL){ std::cout << "MangoPy: Error creating module Core" << std::endl; exit(1); } PyObject *module_opengl = PyImport_ImportModule("OpenGL"); Py_INCREF(module_opengl); PyModule_AddObject(main_module, "OpenGL", module_opengl); if (module_opengl == NULL){ std::cout << "MangoPy: Error creating module OpenGL" << std::endl; exit(1); } PyObject *module_draw = PyImport_ImportModule("Draw"); Py_INCREF(module_draw); PyModule_AddObject(main_module, "Draw", module_draw); if (module_draw == NULL){ std::cout << "MangoPy: Error creating module Draw" << std::endl; exit(1); } // Add absolute path to engine to the module search path PyRun_SimpleString("import os, sys"); PyModule_AddStringConstant(module_core, "MANGO_LAUNCH_PATH", exec_path); PyRun_SimpleString("Core.MANGO_ABSOLUTE_PATH = os.path.dirname(os.path.normpath(os.path.realpath(Core.MANGO_LAUNCH_PATH)))"); PyRun_SimpleString("sys.path.append(Core.MANGO_ABSOLUTE_PATH)"); PyRun_SimpleString("sys.path.append(os.path.normpath(os.path.join(Core.MANGO_ABSOLUTE_PATH, '../script')))"); // Make the Core module globally available PyRun_SimpleString("__builtins__._mpygen = _mpygen"); PyRun_SimpleString("__builtins__.Core = Core"); PyRun_SimpleString("__builtins__.Draw = Draw"); PyRun_SimpleString("__builtins__.OpenGL = OpenGL"); PyRun_SimpleString("__builtins__.Vector = Core.Vector"); PyRun_SimpleString("__builtins__.STEP = Core.STEP"); PyRun_SimpleString("__builtins__.RENDER = Core.RENDER"); PyRun_SimpleString("__builtins__.DRAW = Core.DRAW"); PyRun_SimpleString("__builtins__.INPUT = Core.INPUT"); PyRun_SimpleString("__builtins__.CAMERA_DEFAULT_MODE = Core.CAMERA_DEFAULT_MODE"); PyRun_SimpleString("__builtins__.LOCK_PAN = Core.LOCK_PAN"); PyRun_SimpleString("__builtins__.LOCK_DISTANCE = Core.LOCK_DISTANCE"); PyRun_SimpleString("__builtins__.LOCK_ALPHA = Core.LOCK_ALPHA"); PyRun_SimpleString("__builtins__.LOCK_BETA = Core.LOCK_BETA"); PyRun_SimpleString("__builtins__.LOCK_GAMMA = Core.LOCK_GAMMA"); PyRun_SimpleString("__builtins__.RMB_CYLINDRICAL_ROTATE = Core.RMB_CYLINDRICAL_ROTATE"); PyRun_SimpleString("__builtins__.LMB_DRAG_FOCUS = Core.LMB_DRAG_FOCUS"); PyRun_SimpleString("__builtins__.LOCK_ALL = Core.LOCK_ALL"); PyRun_SimpleString("__builtins__.KEY_WINDOWS_MENU = Core.KEY_WINDOWS_MENU"); PyRun_SimpleString("__builtins__.KEY_WINDOWS_RIGHT = Core.KEY_WINDOWS_RIGHT"); PyRun_SimpleString("__builtins__.KEY_WINDOWS_LEFT = Core.KEY_WINDOWS_LEFT"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_DIVIDE = Core.KEY_NUMPAD_DIVIDE"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_DECIMAL = Core.KEY_NUMPAD_DECIMAL"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_SUBTRACT = Core.KEY_NUMPAD_SUBTRACT"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_SEPARATOR = Core.KEY_NUMPAD_SEPARATOR"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_ADD = Core.KEY_NUMPAD_ADD"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_MULTIPLY = Core.KEY_NUMPAD_MULTIPLY"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_EQUAL = Core.KEY_NUMPAD_EQUAL"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_DELETE = Core.KEY_NUMPAD_DELETE"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_INSERT = Core.KEY_NUMPAD_INSERT"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_BEGIN = Core.KEY_NUMPAD_BEGIN"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_END = Core.KEY_NUMPAD_END"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_PAGEDOWN = Core.KEY_NUMPAD_PAGEDOWN"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_PAGEUP = Core.KEY_NUMPAD_PAGEUP"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_DOWN = Core.KEY_NUMPAD_DOWN"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_RIGHT = Core.KEY_NUMPAD_RIGHT"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_UP = Core.KEY_NUMPAD_UP"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_LEFT = Core.KEY_NUMPAD_LEFT"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_HOME = Core.KEY_NUMPAD_HOME"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_F4 = Core.KEY_NUMPAD_F4"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_F3 = Core.KEY_NUMPAD_F3"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_F2 = Core.KEY_NUMPAD_F2"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_F1 = Core.KEY_NUMPAD_F1"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_ENTER = Core.KEY_NUMPAD_ENTER"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_TAB = Core.KEY_NUMPAD_TAB"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_SPACE = Core.KEY_NUMPAD_SPACE"); PyRun_SimpleString("__builtins__.KEY_PAGEDOWN = Core.KEY_PAGEDOWN"); PyRun_SimpleString("__builtins__.KEY_PAGEUP = Core.KEY_PAGEUP"); PyRun_SimpleString("__builtins__.KEY_SCROLL = Core.KEY_SCROLL"); PyRun_SimpleString("__builtins__.KEY_NUMLOCK = Core.KEY_NUMLOCK"); PyRun_SimpleString("__builtins__.KEY_F24 = Core.KEY_F24"); PyRun_SimpleString("__builtins__.KEY_F23 = Core.KEY_F23"); PyRun_SimpleString("__builtins__.KEY_F22 = Core.KEY_F22"); PyRun_SimpleString("__builtins__.KEY_F21 = Core.KEY_F21"); PyRun_SimpleString("__builtins__.KEY_F20 = Core.KEY_F20"); PyRun_SimpleString("__builtins__.KEY_F19 = Core.KEY_F19"); PyRun_SimpleString("__builtins__.KEY_F18 = Core.KEY_F18"); PyRun_SimpleString("__builtins__.KEY_F17 = Core.KEY_F17"); PyRun_SimpleString("__builtins__.KEY_F16 = Core.KEY_F16"); PyRun_SimpleString("__builtins__.KEY_F15 = Core.KEY_F15"); PyRun_SimpleString("__builtins__.KEY_F14 = Core.KEY_F14"); PyRun_SimpleString("__builtins__.KEY_F13 = Core.KEY_F13"); PyRun_SimpleString("__builtins__.KEY_F12 = Core.KEY_F12"); PyRun_SimpleString("__builtins__.KEY_F11 = Core.KEY_F11"); PyRun_SimpleString("__builtins__.KEY_F10 = Core.KEY_F10"); PyRun_SimpleString("__builtins__.KEY_F9 = Core.KEY_F9"); PyRun_SimpleString("__builtins__.KEY_F8 = Core.KEY_F8"); PyRun_SimpleString("__builtins__.KEY_F7 = Core.KEY_F7"); PyRun_SimpleString("__builtins__.KEY_F6 = Core.KEY_F6"); PyRun_SimpleString("__builtins__.KEY_F5 = Core.KEY_F5"); PyRun_SimpleString("__builtins__.KEY_F4 = Core.KEY_F4"); PyRun_SimpleString("__builtins__.KEY_F3 = Core.KEY_F3"); PyRun_SimpleString("__builtins__.KEY_F2 = Core.KEY_F2"); PyRun_SimpleString("__builtins__.KEY_F1 = Core.KEY_F1"); PyRun_SimpleString("__builtins__.KEY_DIVIDE = Core.KEY_DIVIDE"); PyRun_SimpleString("__builtins__.KEY_DECIMAL = Core.KEY_DECIMAL"); PyRun_SimpleString("__builtins__.KEY_SUBTRACT = Core.KEY_SUBTRACT"); PyRun_SimpleString("__builtins__.KEY_SEPARATOR = Core.KEY_SEPARATOR"); PyRun_SimpleString("__builtins__.KEY_ADD = Core.KEY_ADD"); PyRun_SimpleString("__builtins__.KEY_MULTIPLY = Core.KEY_MULTIPLY"); PyRun_SimpleString("__builtins__.KEY_NUMPAD9 = Core.KEY_NUMPAD9"); PyRun_SimpleString("__builtins__.KEY_NUMPAD8 = Core.KEY_NUMPAD8"); PyRun_SimpleString("__builtins__.KEY_NUMPAD7 = Core.KEY_NUMPAD7"); PyRun_SimpleString("__builtins__.KEY_NUMPAD6 = Core.KEY_NUMPAD6"); PyRun_SimpleString("__builtins__.KEY_NUMPAD5 = Core.KEY_NUMPAD5"); PyRun_SimpleString("__builtins__.KEY_NUMPAD4 = Core.KEY_NUMPAD4"); PyRun_SimpleString("__builtins__.KEY_NUMPAD3 = Core.KEY_NUMPAD3"); PyRun_SimpleString("__builtins__.KEY_NUMPAD2 = Core.KEY_NUMPAD2"); PyRun_SimpleString("__builtins__.KEY_NUMPAD1 = Core.KEY_NUMPAD1"); PyRun_SimpleString("__builtins__.KEY_NUMPAD0 = Core.KEY_NUMPAD0"); PyRun_SimpleString("__builtins__.KEY_HELP = Core.KEY_HELP"); PyRun_SimpleString("__builtins__.KEY_INSERT = Core.KEY_INSERT"); PyRun_SimpleString("__builtins__.KEY_SNAPSHOT = Core.KEY_SNAPSHOT"); PyRun_SimpleString("__builtins__.KEY_EXECUTE = Core.KEY_EXECUTE"); PyRun_SimpleString("__builtins__.KEY_PRINT = Core.KEY_PRINT"); PyRun_SimpleString("__builtins__.KEY_SELECT = Core.KEY_SELECT"); PyRun_SimpleString("__builtins__.KEY_DOWN = Core.KEY_DOWN"); PyRun_SimpleString("__builtins__.KEY_RIGHT = Core.KEY_RIGHT"); PyRun_SimpleString("__builtins__.KEY_UP = Core.KEY_UP"); PyRun_SimpleString("__builtins__.KEY_LEFT = Core.KEY_LEFT"); PyRun_SimpleString("__builtins__.KEY_HOME = Core.KEY_HOME"); PyRun_SimpleString("__builtins__.KEY_END = Core.KEY_END"); PyRun_SimpleString("__builtins__.KEY_CAPITAL = Core.KEY_CAPITAL"); PyRun_SimpleString("__builtins__.KEY_PAUSE = Core.KEY_PAUSE"); PyRun_SimpleString("__builtins__.KEY_MENU = Core.KEY_MENU"); PyRun_SimpleString("__builtins__.KEY_CONTROL = Core.KEY_CONTROL"); PyRun_SimpleString("__builtins__.KEY_ALT = Core.KEY_ALT"); PyRun_SimpleString("__builtins__.KEY_SHIFT = Core.KEY_SHIFT"); PyRun_SimpleString("__builtins__.KEY_CLEAR = Core.KEY_CLEAR"); PyRun_SimpleString("__builtins__.KEY_MBUTTON = Core.KEY_MBUTTON"); PyRun_SimpleString("__builtins__.KEY_CANCEL = Core.KEY_CANCEL"); PyRun_SimpleString("__builtins__.KEY_RBUTTON = Core.KEY_RBUTTON"); PyRun_SimpleString("__builtins__.KEY_LBUTTON = Core.KEY_LBUTTON"); PyRun_SimpleString("__builtins__.KEY_START = Core.KEY_START"); PyRun_SimpleString("__builtins__.KEY_DELETE = Core.KEY_DELETE"); PyRun_SimpleString("__builtins__.KEY_SPACE = Core.KEY_SPACE"); PyRun_SimpleString("__builtins__.KEY_ESCAPE = Core.KEY_ESCAPE"); PyRun_SimpleString("__builtins__.KEY_RETURN = Core.KEY_RETURN"); PyRun_SimpleString("__builtins__.KEY_ENTER = Core.KEY_ENTER"); PyRun_SimpleString("__builtins__.KEY_TAB = Core.KEY_TAB"); PyRun_SimpleString("__builtins__.BUTTON_RIGHT = Core.BUTTON_RIGHT"); PyRun_SimpleString("__builtins__.KEY_BACKSPACE = Core.KEY_BACKSPACE"); PyRun_SimpleString("__builtins__.BUTTON_MIDDLE = Core.BUTTON_MIDDLE"); PyRun_SimpleString("__builtins__.BUTTON_LEFT = Core.BUTTON_LEFT"); PyRun_SimpleString("__builtins__.ANY = Core.ANY"); PyRun_SimpleString("__builtins__.RELEASE = Core.RELEASE"); PyRun_SimpleString("__builtins__.CLICK = Core.CLICK"); PyRun_SimpleString("__builtins__.PRESS = Core.PRESS"); PyRun_SimpleString("__builtins__.MOUSE = Core.MOUSE"); PyRun_SimpleString("__builtins__.KEYBOARD = Core.KEYBOARD"); PyRun_SimpleString("__builtins__.BUTTON_STATE_DOWN = Core.BUTTON_STATE_DOWN"); PyRun_SimpleString("__builtins__.BUTTON_STATE_UP = Core.BUTTON_STATE_UP"); PyRun_SimpleString("__builtins__.KEY_STATE_DOWN = Core.KEY_STATE_DOWN"); PyRun_SimpleString("__builtins__.KEY_STATE_UP = Core.KEY_STATE_UP"); // Make opengl methods available in the global namespace PyRun_SimpleString("__builtins__.GL_POINTS = OpenGL.GL_POINTS"); PyRun_SimpleString("__builtins__.GL_LINES = OpenGL.GL_LINES"); PyRun_SimpleString("__builtins__.GL_LINE_LOOP = OpenGL.GL_LINE_LOOP"); PyRun_SimpleString("__builtins__.GL_LINE_STRIP = OpenGL.GL_LINE_STRIP"); PyRun_SimpleString("__builtins__.GL_TRIANGLES = OpenGL.GL_TRIANGLES"); PyRun_SimpleString("__builtins__.GL_TRIANGLE_STRIP = OpenGL.GL_TRIANGLE_STRIP"); PyRun_SimpleString("__builtins__.GL_TRIANGLE_FAN = OpenGL.GL_TRIANGLE_FAN"); PyRun_SimpleString("__builtins__.GL_QUADS = OpenGL.GL_QUADS"); PyRun_SimpleString("__builtins__.GL_QUAD_STRIP = OpenGL.GL_QUAD_STRIP"); PyRun_SimpleString("__builtins__.GL_POLYGON = OpenGL.GL_POLYGON"); PyRun_SimpleString("__builtins__.glBegin = OpenGL.glBegin"); PyRun_SimpleString("__builtins__.glEnd = OpenGL.glEnd"); PyRun_SimpleString("__builtins__.glVertex = OpenGL.glVertex"); PyRun_SimpleString("__builtins__.glNormal = OpenGL.glNormal"); PyRun_SimpleString("__builtins__.glColor = OpenGL.glColor"); PyRun_SimpleString("__builtins__.glTranslate = OpenGL.glTranslate"); PyRun_SimpleString("__builtins__.glRotate = OpenGL.glRotate"); PyRun_SimpleString("__builtins__.glScale = OpenGL.glScale"); PyRun_SimpleString("__builtins__.glScale = OpenGL.glScale"); PyRun_SimpleString("__builtins__.glPushMatrix = OpenGL.glPushMatrix"); PyRun_SimpleString("__builtins__.glPopMatrix = OpenGL.glPopMatrix"); // Create global engine instance mpy_PyEngine *py_global_engine = mpy_PyEngine_NEW(); //py_global_engine->internalObject = (PyEngine *)Mango::Engine; // WARNING: this downcasting from a CoreEngine to a PyEngine is dangerous, and only works if you are SURE that Mango::Engine is actually a PyEngine! py_global_engine->internalObject = Mango::Engine; // WARNING: this downcasting from a CoreEngine to a PyEngine is dangerous, and only works if you are SURE that Mango::Engine is actually a PyEngine! PyModule_AddObject(module_core, "Engine", reinterpret_cast<PyObject *>(py_global_engine)); PyRun_SimpleString("__builtins__.Engine = Core.Engine"); // Create global keyboard instance mpy_CoreKeyboard *py_global_keyboard = mpy_CoreKeyboard_NEW(); py_global_keyboard->internalObject = Mango::Keyboard; PyModule_AddObject(module_core, "Keyboard", reinterpret_cast<PyObject *>(py_global_keyboard)); PyRun_SimpleString("__builtins__.Keyboard = Core.Keyboard"); // Create global mouse instance mpy_CoreMouse *py_global_mouse = mpy_CoreMouse_NEW(); py_global_mouse->internalObject = Mango::Mouse; PyModule_AddObject(module_core, "Mouse", reinterpret_cast<PyObject *>(py_global_mouse)); PyRun_SimpleString("__builtins__.Mouse = Core.Mouse"); // Create global frame instance PyGlobalFrame = mpy_Frame_NEW(); PyGlobalFrame->internalObject = Mango::GlobalFrame; PyGlobalFrame->parentFrame = NULL; Py_INCREF(PyGlobalFrame); PyModule_AddObject(module_core, "GlobalFrame", reinterpret_cast<PyObject *>(PyGlobalFrame)); PyRun_SimpleString("__builtins__.GlobalFrame = Core.GlobalFrame"); // Create global camera instance if (Mango::Camera != NULL){ mpy_Frame *py_camera_focus = mpy_Frame_NEW(); py_camera_focus->internalObject = Mango::Camera->parentFrame(); mpy_Frame_init(py_camera_focus, NULL, NULL); mpy_CoreCamera *py_global_camera = mpy_CoreCamera_NEW(); Py_INCREF(py_camera_focus); py_global_camera->parentFrame = py_camera_focus; mpy_Object_init((mpy_Object *)py_global_camera, NULL, NULL); py_global_camera->internalObject = Mango::Camera; PyModule_AddObject(module_core, "Camera", reinterpret_cast<PyObject *>(py_global_camera)); PyRun_SimpleString("__builtins__.Camera = Core.Camera"); } // Create global view instance if (Mango::View != NULL){ mpy_CoreCamera *py_global_view = mpy_CoreCamera_NEW(); py_global_view->internalObject = Mango::View; PyModule_AddObject(module_core, "View", reinterpret_cast<PyObject *>(py_global_view)); PyRun_SimpleString("__builtins__.View = Core.View"); } // Make environment available to extensions PyModule_AddObject(module_mpygen, "GLOBAL_FRAME", PyCapsule_New((void *)Mango::GlobalFrame, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "PY_GLOBAL_FRAME", PyCapsule_New((void *)PyGlobalFrame, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "ENGINE", PyCapsule_New((void *)Mango::Engine, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "CAMERA", PyCapsule_New((void *)Mango::Camera, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "VIEW", PyCapsule_New((void *)Mango::View, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "KEYBOARD", PyCapsule_New((void *)Mango::Keyboard, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "MOUSE", PyCapsule_New((void *)Mango::Mouse, NULL, mpy_trivialDelMethod)); //PyModule_AddObject(module_mpygen, "TYPE_OBJECT", PyCObject_FromVoidPtr((void *)&mpy_ObjectType, mpy_trivialDelMethod)); PyObject *global_types = PyDict_New(); PyModule_AddObject(module_mpygen, "TYPES", global_types); //PyDict_SetItemString(global_types, "mangopy.core.object", PyCObject_FromVoidPtr((void *)&mpy_ObjectType, mpy_trivialDelMethod)); MangoPy::register_py_type_object("mangopy.core.input_event", &mpy_InputEventType); MangoPy::register_py_type_object("mangopy.core.vector", &mpy_VectorType); MangoPy::register_py_type_object("mangopy.core.matrix", &mpy_MatrixType); MangoPy::register_py_type_object("mangopy.core.object", &mpy_ObjectType); MangoPy::register_py_type_object("mangopy.core.frame", &mpy_FrameType); MangoPy::register_py_type_object("mangopy.core.py_engine", &mpy_PyEngineType); MangoPy::register_py_type_object("mangopy.core.core_camera", &mpy_CoreCameraType); MangoPy::register_py_type_object("mangopy.core.core_keyboard", &mpy_CoreKeyboardType); MangoPy::register_py_type_object("mangopy.core.core_mouse", &mpy_CoreMouseType); MangoPy::register_py_type_object("mangopy.core.triangle", &mpy_TriangleType); // Setup Python error buffer PyRun_SimpleString("\n\ #import sys \n\ class MangoPy_StdErr: \n\ def write(self, msg): \n\ _mpygen.writeToPythonScriptStderr(msg) \n\ sys.stderr = MangoPy_StdErr() \n\ "); // Setup Paths Array PyRun_SimpleString("Core.SOURCES = []"); }
initpycurl(void) #endif { PyObject *m, *d; const curl_version_info_data *vi; const char *libcurl_version, *runtime_ssl_lib; size_t libcurl_version_len, pycurl_version_len; /* Check the version, as this has caused nasty problems in * some cases. */ vi = curl_version_info(CURLVERSION_NOW); if (vi == NULL) { PyErr_SetString(PyExc_ImportError, "pycurl: curl_version_info() failed"); goto error; } if (vi->version_num < LIBCURL_VERSION_NUM) { PyErr_Format(PyExc_ImportError, "pycurl: libcurl link-time version (%s) is older than compile-time version (%s)", vi->version, LIBCURL_VERSION); goto error; } /* Our compiled crypto locks should correspond to runtime ssl library. */ if (vi->ssl_version == NULL) { runtime_ssl_lib = "none/other"; } else if (!strncmp(vi->ssl_version, "OpenSSL/", 8) || !strncmp(vi->ssl_version, "LibreSSL/", 9)) { runtime_ssl_lib = "openssl"; } else if (!strncmp(vi->ssl_version, "GnuTLS/", 7)) { runtime_ssl_lib = "gnutls"; } else if (!strncmp(vi->ssl_version, "NSS/", 4)) { runtime_ssl_lib = "nss"; } else { runtime_ssl_lib = "none/other"; } if (strcmp(runtime_ssl_lib, COMPILE_SSL_LIB)) { PyErr_Format(PyExc_ImportError, "pycurl: libcurl link-time ssl backend (%s) is different from compile-time ssl backend (%s)", runtime_ssl_lib, COMPILE_SSL_LIB); goto error; } /* Initialize the type of the new type objects here; doing it here * is required for portability to Windows without requiring C++. */ p_Curl_Type = &Curl_Type; p_CurlMulti_Type = &CurlMulti_Type; p_CurlShare_Type = &CurlShare_Type; Py_TYPE(&Curl_Type) = &PyType_Type; Py_TYPE(&CurlMulti_Type) = &PyType_Type; Py_TYPE(&CurlShare_Type) = &PyType_Type; /* Create the module and add the functions */ if (PyType_Ready(&Curl_Type) < 0) goto error; if (PyType_Ready(&CurlMulti_Type) < 0) goto error; if (PyType_Ready(&CurlShare_Type) < 0) goto error; #if PY_MAJOR_VERSION >= 3 m = PyModule_Create(&curlmodule); if (m == NULL) goto error; #else /* returns a borrowed reference, XDECREFing it crashes the interpreter */ m = Py_InitModule3("pycurl", curl_methods, pycurl_module_doc); if (m == NULL || !PyModule_Check(m)) goto error; #endif /* Add error object to the module */ d = PyModule_GetDict(m); assert(d != NULL); ErrorObject = PyErr_NewException("pycurl.error", NULL, NULL); if (ErrorObject == NULL) goto error; if (PyDict_SetItemString(d, "error", ErrorObject) < 0) { goto error; } curlobject_constants = PyDict_New(); if (curlobject_constants == NULL) goto error; curlmultiobject_constants = PyDict_New(); if (curlmultiobject_constants == NULL) goto error; curlshareobject_constants = PyDict_New(); if (curlshareobject_constants == NULL) goto error; /* Add version strings to the module */ libcurl_version = curl_version(); libcurl_version_len = strlen(libcurl_version); #define PYCURL_VERSION_PREFIX_SIZE sizeof(PYCURL_VERSION_PREFIX) /* PYCURL_VERSION_PREFIX_SIZE includes terminating null which will be * replaced with the space; libcurl_version_len does not include * terminating null. */ pycurl_version_len = PYCURL_VERSION_PREFIX_SIZE + libcurl_version_len + 1; g_pycurl_useragent = PyMem_Malloc(pycurl_version_len); if (g_pycurl_useragent == NULL) goto error; memcpy(g_pycurl_useragent, PYCURL_VERSION_PREFIX, PYCURL_VERSION_PREFIX_SIZE); g_pycurl_useragent[PYCURL_VERSION_PREFIX_SIZE-1] = ' '; memcpy(g_pycurl_useragent + PYCURL_VERSION_PREFIX_SIZE, libcurl_version, libcurl_version_len); g_pycurl_useragent[pycurl_version_len - 1] = 0; #undef PYCURL_VERSION_PREFIX_SIZE insstr_modinit(d, "version", g_pycurl_useragent); insstr_modinit(d, "COMPILE_DATE", __DATE__ " " __TIME__); insint(d, "COMPILE_PY_VERSION_HEX", PY_VERSION_HEX); insint(d, "COMPILE_LIBCURL_VERSION_NUM", LIBCURL_VERSION_NUM); /* Types */ insobj2_modinit(d, NULL, "Curl", (PyObject *) p_Curl_Type); insobj2_modinit(d, NULL, "CurlMulti", (PyObject *) p_CurlMulti_Type); insobj2_modinit(d, NULL, "CurlShare", (PyObject *) p_CurlShare_Type); /** ** the order of these constants mostly follows <curl/curl.h> **/ /* Abort curl_read_callback(). */ insint_c(d, "READFUNC_ABORT", CURL_READFUNC_ABORT); insint_c(d, "READFUNC_PAUSE", CURL_READFUNC_PAUSE); /* Pause curl_write_callback(). */ insint_c(d, "WRITEFUNC_PAUSE", CURL_WRITEFUNC_PAUSE); /* constants for ioctl callback return values */ insint_c(d, "IOE_OK", CURLIOE_OK); insint_c(d, "IOE_UNKNOWNCMD", CURLIOE_UNKNOWNCMD); insint_c(d, "IOE_FAILRESTART", CURLIOE_FAILRESTART); /* constants for ioctl callback argument values */ insint_c(d, "IOCMD_NOP", CURLIOCMD_NOP); insint_c(d, "IOCMD_RESTARTREAD", CURLIOCMD_RESTARTREAD); /* curl_infotype: the kind of data that is passed to information_callback */ /* XXX do we actually need curl_infotype in pycurl ??? */ insint_c(d, "INFOTYPE_TEXT", CURLINFO_TEXT); insint_c(d, "INFOTYPE_HEADER_IN", CURLINFO_HEADER_IN); insint_c(d, "INFOTYPE_HEADER_OUT", CURLINFO_HEADER_OUT); insint_c(d, "INFOTYPE_DATA_IN", CURLINFO_DATA_IN); insint_c(d, "INFOTYPE_DATA_OUT", CURLINFO_DATA_OUT); insint_c(d, "INFOTYPE_SSL_DATA_IN", CURLINFO_SSL_DATA_IN); insint_c(d, "INFOTYPE_SSL_DATA_OUT", CURLINFO_SSL_DATA_OUT); /* CURLcode: error codes */ insint_c(d, "E_OK", CURLE_OK); insint_c(d, "E_UNSUPPORTED_PROTOCOL", CURLE_UNSUPPORTED_PROTOCOL); insint_c(d, "E_FAILED_INIT", CURLE_FAILED_INIT); insint_c(d, "E_URL_MALFORMAT", CURLE_URL_MALFORMAT); #ifdef HAVE_CURL_7_21_5 insint_c(d, "E_NOT_BUILT_IN", CURLE_NOT_BUILT_IN); #endif insint_c(d, "E_COULDNT_RESOLVE_PROXY", CURLE_COULDNT_RESOLVE_PROXY); insint_c(d, "E_COULDNT_RESOLVE_HOST", CURLE_COULDNT_RESOLVE_HOST); insint_c(d, "E_COULDNT_CONNECT", CURLE_COULDNT_CONNECT); insint_c(d, "E_FTP_WEIRD_SERVER_REPLY", CURLE_FTP_WEIRD_SERVER_REPLY); insint_c(d, "E_FTP_ACCESS_DENIED", CURLE_FTP_ACCESS_DENIED); #ifdef HAVE_CURL_7_24_0 insint_c(d, "E_FTP_ACCEPT_FAILED", CURLE_FTP_ACCEPT_FAILED); #endif insint_c(d, "E_FTP_WEIRD_PASS_REPLY", CURLE_FTP_WEIRD_PASS_REPLY); insint_c(d, "E_FTP_WEIRD_USER_REPLY", CURLE_FTP_WEIRD_USER_REPLY); insint_c(d, "E_FTP_WEIRD_PASV_REPLY", CURLE_FTP_WEIRD_PASV_REPLY); insint_c(d, "E_FTP_WEIRD_227_FORMAT", CURLE_FTP_WEIRD_227_FORMAT); insint_c(d, "E_FTP_CANT_GET_HOST", CURLE_FTP_CANT_GET_HOST); insint_c(d, "E_FTP_CANT_RECONNECT", CURLE_FTP_CANT_RECONNECT); insint_c(d, "E_FTP_COULDNT_SET_BINARY", CURLE_FTP_COULDNT_SET_BINARY); insint_c(d, "E_PARTIAL_FILE", CURLE_PARTIAL_FILE); insint_c(d, "E_FTP_COULDNT_RETR_FILE", CURLE_FTP_COULDNT_RETR_FILE); insint_c(d, "E_FTP_WRITE_ERROR", CURLE_FTP_WRITE_ERROR); insint_c(d, "E_FTP_QUOTE_ERROR", CURLE_FTP_QUOTE_ERROR); insint_c(d, "E_HTTP_RETURNED_ERROR", CURLE_HTTP_RETURNED_ERROR); insint_c(d, "E_WRITE_ERROR", CURLE_WRITE_ERROR); insint_c(d, "E_FTP_COULDNT_STOR_FILE", CURLE_FTP_COULDNT_STOR_FILE); insint_c(d, "E_READ_ERROR", CURLE_READ_ERROR); insint_c(d, "E_OUT_OF_MEMORY", CURLE_OUT_OF_MEMORY); insint_c(d, "E_OPERATION_TIMEOUTED", CURLE_OPERATION_TIMEOUTED); insint_c(d, "E_OPERATION_TIMEDOUT", CURLE_OPERATION_TIMEDOUT); insint_c(d, "E_FTP_COULDNT_SET_ASCII", CURLE_FTP_COULDNT_SET_ASCII); insint_c(d, "E_FTP_PORT_FAILED", CURLE_FTP_PORT_FAILED); insint_c(d, "E_FTP_COULDNT_USE_REST", CURLE_FTP_COULDNT_USE_REST); insint_c(d, "E_FTP_COULDNT_GET_SIZE", CURLE_FTP_COULDNT_GET_SIZE); insint_c(d, "E_HTTP_RANGE_ERROR", CURLE_HTTP_RANGE_ERROR); insint_c(d, "E_HTTP_POST_ERROR", CURLE_HTTP_POST_ERROR); insint_c(d, "E_SSL_CACERT", CURLE_SSL_CACERT); insint_c(d, "E_SSL_CACERT_BADFILE", CURLE_SSL_CACERT_BADFILE); insint_c(d, "E_SSL_CERTPROBLEM", CURLE_SSL_CERTPROBLEM); insint_c(d, "E_SSL_CIPHER", CURLE_SSL_CIPHER); insint_c(d, "E_SSL_CONNECT_ERROR", CURLE_SSL_CONNECT_ERROR); insint_c(d, "E_SSL_CRL_BADFILE", CURLE_SSL_CRL_BADFILE); insint_c(d, "E_SSL_ENGINE_INITFAILED", CURLE_SSL_ENGINE_INITFAILED); insint_c(d, "E_SSL_ENGINE_NOTFOUND", CURLE_SSL_ENGINE_NOTFOUND); insint_c(d, "E_SSL_ENGINE_SETFAILED", CURLE_SSL_ENGINE_SETFAILED); #if LIBCURL_VERSION_NUM >= 0x072900 /* check for 7.41.0 or greater */ insint_c(d, "E_SSL_INVALIDCERTSTATUS", CURLE_SSL_INVALIDCERTSTATUS); #endif insint_c(d, "E_SSL_ISSUER_ERROR", CURLE_SSL_ISSUER_ERROR); insint_c(d, "E_SSL_PEER_CERTIFICATE", CURLE_SSL_PEER_CERTIFICATE); #if LIBCURL_VERSION_NUM >= 0x072700 /* check for 7.39.0 or greater */ insint_c(d, "E_SSL_PINNEDPUBKEYNOTMATCH", CURLE_SSL_PINNEDPUBKEYNOTMATCH); #endif insint_c(d, "E_SSL_SHUTDOWN_FAILED", CURLE_SSL_SHUTDOWN_FAILED); insint_c(d, "E_BAD_DOWNLOAD_RESUME", CURLE_BAD_DOWNLOAD_RESUME); insint_c(d, "E_FILE_COULDNT_READ_FILE", CURLE_FILE_COULDNT_READ_FILE); insint_c(d, "E_LDAP_CANNOT_BIND", CURLE_LDAP_CANNOT_BIND); insint_c(d, "E_LDAP_SEARCH_FAILED", CURLE_LDAP_SEARCH_FAILED); insint_c(d, "E_LIBRARY_NOT_FOUND", CURLE_LIBRARY_NOT_FOUND); insint_c(d, "E_FUNCTION_NOT_FOUND", CURLE_FUNCTION_NOT_FOUND); insint_c(d, "E_ABORTED_BY_CALLBACK", CURLE_ABORTED_BY_CALLBACK); insint_c(d, "E_BAD_FUNCTION_ARGUMENT", CURLE_BAD_FUNCTION_ARGUMENT); insint_c(d, "E_INTERFACE_FAILED", CURLE_INTERFACE_FAILED); insint_c(d, "E_TOO_MANY_REDIRECTS", CURLE_TOO_MANY_REDIRECTS); #ifdef HAVE_CURL_7_21_5 insint_c(d, "E_UNKNOWN_OPTION", CURLE_UNKNOWN_OPTION); #endif /* same as E_UNKNOWN_OPTION */ insint_c(d, "E_UNKNOWN_TELNET_OPTION", CURLE_UNKNOWN_TELNET_OPTION); insint_c(d, "E_TELNET_OPTION_SYNTAX", CURLE_TELNET_OPTION_SYNTAX); insint_c(d, "E_GOT_NOTHING", CURLE_GOT_NOTHING); insint_c(d, "E_SEND_ERROR", CURLE_SEND_ERROR); insint_c(d, "E_RECV_ERROR", CURLE_RECV_ERROR); insint_c(d, "E_SHARE_IN_USE", CURLE_SHARE_IN_USE); insint_c(d, "E_BAD_CONTENT_ENCODING", CURLE_BAD_CONTENT_ENCODING); insint_c(d, "E_LDAP_INVALID_URL", CURLE_LDAP_INVALID_URL); insint_c(d, "E_FILESIZE_EXCEEDED", CURLE_FILESIZE_EXCEEDED); insint_c(d, "E_FTP_SSL_FAILED", CURLE_FTP_SSL_FAILED); insint_c(d, "E_SEND_FAIL_REWIND", CURLE_SEND_FAIL_REWIND); insint_c(d, "E_LOGIN_DENIED", CURLE_LOGIN_DENIED); insint_c(d, "E_TFTP_NOTFOUND", CURLE_TFTP_NOTFOUND); insint_c(d, "E_TFTP_PERM", CURLE_TFTP_PERM); insint_c(d, "E_TFTP_DISKFULL", CURLE_TFTP_DISKFULL); insint_c(d, "E_TFTP_ILLEGAL", CURLE_TFTP_ILLEGAL); insint_c(d, "E_TFTP_UNKNOWNID", CURLE_TFTP_UNKNOWNID); insint_c(d, "E_TFTP_EXISTS", CURLE_TFTP_EXISTS); insint_c(d, "E_TFTP_NOSUCHUSER", CURLE_TFTP_NOSUCHUSER); insint_c(d, "E_CONV_FAILED", CURLE_CONV_FAILED); insint_c(d, "E_CONV_REQD", CURLE_CONV_REQD); insint_c(d, "E_REMOTE_FILE_NOT_FOUND", CURLE_REMOTE_FILE_NOT_FOUND); insint_c(d, "E_SSH", CURLE_SSH); /* curl_proxytype: constants for setopt(PROXYTYPE, x) */ insint_c(d, "PROXYTYPE_HTTP", CURLPROXY_HTTP); #ifdef HAVE_CURL_7_19_4_OPTS insint_c(d, "PROXYTYPE_HTTP_1_0", CURLPROXY_HTTP_1_0); #endif insint_c(d, "PROXYTYPE_SOCKS4", CURLPROXY_SOCKS4); insint_c(d, "PROXYTYPE_SOCKS4A", CURLPROXY_SOCKS4A); insint_c(d, "PROXYTYPE_SOCKS5", CURLPROXY_SOCKS5); insint_c(d, "PROXYTYPE_SOCKS5_HOSTNAME", CURLPROXY_SOCKS5_HOSTNAME); /* curl_httpauth: constants for setopt(HTTPAUTH, x) */ insint_c(d, "HTTPAUTH_ANY", CURLAUTH_ANY); insint_c(d, "HTTPAUTH_ANYSAFE", CURLAUTH_ANYSAFE); insint_c(d, "HTTPAUTH_BASIC", CURLAUTH_BASIC); insint_c(d, "HTTPAUTH_DIGEST", CURLAUTH_DIGEST); #ifdef HAVE_CURLAUTH_DIGEST_IE insint_c(d, "HTTPAUTH_DIGEST_IE", CURLAUTH_DIGEST_IE); #endif insint_c(d, "HTTPAUTH_GSSNEGOTIATE", CURLAUTH_GSSNEGOTIATE); #if LIBCURL_VERSION_NUM >= 0x072600 /* check for 7.38.0 or greater */ insint_c(d, "HTTPAUTH_NEGOTIATE", CURLAUTH_NEGOTIATE); #endif insint_c(d, "HTTPAUTH_NTLM", CURLAUTH_NTLM); #if LIBCURL_VERSION_NUM >= 0x071600 /* check for 7.22.0 or greater */ insint_c(d, "HTTPAUTH_NTLM_WB", CURLAUTH_NTLM_WB); #endif insint_c(d, "HTTPAUTH_NONE", CURLAUTH_NONE); #if LIBCURL_VERSION_NUM >= 0x071503 /* check for 7.21.3 or greater */ insint_c(d, "HTTPAUTH_ONLY", CURLAUTH_ONLY); #endif #ifdef HAVE_CURL_7_22_0_OPTS insint_c(d, "GSSAPI_DELEGATION_FLAG", CURLGSSAPI_DELEGATION_FLAG); insint_c(d, "GSSAPI_DELEGATION_NONE", CURLGSSAPI_DELEGATION_NONE); insint_c(d, "GSSAPI_DELEGATION_POLICY_FLAG", CURLGSSAPI_DELEGATION_POLICY_FLAG); insint_c(d, "GSSAPI_DELEGATION", CURLOPT_GSSAPI_DELEGATION); #endif /* curl_ftpssl: constants for setopt(FTP_SSL, x) */ insint_c(d, "FTPSSL_NONE", CURLFTPSSL_NONE); insint_c(d, "FTPSSL_TRY", CURLFTPSSL_TRY); insint_c(d, "FTPSSL_CONTROL", CURLFTPSSL_CONTROL); insint_c(d, "FTPSSL_ALL", CURLFTPSSL_ALL); /* curl_ftpauth: constants for setopt(FTPSSLAUTH, x) */ insint_c(d, "FTPAUTH_DEFAULT", CURLFTPAUTH_DEFAULT); insint_c(d, "FTPAUTH_SSL", CURLFTPAUTH_SSL); insint_c(d, "FTPAUTH_TLS", CURLFTPAUTH_TLS); /* curl_ftpauth: constants for setopt(FTPSSLAUTH, x) */ insint_c(d, "FORM_BUFFER", CURLFORM_BUFFER); insint_c(d, "FORM_BUFFERPTR", CURLFORM_BUFFERPTR); insint_c(d, "FORM_CONTENTS", CURLFORM_COPYCONTENTS); insint_c(d, "FORM_FILE", CURLFORM_FILE); insint_c(d, "FORM_CONTENTTYPE", CURLFORM_CONTENTTYPE); insint_c(d, "FORM_FILENAME", CURLFORM_FILENAME); /* FTP_FILEMETHOD options */ insint_c(d, "FTPMETHOD_DEFAULT", CURLFTPMETHOD_DEFAULT); insint_c(d, "FTPMETHOD_MULTICWD", CURLFTPMETHOD_MULTICWD); insint_c(d, "FTPMETHOD_NOCWD", CURLFTPMETHOD_NOCWD); insint_c(d, "FTPMETHOD_SINGLECWD", CURLFTPMETHOD_SINGLECWD); /* CURLoption: symbolic constants for setopt() */ /* FIXME: reorder these to match <curl/curl.h> */ insint_c(d, "FILE", CURLOPT_WRITEDATA); insint_c(d, "URL", CURLOPT_URL); insint_c(d, "PORT", CURLOPT_PORT); insint_c(d, "PROXY", CURLOPT_PROXY); insint_c(d, "USERPWD", CURLOPT_USERPWD); #ifdef HAVE_CURLOPT_USERNAME insint_c(d, "USERNAME", CURLOPT_USERNAME); insint_c(d, "PASSWORD", CURLOPT_PASSWORD); #endif insint_c(d, "PROXYUSERPWD", CURLOPT_PROXYUSERPWD); #ifdef HAVE_CURLOPT_PROXYUSERNAME insint_c(d, "PROXYUSERNAME", CURLOPT_PROXYUSERNAME); insint_c(d, "PROXYPASSWORD", CURLOPT_PROXYPASSWORD); #endif insint_c(d, "RANGE", CURLOPT_RANGE); insint_c(d, "INFILE", CURLOPT_READDATA); /* ERRORBUFFER is not supported */ insint_c(d, "WRITEFUNCTION", CURLOPT_WRITEFUNCTION); insint_c(d, "READFUNCTION", CURLOPT_READFUNCTION); insint_c(d, "TIMEOUT", CURLOPT_TIMEOUT); insint_c(d, "INFILESIZE", CURLOPT_INFILESIZE_LARGE); /* _LARGE ! */ insint_c(d, "POSTFIELDS", CURLOPT_POSTFIELDS); insint_c(d, "REFERER", CURLOPT_REFERER); insint_c(d, "FTPPORT", CURLOPT_FTPPORT); insint_c(d, "USERAGENT", CURLOPT_USERAGENT); insint_c(d, "LOW_SPEED_LIMIT", CURLOPT_LOW_SPEED_LIMIT); insint_c(d, "LOW_SPEED_TIME", CURLOPT_LOW_SPEED_TIME); insint_c(d, "RESUME_FROM", CURLOPT_RESUME_FROM_LARGE); /* _LARGE ! */ insint_c(d, "WRITEDATA", CURLOPT_WRITEDATA); insint_c(d, "READDATA", CURLOPT_READDATA); insint_c(d, "PROXYPORT", CURLOPT_PROXYPORT); insint_c(d, "HTTPPROXYTUNNEL", CURLOPT_HTTPPROXYTUNNEL); insint_c(d, "VERBOSE", CURLOPT_VERBOSE); insint_c(d, "HEADER", CURLOPT_HEADER); insint_c(d, "NOPROGRESS", CURLOPT_NOPROGRESS); insint_c(d, "NOBODY", CURLOPT_NOBODY); insint_c(d, "FAILONERROR", CURLOPT_FAILONERROR); insint_c(d, "UPLOAD", CURLOPT_UPLOAD); insint_c(d, "POST", CURLOPT_POST); insint_c(d, "FTPLISTONLY", CURLOPT_FTPLISTONLY); insint_c(d, "FTPAPPEND", CURLOPT_FTPAPPEND); insint_c(d, "NETRC", CURLOPT_NETRC); insint_c(d, "FOLLOWLOCATION", CURLOPT_FOLLOWLOCATION); insint_c(d, "TRANSFERTEXT", CURLOPT_TRANSFERTEXT); insint_c(d, "PUT", CURLOPT_PUT); insint_c(d, "POSTFIELDSIZE", CURLOPT_POSTFIELDSIZE_LARGE); /* _LARGE ! */ insint_c(d, "COOKIE", CURLOPT_COOKIE); insint_c(d, "HTTPHEADER", CURLOPT_HTTPHEADER); insint_c(d, "HTTPPOST", CURLOPT_HTTPPOST); insint_c(d, "SSLCERT", CURLOPT_SSLCERT); insint_c(d, "SSLCERTPASSWD", CURLOPT_SSLCERTPASSWD); insint_c(d, "CRLF", CURLOPT_CRLF); insint_c(d, "QUOTE", CURLOPT_QUOTE); insint_c(d, "POSTQUOTE", CURLOPT_POSTQUOTE); insint_c(d, "PREQUOTE", CURLOPT_PREQUOTE); insint_c(d, "WRITEHEADER", CURLOPT_WRITEHEADER); insint_c(d, "HEADERFUNCTION", CURLOPT_HEADERFUNCTION); insint_c(d, "SEEKFUNCTION", CURLOPT_SEEKFUNCTION); insint_c(d, "COOKIEFILE", CURLOPT_COOKIEFILE); insint_c(d, "SSLVERSION", CURLOPT_SSLVERSION); insint_c(d, "TIMECONDITION", CURLOPT_TIMECONDITION); insint_c(d, "TIMEVALUE", CURLOPT_TIMEVALUE); insint_c(d, "CUSTOMREQUEST", CURLOPT_CUSTOMREQUEST); insint_c(d, "STDERR", CURLOPT_STDERR); insint_c(d, "INTERFACE", CURLOPT_INTERFACE); insint_c(d, "KRB4LEVEL", CURLOPT_KRB4LEVEL); insint_c(d, "PROGRESSFUNCTION", CURLOPT_PROGRESSFUNCTION); insint_c(d, "SSL_VERIFYPEER", CURLOPT_SSL_VERIFYPEER); insint_c(d, "CAPATH", CURLOPT_CAPATH); insint_c(d, "CAINFO", CURLOPT_CAINFO); insint_c(d, "OPT_FILETIME", CURLOPT_FILETIME); insint_c(d, "MAXREDIRS", CURLOPT_MAXREDIRS); insint_c(d, "MAXCONNECTS", CURLOPT_MAXCONNECTS); insint_c(d, "FRESH_CONNECT", CURLOPT_FRESH_CONNECT); insint_c(d, "FORBID_REUSE", CURLOPT_FORBID_REUSE); insint_c(d, "RANDOM_FILE", CURLOPT_RANDOM_FILE); insint_c(d, "EGDSOCKET", CURLOPT_EGDSOCKET); insint_c(d, "CONNECTTIMEOUT", CURLOPT_CONNECTTIMEOUT); insint_c(d, "HTTPGET", CURLOPT_HTTPGET); insint_c(d, "SSL_VERIFYHOST", CURLOPT_SSL_VERIFYHOST); insint_c(d, "COOKIEJAR", CURLOPT_COOKIEJAR); insint_c(d, "SSL_CIPHER_LIST", CURLOPT_SSL_CIPHER_LIST); insint_c(d, "HTTP_VERSION", CURLOPT_HTTP_VERSION); insint_c(d, "FTP_USE_EPSV", CURLOPT_FTP_USE_EPSV); insint_c(d, "SSLCERTTYPE", CURLOPT_SSLCERTTYPE); insint_c(d, "SSLKEY", CURLOPT_SSLKEY); insint_c(d, "SSLKEYTYPE", CURLOPT_SSLKEYTYPE); insint_c(d, "SSLKEYPASSWD", CURLOPT_SSLKEYPASSWD); insint_c(d, "SSLENGINE", CURLOPT_SSLENGINE); insint_c(d, "SSLENGINE_DEFAULT", CURLOPT_SSLENGINE_DEFAULT); insint_c(d, "DNS_CACHE_TIMEOUT", CURLOPT_DNS_CACHE_TIMEOUT); insint_c(d, "DNS_USE_GLOBAL_CACHE", CURLOPT_DNS_USE_GLOBAL_CACHE); insint_c(d, "DEBUGFUNCTION", CURLOPT_DEBUGFUNCTION); insint_c(d, "BUFFERSIZE", CURLOPT_BUFFERSIZE); insint_c(d, "NOSIGNAL", CURLOPT_NOSIGNAL); insint_c(d, "SHARE", CURLOPT_SHARE); insint_c(d, "PROXYTYPE", CURLOPT_PROXYTYPE); insint_c(d, "ENCODING", CURLOPT_ENCODING); insint_c(d, "HTTP200ALIASES", CURLOPT_HTTP200ALIASES); insint_c(d, "UNRESTRICTED_AUTH", CURLOPT_UNRESTRICTED_AUTH); insint_c(d, "FTP_USE_EPRT", CURLOPT_FTP_USE_EPRT); insint_c(d, "HTTPAUTH", CURLOPT_HTTPAUTH); insint_c(d, "FTP_CREATE_MISSING_DIRS", CURLOPT_FTP_CREATE_MISSING_DIRS); insint_c(d, "PROXYAUTH", CURLOPT_PROXYAUTH); insint_c(d, "FTP_RESPONSE_TIMEOUT", CURLOPT_FTP_RESPONSE_TIMEOUT); insint_c(d, "IPRESOLVE", CURLOPT_IPRESOLVE); insint_c(d, "MAXFILESIZE", CURLOPT_MAXFILESIZE_LARGE); /* _LARGE ! */ insint_c(d, "INFILESIZE_LARGE", CURLOPT_INFILESIZE_LARGE); insint_c(d, "RESUME_FROM_LARGE", CURLOPT_RESUME_FROM_LARGE); insint_c(d, "MAXFILESIZE_LARGE", CURLOPT_MAXFILESIZE_LARGE); insint_c(d, "NETRC_FILE", CURLOPT_NETRC_FILE); insint_c(d, "FTP_SSL", CURLOPT_FTP_SSL); insint_c(d, "POSTFIELDSIZE_LARGE", CURLOPT_POSTFIELDSIZE_LARGE); insint_c(d, "TCP_NODELAY", CURLOPT_TCP_NODELAY); insint_c(d, "FTPSSLAUTH", CURLOPT_FTPSSLAUTH); insint_c(d, "IOCTLFUNCTION", CURLOPT_IOCTLFUNCTION); insint_c(d, "IOCTLDATA", CURLOPT_IOCTLDATA); insint_c(d, "OPENSOCKETFUNCTION", CURLOPT_OPENSOCKETFUNCTION); insint_c(d, "FTP_ACCOUNT", CURLOPT_FTP_ACCOUNT); insint_c(d, "IGNORE_CONTENT_LENGTH", CURLOPT_IGNORE_CONTENT_LENGTH); insint_c(d, "COOKIELIST", CURLOPT_COOKIELIST); insint_c(d, "FTP_SKIP_PASV_IP", CURLOPT_FTP_SKIP_PASV_IP); insint_c(d, "FTP_FILEMETHOD", CURLOPT_FTP_FILEMETHOD); insint_c(d, "CONNECT_ONLY", CURLOPT_CONNECT_ONLY); insint_c(d, "LOCALPORT", CURLOPT_LOCALPORT); insint_c(d, "LOCALPORTRANGE", CURLOPT_LOCALPORTRANGE); insint_c(d, "FTP_ALTERNATIVE_TO_USER", CURLOPT_FTP_ALTERNATIVE_TO_USER); insint_c(d, "MAX_SEND_SPEED_LARGE", CURLOPT_MAX_SEND_SPEED_LARGE); insint_c(d, "MAX_RECV_SPEED_LARGE", CURLOPT_MAX_RECV_SPEED_LARGE); insint_c(d, "SSL_SESSIONID_CACHE", CURLOPT_SSL_SESSIONID_CACHE); #if LIBCURL_VERSION_NUM >= 0x072900 /* check for 7.41.0 or greater */ insint_c(d, "SSL_VERIFYSTATUS", CURLOPT_SSL_VERIFYSTATUS); #endif insint_c(d, "SSH_AUTH_TYPES", CURLOPT_SSH_AUTH_TYPES); insint_c(d, "SSH_PUBLIC_KEYFILE", CURLOPT_SSH_PUBLIC_KEYFILE); insint_c(d, "SSH_PRIVATE_KEYFILE", CURLOPT_SSH_PRIVATE_KEYFILE); #ifdef HAVE_CURL_7_19_6_OPTS insint_c(d, "SSH_KNOWNHOSTS", CURLOPT_SSH_KNOWNHOSTS); #endif insint_c(d, "FTP_SSL_CCC", CURLOPT_FTP_SSL_CCC); insint_c(d, "TIMEOUT_MS", CURLOPT_TIMEOUT_MS); insint_c(d, "CONNECTTIMEOUT_MS", CURLOPT_CONNECTTIMEOUT_MS); insint_c(d, "HTTP_TRANSFER_DECODING", CURLOPT_HTTP_TRANSFER_DECODING); insint_c(d, "HTTP_CONTENT_DECODING", CURLOPT_HTTP_CONTENT_DECODING); insint_c(d, "NEW_FILE_PERMS", CURLOPT_NEW_FILE_PERMS); insint_c(d, "NEW_DIRECTORY_PERMS", CURLOPT_NEW_DIRECTORY_PERMS); insint_c(d, "POST301", CURLOPT_POST301); insint_c(d, "PROXY_TRANSFER_MODE", CURLOPT_PROXY_TRANSFER_MODE); #if LIBCURL_VERSION_NUM >= 0x072b00 /* check for 7.43.0 or greater */ insint_c(d, "SERVICE_NAME", CURLOPT_SERVICE_NAME); insint_c(d, "PROXY_SERVICE_NAME", CURLOPT_PROXY_SERVICE_NAME); #endif insint_c(d, "COPYPOSTFIELDS", CURLOPT_COPYPOSTFIELDS); insint_c(d, "SSH_HOST_PUBLIC_KEY_MD5", CURLOPT_SSH_HOST_PUBLIC_KEY_MD5); insint_c(d, "AUTOREFERER", CURLOPT_AUTOREFERER); insint_c(d, "CRLFILE", CURLOPT_CRLFILE); insint_c(d, "ISSUERCERT", CURLOPT_ISSUERCERT); insint_c(d, "ADDRESS_SCOPE", CURLOPT_ADDRESS_SCOPE); #ifdef HAVE_CURLOPT_RESOLVE insint_c(d, "RESOLVE", CURLOPT_RESOLVE); #endif #ifdef HAVE_CURLOPT_CERTINFO insint_c(d, "OPT_CERTINFO", CURLOPT_CERTINFO); #endif #ifdef HAVE_CURLOPT_POSTREDIR insint_c(d, "POSTREDIR", CURLOPT_POSTREDIR); #endif #ifdef HAVE_CURLOPT_NOPROXY insint_c(d, "NOPROXY", CURLOPT_NOPROXY); #endif #ifdef HAVE_CURLOPT_PROTOCOLS insint_c(d, "PROTOCOLS", CURLOPT_PROTOCOLS); insint_c(d, "REDIR_PROTOCOLS", CURLOPT_REDIR_PROTOCOLS); insint_c(d, "PROTO_HTTP", CURLPROTO_HTTP); insint_c(d, "PROTO_HTTPS", CURLPROTO_HTTPS); insint_c(d, "PROTO_FTP", CURLPROTO_FTP); insint_c(d, "PROTO_FTPS", CURLPROTO_FTPS); insint_c(d, "PROTO_SCP", CURLPROTO_SCP); insint_c(d, "PROTO_SFTP", CURLPROTO_SFTP); insint_c(d, "PROTO_TELNET", CURLPROTO_TELNET); insint_c(d, "PROTO_LDAP", CURLPROTO_LDAP); insint_c(d, "PROTO_LDAPS", CURLPROTO_LDAPS); insint_c(d, "PROTO_DICT", CURLPROTO_DICT); insint_c(d, "PROTO_FILE", CURLPROTO_FILE); insint_c(d, "PROTO_TFTP", CURLPROTO_TFTP); #ifdef HAVE_CURL_7_20_0_OPTS insint_c(d, "PROTO_IMAP", CURLPROTO_IMAP); insint_c(d, "PROTO_IMAPS", CURLPROTO_IMAPS); insint_c(d, "PROTO_POP3", CURLPROTO_POP3); insint_c(d, "PROTO_POP3S", CURLPROTO_POP3S); insint_c(d, "PROTO_SMTP", CURLPROTO_SMTP); insint_c(d, "PROTO_SMTPS", CURLPROTO_SMTPS); #endif #ifdef HAVE_CURL_7_21_0_OPTS insint_c(d, "PROTO_RTSP", CURLPROTO_RTSP); insint_c(d, "PROTO_RTMP", CURLPROTO_RTMP); insint_c(d, "PROTO_RTMPT", CURLPROTO_RTMPT); insint_c(d, "PROTO_RTMPE", CURLPROTO_RTMPE); insint_c(d, "PROTO_RTMPTE", CURLPROTO_RTMPTE); insint_c(d, "PROTO_RTMPS", CURLPROTO_RTMPS); insint_c(d, "PROTO_RTMPTS", CURLPROTO_RTMPTS); #endif #ifdef HAVE_CURL_7_21_2_OPTS insint_c(d, "PROTO_GOPHER", CURLPROTO_GOPHER); #endif insint_c(d, "PROTO_ALL", CURLPROTO_ALL); #endif #ifdef HAVE_CURL_7_19_4_OPTS insint_c(d, "TFTP_BLKSIZE", CURLOPT_TFTP_BLKSIZE); insint_c(d, "SOCKS5_GSSAPI_SERVICE", CURLOPT_SOCKS5_GSSAPI_SERVICE); insint_c(d, "SOCKS5_GSSAPI_NEC", CURLOPT_SOCKS5_GSSAPI_NEC); #endif #ifdef HAVE_CURL_7_20_0_OPTS insint_c(d, "MAIL_FROM", CURLOPT_MAIL_FROM); insint_c(d, "MAIL_RCPT", CURLOPT_MAIL_RCPT); #endif #ifdef HAVE_CURL_7_25_0_OPTS insint_c(d, "MAIL_AUTH", CURLOPT_MAIL_AUTH); #endif #if LIBCURL_VERSION_NUM >= 0x072700 /* check for 7.39.0 or greater */ insint_c(d, "PINNEDPUBLICKEY", CURLOPT_PINNEDPUBLICKEY); #endif insint_m(d, "M_TIMERFUNCTION", CURLMOPT_TIMERFUNCTION); insint_m(d, "M_SOCKETFUNCTION", CURLMOPT_SOCKETFUNCTION); insint_m(d, "M_PIPELINING", CURLMOPT_PIPELINING); insint_m(d, "M_MAXCONNECTS", CURLMOPT_MAXCONNECTS); #ifdef HAVE_CURL_7_30_0_PIPELINE_OPTS insint_m(d, "M_MAX_HOST_CONNECTIONS", CURLMOPT_MAX_HOST_CONNECTIONS); insint_m(d, "M_MAX_TOTAL_CONNECTIONS", CURLMOPT_MAX_TOTAL_CONNECTIONS); insint_m(d, "M_MAX_PIPELINE_LENGTH", CURLMOPT_MAX_PIPELINE_LENGTH); insint_m(d, "M_CONTENT_LENGTH_PENALTY_SIZE", CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE); insint_m(d, "M_CHUNK_LENGTH_PENALTY_SIZE", CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE); #endif /* constants for setopt(IPRESOLVE, x) */ insint_c(d, "IPRESOLVE_WHATEVER", CURL_IPRESOLVE_WHATEVER); insint_c(d, "IPRESOLVE_V4", CURL_IPRESOLVE_V4); insint_c(d, "IPRESOLVE_V6", CURL_IPRESOLVE_V6); /* constants for setopt(HTTP_VERSION, x) */ insint_c(d, "CURL_HTTP_VERSION_NONE", CURL_HTTP_VERSION_NONE); insint_c(d, "CURL_HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0); insint_c(d, "CURL_HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1); insint_c(d, "CURL_HTTP_VERSION_LAST", CURL_HTTP_VERSION_LAST); /* CURL_NETRC_OPTION: constants for setopt(NETRC, x) */ insint_c(d, "NETRC_OPTIONAL", CURL_NETRC_OPTIONAL); insint_c(d, "NETRC_IGNORED", CURL_NETRC_IGNORED); insint_c(d, "NETRC_REQUIRED", CURL_NETRC_REQUIRED); /* constants for setopt(SSLVERSION, x) */ insint_c(d, "SSLVERSION_DEFAULT", CURL_SSLVERSION_DEFAULT); insint_c(d, "SSLVERSION_SSLv2", CURL_SSLVERSION_SSLv2); insint_c(d, "SSLVERSION_SSLv3", CURL_SSLVERSION_SSLv3); insint_c(d, "SSLVERSION_TLSv1", CURL_SSLVERSION_TLSv1); #if LIBCURL_VERSION_NUM >= 0x072200 /* check for 7.34.0 or greater */ insint_c(d, "SSLVERSION_TLSv1_0", CURL_SSLVERSION_TLSv1_0); insint_c(d, "SSLVERSION_TLSv1_1", CURL_SSLVERSION_TLSv1_1); insint_c(d, "SSLVERSION_TLSv1_2", CURL_SSLVERSION_TLSv1_2); #endif /* curl_TimeCond: constants for setopt(TIMECONDITION, x) */ insint_c(d, "TIMECONDITION_NONE", CURL_TIMECOND_NONE); insint_c(d, "TIMECONDITION_IFMODSINCE", CURL_TIMECOND_IFMODSINCE); insint_c(d, "TIMECONDITION_IFUNMODSINCE", CURL_TIMECOND_IFUNMODSINCE); insint_c(d, "TIMECONDITION_LASTMOD", CURL_TIMECOND_LASTMOD); /* constants for setopt(CURLOPT_SSH_AUTH_TYPES, x) */ insint_c(d, "SSH_AUTH_ANY", CURLSSH_AUTH_ANY); insint_c(d, "SSH_AUTH_NONE", CURLSSH_AUTH_NONE); insint_c(d, "SSH_AUTH_PUBLICKEY", CURLSSH_AUTH_PUBLICKEY); insint_c(d, "SSH_AUTH_PASSWORD", CURLSSH_AUTH_PASSWORD); insint_c(d, "SSH_AUTH_HOST", CURLSSH_AUTH_HOST); insint_c(d, "SSH_AUTH_KEYBOARD", CURLSSH_AUTH_KEYBOARD); insint_c(d, "SSH_AUTH_DEFAULT", CURLSSH_AUTH_DEFAULT); /* CURLINFO: symbolic constants for getinfo(x) */ insint_c(d, "EFFECTIVE_URL", CURLINFO_EFFECTIVE_URL); insint_c(d, "HTTP_CODE", CURLINFO_HTTP_CODE); insint_c(d, "RESPONSE_CODE", CURLINFO_HTTP_CODE); insint_c(d, "TOTAL_TIME", CURLINFO_TOTAL_TIME); insint_c(d, "NAMELOOKUP_TIME", CURLINFO_NAMELOOKUP_TIME); insint_c(d, "CONNECT_TIME", CURLINFO_CONNECT_TIME); insint_c(d, "APPCONNECT_TIME", CURLINFO_APPCONNECT_TIME); insint_c(d, "PRETRANSFER_TIME", CURLINFO_PRETRANSFER_TIME); insint_c(d, "SIZE_UPLOAD", CURLINFO_SIZE_UPLOAD); insint_c(d, "SIZE_DOWNLOAD", CURLINFO_SIZE_DOWNLOAD); insint_c(d, "SPEED_DOWNLOAD", CURLINFO_SPEED_DOWNLOAD); insint_c(d, "SPEED_UPLOAD", CURLINFO_SPEED_UPLOAD); insint_c(d, "HEADER_SIZE", CURLINFO_HEADER_SIZE); insint_c(d, "REQUEST_SIZE", CURLINFO_REQUEST_SIZE); insint_c(d, "SSL_VERIFYRESULT", CURLINFO_SSL_VERIFYRESULT); insint_c(d, "INFO_FILETIME", CURLINFO_FILETIME); insint_c(d, "CONTENT_LENGTH_DOWNLOAD", CURLINFO_CONTENT_LENGTH_DOWNLOAD); insint_c(d, "CONTENT_LENGTH_UPLOAD", CURLINFO_CONTENT_LENGTH_UPLOAD); insint_c(d, "STARTTRANSFER_TIME", CURLINFO_STARTTRANSFER_TIME); insint_c(d, "CONTENT_TYPE", CURLINFO_CONTENT_TYPE); insint_c(d, "REDIRECT_TIME", CURLINFO_REDIRECT_TIME); insint_c(d, "REDIRECT_COUNT", CURLINFO_REDIRECT_COUNT); insint_c(d, "REDIRECT_URL", CURLINFO_REDIRECT_URL); insint_c(d, "PRIMARY_IP", CURLINFO_PRIMARY_IP); #ifdef HAVE_CURLINFO_PRIMARY_PORT insint_c(d, "PRIMARY_PORT", CURLINFO_PRIMARY_PORT); #endif #ifdef HAVE_CURLINFO_LOCAL_IP insint_c(d, "LOCAL_IP", CURLINFO_LOCAL_IP); #endif #ifdef HAVE_CURLINFO_LOCAL_PORT insint_c(d, "LOCAL_PORT", CURLINFO_LOCAL_PORT); #endif insint_c(d, "HTTP_CONNECTCODE", CURLINFO_HTTP_CONNECTCODE); insint_c(d, "HTTPAUTH_AVAIL", CURLINFO_HTTPAUTH_AVAIL); insint_c(d, "PROXYAUTH_AVAIL", CURLINFO_PROXYAUTH_AVAIL); insint_c(d, "OS_ERRNO", CURLINFO_OS_ERRNO); insint_c(d, "NUM_CONNECTS", CURLINFO_NUM_CONNECTS); insint_c(d, "SSL_ENGINES", CURLINFO_SSL_ENGINES); insint_c(d, "INFO_COOKIELIST", CURLINFO_COOKIELIST); insint_c(d, "LASTSOCKET", CURLINFO_LASTSOCKET); insint_c(d, "FTP_ENTRY_PATH", CURLINFO_FTP_ENTRY_PATH); #ifdef HAVE_CURLOPT_CERTINFO insint_c(d, "INFO_CERTINFO", CURLINFO_CERTINFO); #endif #ifdef HAVE_CURL_7_19_4_OPTS insint_c(d, "CONDITION_UNMET", CURLINFO_CONDITION_UNMET); #endif /* CURLPAUSE: symbolic constants for pause(bitmask) */ insint_c(d, "PAUSE_RECV", CURLPAUSE_RECV); insint_c(d, "PAUSE_SEND", CURLPAUSE_SEND); insint_c(d, "PAUSE_ALL", CURLPAUSE_ALL); insint_c(d, "PAUSE_CONT", CURLPAUSE_CONT); #ifdef HAVE_CURL_7_19_5_OPTS /* CURL_SEEKFUNC: return values for seek function */ insint_c(d, "SEEKFUNC_OK", CURL_SEEKFUNC_OK); insint_c(d, "SEEKFUNC_FAIL", CURL_SEEKFUNC_FAIL); insint_c(d, "SEEKFUNC_CANTSEEK", CURL_SEEKFUNC_CANTSEEK); #endif #ifdef HAVE_CURLOPT_DNS_SERVERS insint_c(d, "DNS_SERVERS", CURLOPT_DNS_SERVERS); #endif #ifdef HAVE_CURLOPT_POSTREDIR insint_c(d, "REDIR_POST_301", CURL_REDIR_POST_301); insint_c(d, "REDIR_POST_302", CURL_REDIR_POST_302); # ifdef HAVE_CURL_REDIR_POST_303 insint_c(d, "REDIR_POST_303", CURL_REDIR_POST_303); # endif insint_c(d, "REDIR_POST_ALL", CURL_REDIR_POST_ALL); #endif /* options for global_init() */ insint(d, "GLOBAL_SSL", CURL_GLOBAL_SSL); insint(d, "GLOBAL_WIN32", CURL_GLOBAL_WIN32); insint(d, "GLOBAL_ALL", CURL_GLOBAL_ALL); insint(d, "GLOBAL_NOTHING", CURL_GLOBAL_NOTHING); insint(d, "GLOBAL_DEFAULT", CURL_GLOBAL_DEFAULT); #ifdef CURL_GLOBAL_ACK_EINTR /* CURL_GLOBAL_ACK_EINTR was introduced in libcurl-7.30.0 */ insint(d, "GLOBAL_ACK_EINTR", CURL_GLOBAL_ACK_EINTR); #endif /* constants for curl_multi_socket interface */ insint(d, "CSELECT_IN", CURL_CSELECT_IN); insint(d, "CSELECT_OUT", CURL_CSELECT_OUT); insint(d, "CSELECT_ERR", CURL_CSELECT_ERR); insint(d, "SOCKET_TIMEOUT", CURL_SOCKET_TIMEOUT); insint(d, "POLL_NONE", CURL_POLL_NONE); insint(d, "POLL_IN", CURL_POLL_IN); insint(d, "POLL_OUT", CURL_POLL_OUT); insint(d, "POLL_INOUT", CURL_POLL_INOUT); insint(d, "POLL_REMOVE", CURL_POLL_REMOVE); /* curl_lock_data: XXX do we need this in pycurl ??? */ /* curl_lock_access: XXX do we need this in pycurl ??? */ /* CURLSHcode: XXX do we need this in pycurl ??? */ /* CURLSHoption: XXX do we need this in pycurl ??? */ /* CURLversion: constants for curl_version_info(x) */ #if 0 /* XXX - do we need these ?? */ insint(d, "VERSION_FIRST", CURLVERSION_FIRST); insint(d, "VERSION_SECOND", CURLVERSION_SECOND); insint(d, "VERSION_THIRD", CURLVERSION_THIRD); insint(d, "VERSION_NOW", CURLVERSION_NOW); #endif /* version features - bitmasks for curl_version_info_data.features */ #if 0 /* XXX - do we need these ?? */ /* XXX - should we really rename these ?? */ insint(d, "VERSION_FEATURE_IPV6", CURL_VERSION_IPV6); insint(d, "VERSION_FEATURE_KERBEROS4", CURL_VERSION_KERBEROS4); insint(d, "VERSION_FEATURE_SSL", CURL_VERSION_SSL); insint(d, "VERSION_FEATURE_LIBZ", CURL_VERSION_LIBZ); insint(d, "VERSION_FEATURE_NTLM", CURL_VERSION_NTLM); insint(d, "VERSION_FEATURE_GSSNEGOTIATE", CURL_VERSION_GSSNEGOTIATE); insint(d, "VERSION_FEATURE_DEBUG", CURL_VERSION_DEBUG); insint(d, "VERSION_FEATURE_ASYNCHDNS", CURL_VERSION_ASYNCHDNS); insint(d, "VERSION_FEATURE_SPNEGO", CURL_VERSION_SPNEGO); insint(d, "VERSION_FEATURE_LARGEFILE", CURL_VERSION_LARGEFILE); insint(d, "VERSION_FEATURE_IDN", CURL_VERSION_IDN); #endif /** ** the order of these constants mostly follows <curl/multi.h> **/ /* CURLMcode: multi error codes */ insint_m(d, "E_CALL_MULTI_PERFORM", CURLM_CALL_MULTI_PERFORM); insint_m(d, "E_MULTI_OK", CURLM_OK); insint_m(d, "E_MULTI_BAD_HANDLE", CURLM_BAD_HANDLE); insint_m(d, "E_MULTI_BAD_EASY_HANDLE", CURLM_BAD_EASY_HANDLE); insint_m(d, "E_MULTI_OUT_OF_MEMORY", CURLM_OUT_OF_MEMORY); insint_m(d, "E_MULTI_INTERNAL_ERROR", CURLM_INTERNAL_ERROR); /* curl shared constants */ insint_s(d, "SH_SHARE", CURLSHOPT_SHARE); insint_s(d, "SH_UNSHARE", CURLSHOPT_UNSHARE); insint_s(d, "LOCK_DATA_COOKIE", CURL_LOCK_DATA_COOKIE); insint_s(d, "LOCK_DATA_DNS", CURL_LOCK_DATA_DNS); insint_s(d, "LOCK_DATA_SSL_SESSION", CURL_LOCK_DATA_SSL_SESSION); /* Initialize callback locks if ssl is enabled */ #if defined(PYCURL_NEED_SSL_TSL) pycurl_ssl_init(); #endif #ifdef WITH_THREAD /* Finally initialize global interpreter lock */ PyEval_InitThreads(); #endif #if PY_MAJOR_VERSION >= 3 return m; #else PYCURL_MODINIT_RETURN_NULL; #endif error: Py_XDECREF(curlobject_constants); Py_XDECREF(curlmultiobject_constants); Py_XDECREF(curlshareobject_constants); Py_XDECREF(ErrorObject); PyMem_Free(g_pycurl_useragent); if (!PyErr_Occurred()) PyErr_SetString(PyExc_ImportError, "curl module init failed"); PYCURL_MODINIT_RETURN_NULL; }
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 = PyUnicode_FromString(buf); done: PyMem_Free(im); PyMem_Free(pre); PyMem_Free(buf); return result; }
static wchar_t* char2wchar(char* arg) { wchar_t *res; #ifdef HAVE_BROKEN_MBSTOWCS /* Some platforms have a broken implementation of * mbstowcs which does not count the characters that * would result from conversion. Use an upper bound. */ size_t argsize = strlen(arg); #else size_t argsize = mbstowcs(NULL, arg, 0); #endif size_t count; unsigned char *in; wchar_t *out; #ifdef HAVE_MBRTOWC mbstate_t mbs; #endif if (argsize != (size_t)-1) { res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t)); if (!res) goto oom; count = mbstowcs(res, arg, argsize+1); if (count != (size_t)-1) { wchar_t *tmp; /* Only use the result if it contains no surrogate characters. */ for (tmp = res; *tmp != 0 && (*tmp < 0xd800 || *tmp > 0xdfff); tmp++) ; if (*tmp == 0) return res; } PyMem_Free(res); } /* Conversion failed. Fall back to escaping with surrogateescape. */ #ifdef HAVE_MBRTOWC /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ /* Overallocate; as multi-byte characters are in the argument, the actual output could use less memory. */ argsize = strlen(arg) + 1; res = PyMem_Malloc(argsize*sizeof(wchar_t)); if (!res) goto oom; in = (unsigned char*)arg; out = res; memset(&mbs, 0, sizeof mbs); while (argsize) { size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); if (converted == 0) /* Reached end of string; null char stored. */ break; if (converted == (size_t)-2) { /* Incomplete character. This should never happen, since we provide everything that we have - unless there is a bug in the C library, or I misunderstood how mbrtowc works. */ fprintf(stderr, "unexpected mbrtowc result -2\n"); return NULL; } if (converted == (size_t)-1) { /* Conversion error. Escape as UTF-8b, and start over in the initial shift state. */ *out++ = 0xdc00 + *in++; argsize--; memset(&mbs, 0, sizeof mbs); continue; } if (*out >= 0xd800 && *out <= 0xdfff) { /* Surrogate character. Escape the original byte sequence with surrogateescape. */ argsize -= converted; while (converted--) *out++ = 0xdc00 + *in++; continue; } /* successfully converted some bytes */ in += converted; argsize -= converted; out++; } #else /* Cannot use C locale for escaping; manually escape as if charset is ASCII (i.e. escape all bytes > 128. This will still roundtrip correctly in the locale's charset, which must be an ASCII superset. */ res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t)); if (!res) goto oom; in = (unsigned char*)arg; out = res; while(*in) if(*in < 128) *out++ = *in++; else *out++ = 0xdc00 + *in++; *out = 0; #endif return res; oom: fprintf(stderr, "out of memory\n"); return NULL; }
PyObject * PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj) { Py_ssize_t i, j, codelen; int nops, h, adj; int tgt, tgttgt, opcode; unsigned char *codestr = NULL; unsigned char *lineno; int *addrmap = NULL; int new_line, cum_orig_line, last_line, tabsiz; PyObject **const_stack = NULL; Py_ssize_t *load_const_stack = NULL; Py_ssize_t const_stack_top = -1; Py_ssize_t const_stack_size = 0; int in_consts = 0; /* whether we are in a LOAD_CONST sequence */ unsigned int *blocks = NULL; char *name; /* Bail out if an exception is set */ if (PyErr_Occurred()) goto exitError; /* Bypass optimization when the lineno table is too complex */ assert(PyBytes_Check(lineno_obj)); lineno = (unsigned char*)PyBytes_AS_STRING(lineno_obj); tabsiz = PyBytes_GET_SIZE(lineno_obj); if (memchr(lineno, 255, tabsiz) != NULL) goto exitUnchanged; /* Avoid situations where jump retargeting could overflow */ assert(PyBytes_Check(code)); codelen = PyBytes_GET_SIZE(code); if (codelen > 32700) goto exitUnchanged; /* Make a modifiable copy of the code string */ codestr = (unsigned char *)PyMem_Malloc(codelen); if (codestr == NULL) goto exitError; codestr = (unsigned char *)memcpy(codestr, PyBytes_AS_STRING(code), codelen); /* Verify that RETURN_VALUE terminates the codestring. This allows the various transformation patterns to look ahead several instructions without additional checks to make sure they are not looking beyond the end of the code string. */ if (codestr[codelen-1] != RETURN_VALUE) goto exitUnchanged; /* Mapping to new jump targets after NOPs are removed */ addrmap = (int *)PyMem_Malloc(codelen * sizeof(int)); if (addrmap == NULL) goto exitError; blocks = markblocks(codestr, codelen); if (blocks == NULL) goto exitError; assert(PyList_Check(consts)); CONST_STACK_CREATE(); for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) { reoptimize_current: opcode = codestr[i]; if (!in_consts) { CONST_STACK_RESET(); } in_consts = 0; switch (opcode) { /* Replace UNARY_NOT POP_JUMP_IF_FALSE with POP_JUMP_IF_TRUE */ case UNARY_NOT: if (codestr[i+1] != POP_JUMP_IF_FALSE || !ISBASICBLOCK(blocks,i,4)) continue; j = GETARG(codestr, i+1); codestr[i] = POP_JUMP_IF_TRUE; SETARG(codestr, i, j); codestr[i+3] = NOP; goto reoptimize_current; /* not a is b --> a is not b not a in b --> a not in b not a is not b --> a is b not a not in b --> a in b */ case COMPARE_OP: j = GETARG(codestr, i); if (j < 6 || j > 9 || codestr[i+3] != UNARY_NOT || !ISBASICBLOCK(blocks,i,4)) continue; SETARG(codestr, i, (j^1)); codestr[i+3] = NOP; break; /* Replace LOAD_GLOBAL/LOAD_NAME None/True/False with LOAD_CONST None/True/False */ case LOAD_NAME: case LOAD_GLOBAL: j = GETARG(codestr, i); name = _PyUnicode_AsString(PyTuple_GET_ITEM(names, j)); h = load_global(codestr, i, name, consts); if (h < 0) goto exitError; else if (h == 0) continue; CONST_STACK_PUSH_OP(i); break; /* Skip over LOAD_CONST trueconst POP_JUMP_IF_FALSE xx. This improves "while 1" performance. */ case LOAD_CONST: CONST_STACK_PUSH_OP(i); j = GETARG(codestr, i); if (codestr[i+3] != POP_JUMP_IF_FALSE || !ISBASICBLOCK(blocks,i,6) || !PyObject_IsTrue(PyList_GET_ITEM(consts, j))) continue; memset(codestr+i, NOP, 6); CONST_STACK_RESET(); break; /* Try to fold tuples of constants (includes a case for lists and sets which are only used for "in" and "not in" tests). Skip over BUILD_SEQN 1 UNPACK_SEQN 1. Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ case BUILD_TUPLE: case BUILD_LIST: case BUILD_SET: j = GETARG(codestr, i); if (j == 0) break; h = CONST_STACK_OP_LASTN(j); assert((h >= 0 || CONST_STACK_LEN() < j)); if (h >= 0 && j > 0 && j <= CONST_STACK_LEN() && ((opcode == BUILD_TUPLE && ISBASICBLOCK(blocks, h, i-h+3)) || ((opcode == BUILD_LIST || opcode == BUILD_SET) && codestr[i+3]==COMPARE_OP && ISBASICBLOCK(blocks, h, i-h+6) && (GETARG(codestr,i+3)==6 || GETARG(codestr,i+3)==7))) && tuple_of_constants(&codestr[i], j, consts, CONST_STACK_LASTN(j))) { assert(codestr[i] == LOAD_CONST); memset(&codestr[h], NOP, i - h); CONST_STACK_POP(j); CONST_STACK_PUSH_OP(i); break; } if (codestr[i+3] != UNPACK_SEQUENCE || !ISBASICBLOCK(blocks,i,6) || j != GETARG(codestr, i+3) || opcode == BUILD_SET) continue; if (j == 1) { memset(codestr+i, NOP, 6); } else if (j == 2) { codestr[i] = ROT_TWO; memset(codestr+i+1, NOP, 5); CONST_STACK_RESET(); } else if (j == 3) { codestr[i] = ROT_THREE; codestr[i+1] = ROT_TWO; memset(codestr+i+2, NOP, 4); CONST_STACK_RESET(); } break; /* Fold binary ops on constants. LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */ case BINARY_POWER: case BINARY_MULTIPLY: case BINARY_TRUE_DIVIDE: case BINARY_FLOOR_DIVIDE: case BINARY_MODULO: case BINARY_ADD: case BINARY_SUBTRACT: case BINARY_SUBSCR: case BINARY_LSHIFT: case BINARY_RSHIFT: case BINARY_AND: case BINARY_XOR: case BINARY_OR: /* NOTE: LOAD_CONST is saved at `i-2` since it has an arg while BINOP hasn't */ h = CONST_STACK_OP_LASTN(2); assert((h >= 0 || CONST_STACK_LEN() < 2)); if (h >= 0 && ISBASICBLOCK(blocks, h, i-h+1) && fold_binops_on_constants(&codestr[i], consts, CONST_STACK_LASTN(2))) { i -= 2; memset(&codestr[h], NOP, i - h); assert(codestr[i] == LOAD_CONST); CONST_STACK_POP(2); CONST_STACK_PUSH_OP(i); } break; /* Fold unary ops on constants. LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */ case UNARY_NEGATIVE: case UNARY_INVERT: case UNARY_POSITIVE: h = CONST_STACK_OP_LASTN(1); assert((h >= 0 || CONST_STACK_LEN() < 1)); if (h >= 0 && ISBASICBLOCK(blocks, h, i-h+1) && fold_unaryops_on_constants(&codestr[i-3], consts, CONST_STACK_TOP())) { i -= 2; assert(codestr[i] == LOAD_CONST); CONST_STACK_POP(1); CONST_STACK_PUSH_OP(i); } break; /* Simplify conditional jump to conditional jump where the result of the first test implies the success of a similar test or the failure of the opposite test. Arises in code like: "if a and b:" "if a or b:" "a and b or c" "(a and b) and c" x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z --> x:JUMP_IF_FALSE_OR_POP z x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z --> x:POP_JUMP_IF_FALSE y+3 where y+3 is the instruction following the second test. */ case JUMP_IF_FALSE_OR_POP: case JUMP_IF_TRUE_OR_POP: tgt = GETJUMPTGT(codestr, i); j = codestr[tgt]; if (CONDITIONAL_JUMP(j)) { /* NOTE: all possible jumps here are absolute! */ if (JUMPS_ON_TRUE(j) == JUMPS_ON_TRUE(opcode)) { /* The second jump will be taken iff the first is. */ tgttgt = GETJUMPTGT(codestr, tgt); /* The current opcode inherits its target's stack behaviour */ codestr[i] = j; SETARG(codestr, i, tgttgt); goto reoptimize_current; } else { /* The second jump is not taken if the first is (so jump past it), and all conditional jumps pop their argument when they're not taken (so change the first jump to pop its argument when it's taken). */ if (JUMPS_ON_TRUE(opcode)) codestr[i] = POP_JUMP_IF_TRUE; else codestr[i] = POP_JUMP_IF_FALSE; SETARG(codestr, i, (tgt + 3)); goto reoptimize_current; } } /* Intentional fallthrough */ /* Replace jumps to unconditional jumps */ case POP_JUMP_IF_FALSE: case POP_JUMP_IF_TRUE: case FOR_ITER: case JUMP_FORWARD: case JUMP_ABSOLUTE: case CONTINUE_LOOP: case SETUP_LOOP: case SETUP_EXCEPT: case SETUP_FINALLY: case SETUP_WITH: tgt = GETJUMPTGT(codestr, i); /* Replace JUMP_* to a RETURN into just a RETURN */ if (UNCONDITIONAL_JUMP(opcode) && codestr[tgt] == RETURN_VALUE) { codestr[i] = RETURN_VALUE; memset(codestr+i+1, NOP, 2); continue; } if (!UNCONDITIONAL_JUMP(codestr[tgt])) continue; tgttgt = GETJUMPTGT(codestr, tgt); if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */ opcode = JUMP_ABSOLUTE; if (!ABSOLUTE_JUMP(opcode)) tgttgt -= i + 3; /* Calc relative jump addr */ if (tgttgt < 0) /* No backward relative jumps */ continue; codestr[i] = opcode; SETARG(codestr, i, tgttgt); break; case EXTENDED_ARG: if (codestr[i+3] != MAKE_FUNCTION) goto exitUnchanged; /* don't visit MAKE_FUNCTION as GETARG will be wrong */ i += 3; break; /* Replace RETURN LOAD_CONST None RETURN with just RETURN */ /* Remove unreachable JUMPs after RETURN */ case RETURN_VALUE: if (i+4 >= codelen) continue; if (codestr[i+4] == RETURN_VALUE && ISBASICBLOCK(blocks,i,5)) memset(codestr+i+1, NOP, 4); else if (UNCONDITIONAL_JUMP(codestr[i+1]) && ISBASICBLOCK(blocks,i,4)) memset(codestr+i+1, NOP, 3); break; } } /* Fixup linenotab */ for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) { addrmap[i] = i - nops; if (codestr[i] == NOP) nops++; } cum_orig_line = 0; last_line = 0; for (i=0 ; i < tabsiz ; i+=2) { cum_orig_line += lineno[i]; new_line = addrmap[cum_orig_line]; assert (new_line - last_line < 255); lineno[i] =((unsigned char)(new_line - last_line)); last_line = new_line; } /* Remove NOPs and fixup jump targets */ for (i=0, h=0 ; i<codelen ; ) { opcode = codestr[i]; switch (opcode) { case NOP: i++; continue; case JUMP_ABSOLUTE: case CONTINUE_LOOP: case POP_JUMP_IF_FALSE: case POP_JUMP_IF_TRUE: case JUMP_IF_FALSE_OR_POP: case JUMP_IF_TRUE_OR_POP: j = addrmap[GETARG(codestr, i)]; SETARG(codestr, i, j); break; case FOR_ITER: case JUMP_FORWARD: case SETUP_LOOP: case SETUP_EXCEPT: case SETUP_FINALLY: case SETUP_WITH: j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3; SETARG(codestr, i, j); break; } adj = CODESIZE(opcode); while (adj--) codestr[h++] = codestr[i++]; } assert(h + nops == codelen); code = PyBytes_FromStringAndSize((char *)codestr, h); CONST_STACK_DELETE(); PyMem_Free(addrmap); PyMem_Free(codestr); PyMem_Free(blocks); return code; exitError: code = NULL; exitUnchanged: CONST_STACK_DELETE(); if (blocks != NULL) PyMem_Free(blocks); if (addrmap != NULL) PyMem_Free(addrmap); if (codestr != NULL) PyMem_Free(codestr); Py_XINCREF(code); return code; }
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); buf = *localtime(&tt); } else if (!gettmarg(tup, &buf) || !checktm(&buf)) return NULL; /* 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_AsEncodedString(format_arg, TZNAME_ENCODING, NULL); 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 = wcschr(fmt, L'%'); outbuf != NULL; outbuf = wcschr(outbuf+2, L'%')) { if (outbuf[1]=='#') ++outbuf; /* not documented by python, */ if (outbuf[1]=='\0' || !wcschr(L"aAbBcdfHIjmMpSUwWxXyYzZ%", outbuf[1])) { PyErr_SetString(PyExc_ValueError, "Invalid format string"); return 0; } } #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) { outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char)); if (outbuf == NULL) { PyErr_NoMemory(); break; } buflen = format_time(outbuf, i, fmt, &buf); 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_Decode(outbuf, buflen, TZNAME_ENCODING, NULL); #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 && errno == EINVAL) { PyErr_SetString(PyExc_ValueError, "Invalid format string"); break; } #endif } #ifdef HAVE_WCSFTIME PyMem_Free(format); #else Py_DECREF(format); #endif return ret; }
int splat_signal_init(struct splat_signal *s, size_t length, size_t origin, PyObject **signals, size_t n_signals, unsigned rate) { size_t i; s->origin = origin; s->length = length + s->origin; s->n_vectors = n_signals; s->vectors = PyMem_Malloc(n_signals * sizeof(struct splat_vector)); s->rate = rate; if (s->vectors == NULL) { PyErr_NoMemory(); return -1; } s->py_float = PyFloat_FromDouble(0); if (s->py_float == NULL) { PyErr_SetString(PyExc_AssertionError, "Failed to create float object"); return -1; } s->py_args = PyTuple_New(1); if (s->py_args == NULL) { PyErr_NoMemory(); return -1; } PyTuple_SET_ITEM(s->py_args, 0, s->py_float); for (i = 0; i < n_signals; ++i) { struct splat_vector *v = &s->vectors[i]; PyObject *signal = signals[i]; struct splat_spline *spline = splat_spline_from_obj(signal); struct splat_fragment *frag = splat_frag_from_obj(signal); if (PyFloat_Check(signal)) { const sample_t value = PyFloat_AS_DOUBLE(signal); size_t j; for (j = 0; j < SPLAT_VECTOR_LEN; ++j) v->data[j] = value; v->signal = NULL; } else if (PyCallable_Check(signal)) { v->signal = splat_signal_func; } else if (frag != NULL) { if (frag->n_channels != 1) { PyErr_SetString(PyExc_ValueError, "Fragment signal must have only 1 channel"); return -1; } if (s->length > frag->length) { PyErr_SetString(PyExc_ValueError, "Fragment signal length too short"); return -1; } v->signal = splat_signal_frag; } else if (spline != NULL) { size_t spline_length = spline->end * rate; if (s->length > spline_length) { PyErr_SetString(PyExc_ValueError, "Spline signal length too short"); return -1; } v->signal = splat_signal_spline; } else { PyErr_SetString(PyExc_TypeError, "unsupported signal type"); return -1; } v->obj = signal; } s->cur = s->origin; s->end = 0; s->len = 0; s->stat = SPLAT_SIGNAL_CONTINUE; return 0; }
/* the following three adapter functions are used as arguments to picosat_minit, such that picosat used the Python memory manager */ inline static void *py_malloc(void *mmgr, size_t bytes) { return PyMem_Malloc(bytes); }
/** * Parses calibration data. The calibration data should be in the form of a * sequence of sequences. eg: * [ * [ xs, ys, zs, xi, yi ], ... * ] * where (xs, ys, zs) are the coordinates of a point in 3D space, and (xi, yi) * are the coordinates of the corresponding point in an image. * * If the method fails, it returns NULL and raises an appropriate exception. * On success, a flat array of doubles will be PyMem_Malloc()'d and filled * with the calibration data: * { xs1, ys1, zs1, xi1, yi1, xs2, ys2, zs2, xi2, yi2, ... } * size will be populated with the number of calibration points. */ static double* parse_calibration_data(PyObject *pyobj, int *size) { PyObject *subseq = NULL, *sstuple = NULL; int i = 0, ncoords = 0, index = 0; double *array = NULL, xs, ys, zs, xi, yi; /* check that pyobj is a sequence, and find out its size */ if (PySequence_Check(pyobj) == 0) { PyErr_SetString(PyExc_TypeError, "First argument must be a sequence of coordinate " \ "sequences."); return NULL; } ncoords = PySequence_Size(pyobj); *size = ncoords; /* allocate memory */ array = PyMem_Malloc(sizeof(double) * ncoords * 5); if (array == NULL) return NULL; /* iterate over each sub-sequence within pyobj, performing appropriate * checks and fetching data. */ for (i = 0; i < ncoords; i++) { subseq = PySequence_GetItem(pyobj, i); if (PySequence_Check(subseq) == 0) { Py_DECREF(subseq); PyMem_Free(array); PyErr_SetString(PyExc_TypeError, "First argument must be a sequence of " \ "coordinate sequences."); return NULL; } sstuple = PySequence_Tuple(subseq); Py_DECREF(subseq); if (sstuple == NULL) { Py_DECREF(sstuple); PyMem_Free(array); PyErr_SetString(PyExc_TypeError, "First argument must be a sequence of " \ "coordinate sequences."); return NULL; } if (!PyArg_ParseTuple(sstuple, "ddddd", &xs, &ys, &zs, &xi, &yi)) { Py_DECREF(sstuple); PyMem_Free(array); PyErr_SetString(PyExc_TypeError, "First argument's coordinate sequences must " \ "contain 5 elements each: [x,y,z,xi,yi]"); return NULL; } array[index++] = xs; array[index++] = ys; array[index++] = zs; array[index++] = xi; array[index++] = yi; /* printf("xs = %f, ys = %f, zs = %f, xi = %f, yi = %f\n", xs, ys, zs, xi, yi); */ Py_DECREF(sstuple); } return array; }
//----------------------------------------------------------------------------- // NumberVar_GetFormatAndTextFromDecimal() // Return the number format and text to use for the Decimal object. //----------------------------------------------------------------------------- static int NumberVar_GetFormatAndTextFromDecimal( PyObject *tupleValue, // decimal as_tuple() value PyObject **textObj, // text string for conversion PyObject **formatObj) // format for conversion { long numDigits, scale, i, sign, length, digit; char *textValue, *format, *textPtr, *formatPtr; PyObject *digits; // acquire basic information from the value tuple sign = PyInt_AsLong(PyTuple_GET_ITEM(tupleValue, 0)); if (PyErr_Occurred()) return -1; digits = PyTuple_GET_ITEM(tupleValue, 1); scale = PyInt_AsLong(PyTuple_GET_ITEM(tupleValue, 2)); if (PyErr_Occurred()) return -1; numDigits = PyTuple_GET_SIZE(digits); // allocate memory for the string and format to use in conversion length = numDigits + abs(scale) + 3; textValue = textPtr = PyMem_Malloc(length); if (!textValue) { PyErr_NoMemory(); return -1; } format = formatPtr = PyMem_Malloc(length); if (!format) { PyMem_Free(textValue); PyErr_NoMemory(); return -1; } // populate the string and format if (sign) *textPtr++ = '-'; for (i = 0; i < numDigits + scale; i++) { *formatPtr++ = '9'; if (i < numDigits) { digit = PyInt_AsLong(PyTuple_GetItem(digits, i)); if (PyErr_Occurred()) { PyMem_Free(textValue); return -1; } } else digit = 0; *textPtr++ = '0' + (char) digit; } if (scale < 0) { *formatPtr++ = 'D'; *textPtr++ = '.'; for (i = scale; i < 0; i++) { *formatPtr++ = '9'; if (numDigits + i < 0) digit = 0; else { digit = PyInt_AsLong(PyTuple_GetItem(digits, numDigits + i)); if (PyErr_Occurred()) { PyMem_Free(textValue); return -1; } } *textPtr++ = '0' + (char) digit; } } *formatPtr = '\0'; *textPtr = '\0'; *textObj = cxString_FromAscii(textValue); PyMem_Free(textValue); if (!*textObj) { PyMem_Free(format); return -1; } *formatObj = cxString_FromAscii(format); PyMem_Free(format); if (!*formatObj) { Py_DECREF(*textObj); return -1; } return 0; }
static int encode_current_locale(const wchar_t *text, char **str, size_t *error_pos, const char **reason, int raw_malloc, int surrogateescape) { const size_t len = wcslen(text); char *result = NULL, *bytes = NULL; size_t i, size, converted; wchar_t c, buf[2]; /* The function works in two steps: 1. compute the length of the output buffer in bytes (size) 2. outputs the bytes */ size = 0; buf[1] = 0; while (1) { for (i=0; i < len; i++) { c = text[i]; if (c >= 0xdc80 && c <= 0xdcff) { if (!surrogateescape) { goto encode_error; } /* UTF-8b surrogate */ if (bytes != NULL) { *bytes++ = c - 0xdc00; size--; } else { size++; } continue; } else { buf[0] = c; if (bytes != NULL) { converted = wcstombs(bytes, buf, size); } else { converted = wcstombs(NULL, buf, 0); } if (converted == (size_t)-1) { goto encode_error; } if (bytes != NULL) { bytes += converted; size -= converted; } else { size += converted; } } } if (result != NULL) { *bytes = '\0'; break; } size += 1; /* nul byte at the end */ if (raw_malloc) { result = PyMem_RawMalloc(size); } else { result = PyMem_Malloc(size); } if (result == NULL) { return -1; } bytes = result; } *str = result; return 0; encode_error: if (raw_malloc) { PyMem_RawFree(result); } else { PyMem_Free(result); } if (error_pos != NULL) { *error_pos = i; } if (reason) { *reason = "encoding error"; } return -2; }
/* Decode a byte string from the locale encoding with the surrogateescape error handler (undecodable bytes are decoded as characters in range U+DC80..U+DCFF). If a byte sequence can be decoded as a surrogate character, escape the bytes using the surrogateescape error handler instead of decoding them. Use _Py_wchar2char() to encode the character string back to a byte string. Return a pointer to a newly allocated wide character string (use PyMem_Free() to free the memory) and write the number of written wide characters excluding the null character into *size if size is not NULL, or NULL on error (decoding or memory allocation error). If size is not NULL, *size is set to (size_t)-1 on memory error and (size_t)-2 on decoding error. Conversion errors should never happen, unless there is a bug in the C library. */ wchar_t* _Py_char2wchar(const char* arg, size_t *size) { #ifdef __APPLE__ wchar_t *wstr; wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg)); if (size != NULL) { if (wstr != NULL) *size = wcslen(wstr); else *size = (size_t)-1; } return wstr; #else wchar_t *res; size_t argsize; size_t count; unsigned char *in; wchar_t *out; #ifdef HAVE_MBRTOWC mbstate_t mbs; #endif #ifndef MS_WINDOWS if (force_ascii == -1) force_ascii = check_force_ascii(); if (force_ascii) { /* force ASCII encoding to workaround mbstowcs() issue */ res = decode_ascii_surrogateescape(arg, size); if (res == NULL) goto oom; return res; } #endif #ifdef HAVE_BROKEN_MBSTOWCS /* Some platforms have a broken implementation of * mbstowcs which does not count the characters that * would result from conversion. Use an upper bound. */ argsize = strlen(arg); #else argsize = mbstowcs(NULL, arg, 0); #endif if (argsize != (size_t)-1) { res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t)); if (!res) goto oom; count = mbstowcs(res, arg, argsize+1); if (count != (size_t)-1) { wchar_t *tmp; /* Only use the result if it contains no surrogate characters. */ for (tmp = res; *tmp != 0 && (*tmp < 0xd800 || *tmp > 0xdfff); tmp++) ; if (*tmp == 0) { if (size != NULL) *size = count; return res; } } PyMem_Free(res); } /* Conversion failed. Fall back to escaping with surrogateescape. */ #ifdef HAVE_MBRTOWC /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ /* Overallocate; as multi-byte characters are in the argument, the actual output could use less memory. */ argsize = strlen(arg) + 1; res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t)); if (!res) goto oom; in = (unsigned char*)arg; out = res; memset(&mbs, 0, sizeof mbs); while (argsize) { size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); if (converted == 0) /* Reached end of string; null char stored. */ break; if (converted == (size_t)-2) { /* Incomplete character. This should never happen, since we provide everything that we have - unless there is a bug in the C library, or I misunderstood how mbrtowc works. */ PyMem_Free(res); if (size != NULL) *size = (size_t)-2; return NULL; } if (converted == (size_t)-1) { /* Conversion error. Escape as UTF-8b, and start over in the initial shift state. */ *out++ = 0xdc00 + *in++; argsize--; memset(&mbs, 0, sizeof mbs); continue; } if (*out >= 0xd800 && *out <= 0xdfff) { /* Surrogate character. Escape the original byte sequence with surrogateescape. */ argsize -= converted; while (converted--) *out++ = 0xdc00 + *in++; continue; } /* successfully converted some bytes */ in += converted; argsize -= converted; out++; } if (size != NULL) *size = out - res; #else /* HAVE_MBRTOWC */ /* Cannot use C locale for escaping; manually escape as if charset is ASCII (i.e. escape all bytes > 128. This will still roundtrip correctly in the locale's charset, which must be an ASCII superset. */ res = decode_ascii_surrogateescape(arg, size); if (res == NULL) goto oom; #endif /* HAVE_MBRTOWC */ return res; oom: if (size != NULL) *size = (size_t)-1; return NULL; #endif /* __APPLE__ */ }
groups -- sequence of groups to use in defaults files\n\ "; static PyObject *_mysql_server_init( PyObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = {"args", "groups", NULL}; char **cmd_args_c=NULL, **groups_c=NULL, *s; Py_ssize_t cmd_argc=0, i, groupc; PyObject *cmd_args=NULL, *groups=NULL, *ret=NULL, *item; if (_mysql_server_init_done) { PyErr_SetString(_mysql_ProgrammingError, "already initialized"); return NULL; } if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", kwlist, &cmd_args, &groups)) return NULL; #if MYSQL_VERSION_ID >= 40000 if (cmd_args) { if (!PySequence_Check(cmd_args)) { PyErr_SetString(PyExc_TypeError, "args must be a sequence"); goto finish; } cmd_argc = PySequence_Size(cmd_args); if (cmd_argc == -1) { PyErr_SetString(PyExc_TypeError, "args could not be sized"); goto finish; } cmd_args_c = (char **) PyMem_Malloc(cmd_argc*sizeof(char *)); for (i=0; i< cmd_argc; i++) { item = PySequence_GetItem(cmd_args, i); s = PyString_AsString(item); Py_DECREF(item); if (!s) { PyErr_SetString(PyExc_TypeError, "args must contain strings"); goto finish; } cmd_args_c[i] = s; } } if (groups) { if (!PySequence_Check(groups)) { PyErr_SetString(PyExc_TypeError, "groups must be a sequence"); goto finish; } groupc = PySequence_Size(groups); if (groupc == -1) { PyErr_SetString(PyExc_TypeError, "groups could not be sized"); goto finish; } groups_c = (char **) PyMem_Malloc((1+groupc)*sizeof(char *)); for (i=0; i< groupc; i++) { item = PySequence_GetItem(groups, i); s = PyString_AsString(item); Py_DECREF(item); if (!s) { PyErr_SetString(PyExc_TypeError, "groups must contain strings"); goto finish; } groups_c[i] = s; } groups_c[groupc] = (char *)NULL; } /* even though this may block, don't give up the interpreter lock so that the server can't be initialized multiple times. */ if (mysql_server_init(cmd_argc, cmd_args_c, groups_c)) { _mysql_Exception(NULL); goto finish; } #endif ret = Py_None; Py_INCREF(Py_None); _mysql_server_init_done = 1; finish: PyMem_Free(groups_c); PyMem_Free(cmd_args_c); return ret; }
static PyObject * Z_set(void *ptr, PyObject *value, Py_ssize_t size) { if (value == Py_None) { *(wchar_t **)ptr = NULL; Py_INCREF(value); return value; } if (PyLong_Check(value) || PyLong_Check(value)) { #if SIZEOF_VOID_P == SIZEOF_LONG_LONG *(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongLongMask(value); #else *(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongMask(value); #endif Py_INCREF(Py_None); return Py_None; } if (PyBytes_Check(value)) { value = PyUnicode_FromEncodedObject(value, _ctypes_conversion_encoding, _ctypes_conversion_errors); if (!value) return NULL; } else if (!PyUnicode_Check(value)) { PyErr_Format(PyExc_TypeError, "unicode string or integer address expected instead of %s instance", value->ob_type->tp_name); return NULL; } else Py_INCREF(value); #ifdef HAVE_USABLE_WCHAR_T /* HAVE_USABLE_WCHAR_T means that Py_UNICODE and wchar_t is the same type. So we can copy directly. Hm, are unicode objects always NUL terminated in Python, internally? */ *(wchar_t **)ptr = PyUnicode_AS_UNICODE(value); return value; #else { /* We must create a wchar_t* buffer from the unicode object, and keep it alive */ PyObject *keep; wchar_t *buffer; int size = PyUnicode_GET_SIZE(value); size += 1; /* terminating NUL */ size *= sizeof(wchar_t); buffer = (wchar_t *)PyMem_Malloc(size); if (!buffer) { Py_DECREF(value); return PyErr_NoMemory(); } memset(buffer, 0, size); keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor); if (!keep) { Py_DECREF(value); PyMem_Free(buffer); return NULL; } *(wchar_t **)ptr = (wchar_t *)buffer; if (-1 == PyUnicode_AsWideChar((PyUnicodeObject *)value, buffer, PyUnicode_GET_SIZE(value))) { Py_DECREF(value); Py_DECREF(keep); return NULL; } Py_DECREF(value); return keep; } #endif }
static void calculate_path(void) { extern char *Py_GetProgramName(void); static char delimiter[2] = {DELIM, '\0'}; static char separator[2] = {SEP, '\0'}; char *pythonpath = PYTHONPATH; char *rtpypath = Py_GETENV("PYTHONPATH"); char *home = Py_GetPythonHome(); char *path = getenv("PATH"); char *prog = Py_GetProgramName(); char argv0_path[MAXPATHLEN+1]; char zip_path[MAXPATHLEN+1]; int pfound, efound; /* 1 if found; -1 if found build directory */ char *buf; size_t bufsz; size_t prefixsz; char *defpath = pythonpath; #ifdef WITH_NEXT_FRAMEWORK NSModule pythonModule; #endif #ifdef __APPLE__ #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 uint32_t nsexeclength = MAXPATHLEN; #else unsigned long nsexeclength = MAXPATHLEN; #endif #endif /* If there is no slash in the argv0 path, then we have to * assume python is on the user's $PATH, since there's no * other way to find a directory to start the search from. If * $PATH isn't exported, you lose. */ if (strchr(prog, SEP)) strncpy(progpath, prog, MAXPATHLEN); #ifdef __APPLE__ /* On Mac OS X, if a script uses an interpreter of the form * "#!/opt/python2.3/bin/python", the kernel only passes "python" * as argv[0], which falls through to the $PATH search below. * If /opt/python2.3/bin isn't in your path, or is near the end, * this algorithm may incorrectly find /usr/bin/python. To work * around this, we can use _NSGetExecutablePath to get a better * hint of what the intended interpreter was, although this * will fail if a relative path was used. but in that case, * absolutize() should help us out below */ else if(0 == _NSGetExecutablePath(progpath, &nsexeclength) && progpath[0] == SEP) ; #endif /* __APPLE__ */ else if (path) { while (1) { char *delim = strchr(path, DELIM); if (delim) { size_t len = delim - path; if (len > MAXPATHLEN) len = MAXPATHLEN; strncpy(progpath, path, len); *(progpath + len) = '\0'; } else strncpy(progpath, path, MAXPATHLEN); joinpath(progpath, prog); if (isxfile(progpath)) break; if (!delim) { progpath[0] = '\0'; break; } path = delim + 1; } } else progpath[0] = '\0'; if (progpath[0] != SEP && progpath[0] != '\0') absolutize(progpath); strncpy(argv0_path, progpath, MAXPATHLEN); argv0_path[MAXPATHLEN] = '\0'; #ifdef WITH_NEXT_FRAMEWORK /* On Mac OS X we have a special case if we're running from a framework. ** This is because the python home should be set relative to the library, ** which is in the framework, not relative to the executable, which may ** be outside of the framework. Except when we're in the build directory... */ pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize")); /* Use dylib functions to find out where the framework was loaded from */ buf = (char *)NSLibraryNameForModule(pythonModule); if (buf != NULL) { /* We're in a framework. */ /* See if we might be in the build directory. The framework in the ** build directory is incomplete, it only has the .dylib and a few ** needed symlinks, it doesn't have the Lib directories and such. ** If we're running with the framework from the build directory we must ** be running the interpreter in the build directory, so we use the ** build-directory-specific logic to find Lib and such. */ strncpy(argv0_path, buf, MAXPATHLEN); reduce(argv0_path); joinpath(argv0_path, lib_python); joinpath(argv0_path, LANDMARK); if (!ismodule(argv0_path)) { /* We are in the build directory so use the name of the executable - we know that the absolute path is passed */ strncpy(argv0_path, progpath, MAXPATHLEN); } else { /* Use the location of the library as the progpath */ strncpy(argv0_path, buf, MAXPATHLEN); } } #endif #if HAVE_READLINK { char tmpbuffer[MAXPATHLEN+1]; int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN); while (linklen != -1) { /* It's not null terminated! */ tmpbuffer[linklen] = '\0'; if (tmpbuffer[0] == SEP) /* tmpbuffer should never be longer than MAXPATHLEN, but extra check does not hurt */ strncpy(argv0_path, tmpbuffer, MAXPATHLEN); else { /* Interpret relative to progpath */ reduce(argv0_path); joinpath(argv0_path, tmpbuffer); } linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN); } } #endif /* HAVE_READLINK */ reduce(argv0_path); /* At this point, argv0_path is guaranteed to be less than MAXPATHLEN bytes long. */ if (!(pfound = search_for_prefix(argv0_path, home))) { if (!Py_FrozenFlag) fprintf(stderr, "Could not find platform independent libraries <prefix>\n"); strncpy(prefix, PREFIX, MAXPATHLEN); joinpath(prefix, lib_python); } else reduce(prefix); strncpy(zip_path, prefix, MAXPATHLEN); zip_path[MAXPATHLEN] = '\0'; if (pfound > 0) { /* Use the reduced prefix returned by Py_GetPrefix() */ reduce(zip_path); reduce(zip_path); } else strncpy(zip_path, PREFIX, MAXPATHLEN); joinpath(zip_path, "lib/python00.zip"); bufsz = strlen(zip_path); /* Replace "00" with version */ zip_path[bufsz - 6] = VERSION[0]; zip_path[bufsz - 5] = VERSION[2]; if (!(efound = search_for_exec_prefix(argv0_path, home))) { if (!Py_FrozenFlag) fprintf(stderr, "Could not find platform dependent libraries <exec_prefix>\n"); strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN); joinpath(exec_prefix, "lib/lib-dynload"); } /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */ if ((!pfound || !efound) && !Py_FrozenFlag) fprintf(stderr, "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n"); /* Calculate size of return buffer. */ bufsz = 0; if (rtpypath) bufsz += strlen(rtpypath) + 1; prefixsz = strlen(prefix) + 1; while (1) { char *delim = strchr(defpath, DELIM); if (defpath[0] != SEP) /* Paths are relative to prefix */ bufsz += prefixsz; if (delim) bufsz += delim - defpath + 1; else { bufsz += strlen(defpath) + 1; break; } defpath = delim + 1; } bufsz += strlen(zip_path) + 1; bufsz += strlen(exec_prefix) + 1; /* This is the only malloc call in this file */ buf = (char *)PyMem_Malloc(bufsz); if (buf == NULL) { /* We can't exit, so print a warning and limp along */ fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n"); fprintf(stderr, "Using default static PYTHONPATH.\n"); module_search_path = PYTHONPATH; } else { /* Run-time value of $PYTHONPATH goes first */ if (rtpypath) { strcpy(buf, rtpypath); strcat(buf, delimiter); } else buf[0] = '\0'; /* Next is the default zip path */ strcat(buf, zip_path); strcat(buf, delimiter); /* Next goes merge of compile-time $PYTHONPATH with * dynamically located prefix. */ defpath = pythonpath; while (1) { char *delim = strchr(defpath, DELIM); if (defpath[0] != SEP) { strcat(buf, prefix); strcat(buf, separator); } if (delim) { size_t len = delim - defpath + 1; size_t end = strlen(buf) + len; strncat(buf, defpath, len); *(buf + end) = '\0'; } else { strcat(buf, defpath); break; } defpath = delim + 1; } strcat(buf, delimiter); /* Finally, on goes the directory for dynamic-load modules */ strcat(buf, exec_prefix); /* And publish the results */ module_search_path = buf; } /* Reduce prefix and exec_prefix to their essence, * e.g. /usr/local/lib/python1.5 is reduced to /usr/local. * If we're loading relative to the build directory, * return the compiled-in defaults instead. */ if (pfound > 0) { reduce(prefix); reduce(prefix); /* The prefix is the root directory, but reduce() chopped * off the "/". */ if (!prefix[0]) strcpy(prefix, separator); } else strncpy(prefix, PREFIX, MAXPATHLEN); if (efound > 0) { reduce(exec_prefix); reduce(exec_prefix); reduce(exec_prefix); if (!exec_prefix[0]) strcpy(exec_prefix, separator); } else strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN); }
static PyObject *sendmsg_recvmsg(PyObject *self, PyObject *args, PyObject *keywds) { int fd = -1; int flags = 0; int maxsize = 8192; int cmsg_size = 4096; size_t cmsg_space; size_t cmsg_overhead; Py_ssize_t recvmsg_result; struct msghdr message_header; struct cmsghdr *control_message; struct iovec iov[1]; char *cmsgbuf; PyObject *ancillary; PyObject *final_result = NULL; static char *kwlist[] = {"fd", "flags", "maxsize", "cmsg_size", NULL}; if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|iii:recvmsg", kwlist, &fd, &flags, &maxsize, &cmsg_size)) { return NULL; } cmsg_space = CMSG_SPACE(cmsg_size); /* overflow check */ if (cmsg_space > SOCKLEN_MAX) { PyErr_Format(PyExc_OverflowError, "CMSG_SPACE(cmsg_size) greater than SOCKLEN_MAX: %d", cmsg_size); return NULL; } message_header.msg_name = NULL; message_header.msg_namelen = 0; iov[0].iov_len = maxsize; iov[0].iov_base = PyMem_Malloc(maxsize); if (!iov[0].iov_base) { PyErr_NoMemory(); return NULL; } message_header.msg_iov = iov; message_header.msg_iovlen = 1; cmsgbuf = PyMem_Malloc(cmsg_space); if (!cmsgbuf) { PyMem_Free(iov[0].iov_base); PyErr_NoMemory(); return NULL; } memset(cmsgbuf, 0, cmsg_space); message_header.msg_control = cmsgbuf; /* see above for overflow check */ message_header.msg_controllen = (socklen_t) cmsg_space; recvmsg_result = recvmsg(fd, &message_header, flags); if (recvmsg_result < 0) { PyErr_SetFromErrno(sendmsg_socket_error); goto finished; } ancillary = PyList_New(0); if (!ancillary) { goto finished; } for (control_message = CMSG_FIRSTHDR(&message_header); control_message; control_message = CMSG_NXTHDR(&message_header, control_message)) { PyObject *entry; /* Some platforms apparently always fill out the ancillary data structure with a single bogus value if none is provided; ignore it, if that is the case. */ if ((!(control_message->cmsg_level)) && (!(control_message->cmsg_type))) { continue; } /* * Figure out how much of the cmsg size is cmsg structure overhead - in * other words, how much is not part of the application data. This lets * us compute the right application data size below. There should * really be a CMSG_ macro for this. */ cmsg_overhead = (char*)CMSG_DATA(control_message) - (char*)control_message; entry = Py_BuildValue( "(iis#)", control_message->cmsg_level, control_message->cmsg_type, CMSG_DATA(control_message), (Py_ssize_t) (control_message->cmsg_len - cmsg_overhead)); if (!entry) { Py_DECREF(ancillary); goto finished; } if (PyList_Append(ancillary, entry) < 0) { Py_DECREF(ancillary); Py_DECREF(entry); goto finished; } else { Py_DECREF(entry); } } final_result = Py_BuildValue( "s#iO", iov[0].iov_base, recvmsg_result, message_header.msg_flags, ancillary); Py_DECREF(ancillary); finished: PyMem_Free(iov[0].iov_base); PyMem_Free(cmsgbuf); return final_result; }
static int ver_coerce(PyObject **l, PyObject **r) { PgVersion *s; char *vstr; if ((vstr = PyMem_Malloc(128)) == (char *)NULL) { PyErr_NoMemory(); return (-1); } if (PyString_Check(*r)) { sprintf(vstr, "PostgreSQL %.80s on UNKNOWN, compiled by UNKNOWN", PyString_AsString(*r)); } else { long value = 0; if (PgInt2_Check(*r)) { value = PgInt2_AsLong((PgInt2Object *)(*r)); } else if (PyInt_Check(*r)) { value = PyInt_AsLong(*r); } #if defined(HAVE_LONG_LONG_SUPPORT) if (PgInt8_Check(*r)) { value = PgInt8_AsLong((PgInt8Object *)(*r)); } #endif else if (PyLong_Check(*r)) { value = PyLong_AsLong(*r); } else if (PyFloat_Check(*r)) { double d = PyFloat_AsDouble(*r); if (d > (double)INT_MAX) { PyErr_SetString(PyExc_OverflowError, "float too large to convert"); } else value = (long)d; } if (PyErr_Occurred()) goto coerce_error; sprintf(vstr, "PostgreSQL %ld.%ld.%ld on UNKNOWN, compiled by UNKNOWN", (value / 10000), ((value / 100) % 100), (value % 100)); } s = (PgVersion *)PgVersion_New(vstr); if (PyErr_Occurred()) { Py_XDECREF(s); goto coerce_error; } PyMem_Free(vstr); *r = (PyObject *)s; Py_XINCREF(*l); return (0); coerce_error: PyMem_Free(vstr); return (-1); }
static int AvroFileReader_init(AvroFileReader *self, PyObject *args, PyObject *kwds) { int rval; PyObject *pyfile; PyObject *types = NULL; FILE *file; char *schema_json; avro_writer_t schema_json_writer; size_t len; static char *kwlist[] = {"file", "types", NULL}; self->pyfile = NULL; self->flags = 0; self->iface = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, &pyfile, &types)) { return -1; } file = PyFile_AsFile(pyfile); if (file == NULL) { return -1; } self->pyfile = pyfile; Py_INCREF(pyfile); if (avro_file_reader_fp(file, "pyfile", 0, &self->reader)) { PyErr_Format(PyExc_IOError, "Error opening file: %s", avro_strerror()); return -1; } self->flags |= AVROFILE_READER_OK; self->schema = avro_file_reader_get_writer_schema(self->reader); if (self->schema == NULL) { PyErr_Format(PyExc_IOError, "Error reading schema: %s", avro_strerror()); return -1; } len = 256; do { /* XXX horrible loop to get a big enough buffer for schema. */ len *= 2; schema_json = (char *)PyMem_Malloc(len); schema_json_writer = avro_writer_memory(schema_json, len); rval = avro_schema_to_json(self->schema, schema_json_writer); if (!rval) { rval = avro_write(schema_json_writer, (void *)"", 1); /* zero terminate */ if (!rval) { self->schema_json = PyString_FromString(schema_json); } } avro_writer_free(schema_json_writer); PyMem_Free(schema_json); } while (rval == ENOSPC); if (rval) { PyErr_Format(PyExc_IOError, "Error saving schema: %s", avro_strerror()); return -1; } self->flags |= AVROFILE_SCHEMA_OK; self->iface = avro_generic_class_from_schema(self->schema); if (self->iface == NULL) { PyErr_SetString(PyExc_IOError, "Error creating generic class interface"); return -1; } if (types != NULL && PyObject_IsTrue(types)) { /* we still haven't incref'ed types here */ if (Py_TYPE(types) == get_avro_types_type()) { Py_INCREF(types); self->info.types = types; } else { self->info.types = PyObject_CallFunctionObjArgs((PyObject *)get_avro_types_type(), NULL); if (self->info.types == NULL) { return -1; } declare_types(&self->info, self->schema); } } else { self->info.types = NULL; } return 0; }
/* Retrieves the MD5 hash * Returns a Python object holding the offset if successful or NULL on error */ PyObject *pyewf_file_entry_get_hash_value_md5( pyewf_file_entry_t *pyewf_file_entry ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; libcerror_error_t *error = NULL; PyObject *string_object = NULL; const char *errors = NULL; uint8_t *hash_value = NULL; static char *function = "pyewf_file_entry_get_hash_value_md5"; size_t hash_value_size = 33; int result = 0; if( pyewf_file_entry == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid file entry.", function ); return( NULL ); } hash_value = (uint8_t *) PyMem_Malloc( sizeof( uint8_t ) * hash_value_size ); if( hash_value == NULL ) { PyErr_Format( PyExc_IOError, "%s: unable to create hash value.", function ); goto on_error; } Py_BEGIN_ALLOW_THREADS result = libewf_file_entry_get_utf8_hash_value_md5( pyewf_file_entry->file_entry, hash_value, hash_value_size, &error ); Py_END_ALLOW_THREADS if( result != 1 ) { if( libcerror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to retrieve hash value MD5.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to retrieve hash value MD5.\n%s", function, error_string ); } libcerror_error_free( &error ); goto on_error; } /* Pass the string length to PyUnicode_DecodeUTF8 * otherwise it makes the end of string character is part * of the string */ string_object = PyUnicode_DecodeUTF8( (char *) hash_value, (Py_ssize_t) hash_value_size - 1, errors ); PyMem_Free( hash_value ); return( string_object ); on_error: if( hash_value != NULL ) { PyMem_Free( hash_value ); } return( NULL ); }