473,763 Members | 6,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(PyObje ct *self, PyObject *args) {
PyObject *resultobj;
char* output_samples;
int len;
if (!PyArg_ParseTu ple(args,"s#|l" ,&output_sample s, &len)) {
return NULL; /* wrong arguments provided */
}
for (int i=0;i<len;i++) {
output_samples[i] *= 2; // for example
}
resultobj = PyString_FromSt ringAndSize(out put_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 2396
tk****@gmail.co m 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(PyObje ct *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_ParseTu ple(args,"s#|l" ,&output_sample s, &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_FromSt ringAndSize(out put_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 = theextensionmod ule.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.co m 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(PyObje ct *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_ParseTu ple(args,"s#|l" ,&output_sample s, &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_FromSt ringAndSize(out put_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(PyObje ct *self, PyObject *args) {
PyObject *resultobj;
char* output_samples;
int len;
if (!PyArg_ParseTu ple(args,"s#|l" ,&output_sample s, &len)) {
return NULL; /* wrong arguments provided */
}
for (int i=0;i<len;i++) output_samples[i] *= 2;
resultobj = PyString_FromSt ringAndSize(out put_samples, len);
return resultobj;
}
// Module
=============== =============== =============== =============== ==========
BOOST_PYTHON_MO DULE(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.co m 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(PyObje ct *self, PyObject *args) {
PyObject *resultobj;
char* output_samples;
int len;
if (!PyArg_ParseTu ple(args,"s#|l" ,&output_sample s, &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_ParseTu ple(args,"s#|l" ,&output_sample s, &len,
&third_arg)) {
return NULL; /* wrong arguments provided */
}
for (int i=0;i<len;i++) output_samples[i] *= 2;
resultobj = PyString_FromSt ringAndSize(out put_samples, len);
return resultobj;
}
// Module
=============== =============== =============== =============== ==========
BOOST_PYTHON_MO DULE(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
1773
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 server is in the US, so any dates need to be in US format before they will convert using strtotime() Here is what i'm doing at the moment.. // need to convert date to use USA mm/dd/yyyy format on server // $lt contains the date in the client...
2
1536
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, but have gotten my code to work well, without errors on IE, Opera and netscapes recent builds. While testing, I found that it doesnt execute on Netscape 4.7 In the head I have a script that creates a custom object/class for products,
7
31564
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" slows the system down considerably. I've tried creating a temp db, but I can't figure out how to execute two select commands. (It throws the exception "The column prefix 'tempdb' does not match with a table name or alias name used in the query.")
2
1253
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 infinite number of LockTest objects to be created (i.e., until it consumes all available memory)? I don't understand why only two threads are created. using System; using System.Threading;
4
1542
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 explain to me why this recursive class definition of LockTest does not cause an infinite number of LockTest objects to be created (i.e., until it consumes all available memory)? I don't understand why only two threads are created. using System;
10
42968
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, the XML specification seems a little ambiguous on this, so I defer to the XML authorities. Refer to sections 2.4 and 2.7 (it all hinges on if CDATA attribute values are part of markup or not.) Thanks.
8
1786
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 questions: 1. What is the reward, the reason to strive for a validating page? 2. Given the code snippet below which does not validate - how can accomplish the same "look" with code that does validate? Please without CSS, just html. Thanks ....
0
1019
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 and is unformatted. I display the text to my user in the listview. The textbox I create logically in the program, and I initialize the correct properties for a normal multiline editor.
9
2042
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 nevertheless it demonstrates some arguably serious abuse of the preprocessor. If you can make it even more perverse, I'd appreciate it. And for those of you who might ask what my C question is ... is this Standard C? #include <ctype.h>
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.