[Tutor] Pack/Unpack hacking

Kent Johnson kent37 at tds.net
Sun Sep 6 21:44:37 CEST 2009


On Sun, Sep 6, 2009 at 2:20 PM, Tino Dai <oberoc at gmail.com> wrote:
> Hi All,
>
>     Hope the people in the US are having a nice Labor Day! I am looking for
> the source code
> for the pack/unpack functions found in the struct package. As of this email,
> I have tried a
> strings on the struct.pyc file. The inspection of the pyc file was hoping
> that I could find a
> stub to the source. I also looked directly at struct.py, with no success.
> Finally, I also tried
> downloading the source and grepping through the files, which didn't prove
> all that useful.
> Does anybody have any ideas on how I can find the source without having to
> go through
> the entire source tree file by file?

In general, xxx.pyc is the compiled Python bytecode for xxx.py, so
struct.py is the source for struct.pyc.

Looking at struct.py, it's entire contents is
from _struct import *
from _struct import _clearcache

This is a pretty common idiom in the std lib for modules that are
implemented partially or completely as C extensions - there is a
Python wrapper, called xxx.py, which imports functions from a C
extension called _xxx. Often there are some functions in the Python
module; in this case, the implementation is entirely in _struct and
struct.py is just a shell.

The source for C extension modules in the std lib is in the Modules
folder. Look for _struct.c.

BTW another common convention is for modules that are implemented
entirely in C; they will have source in Modules/xxxmodule.c, for
example datetimemodule.c.

Kent


More information about the Tutor mailing list