[IronPython] array.fromfile

Dino Viehland dinov at exchange.microsoft.com
Fri Feb 23 18:58:32 CET 2007

We're basically doing the same thing that the array.py is doing - the difference truly is just the item size.  Python is implicitly doing the check by calling range(n) and doing the size check on each read.  We're reading everything in at once and doing the same basic size check just doing it before intsead of at each read.  They should both be equivalent though.

Is there an issue w/ switching to using the I type for the array instead of the L type?  If that seems too cumbersome we could consider changing the default size of L to match CPython (I suspect this means 4 bytes on a 32-bit platform and 8 bytes on 64-bit but I don't have a 64-bit install of CPython).

From: users-bounces at lists.ironpython.com [users-bounces at lists.ironpython.com] On Behalf Of JoeSox [joesox at gmail.com]
Sent: Thursday, February 22, 2007 5:25 PM
To: Discussion of IronPython
Subject: Re: [IronPython] array.fromfile

Ok, I found out that the length value of 260759 appears to be pulled
from a file that holds that value.  So it is the same nomatter what
the environment is.

Now, here is the logic that my array.py file was using:
self.itemsize = self._descriptor[1] """I think _descriptor[1] =
typecode L, which in this case would be 4 bytes

    def fromfile(self, f, n):
        """Read n objects from the file object f and append them to the end of
        the array. Also called as read."""
        if not isinstance(f, file):
            raise TypeError("arg1 must be open file")
        for i in range(n):
            item = f.read(self.itemsize)
            if len(item) < self.itemsize:
                raise EOFError("not enough items in file")

The full array.py may be found at
            public void FromFile(PythonFile f, int n) {
                int bytesNeeded = n * ItemSize;
                string bytes = f.Read(bytesNeeded);
                if (bytes.Length < bytesNeeded) throw
Ops.EofError("file not large enough");


I maybe incorrect but it seems that the 4 vs 8 bytes is an issue for
me. Using 1.1B, I can not use my array.py because it uses IP's first
when 'import array' is called.
Sorry, I can't think of a suggested fix right now as I have ran out of
time to look at this more today.  I am sort of confused why IP uses n
* ItemSize.

Thanks, Joe

On 2/22/07, Dino Viehland <dinov at exchange.microsoft.com> wrote:
> Depending on how similar your array.py was to CPython's built-in array the problem here may be that our long data type is 8 bytes instead of 4 like on CPython.  The documentation defines the "minimum" size and we use the native sizes for the similar .NET types.
> Try switching to using 'I' for the type instead.  You could also check the itemsize on array.py's array and see if that's the case or not.
users mailing list
users at lists.ironpython.com

More information about the Ironpython-users mailing list