Beispiel #1
0
char *
PyTokenizer_RestoreEncoding(struct tok_state* tok, int len, int *offset)
{
	char *text = NULL;
	if (tok->encoding) {
		/* convert source to original encondig */
		PyObject *lineobj = dec_utf8(tok->encoding, tok->buf, len);
		if (lineobj != NULL) {
			int linelen = PyString_Size(lineobj);
			const char *line = PyString_AsString(lineobj);
			text = PyObject_MALLOC(linelen + 1);
			if (text != NULL && line != NULL) {
				if (linelen)
					strncpy(text, line, linelen);
				text[linelen] = '\0';
			}
			Py_DECREF(lineobj);
					
			/* adjust error offset */
			if (*offset > 1) {
				PyObject *offsetobj = dec_utf8(tok->encoding, 
							       tok->buf, *offset-1);
				if (offsetobj) {
					*offset = PyString_Size(offsetobj) + 1;
					Py_DECREF(offsetobj);
				}
			}
			
		}
	}
	return text;

}
Beispiel #2
0
/* pycalcsims16(window, data1, offset1, data2, offset2); */
static PyObject* pycalcsims16(PyObject* self, PyObject* args)
{
    int window;
    PyObject* data1;
    PyObject* data2;
    int offset1;
    int offset2;

    if (!PyArg_ParseTuple(args, "iOiOi", &window, 
			  &data1, &offset1, &data2, &offset2)) {
	return NULL;
    }

    if (!PyString_CheckExact(data1) ||
	!PyString_CheckExact(data2)) {
	PyErr_SetString(PyExc_TypeError, "Must be string");
	return NULL;
    }

    int length1 = PyString_Size(data1) / sizeof(short);
    int length2 = PyString_Size(data2) / sizeof(short);
    if (window < 0 ||
	offset1 < 0 || length1 < offset1+window ||
	offset2 < 0 || length2 < offset2+window) {
	PyErr_SetString(PyExc_ValueError, "Invalid offset/window");
	return NULL;
    }

    short* seq1 = (short*)PyString_AsString(data1);
    short* seq2 = (short*)PyString_AsString(data2);
    double sim = calcsims16(window, &seq1[offset1], &seq2[offset2]);

    return PyFloat_FromDouble(sim);
}
Beispiel #3
0
// comparison function for use with qsort
int ctrlp_comp_alpha(const void *a, const void *b) {
    matchobj_t a_val = *(matchobj_t *)a;
    matchobj_t b_val = *(matchobj_t *)b;

    char *a_p = PyString_AsString(a_val.str);
    long a_len = PyString_Size(a_val.str);
    char *b_p = PyString_AsString(b_val.str);
    long b_len = PyString_Size(b_val.str);

    int order = 0;
    if (a_len > b_len) {
        order = strncmp(a_p, b_p, b_len);
        if (order == 0)
            order = 1; // shorter string (b) wins
    }
    else if (a_len < b_len) {
        order = strncmp(a_p, b_p, a_len);
        if (order == 0)
            order = -1; // shorter string (a) wins
    }
    else {
        order = strncmp(a_p, b_p, a_len);
    }

    return order;
}
Beispiel #4
0
// ---------------------------------------------------------------------------------
int SetExtraData( AVCodecContext *cCodec, PyObject* cObj )
{
	PyObject* obj =PyDict_GetItemString(cObj,EXTRA_DATA);
	if (!obj || !PyString_Check(obj))
	  return 0;

	cCodec->extradata= av_malloc( PyString_Size( obj ));
	if( !cCodec->extradata )
		return -1;
	cCodec->extradata_size= PyString_Size( obj );
	memcpy( cCodec->extradata, PyString_AsString( obj ), cCodec->extradata_size );
	return 1;
}
Beispiel #5
0
// @object PySBinaryArray|A sequence of strings containing binary data.
BOOL PyMAPIObject_AsSBinaryArray(PyObject *ob, SBinaryArray *pba)
{
    BOOL bSeq = TRUE;
    int seqLen;
    if (PyString_Check(ob)) {
        seqLen = 1;
        bSeq = FALSE;
    } else if (PySequence_Check(ob)) {
        seqLen = PySequence_Length(ob);
    } else {
        PyErr_SetString(PyExc_TypeError, "SBinaryArray must be a sequence of strings");
        return FALSE;
    }
    DWORD cBytes = (seqLen * sizeof(SBinary));
    SBinary *pBin;
    HRESULT hr = MAPIAllocateBuffer(cBytes, (void **)&pBin);
    pba->lpbin = pBin;
    if (FAILED(hr)) {
        OleSetOleError(hr);
        return FALSE;
    }
    pba->cValues = seqLen;
    if (bSeq) {
        for (ULONG i=0; i<(ULONG)seqLen; i++) {
            PyObject *obItem = PySequence_GetItem(ob, i);
            if (obItem==NULL) {
                MAPIFreeBuffer(pba);
                return FALSE;
            }
            if (!PyString_Check(obItem)) {
                PyErr_SetString(PyExc_TypeError, "SBinary must be a string");
                Py_DECREF(obItem);
                MAPIFreeBuffer(pba);
                return FALSE;
            }
            pBin[i].cb = PyString_Size(obItem);
            pBin[i].lpb = (LPBYTE )PyString_AsString(obItem);
            Py_DECREF(obItem);
        }
    } else {
        if (!PyString_Check(ob)) {
            PyErr_SetString(PyExc_TypeError, "SBinary must be a string");
            MAPIFreeBuffer(pba);
            return FALSE;
        }
        // Simple string
        pBin[0].cb = PyString_Size(ob);
        pBin[0].lpb = (LPBYTE)PyString_AsString(ob);
    }
    return TRUE;
}
static PyObject *secure_compare(PyObject *self, PyObject *args)
{
    PyObject *temp_string_one;
    PyObject *temp_string_two;
    Py_ssize_t string_size;
    volatile const unsigned char *left;
    volatile const unsigned char *right;
    volatile unsigned char sentinel = 0;

    int i; /* for use in loops */

    /* How many arguments were provided to the function */
    if (PyTuple_Size(args) != 2)
    {
        PyErr_SetString(PyExc_ValueError, "You must only supply 2 arguments");
        return NULL;
    }
    /* Can we parse them? */
    if (!PyArg_ParseTuple(args, "OO", &temp_string_one, &temp_string_two)) 
    {
        PyErr_SetString(PyExc_TypeError, "Unable To Parse Function Arguments");
        return NULL;
    }

    /* Are they Python Strings */
    if (!(PyString_CheckExact(temp_string_one)) || !(PyString_CheckExact(temp_string_two))) 
    {
        PyErr_SetString(PyExc_TypeError, "You must supply 2 string objects");
        return NULL;   
    } 

    /* Are they the same size? */
    if (PyString_Size(temp_string_one) != PyString_Size(temp_string_two)) 
    {
        Py_RETURN_FALSE;
    }

    
    left = (const volatile unsigned char *) PyString_AsString(temp_string_one);
    right = (const volatile unsigned char *) PyString_AsString(temp_string_two);
    string_size = PyString_Size(temp_string_one);

    for(i = 0; i < string_size; i++)
    {
        sentinel |= *left++ ^ *right++;
    }

    return constant_byte_compare(sentinel, 0);
}
Beispiel #7
0
/* uwsgi MARSHAL|33 */
int uwsgi_request_marshal(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
	PyObject *func_result;

	PyObject *umm = PyDict_GetItemString(uwsgi->embedded_dict,
					     "message_manager_marshal");
	if (umm) {
		PyObject *ummo = PyMarshal_ReadObjectFromString(wsgi_req->buffer,
								wsgi_req->uh.pktsize);
		if (ummo) {
			if (!PyTuple_SetItem(uwsgi->embedded_args, 0, ummo)) {
				if (!PyTuple_SetItem(uwsgi->embedded_args, 1, PyInt_FromLong(wsgi_req->uh.modifier2))) {
					func_result = PyEval_CallObject(umm, uwsgi->embedded_args);
					if (PyErr_Occurred()) {
						PyErr_Print();
					}
					if (func_result) {
						PyObject *marshalled = PyMarshal_WriteObjectToString(func_result, 1);
						if (!marshalled) {
							PyErr_Print();
						}
						else {
							if (PyString_Size(marshalled) <= 0xFFFF) {
								wsgi_req->uh.pktsize = (uint16_t)
									PyString_Size(marshalled);
								if (write(wsgi_req->poll.fd, wsgi_req, 4) == 4) {
									if (write(wsgi_req->poll.fd, PyString_AsString(marshalled), wsgi_req->uh.pktsize) != wsgi_req->uh.pktsize) {
										uwsgi_error("write()");
									}
								}
								else {
									uwsgi_error("write()");
								}
							}
							else {
								fprintf(stderr, "marshalled object is too big. skip\n");
							}
							Py_DECREF(marshalled);
						}
						Py_DECREF(func_result);
					}
				}
			}
			//Py_DECREF(ummo);
		}
	}
	PyErr_Clear();

	return 0;
}
Beispiel #8
0
static jsval to_javascript_object(JSContext *context, PyObject *value) {
    if (PyString_Check(value)) {
        JSString *obj = JS_NewStringCopyN(context, PyString_AsString(value), PyString_Size(value));
        return STRING_TO_JSVAL(obj);
    } else if (PyUnicode_Check(value)) {
        PyObject *encoded = PyUnicode_AsUTF8String(value);
        JSString *obj = JS_NewStringCopyN(context, PyString_AsString(encoded), PyString_Size(encoded));
        Py_DECREF(encoded);
        return STRING_TO_JSVAL(obj);
    } else if (PyFloat_Check(value)) {
        return DOUBLE_TO_JSVAL(PyFloat_AsDouble(value));
    } else if (PyInt_Check(value)) {
        return INT_TO_JSVAL(PyInt_AsLong(value));
    } else if (PyLong_Check(value)) {
        return INT_TO_JSVAL(PyLong_AsLong(value));
    } else if (PyList_Check(value)) {
        JSObject *obj = JS_NewArrayObject(context, 0, NULL);
        int i;
        for (i = 0; i < PyList_Size(value); i++) {
            jsval item = to_javascript_object(context, PyList_GetItem(value, i));
            JS_SetElement(context, obj, i, &item);
        }
        return OBJECT_TO_JSVAL(obj);
    } else if (PyTuple_Check(value)) {
        JSObject *obj = JS_NewArrayObject(context, 0, NULL);
        int i;
        for (i = 0; i < PyTuple_Size(value); i++) {
            jsval item = to_javascript_object(context, PyTuple_GetItem(value, i));
            JS_SetElement(context, obj, i, &item);
        }
        return OBJECT_TO_JSVAL(obj);
    } else if (PyDict_Check(value)) {
        JSObject *obj = JS_NewObject(context, NULL, NULL, NULL);
        populate_javascript_object(context, obj, value);
        return OBJECT_TO_JSVAL(obj);
    } else if (PyDateTime_Check(value)) {
        JSObject *obj = JS_NewDateObject(context,
            PyDateTime_GET_YEAR(value),
            PyDateTime_GET_MONTH(value) - 1,
            PyDateTime_GET_DAY(value),
            PyDateTime_DATE_GET_HOUR(value),
            PyDateTime_DATE_GET_MINUTE(value),
            PyDateTime_DATE_GET_SECOND(value));
        return OBJECT_TO_JSVAL(obj);
    } else {
        return JSVAL_NULL;
    }
}
Beispiel #9
0
PyObject *py_uwsgi_write(PyObject * self, PyObject * args) {
	PyObject *data;
	char *content;
	size_t content_len;

	struct wsgi_request *wsgi_req = py_current_wsgi_req();

	data = PyTuple_GetItem(args, 0);
	if (PyString_Check(data)) {
		content = PyString_AsString(data);
		content_len = PyString_Size(data);
		UWSGI_RELEASE_GIL
		uwsgi_response_write_body_do(wsgi_req, content, content_len);
		UWSGI_GET_GIL
		// this is a special case for the write callable
		// no need to honout write-errors-exception-only
		if (wsgi_req->write_errors > uwsgi.write_errors_tolerance && !uwsgi.disable_write_exception) {
                        uwsgi_py_write_set_exception(wsgi_req);
			return NULL;
		}
	}

	Py_INCREF(Py_None);
	return Py_None;
}
Beispiel #10
0
static PyObject *uwsgi_Input_readlines(uwsgi_Input *self, PyObject *args) {

	long hint = 0;

        if (!PyArg_ParseTuple(args, "|l:readline", &hint)) {
                return NULL;
        }


	PyObject *res = PyList_New(0);
	for(;;) {
		PyObject *line = uwsgi_Input_getline(self, hint);
		if (!line) {
			Py_DECREF(res);
			return NULL;
		}
		if (PyString_Size(line) == 0) {
			Py_DECREF(line);
			return res;
		}
		PyList_Append(res, line);	
		Py_DECREF(line);
	}

	return res;
}
Beispiel #11
0
/* Note that this always assumes in and out are sys.stdin and sys.stdout. */
static char *bits_readline_function(FILE *in, FILE *out, char *prompt)
{
    PyObject *pyret;
    Py_ssize_t len;
    char *temp;
    char *ret;

    (void)in;
    (void)out;

    pyret = PyObject_CallFunction(readline_callback, "s", prompt);

    if (!pyret)
        return NULL;
    if (!PyString_Check(pyret)) {
        PyErr_Format(PyExc_TypeError, "Python readline callback returned a non-string");
        return NULL;
    }
    temp = PyString_AsString(pyret);
    if (!temp)
        return NULL;
    len = PyString_Size(pyret);
    ret = PyMem_Malloc(len+1);
    if (!ret) {
        PyErr_NoMemory();
        return NULL;
    }
    return memcpy(ret, temp, len+1);
}
BOOL py_to_DEVICEMODE(DEVICEMODE *devmode, PyObject *dict)
{
	PyObject *obj, *dict_copy = PyDict_Copy(dict);
	BOOL result = False;

	if (!(obj = PyDict_GetItemString(dict_copy, "private")))
		goto done;

	if (!PyString_Check(obj))
		goto done;

	devmode->dev_private = PyString_AsString(obj);
	devmode->driverextra = PyString_Size(obj);

	PyDict_DelItemString(dict_copy, "private");

	if (!to_struct(devmode, dict_copy, py_DEVICEMODE))
		goto done;

	result = True;

done:
	Py_DECREF(dict_copy);
	return result;
}
Beispiel #13
0
static PyObject *
CSVParser_iternext_filelike(CSVParser *self)
{
    char *buf;
    char c;
    PyObject *fields = NULL;
    PyObject *lineobj = NULL;
    long i;
    long linelen;
    Logq_Engine_reset(self->engine);
    while(!self->engine->is_success){
        if (parse_reset(self) < 0)
            return NULL;
        do {
            lineobj = PyIter_Next(self->pyfile);
            if (lineobj == NULL) {
                /* End of input OR exception */
                if (!PyErr_Occurred() && (self->field_len != 0 ||
                                          self->state == IN_QUOTED_FIELD)) {
                    if (parse_save_field(self) >= 0 )
                        break;
                }
                return NULL;
            }
            ++self->line_num;

            buf = PyString_AsString(lineobj);
            linelen = PyString_Size(lineobj);

            if (buf == NULL || linelen < 0) {
                return NULL;
            }
            for(i=0; i<linelen; ++i){
                c = buf[i];
                if (c == '\0') {
                    Py_DECREF(lineobj);
                    PyErr_Format(csv_error_obj,
                                 "line contains NULL byte");
                    goto err;
                }
                if (parse_process_char(self, c) < 0) {
                    Py_DECREF(lineobj);
                    goto err;
                }
                //query fail. go next line
                if (self->state == QUERY_FAIL){
                    if(buf[linelen-1]=='\n'){
                        self->state = START_RECORD;
                    }
                    break;
                }
            }
            Py_DECREF(lineobj);
        } while (self->state != START_RECORD);
    }
    fields = self->fields;
    self->fields = NULL;
err:
    return fields;
}
Beispiel #14
0
static PyObject *
pygimp_bilinear(PyObject *self, PyObject *args, PyObject *kwargs)
{
    gdouble x, y;
    gdouble values[4];
    PyObject *py_values;
    static char *kwlist[] = { "x", "y", "values", NULL };

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "ddO:bilinear", kwlist,
				     &x, &y, &py_values))
  	return NULL;

    if (PyString_Check(py_values)) {
        if (PyString_Size(py_values) == 4) {
            guchar ret;
            ret = gimp_bilinear_8(x, y, (guchar *)PyString_AsString(py_values));
            return PyString_FromStringAndSize((char *)&ret, 1);
        }
    } else if (PySequence_Check(py_values)) {
        if (PySequence_Size(py_values) == 4) {
            int i;
            for (i = 0; i < 4; i++) {
                PyObject *v;
                v = PySequence_GetItem(py_values, i);
                values[i] = PyFloat_AsDouble(v);
                Py_DECREF(v);
            }
            return PyFloat_FromDouble(gimp_bilinear(x, y, values));
        }
    }

    PyErr_SetString(PyExc_TypeError, "values is not a sequence of 4 items");
    return NULL;
}
Beispiel #15
0
int c4_PyStream::Read(void *buffer_, int length_) {
  PyObject *o = PyObject_CallMethod(_stream, "read", "i", length_);
  int n = o != 0 ? PyString_Size(o): 0;
  if (n > 0)
    memcpy(buffer_, PyString_AsString(o), n);
  return n;
}
static PyObject *
find_builtin_names(void)
{
	PyObject *builtins, *names, *key, *value;
	int pos = 0;
	builtins = PyEval_GetBuiltins();
	if (builtins == NULL || !PyDict_Check(builtins)) {
  		PyErr_SetString(PyExc_SystemError, "no builtins dict!");
		return NULL;
	}
	names = PyDict_New();
	if (names == NULL)
		return NULL;
	while (PyDict_Next(builtins, &pos, &key, &value)) {
		if (PyString_Check(key) &&
				PyString_Size(key) > 0 &&
				PyString_AS_STRING(key)[0] != '_') {
			if (PyDict_SetItem(names, key, Py_None) < 0) {
				Py_DECREF(names);
				return NULL;
			}
		}
	}
	return names;
}
Beispiel #17
0
static int __Pyx_Print(PyObject *arg_tuple, int newline) {
    PyObject *f;
    PyObject* v;
    int i;

    if (!(f = __Pyx_GetStdout()))
        return -1;
    for (i=0; i < PyTuple_GET_SIZE(arg_tuple); i++) {
        if (PyFile_SoftSpace(f, 1)) {
            if (PyFile_WriteString(" ", f) < 0)
                return -1;
        }
        v = PyTuple_GET_ITEM(arg_tuple, i);
        if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0)
            return -1;
        if (PyString_Check(v)) {
            char *s = PyString_AsString(v);
            Py_ssize_t len = PyString_Size(v);
            if (len > 0 &&
                isspace(Py_CHARMASK(s[len-1])) &&
                s[len-1] != ' ')
                    PyFile_SoftSpace(f, 0);
        }
    }
    if (newline) {
        if (PyFile_WriteString("\n", f) < 0)
            return -1;
        PyFile_SoftSpace(f, 0);
    }
    return 0;
}
Beispiel #18
0
// String conversions
// Convert a Python string object to a BSTR - allow embedded NULLs, etc.
static BOOL PyString_AsBstr(PyObject *stringObject, BSTR *pResult)
{
	int size=PyString_Size(stringObject);
	const char *buf = PyString_AsString(stringObject);
	if (buf==NULL) return FALSE;

	/* We assume that we dont need more 'wide characters' for the result
	   then the number of bytes in the input. Often we
	   will need less, as the input may contain multi-byte chars, but we
	   should never need more 
	*/

	LPWSTR wstr = (LPWSTR)malloc(size*sizeof(WCHAR));
	if (wstr==NULL) {
		PyErr_SetString(PyExc_MemoryError, "No memory for wide string buffer");
		return FALSE;
	}
	/* convert and get the final character size */
	size = MultiByteToWideChar(CP_ACP, 0, buf, size, wstr, size);
	*pResult = SysAllocStringLen(wstr, size);
	if (*pResult==NULL)
		PyErr_SetString(PyExc_MemoryError, "allocating BSTR");
	free(wstr);
	return *pResult != NULL;
}
Beispiel #19
0
// Convert a Python object to a "char *" - allow embedded NULLs, None, etc.
BOOL PyWinObject_AsString(PyObject *stringObject, char **pResult, BOOL bNoneOK /*= FALSE*/, DWORD *pResultLen /* = NULL */)
{
	PyObject *tempObject = NULL;
	if (stringObject==Py_None) {
		if (!bNoneOK) {
			PyErr_SetString(PyExc_TypeError, "None is not a valid string in this context");
			return FALSE;
		}
		*pResult = NULL;
		if (pResultLen) *pResultLen = 0;
		return TRUE;
	}
	// Convert the string if a WIDE string.
	if (PyUnicode_Check(stringObject))
		stringObject = tempObject = PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(stringObject), PyUnicode_GET_SIZE(stringObject), NULL);

	if (!PyString_Check(stringObject)) {
		PyErr_Format(PyExc_TypeError, "The object must be a string or unicode object (got '%s')",
					 stringObject->ob_type->tp_name);
		return FALSE;
	}
	char *temp = PyString_AsString(stringObject);
	int len = PyString_Size(stringObject);
	*pResult = (char *)PyMem_Malloc(len+1);
	if (*pResult) {
		memcpy(*pResult, temp, len+1);
		if (pResultLen) *pResultLen = len;
	}
	Py_XDECREF(tempObject);
	return (*pResult != NULL);
}
Beispiel #20
0
struct matcher_entry *matchers_get_response(u_char *data, u_int datalen, struct ctx *ctx, u_int type, u_int src_port, u_int dst_port)
{

    struct matcher_entry *matcher;

#ifdef HAVE_PYTHON
    PyObject *args;
    PyObject *value;
    Py_ssize_t rdatalen;
    char *rdata;
#endif

    if(!(matcher = matchers_match((const char *)data, datalen, ctx, type, src_port, dst_port))) {
        logger(DBG, "No matchers found for data");
        return NULL;
    }

#ifdef HAVE_PYTHON
    if(matcher->pyfunc) {
        logger(DBG, "We have a Python code to construct response");
        args = PyTuple_New(2);
        PyTuple_SetItem(args,0,PyString_FromStringAndSize((const char *)data, datalen)); // here is data
        PyTuple_SetItem(args,1,PyInt_FromSsize_t(datalen));

        value = PyObject_CallObject(matcher->pyfunc, args);
        if(value == NULL) {
            PyErr_Print();
            logger(WARN, "Python function returns no data!");
            return NULL;
        }

        rdata = PyString_AsString(value);
        rdatalen = PyString_Size(value);

        if(rdata != NULL && rdatalen > 0) {
            matcher->response_len = (u_int) rdatalen;
            if(matcher->response) {
                // We already have previous response, free it
                free(matcher->response);
            }
            matcher->response = malloc(matcher->response_len);
            memcpy(matcher->response, (u_char *) rdata, rdatalen);
        } else {
            PyErr_Print();
            logger(WARN, "Python cannot convert return string");
            return NULL;
        }
        return matcher;
    }
#endif

    if(matcher->response) {
        logger(DBG, "We have a plain text response");
        return matcher;
    }

    logger(WARN, "There is no response data!");
    return NULL;

}
Beispiel #21
0
PyObject *
Filter_SubFileDecode(PyObject * self, PyObject * args)
{
    PyObject * target;
    PyObject * delim_object;
    SubFileDecodeState * state;
    int length;

    if (!PyArg_ParseTuple(args, "OS", &target, &delim_object))
	return NULL;

    length = PyString_Size(delim_object);
    if (length < 1)
	return PyErr_Format(PyExc_ValueError, "empty delimiter");
    state = PyMem_Malloc(sizeof (SubFileDecodeState) + length * sizeof(int));
    if (!state)
	return PyErr_NoMemory();
    state->delim_object = delim_object;
    Py_INCREF(state->delim_object);
    state->delim = PyString_AsString(delim_object);
    state->chars_matched = 0;
    state->length = length;
    init_shift(state);
    
    return Filter_NewDecoder(target, "SubFileDecode", 0, read_subfile,
			     NULL, dealloc_subfile, state);
}
Beispiel #22
0
PyObject* vm_set_mem(JitCpu *self, PyObject* args)
{
       PyObject *py_addr;
       PyObject *py_buffer;
       Py_ssize_t py_length;

       char * buffer;
       uint64_t size;
       uint64_t addr;
       int ret = 0x1337;

       if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer))
	       return NULL;

       PyGetInt(py_addr, addr);

       if(!PyString_Check(py_buffer))
	       RAISE(PyExc_TypeError,"arg must be str");

       size = PyString_Size(py_buffer);
       PyString_AsStringAndSize(py_buffer, &buffer, &py_length);

       ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, size);
       if (ret < 0)
	       RAISE(PyExc_TypeError,"arg must be str");
       check_automod(self, addr, size*8);

       Py_INCREF(Py_None);
       return Py_None;
}
Beispiel #23
0
/* {{{ pip_pyobject_to_zval(PyObject *obj)
   Converts the given PyObject into an equivalent zval */
zval *
pip_pyobject_to_zval(PyObject *obj)
{
	zval *ret;

	if (obj == NULL) {
		return NULL;
	}

	/* Initialize the return value */
	MAKE_STD_ZVAL(ret);

	if (PyInt_Check(obj)) {
		ZVAL_LONG(ret, PyInt_AsLong(obj));
	} else if (PyLong_Check(obj)) {
		ZVAL_LONG(ret, PyLong_AsLong(obj));
	} else if (PyFloat_Check(obj)) {
		ZVAL_DOUBLE(ret, PyFloat_AsDouble(obj));
	} else if (PyString_Check(obj)) {
		ZVAL_STRINGL(ret, PyString_AsString(obj), PyString_Size(obj), 1);
	} else if (obj == Py_None) {
		ZVAL_NULL(ret);
	} else if (PyTuple_Check(obj) || PyList_Check(obj)) {
		ret = pip_sequence_to_hash(obj);
	} else if (PyDict_Check(obj)) {
		ret = pip_mapping_to_hash(obj);
	} else {
		ret = pip_pyobject_to_zobject(obj);
	}

	return ret;
}
Beispiel #24
0
static int
LogCount_sethash(LogCount *self, PyObject *value, void *closure)
{
  if (!value) {
    PyErr_SetString(PyExc_TypeError, "cannot delete the hash attribute");
    return -1;
  }

  if (! PyString_Check(value)) {
    PyErr_SetString(PyExc_TypeError, "hash attribute must be a string");
    return -1;
  }

  int len = PyString_Size(value);
  if (len % sizeof(int)) {
    PyErr_SetString(PyExc_ValueError, "hash attribute must be a string whose length is even mod 4");
    return -1;
  }

  int nhash = len / sizeof(int);
  if (nhash != self->lc.nhash) {
    logcount_finish(&self->lc);
    logcount_init(&self->lc, nhash);
  }

  memcpy(self->lc.hashes, PyString_AsString(value), len);
  return 0;
}
Beispiel #25
0
static int Pixels__setitem__ (PixelsObject *self, int i, PyObject *val)
{
	dbg();
	if (i < 0)	
		i = i + self->length;

	if (i >= self->length  ||  i < 0)
	{	PyErr_SetString (PyExc_IndexError, "pixel index out of range");
		return -1;
	}

	if (PyInt_Check(val))
	{	self->pixels[i] = (int) PyInt_AsLong (val);
		return 0;
	}

	if (!PyString_Check(val)  ||  PyString_Size(val) != 1)
	{	PyErr_SetString (PyExc_TypeError, 
				"assignment value isn't a character");	
		return -1;
	}

	self->pixels[i] = PyString_AsString(val)[0];
	return 0;
}
Beispiel #26
0
static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
{
	TDB_DATA ret;
	ret.dptr = (unsigned char *)PyString_AsString(data);
	ret.dsize = PyString_Size(data);
	return ret;
}
/* Replace any occurances of "\r\n?" in the input string with "\n".
   This converts DOS and Mac line endings to Unix line endings.
   Also append a trailing "\n" to be compatible with
   PyParser_SimpleParseFile(). Returns a new reference. */
static PyObject *
normalize_line_endings(PyObject *source)
{
    char *buf, *q, *p = PyString_AsString(source);
    PyObject *fixed_source;

    if (!p)
        return NULL;

    /* one char extra for trailing \n and one for terminating \0 */
    buf = (char *)PyMem_Malloc(PyString_Size(source) + 2);
    if (buf == NULL) {
        PyErr_SetString(PyExc_MemoryError,
                        "zipimport: no memory to allocate "
                        "source buffer");
        return NULL;
    }
    /* replace "\r\n?" by "\n" */
    for (q = buf; *p != '\0'; p++) {
        if (*p == '\r') {
            *q++ = '\n';
            if (*(p + 1) == '\n')
                p++;
        }
        else
            *q++ = *p;
    }
    *q++ = '\n';  /* add trailing \n */
    *q = '\0';
    fixed_source = PyString_FromString(buf);
    PyMem_Free(buf);
    return fixed_source;
}
Beispiel #28
0
static CORBA_TypeCode
get_union_tc(CORBA_TypeCode tc, PyObject *discrim)
{
    CORBA_TypeCode subtc = NULL;
    glong discriminator, i;

    if (PyString_Check(discrim)) {
	if (PyString_Size(discrim) != 1)
	    return NULL;
	discriminator = *(CORBA_octet *)PyString_AsString(discrim);
    } else {
	discriminator = PyInt_AsLong(discrim);
	if (PyErr_Occurred())
	    return NULL;
    }

    for (i = 0; i < tc->sub_parts; i++) {
	if (i == tc->default_index) continue;
	if (tc->sublabels[i] == discriminator) {
	    subtc = tc->subtypes[i];
	    break;
	}
    }
    if (i == tc->sub_parts) {
	if (tc->default_index >= 0)
	    subtc = tc->subtypes[tc->default_index];
	else
	    subtc = TC_null;
    }

    return subtc;
}
/**
  Convert a Python decimal.Decimal to MySQL DECIMAL.

  Convert a Python decimal.Decimal to MySQL DECIMAL. This function also
  removes the 'L' suffix from the resulting string when using Python v2.

  @param    obj         PyObject to be converted

  @return   Converted decimal as string
    @retval PyBytes     Python v3
    @retval PyString    Python v2
*/
PyObject*
pytomy_decimal(PyObject *obj)
{
#ifdef PY3
    return PyBytes_FromString((const char *)PyUnicode_1BYTE_DATA(
                              PyObject_Str(obj)));
#else
    PyObject *numeric, *new_num;
    int tmp_size;
    char *tmp;

    numeric= PyObject_Str(obj);
    tmp= PyString_AsString(numeric);
    tmp_size= (int)PyString_Size(numeric);
    if (tmp[tmp_size - 1] == 'L')
    {
        new_num= PyString_FromStringAndSize(tmp, tmp_size);
        _PyString_Resize(&new_num, tmp_size - 1);
        return new_num;
    }
    else
    {
        return numeric;
    }

#endif
}
Beispiel #30
0
/* New reference. */
static PyObject *
normalize_module(PyObject *filename)
{
    PyObject *module;
    const char *mod_str;
    Py_ssize_t len;

    int rc = PyObject_IsTrue(filename);
    if (rc == -1)
        return NULL;
    else if (rc == 0)
        return PyString_FromString("<unknown>");

    mod_str = PyString_AsString(filename);
    if (mod_str == NULL)
        return NULL;
    len = PyString_Size(filename);
    if (len < 0)
        return NULL;
    if (len >= 3 &&
            strncmp(mod_str + (len - 3), ".py", 3) == 0) {
        module = PyString_FromStringAndSize(mod_str, len-3);
    }
    else {
        module = filename;
        Py_INCREF(module);
    }
    return module;
}