[Pythonmac-SIG] In Mac/Modules/file/_Filemodule.c dataLogicalSize, etc., should be UInt64 ("L")

Bob Ippolito bob at redivi.com
Tue Nov 1 02:51:54 CET 2005


On Oct 31, 2005, at 5:26 PM, Laura McColm wrote:

> Greetings All!
>
>     I am moving a document management app from C++/OS9 to Python/ 
> OSX Carbon and I am delighted with the functionality I'm finding in  
> the MacOS specific extension modules.  Just what I needed!  But I  
> was dismayed to be unable to get anything but the value 0L for the  
> file sizes (dataLogicalSize, dataPhysicalSize, rsrcLogicalSize,  
> rsrcPhysicalSize) in the catinfo structure returned by  
> FSRef.FSGetCatalogInfo().
>
>     Python 2.3 and 2.4.2 (and 2.5!) on MacOS 10.3.9 returns 0L for  
> the various file size
> values and correct values for other elements in the structure such  
> as parentDirID:

That's a bug :)

> I'm new to the mechanisms inside the extensions themselves, but it  
> looks to me that the routines building the return values (such as  
> FSCatalogInfo_get_dataLogicalSize() in source file Mac/Modules/file/ 
> _Filemodule.c) should specify UInt64 instead of UInt32.
>
> static PyObject *FSCatalogInfo_get_dataLogicalSize(FSCatalogInfoObject
> *self, void *closure)
> {
>         return Py_BuildValue("l", self->ob_itself.dataLogicalSize);
>
> }
>
> Should change the "l" to "L" in each of the four routines:

Should be "K" actually, those are UInt64.. though, god help you if  
your file is big enough to hit the sign bit on a 64-bit number! :)

> But my questions are:
>
> Has anyone else seen this?  The os.stat() function returns a "unix- 
> like" file size, i.e. data fork only, and I needed the "Mac-like"  
> size (catinfo.dataLogicalSize+catinfo.rsrcLogicalSize)
>
> Does this seem like the right fix?  Am I  catching all the  
> implications?

Well if you're hard pressed to get something that works right now  
without building a custom Python, you can try:

def getResourceSize(fn):
     if os.path.exists(fn + "/..namedfork/rsrc"):
         # "Private" API for HFS partitions
         return os.path.getsize(fn + "/..namedfork/rsrc")
     if os.path.exists("._" + fn):
         # Non-HFS API for storing resforks
         return os.path.getsize("._" + fn)
     return 0

def getFullSize(fn):
     return os.path.getsize(fn) + getResourceSize(fn)

Someday this might break if Apple decides to support extended  
attributes on UFS/NFS/etc. partitions and to store the resource forks  
there instead of in "._" files, though.  That's probably unlikely  
though.

> There is another routine in _Filemodule.c:
>
> static int FSCatalogInfo_tp_init(PyObject *_self, PyObject *_args,  
> PyObject *_kwds)
> {
>     static char *kw[] = {
> [...]
>                 "dataLogicalSize",
>                 "dataPhysicalSize",
>                 "rsrcLogicalSize",
>                 "rsrcPhysicalSize",
> [...]
>                 , 0};
>
>     if (!PyArg_ParseTupleAndKeywords(_args, _kwds, "| 
> HhllO&O&O&O&O&llllllb", kw, &((FSCatalogInfoObject *)_self)- 
> >ob_itself.nodeFlags,
> [...]
>
>
> This also seems to hold information about parameter size.  Should  
> these four "l"'s be changed as well? (My intuition/luck is running  
> out here, guess I'll have to read up on what all of these  
> mechanisms are actually doing :-)

Yeah, they should be changed.  The Python C API is quite simple and  
well documented, easy to double-check your (almost correct) guesses:
http://docs.python.org/api/

> If this is a good patch, what's the best place to submit this?  
> Sourceforge?  This mailing list?

Sourceforge

> Since I'm porting a MacOS9 application from C++ to Python I'm  
> prepared to spend some time with Python and the extension  
> mechanisms in the
> near future!

Normally you shouldn't have to deal with Python's internals, but the  
MacOS-specific stuff that ships with Python was all written in the  
pre-Carbon era and a lot of it is/was automatically generated and  
never verified against reality.  I'd avoid anything in the MacOS or  
Carbon packages unless you absolutely need that functionality and you  
can't get it elsewhere (e.g. from Cocoa via PyObjC).

-bob



More information about the Pythonmac-SIG mailing list