473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help with c <-> python buffer transfer

How does one transfer a buffer object from python -c and back again
(assuming the data gets modified)?
I can't seem to get this or anything else to work, but am clueless as
to what I'm doing wrong
using namespace boost::python;

static PyObject * proc_buf(PyObject *self, PyObject *args) {
PyObject *resultobj;
char* output_samples;
int len;
if (!PyArg_ParseTuple(args,"s#|l",&output_samples, &len)) {
return NULL; /* wrong arguments provided */
}
for (int i=0;i<len;i++) {
output_samples[i] *= 2; // for example
}
resultobj = PyString_FromStringAndSize(output_samples, len);
return resultobj;
}
This compiles ok, but when in python I do

buf = proc_buf( bufx, len(bufx)
len(buf)

I get
len() of unsized object

Thanks

Aug 11 '06 #1
3 2383
tk****@gmail.com wrote:
How does one transfer a buffer object from python -c and back again
(assuming the data gets modified)?
I can't seem to get this or anything else to work, but am clueless as
to what I'm doing wrong
using namespace boost::python;
Looks like C++, not C.
>
static PyObject * proc_buf(PyObject *self, PyObject *args) {
[I'm not familiar with the boost gadget, but ...] Doesn't "static" mean
that this function is *not* externally visible?
PyObject *resultobj;
char* output_samples;
int len;
if (!PyArg_ParseTuple(args,"s#|l",&output_samples, &len)) {
You have made the length an optional argument, but not initialised the
receiving variable "len". Nothing to do with your current problem, but
highly dangerous.
return NULL; /* wrong arguments provided */
}
for (int i=0;i<len;i++) {
output_samples[i] *= 2; // for example
}
This is updating the internal representation of the input in situ. Not
a very good idea at all. Take a copy. Return the updated copy.
resultobj = PyString_FromStringAndSize(output_samples, len);
return resultobj;
}
This compiles ok, but when in python I do
Put print repr(bufx), type(bufx) here so that we're all clued in on
what you are talking about. You say "transfer a buffer object" but your
C[++] is returning a string object.
buf = proc_buf( bufx, len(bufx)
You are missing both a module name and a ")" here. It should look
something like:

buf = theextensionmodule.proc_buf( bufx, len(bufx))

Please *always* copy/paste the actual code that you executed.
len(buf)

I get
len() of unsized object
Please *always* copy/paste the actual error message & stack trace that
you get.

Try print repr(buf), type(buf) here; it might give you a clue as to
what type of object you have that is unsized. On the surface this is a
mystery, as (based on the info that you have supplied), "buf" should be
a string.

HTH .... alternatively come up a level or three and tell us what your
basic requirement is; maybe it can be solved more easily in Python or
Pyrex.

Cheers,
John

Aug 11 '06 #2
John Machin wrote:
tk****@gmail.com wrote:
How does one transfer a buffer object from python -c and back again
(assuming the data gets modified)?
I can't seem to get this or anything else to work, but am clueless as
to what I'm doing wrong
using namespace boost::python;

Looks like C++, not C.

static PyObject * proc_buf(PyObject *self, PyObject *args) {

[I'm not familiar with the boost gadget, but ...] Doesn't "static" mean
that this function is *not* externally visible
Yes. It's C++. I've built python extensions with Boost Python
successfully and copied the structure of most of this example from
other people's code.
>
PyObject *resultobj;
char* output_samples;
int len;
if (!PyArg_ParseTuple(args,"s#|l",&output_samples, &len)) {

You have made the length an optional argument, but not initialised the
receiving variable "len". Nothing to do with your current problem, but
highly dangerous.
return NULL; /* wrong arguments provided */
}
for (int i=0;i<len;i++) {
output_samples[i] *= 2; // for example
}

This is updating the internal representation of the input in situ. Not
a very good idea at all. Take a copy. Return the updated copy.
Thanks for the pointers...
>
resultobj = PyString_FromStringAndSize(output_samples, len);
return resultobj;
}
This is the part I need help with. I've also used PyBuffer_...
subroutines which gave similar problems. Thanks for all of your other
comments, but I was hoping someone could just tell me what was wrong
with the code without having to worry about all of the other things
that could go wrong.
For completeness here is the complete c++ module & python output

//================================================== =====================
// Boost Includes
//================================================== ============
#include <boost/python.hpp>
#include <boost/cstdint.hpp>
#include <boost/python/def.hpp>
#include <boost/python/args.hpp>
#include <boost/python/overloads.hpp>

// Using
================================================== =====================
using namespace boost::python;

static PyObject * proc_buf(PyObject *self, PyObject *args) {
PyObject *resultobj;
char* output_samples;
int len;
if (!PyArg_ParseTuple(args,"s#|l",&output_samples, &len)) {
return NULL; /* wrong arguments provided */
}
for (int i=0;i<len;i++) output_samples[i] *= 2;
resultobj = PyString_FromStringAndSize(output_samples, len);
return resultobj;
}
// Module
================================================== ====================
BOOST_PYTHON_MODULE(spuctest)
{
def("pass_buf",&proc_buf);
}

# python code
.....
buffy = mf.read()
print type(buffy)
buf = pass_buf(buffy, len(buffy))
print type(buf)
#

#python output
<type 'buffer'>
<type 'Nonetype'>

Aug 12 '06 #3

tk****@gmail.com wrote:
This is the part I need help with. I've also used PyBuffer_...
subroutines which gave similar problems. Thanks for all of your other
comments, but I was hoping someone could just tell me what was wrong
with the code without having to worry about all of the other things
that could go wrong.
For completeness here is the complete c++ module & python output

//================================================== =====================
// Boost Includes
//================================================== ============
#include <boost/python.hpp>
#include <boost/cstdint.hpp>
#include <boost/python/def.hpp>
#include <boost/python/args.hpp>
#include <boost/python/overloads.hpp>

// Using
================================================== =====================
using namespace boost::python;

static PyObject * proc_buf(PyObject *self, PyObject *args) {
PyObject *resultobj;
char* output_samples;
int len;
if (!PyArg_ParseTuple(args,"s#|l",&output_samples, &len)) {
Sorry, I read that too quyickly. You need THREE receptors, one for "s",
2nd for "#", 3rd for "l".
e.g. something like:
int len;
long third_arg; /* needs initialisation */
if (!PyArg_ParseTuple(args,"s#|l",&output_samples, &len,
&third_arg)) {
return NULL; /* wrong arguments provided */
}
for (int i=0;i<len;i++) output_samples[i] *= 2;
resultobj = PyString_FromStringAndSize(output_samples, len);
return resultobj;
}
// Module
================================================== ====================
BOOST_PYTHON_MODULE(spuctest)
{
def("pass_buf",&proc_buf);
}

# python code
....
buffy = mf.read()
print type(buffy)
buf = pass_buf(buffy, len(buffy))
print type(buf)
#

#python output
<type 'buffer'>
<type 'Nonetype'>
I'd guess the return value of None was fortuitous -- when I tried your
code using C, the call to pass_buf just crashed. With the above fix,
the problem goes away:

|>>import procbuf
|>>foo = '\x01\x03\xff'
|>>x = procbuf.passbuf(foo)
|>>x
'\x02\x06\xfe'

HTH take2 :-)
John

Aug 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Adrian Parker | last post by:
I have a server app which is sent a date in the local format of the client machine. I've no control over the client app at all, so have to code at the server end to cope with any problems. The...
2
by: ASallade | last post by:
Hello, I've scoured my books and the web, but am still daunted, hopefully some of the users in this newsgroup will have advice for my problem. I am not an experienced javascript programmer,...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
2
by: Mark Huebner | last post by:
The following sample code for the lock statement is on page 112 of the O'Reilly book "C# Essentials". Can somebody explain to me why this recursive class definition of LockTest does not cause an...
4
by: Mark Huebner | last post by:
My reply e-mail address was wrong in the prior message. Sorry about that. The following sample code for the lock statement is on page 112 of the O'Reilly book "C# Essentials". Can somebody...
10
by: Jon Noring | last post by:
Out of curiosity, may a CDATA section appear within an attribute value with datatype CDATA? And if so, how about other attribute value datatypes which accept the XML markup characters? To me,...
8
by: goldtech | last post by:
Newbie esp about html validation - seems like something to strive for. Yet many pages even "enterprise" corporate type pages do not validate, yet seem to display well on most browsers. Two...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
9
by: Jerret Johnson | last post by:
A challenged co-worker of mine challenged me to come up with a perverse example of a conforming Standard C program. This is by no means a gold medal winner in a C obfuscation contest, but...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.