ctypes struct alignment specification

2020-11-10 Thread Chris
I am working on a CUDA python API using ctypes. Within CUDA there are vector type structs defined (float2, float3, float4, int2, int3, int4, ...), all of which have an alignment specification (example of how this is specified https://stackoverflow.com/questions/12778949/cuda-memory-alignment/127

Re: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-07 Thread Peter J. Holzer
On 2019-12-02 09:55:16 -0800, Rob Gaddi wrote: > The struct situation is, as you said, a bit different. I believe that with > the default native alignment @, you're seeing 4-byte data padded to an > 8-byte alignment, not 8-byte data. Nope. That's really an 8 byte long: Py

Re: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-07 Thread Peter J. Holzer
DON'T have exact control with those sizes, it just happens > > that all the platforms you are using happen to have the same size for > > those types. > > According to the docs for struct (python 2.7 and python 3.8) I do have > exact control for the types I listed. You also have exact

Re: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-05 Thread Barry Scott
his returns 8. >>>> Documentation at https://docs.python.org/3/library/array.html explicitly >>>> states 'L' is for size 4. >>>> It impacts all uses types of array (e.g. reading from byte strings). >>>> The struct module is a little different:

Re: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-03 Thread Chris Angelico
pical > but *not* guaranteed sizes", that would be more clear. > I think array.array() is possibly the wrong tool for this job. If you have a collection of bytes from some well-defined source (eg you're parsing a file in a known format), struct is better suited to it, because it's

RE: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-03 Thread Chris Clark
by using the word "minimum". Minimum would suggest that it would accept a 4-byte value as a minimum and on 64-bit it does *not*, it hard fails. If it were to document that, "the sizes are native integer types for the platform, the table documents some typical but *not* guaranteed

Re: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-03 Thread Rob Gaddi
On 12/2/19 5:50 PM, Richard Damon wrote: Perhaps array could be extended so that it took '4' for a 4 byte integer and '8' for an 8 byte integer (maybe 'U4' and 'U8' for unsigned). Might as well also allow 1 and 2 for completeness for char and short (but those are currently consistent). I wil

Re: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-02 Thread Richard Damon
tes 'L' is for size 4. >>> It impacts all uses types of array (e.g. reading from byte strings). >>> The struct module is a little different: >>> import struct >>> x = struct.pack('L', 0) >>> # len(x) ===8 rather than 4 >>>

Re: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-02 Thread Christian Heimes
states 'L' is for size 4. >>> It impacts all uses types of array (e.g. reading from byte strings). >>> The struct module is a little different: >>> import struct >>> x = struct.pack('L', 0) >>> # len(x) ===8 rather than 4 >>&

Re: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-02 Thread Barry Scott
under Windows Python 3.7.3 64-bit build. >> Under Ubuntu; Python 2.7.15rc1, 3.6.5, 3.70b3 64-bit this returns 8. >> Documentation at https://docs.python.org/3/library/array.html explicitly >> states 'L' is for size 4. >> It impacts all uses types of array (e.g. readi

Re: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-02 Thread Rob Gaddi
returns 8. Documentation at https://docs.python.org/3/library/array.html explicitly states 'L' is for size 4. It impacts all uses types of array (e.g. reading from byte strings). The struct module is a little different: import struct x = struct.pack('L', 0) # len(x) ===8 rather tha

Re: array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-02 Thread Richard Damon
Ubuntu; Python 2.7.15rc1, 3.6.5, 3.70b3 64-bit this returns 8. > Documentation at https://docs.python.org/3/library/array.html explicitly > states 'L' is for size 4. > It impacts all uses types of array (e.g. reading from byte strings). > > The struct module is a little

array and struct 64-bit Linux change in behavior Python 3.7 and 2.7

2019-12-02 Thread Chris Clark
thon.org/3/library/array.html explicitly states 'L' is for size 4. It impacts all uses types of array (e.g. reading from byte strings). The struct module is a little different: import struct x = struct.pack('L', 0) # len(x) ===8 rather than 4 This can be worked around by

Re: Can't figure out how to do something using ctypes (and maybe struct?)

2018-08-11 Thread Peter Otten
inhahe wrote: > I need to make a list of instances of a Structure, then I need to make an > instance of another Structure, one of the fields of which needs to be an > arbitrary-length array of pointers to the instances in the list. How do I > do that? > > Just in case it helps, I'll include what

Re: Can't figure out how to do something using ctypes (and maybe struct?)

2018-08-10 Thread Steven D'Aprano
On Fri, 10 Aug 2018 18:40:11 -0400, inhahe wrote: > I need to make a list of instances of a Structure, then I need to make > an instance of another Structure, one of the fields of which needs to be > an arbitrary-length array of pointers to the instances in the list. How > do I do that? > > Just

Can't figure out how to do something using ctypes (and maybe struct?)

2018-08-10 Thread inhahe
I need to make a list of instances of a Structure, then I need to make an instance of another Structure, one of the fields of which needs to be an arbitrary-length array of pointers to the instances in the list. How do I do that? Just in case it helps, I'll include what I tried that didn't work: -

Re: Bug in python34 package struct

2016-03-24 Thread Marko Rauhamaa
Rudi Lopez Lopez : > from struct import pack > > print(hex(126)) > print(pack('>H',126)) I can't see any bug. The tilde (~) has an ASCII code 126. Marko -- https://mail.python.org/mailman/listinfo/python-list

Re: Bug in python34 package struct

2016-03-24 Thread Chris Angelico
On Thu, Mar 24, 2016 at 7:17 PM, Rudi Lopez Lopez wrote: > from struct import pack > > print(hex(126)) > print(pack('>H',126)) Explain the bug? ChrisA -- https://mail.python.org/mailman/listinfo/python-list

Bug in python34 package struct

2016-03-24 Thread Rudi Lopez Lopez
from struct import pack print(hex(126)) print(pack('>H',126)) Rudi G. Lopez -- https://mail.python.org/mailman/listinfo/python-list

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-17 Thread Dave Farrance
Steven D'Aprano wrote: >On Mon, 16 Nov 2015 05:15 pm, Gregory Ewing wrote: > >> Ints are not the only thing that // can be applied to: >> >> >>> 1.0//0.01 >> 99.0 > >Good catch! Hmmm. I see that the float for 0.01 _is_ slightly larger than 0.01 >>> Decimal(0.01) Decimal('0.012

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-16 Thread Chris Angelico
On Tue, Nov 17, 2015 at 12:17 AM, Steven D'Aprano wrote: > On Sun, 15 Nov 2015 01:23 pm, Chris Angelico wrote: > >> On Sun, Nov 15, 2015 at 1:08 PM, Steven D'Aprano >> wrote: >>> number = +raw_input("enter a number: ") >>> >>> versus: >>> >>> text = raw_input("enter a number: ") >>> try: >>>

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-16 Thread Steven D'Aprano
On Sun, 15 Nov 2015 01:23 pm, Chris Angelico wrote: > On Sun, Nov 15, 2015 at 1:08 PM, Steven D'Aprano > wrote: >> number = +raw_input("enter a number: ") >> >> versus: >> >> text = raw_input("enter a number: ") >> try: >> number = float(text) >> except ValueError: >> number = int(text) >

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-16 Thread Steven D'Aprano
On Mon, 16 Nov 2015 05:15 pm, Gregory Ewing wrote: > Ian Kelly wrote: >> Unary integer division seems pretty silly since the only possible results >> would be 0, 1 or -1. > > Ints are not the only thing that // can be applied to: > > >>> 1.0//0.01 > 99.0 Good catch! -- Steven -- https:/

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-15 Thread Gregory Ewing
Chris Angelico wrote: Small problem: Since we have / and // operators, it's impossible to have a unary / operator: No, it's not -- '//' is already recognised as a single token distinct from '/ /'. You would just have to leave a space or use parens in some cases. -- Greg -- https://mail.python.

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-15 Thread Gregory Ewing
Ian Kelly wrote: Unary integer division seems pretty silly since the only possible results would be 0, 1 or -1. Ints are not the only thing that // can be applied to: >>> 1.0//0.01 99.0 -- Greg -- https://mail.python.org/mailman/listinfo/python-list

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-15 Thread Gregory Ewing
Dennis Lee Bieber wrote: And we'd be left looking for a symbol for bitwise inversion. Who needs negation when you have bitwise inversion? minusx = ~x + 1 I know, it doesn't work for floats, but that's just a simple matter of defining ~ on floats appropriately... -- Greg -- https://ma

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Chris Angelico
On Sun, Nov 15, 2015 at 1:08 PM, Steven D'Aprano wrote: > number = +raw_input("enter a number: ") > > versus: > > text = raw_input("enter a number: ") > try: > number = float(text) > except ValueError: > number = int(text) What kinds of strings can float() not handle but int() can, and in

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Steven D'Aprano
On Sun, 15 Nov 2015 02:43 am, Ian Kelly wrote: > On Fri, Nov 13, 2015 at 10:40 PM, Steven D'Aprano > wrote: >> Python has operator overloading, so it can be anything you want it to be. >> E.g. you might have a DSL where +feature turns something on and -feature >> turns it off. > > By that argume

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Steven D'Aprano
On Sun, 15 Nov 2015 09:53 am, Random832 wrote: > Marko Rauhamaa writes: >> Actually, the real question is, is the unary - *really* so useful that >> it merits existence or is it just something that was mindlessly copied >> into programming languages from elementary school arithmetics? > > The al

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread MRAB
On 2015-11-14 22:53, Random832 wrote: Marko Rauhamaa writes: Actually, the real question is, is the unary - *really* so useful that it merits existence or is it just something that was mindlessly copied into programming languages from elementary school arithmetics? The alternative, if you wan

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Marko Rauhamaa
Random832 : > Marko Rauhamaa writes: >> Actually, the real question is, is the unary - *really* so useful >> that it merits existence or is it just something that was mindlessly >> copied into programming languages from elementary school arithmetics? > > The alternative, if you want to be able to

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Random832
Marko Rauhamaa writes: > Actually, the real question is, is the unary - *really* so useful that > it merits existence or is it just something that was mindlessly copied > into programming languages from elementary school arithmetics? The alternative, if you want to be able to specify negative num

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Marko Rauhamaa
Ian Kelly : > On Nov 14, 2015 9:56 AM, "Marko Rauhamaa" wrote: >>r = //(//r1 + //r2 + //r3) > > Unary integer division seems pretty silly since the only possible > results would be 0, 1 or -1. Yep, mixed them up. Marko -- https://mail.python.org/mailman/listinfo/python-list

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Ian Kelly
On Nov 14, 2015 10:10 AM, "Chris Angelico" wrote: > > On Sun, Nov 15, 2015 at 4:04 AM, Ian Kelly wrote: > > Unary integer division seems pretty silly since the only possible results > > would be 0, 1 or -1. > > 1, -1, or ZeroDivisionError. The zero's on the other side. But yes. // 42 == 1 // 42

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Ian Kelly
On Nov 14, 2015 9:56 AM, "Marko Rauhamaa" wrote: > > Ian Kelly : > > > For somebody reading one of these uses of unary plus in real code, I > > imagine it would be a bit of a WTF moment if it's the first time > > they've encountered it. I don't recall ever seeing any code that > > actually used th

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Chris Angelico
On Sun, Nov 15, 2015 at 3:52 AM, Marko Rauhamaa wrote: > What I don't understand is why there is a unary + but no unary /: > >-x ≡ 0 - x >+x ≡ 0 + x >/x ≡ 1 / x >//x ≡ 1 // x >*x ≡ 1 * x > > You could write: > >r = //(//r1 + //r2 + //r3) > > for > >r = 1 // (1//r1 + 1//

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Chris Angelico
On Sun, Nov 15, 2015 at 2:43 AM, Ian Kelly wrote: > On Fri, Nov 13, 2015 at 10:40 PM, Steven D'Aprano wrote: >> Python has operator overloading, so it can be anything you want it to be. >> E.g. you might have a DSL where +feature turns something on and -feature >> turns it off. > > By that argume

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Marko Rauhamaa
Ian Kelly : > For somebody reading one of these uses of unary plus in real code, I > imagine it would be a bit of a WTF moment if it's the first time > they've encountered it. I don't recall ever seeing any code that > actually used this, though. What I don't understand is why there is a unary +

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Ian Kelly
On Fri, Nov 13, 2015 at 10:40 PM, Steven D'Aprano wrote: > Python has operator overloading, so it can be anything you want it to be. > E.g. you might have a DSL where +feature turns something on and -feature > turns it off. By that argument we should also have operators ~, !, $, \, ? because some

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Steven D'Aprano
On Sat, 14 Nov 2015 02:01 pm, Chris Angelico wrote: > On Sat, Nov 14, 2015 at 1:40 PM, Steven D'Aprano > wrote: >> On Sat, 14 Nov 2015 09:42 am, Chris Angelico wrote: >> >>> However, this is a reasonable call for the abolition of unary plus... >> >> The only way you'll take unary plus out of Pyth

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Chris Angelico
On Sat, Nov 14, 2015 at 2:48 PM, Ian Kelly wrote: >> Yes, unary minus has the same issue - but it's a lot more important >> than unary plus is. In ECMAScript, unary plus means "force this to be >> a number"; what's its purpose in Python? > > I'm not sure "force this to be a number" is really a jus

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Chris Angelico
On Sat, Nov 14, 2015 at 2:45 PM, Ian Kelly wrote: >> Yes, unary minus has the same issue - but it's a lot more important >> than unary plus is. In ECMAScript, unary plus means "force this to be >> a number"; what's its purpose in Python? > > It forces a Counter to contain only positive counts? Di

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
On Nov 13, 2015 8:03 PM, "Chris Angelico" wrote: > > On Sat, Nov 14, 2015 at 1:40 PM, Steven D'Aprano wrote: > > On Sat, 14 Nov 2015 09:42 am, Chris Angelico wrote: > > > >> However, this is a reasonable call for the abolition of unary plus... > > > > The only way you'll take unary plus out of Py

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
On Nov 13, 2015 8:03 PM, "Chris Angelico" wrote: > > On Sat, Nov 14, 2015 at 1:40 PM, Steven D'Aprano wrote: > > On Sat, 14 Nov 2015 09:42 am, Chris Angelico wrote: > > > >> However, this is a reasonable call for the abolition of unary plus... > > > > The only way you'll take unary plus out of Py

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Chris Angelico
On Sat, Nov 14, 2015 at 1:40 PM, Steven D'Aprano wrote: > On Sat, 14 Nov 2015 09:42 am, Chris Angelico wrote: > >> However, this is a reasonable call for the abolition of unary plus... > > The only way you'll take unary plus out of Python is by prying it from my > cold, dead hands. > > > BTW, unar

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Steven D'Aprano
On Sat, 14 Nov 2015 09:42 am, Chris Angelico wrote: > However, this is a reasonable call for the abolition of unary plus... The only way you'll take unary plus out of Python is by prying it from my cold, dead hands. BTW, unary minus suffers from the same "problem": x =- y # oops, meant x -= y

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Chris Angelico
On Sat, Nov 14, 2015 at 6:45 AM, Ian Kelly wrote: > This sets RegisterAX to 2. Specifically, positive 2. If you want to > *add* 2, you probably meant to write: > > RegisterAX += 2 > > This also points out a good reason for using spaces around operators, > as "RegisterAX =+ 2" would have caused

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Grant Edwards
On 2015-11-13, kent nyberg wrote: > Though, as many times before, the problem was due to misunderstanding > of how python works. I assumed file.read()[xx:yy] was to be > understood as, in the file, read from index xx to place yy. Nope. First, the 'file.read()' part is evaluated. That return

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread kent nyberg
The main problem was that I forgot to do seek(0). Thanks alot people. Though, as many times before, the problem was due to misunderstanding of how python works. I assumed file.read()[xx:yy] was to be understood as, in the file, read from index xx to place yy. That is, [10:20] was the same a

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Grant Edwards
On 2015-11-13, Ian Kelly wrote: > Either retain the read data between calls, or call seek(0) before > reading it again. It has always saddened me that Python files don't have a rewind() method. On Unix, calling rewind() is the same as calling seek(0), so it's utterly pointless except as an amus

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
On Fri, Nov 13, 2015 at 1:15 PM, kent nyberg wrote: > What bothers me, is the error that says > unpack requires a string argument of 4 bytes. > Im thinking in the line of arguments? Does unpack look at the 4 bytes it has > read, and tell for some > reason say that unpacking needs an argument of 4

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
On Fri, Nov 13, 2015 at 1:15 PM, kent nyberg wrote: > Even with that, it still gets wrong. > I also tried .read()[RegisterAX:RegisterAX+4] When you call read for the second time, are you just reading the same file again without closing or seeking it in the interim? If that's the case, then you wo

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Laura Creighton
I forgot to add. You get this wretched error message if your data is shorter than expected, and you ask struct to read more than you have, as well. most annoying. Laura -- https://mail.python.org/mailman/listinfo/python-list

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread kent nyberg
On Fri, Nov 13, 2015 at 12:36:22PM -0700, Ian Kelly wrote: > On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg wrote: > > def LoadCommandAndReact(place_to_read): > > global RegisterAX > > > > tmp = place_to_read.read()[RegisterAX:calcsize('HH')] > > It looks like you're trying to get a slice

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Laura Creighton
second time, unpack doesnt get the size of '>HH' to >read. But surely >.read()[RegisterAX:calcsize('HH')] should assure that its fed with correct >data? > >I have filled the file with data of size '>HH' and first command reads that. >So n

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
As long as I'm replying to this, I see a few more issues to comment on: On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg wrote: > if place_to_read.closed: >print("Drive error. Drive closed.") You probably also want to break or return here. Even better: raise an exception instead of prin

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg wrote: > def LoadCommandAndReact(place_to_read): > global RegisterAX > > tmp = place_to_read.read()[RegisterAX:calcsize('HH')] It looks like you're trying to get a slice of length 4 here, starting at the value of RegisterAX. What you're actual

Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread kent nyberg
Hi there, Im deeply sorry for yet another question to this list. I have come across a problem to which google seems not to eager to supply the anwser. The problem is the following. First I do this: def setup_drive(): test = pack('>HH', 0b1000, 0b10010001) file = op

Re: Adding a 'struct' into new python type

2015-03-08 Thread Lakshmipathi.G
Hi Jason, Thanks for the response. Okay, Will try to convert 'struct test' into new python object (say 'PyStructTest') and include it inside CountDictType as pyobject instead of 'struct'. Will post details here, If i encounter any issues. Cheers,

Re: Adding a 'struct' into new python type

2015-03-07 Thread Jason Swails
On Sat, Mar 7, 2015 at 4:15 AM, Lakshmipathi.G wrote: > Hi, > > I'm following this example : > http://nedbatchelder.com/text/whirlext.html#h_making_a_type and trying > to add > new data into 'CountDict' type > > Adding a simple 'char' wo

Adding a 'struct' into new python type

2015-03-07 Thread Lakshmipathi.G
Hi, I'm following this example : http://nedbatchelder.com/text/whirlext.html#h_making_a_type and trying to add new data into 'CountDict' type Adding a simple 'char' works well. typedef struct { PyObject_HEAD PyObject * dict; int count; char c; //add this

Re: Two locations for module struct ?

2014-11-14 Thread Terry Reedy
On 11/14/2014 10:11 AM, ast wrote: Hello In module wave there is a sub module struct. struct is not a documented part of the wave module. You can call function pack() with: import wave val = wave.struct.pack(...) wave imports several other stdlib modules. All are accessible the same way

Re: Two locations for module struct ?

2014-11-14 Thread ast
"Peter Otten" <__pete...@web.de> a écrit dans le message de news:mailman.15823.1415983912.18130.python-l...@python.org... Do you see the pattern? You should ;) Understood thx -- https://mail.python.org/mailman/listinfo/python-list

Re: Two locations for module struct ?

2014-11-14 Thread Peter Otten
ast wrote: > In module wave there is a sub module struct. > You can call function pack() with: > > import wave > val = wave.struct.pack(...) > > but the same function can be called with: > > import struct > val = struct.pack(...) > > Is it exactly the sam

Re: Two locations for module struct ?

2014-11-14 Thread Chris Angelico
On Sat, Nov 15, 2014 at 2:11 AM, ast wrote: > In module wave there is a sub module struct. > You can call function pack() with: > > import wave > val = wave.struct.pack(...) > > but the same function can be called with: > > import struct > val = struct.pack(...) >

Two locations for module struct ?

2014-11-14 Thread ast
Hello In module wave there is a sub module struct. You can call function pack() with: import wave val = wave.struct.pack(...) but the same function can be called with: import struct val = struct.pack(...) Is it exactly the same module in both location ? Why putting struct in two places

Re: Packing byte fields and an array object into struct

2013-11-13 Thread Ned Batchelder
On Wednesday, November 13, 2013 3:41:03 PM UTC-5, krishna...@gmail.com wrote: > Thanks for your reply Ned! > > I tried this your suggestion and this is what it complains... > > os_inst_bytes = struct.pack('7BI512s', 0, 0x51, 0x10, 5, 0, 0xD, 0x80, 0, '') > > -

Re: Packing byte fields and an array object into struct

2013-11-13 Thread krishna2prasad
Correction in the last input line... In [16]: result = struct.unpack('5p', os_inst[11:16]) --- error Traceback (most recent call last) in () > 1 result = struct.unpack('5p', os_inst[11

Re: Packing byte fields and an array object into struct

2013-11-13 Thread krishna2prasad
o the IOCTL to be able to get the data back from the driver. Without bytearray(), the ioctl with mutable flag set to 1 would complain. I tried to use the p format specifier with pack after converting the array object to byte stream. Packing seems fine. However, I cant seem to unpack. In [

Re: Packing byte fields and an array object into struct

2013-11-13 Thread Ned Batchelder
On Wednesday, November 13, 2013 1:31:49 PM UTC-5, krishna...@gmail.com wrote: > Hello, > > I am trying to build a structure to be passed down to an I2C device driver. > The driver expects a struct that has a data array of size 512 bytes among > other things. This is my code -

Packing byte fields and an array object into struct

2013-11-13 Thread krishna2prasad
Hello, I am trying to build a structure to be passed down to an I2C device driver. The driver expects a struct that has a data array of size 512 bytes among other things. This is my code - rd_wr = 0x0 # Read operation i2c_addr= addr mux = mux_sel multi_len

Re: Reading c struct via python

2013-11-13 Thread Lakshmipathi.G
> Looks like your "e"... Is that the key field? Yes, you are right 'e' is the key. And rest of them are data. >AND the order of the items is "o" before "i" >-- that doesn't seem to match your C struct definition. Sorry, I was testing the bdb

Reading c struct via python

2013-11-11 Thread Lakshmipathi.G
Hi - We have C code which writes following struct into berkeley db ("my_db.db"). struct my_info { unsigned long int i, e; int o; char *f; char *s; }; How to read this via Python? Google search gave this code --- $ cat pybsd2.py from bsddb import db fruitDB = db.DB() fr

Re: struct calcsize discrepency?

2011-12-06 Thread Nobody
On Mon, 05 Dec 2011 00:20:32 -0800, Mark Dickinson wrote: >> May be, yes, but since calcsize() is returning 12 when the elements >> are put in the other order, it would seem to be not counting such >> padding. > > Indeed. That's arguably a bug in the struct module,

Re: struct calcsize discrepency?

2011-12-05 Thread Mark Dickinson
On Dec 5, 8:09 am, Chris Angelico wrote: > May be, yes, but since calcsize() is returning 12 when the elements > are put in the other order, it would seem to be not counting such > padding. Indeed. That's arguably a bug in the struct module, and one that people have had to find w

Re: struct calcsize discrepency?

2011-12-05 Thread Chris Angelico
On Mon, Dec 5, 2011 at 6:42 PM, Mark Dickinson wrote: > That's a strange way to think of it, especially since the padding also > happens for a single struct object when there's no array present.  I > find it cleaner to think of C as having no padding in arrays, but > p

Re: struct calcsize discrepency?

2011-12-05 Thread Mark Dickinson
On Dec 4, 3:17 pm, Chris Angelico wrote: > On Mon, Dec 5, 2011 at 1:51 AM, Dave Angel wrote: > > In C, the padding to the largest alignment occurs at the > > end of a structure as well as between items.  Otherwise, an array of the > > struct would not be safely aligned.

Re: struct calcsize discrepency?

2011-12-04 Thread Chris Angelico
gt; The eight-byte integer is aligned on an eight-byte boundary, so when >> it follows a four-byte string, you get four padding bytes inserted. >> Put them in the other order, and the padding disappears. >> > NOT disappears.  In C, the padding to the largest alignment occurs at t

Re: struct calcsize discrepency?

2011-12-04 Thread Glen Rice
On Dec 4, 9:38 am, Duncan Booth wrote: > Glen Rice wrote: > > In IPython: > >>import struct > >>struct.calcsize('4s') > > 4 > >>struct.calcsize('Q') > > 8 > >>struct.calcsize('4sQ') > > 16 > >

Re: struct calcsize discrepency?

2011-12-04 Thread Peter Otten
Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me. Can anyone explain? A C compiler can insert paddin

Re: struct calcsize discrepency?

2011-12-04 Thread Dave Angel
On 12/04/2011 09:35 AM, Chris Angelico wrote: On Mon, Dec 5, 2011 at 1:25 AM, Glen Rice wrote: In IPython: import struct struct.calcsize('4s') 4 struct.calcsize('Q') 8 struct.calcsize('4sQ') 16 This doesn't make sense to me. Can anyone explain? Sa

Re: struct calcsize discrepency?

2011-12-04 Thread Duncan Booth
Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me. Can anyone explain? > When you mix different

Re: struct calcsize discrepency?

2011-12-04 Thread Chris Angelico
On Mon, Dec 5, 2011 at 1:25 AM, Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me.  Can anyone explai

struct calcsize discrepency?

2011-12-04 Thread Glen Rice
In IPython: >import struct >struct.calcsize('4s') 4 >struct.calcsize('Q') 8 >struct.calcsize('4sQ') 16 This doesn't make sense to me. Can anyone explain? -- http://mail.python.org/mailman/listinfo/python-list

Sockets: Receiving C Struct

2011-08-05 Thread Johnny Venter
New to python and would like to test network/sockets with it. I am having a problem where I have setup the following: import socket, sys, struct HOST = '1.1.1.1' PORT = 153 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) data = struct.unpack('!

Re: Sockets: Receiving C Struct

2011-08-05 Thread Dan Stromberg
ship was accepted. > > Please disregard if it's a duplicate. > > Thanks > > Begin forwarded message: > > *From: *Johnny Venter > *Date: *August 5, 2011 8:15:53 AM EDT > *To: *python-list@python.org > *Subject: **Sockets: Receiving C Struct* > > New to python a

Fwd: Sockets: Receiving C Struct

2011-08-05 Thread Johnny Venter
I was not sure if this message was sent before my membership was accepted. Please disregard if it's a duplicate. Thanks Begin forwarded message: > From: Johnny Venter > Date: August 5, 2011 8:15:53 AM EDT > To: python-list@python.org > Subject: Sockets: Receiving C Struct &

Re: C struct to Python

2010-11-30 Thread geremy condra
On Tue, Nov 30, 2010 at 10:57 AM, Eric Frederich wrote: > I am not sure how to proceed. > I am writing a Python interface to a C library. > The C library uses structures. > I was looking at the struct module but struct.unpack only seems to > deal with data that was packed using

C struct to Python

2010-11-30 Thread Eric Frederich
I am not sure how to proceed. I am writing a Python interface to a C library. The C library uses structures. I was looking at the struct module but struct.unpack only seems to deal with data that was packed using struct.pack or some other buffer. All I have is the struct itself, a pointer in C. Is

Re: Whining about "struct"

2010-10-14 Thread Brendan Simon (eTRIX)
On 14/10/10 5:17 PM, python-list-requ...@python.org wrote: > Subject: > Whining about "struct" > From: > Tim Roberts > Date: > Wed, 13 Oct 2010 21:30:38 -0700 > > To: > python-list@python.org > > > I have a bad memory. I admit it. Because of that,

Re: Whining about "struct"

2010-10-14 Thread Tim Golden
On 14/10/2010 05:30, Tim Roberts wrote: I have a bad memory. I admit it. Because of that, the Python "help" system is invaluable to me. Up through Python 2.5, I could get a quick reference to the format specifiers for the struct module via import struct; help(struct) I used

Re: Whining about "struct"

2010-10-14 Thread Lawrence D'Oliveiro
In message , Tim Roberts wrote: > I have a bad memory. I admit it. Because of that, the Python "help" > system is invaluable to me. I’ve tried using that occasionally, but found it’s easier by far to have a Web page open with full documentation that I can read and flip through while simultane

Re: Whining about "struct"

2010-10-13 Thread Ned Deily
In article , Tim Roberts wrote: > I have a bad memory. I admit it. Because of that, the Python "help" > system is invaluable to me. Up through Python 2.5, I could get a quick > reference to the format specifiers for the struct module via > import struct; help(struct)

Re: Whining about "struct"

2010-10-13 Thread Terry Reedy
On 10/14/2010 12:30 AM, Tim Roberts wrote: I have a bad memory. I admit it. Because of that, the Python "help" system is invaluable to me. Up through Python 2.5, I could get a quick reference to the format specifiers for the struct module via import struct; help(struct) I used

Whining about "struct"

2010-10-13 Thread Tim Roberts
I have a bad memory. I admit it. Because of that, the Python "help" system is invaluable to me. Up through Python 2.5, I could get a quick reference to the format specifiers for the struct module via import struct; help(struct) I used that a LOT. But in Python 2.6, the struct mo

Re: struct pointing to another struct?

2010-08-13 Thread Peter Otten
inhahe wrote: > On Aug 13, 4:07 pm, Peter Otten <__pete...@web.de> wrote: >> inhahe wrote: >> > say i have this definition: >> >> > 1 typedef struct SDL_Surface { >> > 2 Uint32 flags; /* Read-only */ >> > 3

Re: struct pointing to another struct?

2010-08-13 Thread inhahe
On Aug 13, 4:07 pm, Peter Otten <__pete...@web.de> wrote: > inhahe wrote: > > say i have this definition: > > >    1 typedef struct SDL_Surface { > >    2     Uint32 flags;                           /* Read-only */ > >    3     SDL_PixelFormat *format;    

Re: struct pointing to another struct?

2010-08-13 Thread Peter Otten
inhahe wrote: > say i have this definition: > >1 typedef struct SDL_Surface { >2 Uint32 flags; /* Read-only */ >3 SDL_PixelFormat *format;/* Read-only */ >4 int w, h; /* R

struct pointing to another struct?

2010-08-13 Thread inhahe
say i have this definition: 1 typedef struct SDL_Surface { 2 Uint32 flags; /* Read-only */ 3 SDL_PixelFormat *format;/* Read-only */ 4 int w, h; /* Read-only */ 5 Uint16 pitch

Re: struct

2010-05-19 Thread Gary Herron
On 05/19/2010 02:53 PM, Back9 wrote: Can anyone explain the difference between f and d in struct unpack? When using them, some data work in either one not both. To me it seems to be same, TIA 'f' is single precision float (32 bits), and 'd' is a double precision f

  1   2   3   4   5   >