Python and C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • diffuser78@gmail.com

    #1

    Python and C

    I was a C Programmer for a while. Lately started to learn Python for
    one small project at school. I joined a small company where they use
    C++ for development.

    Can we use Python and C together ? I mean create some classes in Python
    and some number crunching algorithms coded in C (for speed) and
    integreate both of them.

    Could somebody just show a real small example or give pointers to the
    same.

    Thanks

  • Diez B. Roggisch

    #2
    Re: Python and C

    diffuser78@gmai l.com wrote:
    [color=blue]
    > I was a C Programmer for a while. Lately started to learn Python for
    > one small project at school. I joined a small company where they use
    > C++ for development.
    >
    > Can we use Python and C together ? I mean create some classes in Python
    > and some number crunching algorithms coded in C (for speed) and
    > integreate both of them.
    >
    > Could somebody just show a real small example or give pointers to the
    > same.[/color]

    There is even a whole section in the documentation geared towards that -
    and to spoil the fun of actually reading it: Yes, Python and C go aleong
    _very_ well, and for C++ there are several specialized wrapping generators
    that e.g. make PyQt go round. Check out SIP or boost.python or SWIG.



    Diez

    Comment

    • Diez B. Roggisch

      #3
      Re: Python and C

      > http://docs.python.org/api/api.html


      That should have been



      but you need the other one sooner or later.

      Diez

      Comment

      • Alex Martelli

        #4
        Re: Python and C

        <diffuser78@gma il.com> wrote:
        [color=blue]
        > I was a C Programmer for a while. Lately started to learn Python for
        > one small project at school. I joined a small company where they use
        > C++ for development.
        >
        > Can we use Python and C together ? I mean create some classes in Python
        > and some number crunching algorithms coded in C (for speed) and
        > integreate both of them.[/color]

        Sure! It's extremely common practice, known as "extending Python".

        [color=blue]
        > Could somebody just show a real small example or give pointers to the
        > same.[/color]

        Besides the Extending/Embedding and C API documents which are part of
        the standard set of Python docs, to which you've already been pointed in
        other responses, you can find tutorials on the net. A tiny one is at
        <http://www.developer.c om/lang/other/article.php/2191421>, with pointers
        to other materials. Also, examples can be found in the Demos directory
        of the Python source distribution -- even if you have a binary distro
        installed, get the source distro anyway, it's full of goodies and you
        can use much of Python itself as an example of Extending (the Modules
        and Objects directories of the source distro, in particular, are just
        such examples).

        C is the lowest, most fundamental level of extension, but there are many
        other alternatives -- SWIG to wrap existing libraries, Boost or SCXX or
        SIP to wrap specifically C++ with very different philosophies (template
        heavy, minimal, Qt-based), pyrex (a Python "dialect" plus C-like
        declarations to make it compilable to fast machine code), and others
        yet.


        Alex

        Comment

        • David Boddie

          #5
          Re: Python and C

          Alex Martelli wrote:[color=blue]
          > <diffuser78@gma il.com> wrote:[/color]
          [color=blue]
          > C is the lowest, most fundamental level of extension, but there are many
          > other alternatives -- SWIG to wrap existing libraries, Boost or SCXX or
          > SIP to wrap specifically C++ with very different philosophies (template
          > heavy, minimal, Qt-based), pyrex (a Python "dialect" plus C-like
          > declarations to make it compilable to fast machine code), and others
          > yet.[/color]

          It is a common misconception that SIP is only really used to wrap
          Qt-based libraries, though that may be its main use in many projects:

          "SIP is a tool that makes it very easy to create Python bindings
          for C and C++ libraries. It was originally developed to create PyQt,
          the Python bindings for the Qt toolkit, but can be used to create
          bindings for any C or C++ library."

          [http://www.riverbankcomputing.co.uk/sip/]

          David

          Comment

          • P Boy

            #6
            Re: Python and C

            I have written some C extension before but it was pretty tedious. I
            have recently found another approach by using ctypes
            (http://starship.python.net/crew/theller/ctypes/). Which you develop
            your C module in dynamic library (DLL in Windows world), the from
            Python, you can call the C functions in the DLL using ctypes.

            I have a library from Microsoft, ewfapi.dll, ewfapi.lib (not required),
            and ewfapi.h (see download) which I want to program the EWF capability
            in Windows XP Embedded. I was thinking about writing a Python C
            extension for it, but using ctypes is much easier. I only need to
            redefine some data structures from the header file, and not worry about
            common C tasks such as memory management, reference counting, etc.

            Ewfapi files can be downloaded from
            http://www.microsoft.com/downloads/d...displaylang=en

            I have some sample usage in Python shown below, which I can
            interactively control the EWF feature using Python shell (in an XP
            embedded machine).


            # ewf.py

            import ctypes
            import string

            # These are the functions from the EWF API
            ##EwfMgrGetDriv eLetterFromVolu meName(volumeNa me)
            ##
            ##EwfMgrVolumeN ameListIsEmpty( volumeEntry)
            ##
            ##EwfMgrVolumeN ameEntryPop(vol umeEntry)
            ##
            ##EwfMgrVolumeN ameListDelete
            ##
            ##EwfMgrOpenPro tected
            ##
            ##EwfMgrClose = _ewfapiPy.EwfMg rClose
            ##
            ##EwfMgrClearCo mmand = _ewfapiPy.EwfMg rClearCommand
            ##
            ##EwfMgrSetPers istentData = _ewfapiPy.EwfMg rSetPersistentD ata
            ##
            ##EwfMgrGetPers istentData = _ewfapiPy.EwfMg rGetPersistentD ata
            ##
            ##EwfMgrCheckpo int = _ewfapiPy.EwfMg rCheckpoint
            ##
            ##EwfMgrRestore = _ewfapiPy.EwfMg rRestore
            ##
            ##EwfMgrDisable = _ewfapiPy.EwfMg rDisable
            ##
            ##EwfMgrEnable = _ewfapiPy.EwfMg rEnable
            ##
            ##EwfMgrCommit = _ewfapiPy.EwfMg rCommit
            ##
            ##EwfMgrCommitA ndDisableLive = _ewfapiPy.EwfMg rCommitAndDisab leLive
            ##
            ##EwfMgrCommitF ile = _ewfapiPy.EwfMg rCommitFile
            ##
            ##EwfMgrSetLeve l = _ewfapiPy.EwfMg rSetLevel
            ##
            ##EwfMgrGetProt ectedVolumeConf ig =
            _ewfapiPy.EwfMg rGetProtectedVo lumeConfig
            ##
            ##EwfMgrGetProt ectedVolumeList = _ewfapiPy.EwfMg rGetProtectedVo lumeList
            ##
            ##EwfMgrOpenOve rlayStore = _ewfapiPy.EwfMg rOpenOverlaySto re
            ##
            ##EwfMgrGetOver layStoreConfig = _ewfapiPy.EwfMg rGetOverlayStor eConfig
            ##
            ##EwfMgrRegiste rLowSpaceNotifi cation =
            _ewfapiPy.EwfMg rRegisterLowSpa ceNotification

            # Short handle to the API
            ewfapi = ctypes.windll.e wfapi

            # Data structure from EWFAPI.h translated into Python

            # Map these enumerations to string
            EWF_CMD = { 0 : 'NO Command',
            1 : 'Enable',
            2 : 'Disable',
            3 : 'Set Level',
            4 : 'Commit',
            }

            EWF_STATE = { 0 : 'Enabled',
            1 : 'Disabled',
            }

            EWF_TYPE = { 0 : 'Disk',
            1 : 'RAM',
            2 : 'RAM Reg',
            }

            # Forward Declaration
            pVolumeNameEntr y = ctypes.POINTER( 'VolumeNameEntr y')

            # Structure of a linked-list
            class VolumeNameEntry (ctypes.Structu re):
            _fields_ = [('Next', ctypes.POINTER( pVolumeNameEntr y)),
            ('Name', ctypes.c_wchar * 256),
            ]

            # Set the pointer to the structure
            ctypes.SetPoint erType(pVolumeN ameEntry, VolumeNameEntry )


            # Volume descriptor
            class VolumeDesc(ctyp es.Structure):
            _fields_ = [('DeviceName', ctypes.c_wchar * 256),
            ('VolumeID', ctypes.c_ubyte * 16),
            ]

            pVolumeDesc = ctypes.POINTER( VolumeDesc)


            # Overlay configuration
            class OverlayStoreCon fig(ctypes.Stru cture):
            _fields_ = [('FormatVersion ', ctypes.c_ulong) ,
            ('VolumeSize', ctypes.c_longlo ng),
            ('NumSegments', ctypes.c_ulong) ,
            ('FreeSegments' , ctypes.c_ulong) ,
            ('SegmentSize', ctypes.c_ulong) ,
            ('MaxVolumes', ctypes.c_ulong) ,
            ('NumVolumes', ctypes.c_ulong) ,
            ('MaxLevels', ctypes.c_ushort ),
            ('VolumeDescArr ay', pVolumeDesc),
            ]

            pOverlayStoreCo nfig = ctypes.POINTER( OverlayStoreCon fig)

            class Command(ctypes. Structure):
            _fields_ = [('Command', ctypes.c_int),
            ('Param1', ctypes.c_ulong) ,
            ('Param2', ctypes.c_ulong) ,
            ]

            pCommand = ctypes.POINTER( Command)

            class FileTime(ctypes .Structure):
            _fields_ = [('LowDateTime', ctypes.c_ulong) ,
            ('HighDateTime' , ctypes.c_ulong) ,
            ]

            pFileTime = ctypes.POINTER( FileTime)

            class LevelDesc(ctype s.Structure):
            _fields_ = [('LevelName', ctypes.c_wchar * 64),
            ('LevelEndTime' , FileTime),
            ('LevelDataSize ', ctypes.c_longlo ng),
            ]

            pLevelDesc = ctypes.POINTER( LevelDesc)


            class VolumeConfig(ct ypes.Structure) :
            _fields_ = [('Type', ctypes.c_int),
            ('State', ctypes.c_int),
            ('BootCommand', Command),
            ('PersistentDat a', ctypes.c_ubyte * 32),
            ('MaxLevels', ctypes.c_ushort ),
            ('ClumpSize', ctypes.c_ulong) ,
            ('CurrentLevel' , ctypes.c_ushort ),
            ('DiskMap_RamDa taSize', ctypes.c_longlo ng),
            ('DiskDataSize' , ctypes.c_longlo ng),
            ('MemMapSize', ctypes.c_ulong) ,
            ('VolumeDesc', VolumeDesc),
            ('LevelDescArra y', pLevelDesc),
            ]

            def formatUnsigned( u):
            return '%02X' % u

            def unsignedArrayTo Hex(array):
            return string.join(map (formatUnsigned , array)).replace (' ','')

            def displayVolumeCo nfiguration(con fig, drive):
            config_type = EWF_TYPE[config.Type]
            print 'Type : %s' % config_type
            print 'State : %s' % EWF_STATE[config.State]
            print 'Boot Cmd : %s' % EWF_CMD[config.BootComm and.Command]
            print 'Param 1 : %u' % config.BootComm and.Param1
            print 'Param 2 : %u' % config.BootComm and.Param2
            print 'Persistent Data: %s' %
            unsignedArrayTo Hex(config.Pers istentData)
            print
            print 'Maximum Levels : %u' % config.MaxLevel s
            print 'Clump Size : %u Bytes' % config.ClumpSiz e
            print 'Current Level : %u' % config.CurrentL evel
            if config_type == 'Disk':
            print 'Disk Map Size : %u Bytes' %
            config.DiskMap_ RamDataSize
            print 'Disk Data Size : %u Bytes' % config.DiskData Size
            else:
            print 'RAM Data Size : %u Bytes' %
            config.DiskMap_ RamDataSize

            print 'Memory Map Size : %u Bytes' % config.MemMapSi ze
            print 'Device Name : %s' % config.VolumeDe sc.DeviceName
            print 'Volume ID : %s' %
            unsignedArrayTo Hex(config.Volu meDesc.VolumeID )
            print 'Drive : %s' % drive

            def information():
            # Get the pointer and convert to linked-list node
            volume_list =
            VolumeNameEntry .from_address(e wfapi.EwfMgrGet ProtectedVolume List())
            while 1:
            drive =
            chr(ewfapi.EwfM grGetDriveLette rFromVolumeName (volume_list.Na me))
            print '%s: = %s' % (drive, volume_list.Nam e)
            handle = ewfapi.EwfMgrOp enProtected(vol ume_list.Name)
            config =
            VolumeConfig.fr om_address(ewfa pi.EwfMgrGetPro tectedVolumeCon fig(handle))
            displayVolumeCo nfiguration(con fig, drive)
            if
            ewfapi.EwfMgrVo lumeNameListIsE mpty(ctypes.c_v oid_p.from_addr ess(ctypes.addr essof(volume_li st))):
            break


            if __name__ == "__main__ ":
            print 'Running'
            information()

            Comment

            • P Boy

              #7
              Re: Python and C

              I have written some C extension before but it was pretty tedious. I
              have recently found another approach by using ctypes
              (http://starship.python.net/crew/theller/ctypes/). Which you develop
              your C module in dynamic library (DLL in Windows world), the from
              Python, you can call the C functions in the DLL using ctypes.

              I have a library from Microsoft, ewfapi.dll, ewfapi.lib (not required),
              and ewfapi.h (from msdn.com) which I want to program the EWF capability
              in Windows XP Embedded. I was thinking about writing a Python C
              extension for it, but using ctypes is much easier. I only need to
              redefine some data structures from the header file, and not worry about
              common C tasks such as memory management, reference counting, etc.


              I have some sample usage in Python shown below, which I can
              interactively control the EWF feature using Python shell (in an XP
              embedded machine).


              # ewf.py


              import ctypes
              import string


              # These are the functions from the EWF API
              ##EwfMgrGetDriv eLetterFromVolu meName(volumeNa me)
              ##
              ##EwfMgrVolumeN ameListIsEmpty( volumeEntry)
              ##
              ##EwfMgrVolumeN ameEntryPop(vol umeEntry)
              ##
              ##EwfMgrVolumeN ameListDelete
              ##
              ##EwfMgrOpenPro tected
              ##
              ##EwfMgrClose = _ewfapiPy.EwfMg rClose
              ##
              ##EwfMgrClearCo mmand = _ewfapiPy.EwfMg rClearCommand
              ##
              ##EwfMgrSetPers istentData = _ewfapiPy.EwfMg rSetPersistentD ata
              ##
              ##EwfMgrGetPers istentData = _ewfapiPy.EwfMg rGetPersistentD ata
              ##
              ##EwfMgrCheckpo int = _ewfapiPy.EwfMg rCheckpoint
              ##
              ##EwfMgrRestore = _ewfapiPy.EwfMg rRestore
              ##
              ##EwfMgrDisable = _ewfapiPy.EwfMg rDisable
              ##
              ##EwfMgrEnable = _ewfapiPy.EwfMg rEnable
              ##
              ##EwfMgrCommit = _ewfapiPy.EwfMg rCommit
              ##
              ##EwfMgrCommitA ndDisableLive = _ewfapiPy.EwfMg rCommitAndDisab leLive
              ##
              ##EwfMgrCommitF ile = _ewfapiPy.EwfMg rCommitFile
              ##
              ##EwfMgrSetLeve l = _ewfapiPy.EwfMg rSetLevel
              ##
              ##EwfMgrGetProt ectedVolumeConf ig =
              _ewfapiPy.EwfMg rGetProtectedVo lumeConfig
              ##
              ##EwfMgrGetProt ectedVolumeList = _ewfapiPy.EwfMg rGetProtectedVo lumeList

              ##
              ##EwfMgrOpenOve rlayStore = _ewfapiPy.EwfMg rOpenOverlaySto re
              ##
              ##EwfMgrGetOver layStoreConfig = _ewfapiPy.EwfMg rGetOverlayStor eConfig
              ##
              ##EwfMgrRegiste rLowSpaceNotifi cation =
              _ewfapiPy.EwfMg rRegisterLowSpa ceNotification


              # Short handle to the API
              ewfapi = ctypes.windll.e wfapi


              # Data structure from EWFAPI.h translated into Python


              # Map these enumerations to string
              EWF_CMD = { 0 : 'NO Command',
              1 : 'Enable',
              2 : 'Disable',
              3 : 'Set Level',
              4 : 'Commit',
              }


              EWF_STATE = { 0 : 'Enabled',
              1 : 'Disabled',
              }


              EWF_TYPE = { 0 : 'Disk',
              1 : 'RAM',
              2 : 'RAM Reg',
              }


              # Forward Declaration
              pVolumeNameEntr y = ctypes.POINTER( 'VolumeNameEntr y')


              # Structure of a linked-list
              class VolumeNameEntry (ctypes.Structu re):
              _fields_ = [('Next', ctypes.POINTER( pVolumeNameEntr y)),
              ('Name', ctypes.c_wchar * 256),
              ]


              # Set the pointer to the structure
              ctypes.SetPoint erType(pVolumeN ameEntry, VolumeNameEntry )


              # Volume descriptor
              class VolumeDesc(ctyp es.Structure):
              _fields_ = [('DeviceName', ctypes.c_wchar * 256),
              ('VolumeID', ctypes.c_ubyte * 16),
              ]


              pVolumeDesc = ctypes.POINTER( VolumeDesc)


              # Overlay configuration
              class OverlayStoreCon fig(ctypes.Stru cture):
              _fields_ = [('FormatVersion ', ctypes.c_ulong) ,
              ('VolumeSize', ctypes.c_longlo ng),
              ('NumSegments', ctypes.c_ulong) ,
              ('FreeSegments' , ctypes.c_ulong) ,
              ('SegmentSize', ctypes.c_ulong) ,
              ('MaxVolumes', ctypes.c_ulong) ,
              ('NumVolumes', ctypes.c_ulong) ,
              ('MaxLevels', ctypes.c_ushort ),
              ('VolumeDescArr ay', pVolumeDesc),
              ]


              pOverlayStoreCo nfig = ctypes.POINTER( OverlayStoreCon fig)


              class Command(ctypes. Structure):
              _fields_ = [('Command', ctypes.c_int),
              ('Param1', ctypes.c_ulong) ,
              ('Param2', ctypes.c_ulong) ,
              ]


              pCommand = ctypes.POINTER( Command)


              class FileTime(ctypes .Structure):
              _fields_ = [('LowDateTime', ctypes.c_ulong) ,
              ('HighDateTime' , ctypes.c_ulong) ,
              ]


              pFileTime = ctypes.POINTER( FileTime)


              class LevelDesc(ctype s.Structure):
              _fields_ = [('LevelName', ctypes.c_wchar * 64),
              ('LevelEndTime' , FileTime),
              ('LevelDataSize ', ctypes.c_longlo ng),
              ]


              pLevelDesc = ctypes.POINTER( LevelDesc)


              class VolumeConfig(ct ypes.Structure) :
              _fields_ = [('Type', ctypes.c_int),
              ('State', ctypes.c_int),
              ('BootCommand', Command),
              ('PersistentDat a', ctypes.c_ubyte * 32),
              ('MaxLevels', ctypes.c_ushort ),
              ('ClumpSize', ctypes.c_ulong) ,
              ('CurrentLevel' , ctypes.c_ushort ),
              ('DiskMap_RamDa taSize', ctypes.c_longlo ng),
              ('DiskDataSize' , ctypes.c_longlo ng),
              ('MemMapSize', ctypes.c_ulong) ,
              ('VolumeDesc', VolumeDesc),
              ('LevelDescArra y', pLevelDesc),
              ]


              def formatUnsigned( u):
              return '%02X' % u


              def unsignedArrayTo Hex(array):
              return string.join(map (formatUnsigned , array)).replace (' ','')


              def displayVolumeCo nfiguration(con fig, drive):
              config_type = EWF_TYPE[config.Type]
              print 'Type : %s' % config_type
              print 'State : %s' % EWF_STATE[config.State]
              print 'Boot Cmd : %s' % EWF_CMD[config.BootComm and.Command]
              print 'Param 1 : %u' % config.BootComm and.Param1
              print 'Param 2 : %u' % config.BootComm and.Param2
              print 'Persistent Data: %s' %
              unsignedArrayTo Hex(config.Pers istentData)
              print
              print 'Maximum Levels : %u' % config.MaxLevel s
              print 'Clump Size : %u Bytes' % config.ClumpSiz e
              print 'Current Level : %u' % config.CurrentL evel
              if config_type == 'Disk':
              print 'Disk Map Size : %u Bytes' %
              config.DiskMap_ RamDataSize
              print 'Disk Data Size : %u Bytes' % config.DiskData Size
              else:
              print 'RAM Data Size : %u Bytes' %
              config.DiskMap_ RamDataSize


              print 'Memory Map Size : %u Bytes' % config.MemMapSi ze
              print 'Device Name : %s' % config.VolumeDe sc.DeviceName
              print 'Volume ID : %s' %
              unsignedArrayTo Hex(config.Volu meDesc.VolumeID )
              print 'Drive : %s' % drive


              def information():
              # Get the pointer and convert to linked-list node
              volume_list =
              VolumeNameEntry .from_address(e wfapi.EwfMgrGet ProtectedVolume List())
              while 1:
              drive =
              chr(ewfapi.EwfM grGetDriveLette rFromVolumeName (volume_list.Na me))
              print '%s: = %s' % (drive, volume_list.Nam e)
              handle = ewfapi.EwfMgrOp enProtected(vol ume_list.Name)
              config =
              VolumeConfig.fr om_address(ewfa pi.EwfMgrGetPro tectedVolumeCon fig(handle))

              displayVolumeCo nfiguration(con fig, drive)
              if
              ewfapi.EwfMgrVo lumeNameListIsE mpty(ctypes.c_v oid_p.from_addr ess(ctypes.addr ­essof(volume_l ist))):

              break


              if __name__ == "__main__ ":
              print 'Running'
              information()

              Comment

              • Terry Reedy

                #8
                Re: Python and C


                "P Boy" <prettyboy988-google@yahoo.co m> wrote in message
                news:1142028064 .100012.256170@ v46g2000cwv.goo glegroups.com.. .[color=blue]
                > I have recently found another approach by using ctypes[/color]
                (http://starship.python.net/crew/theller/ctypes/).

                Ctypes is now a 2.5 standard lib module and in being tested as such on
                multiple systems.
                [color=blue]
                > I have a library from Microsoft, ewfapi.dll, ewfapi.lib (not required),[/color]
                and ewfapi.h (from msdn.com)

                Has anyone yet written a program to grab C struct declaration from the .h
                to produce code like

                # Overlay configuration
                class OverlayStoreCon fig(ctypes.Stru cture):
                _fields_ = [('FormatVersion ', ctypes.c_ulong) ,
                ('VolumeSize', ctypes.c_longlo ng),
                ('NumSegments', ctypes.c_ulong) ,
                ('FreeSegments' , ctypes.c_ulong) ,
                ('SegmentSize', ctypes.c_ulong) ,
                ('MaxVolumes', ctypes.c_ulong) ,
                ('NumVolumes', ctypes.c_ulong) ,
                ('MaxLevels', ctypes.c_ushort ),
                ('VolumeDescArr ay', pVolumeDesc),
                ]
                ?
                Would save tedium and errors and make ctypes even more helpful.

                Terry Jan Reedy



                Comment

                • Alex Martelli

                  #9
                  Re: Python and C

                  David Boddie <davidb@mcs.s t-and.ac.uk> wrote:
                  [color=blue]
                  > Alex Martelli wrote:[color=green]
                  > > <diffuser78@gma il.com> wrote:[/color]
                  >[color=green]
                  > > C is the lowest, most fundamental level of extension, but there are many
                  > > other alternatives -- SWIG to wrap existing libraries, Boost or SCXX or
                  > > SIP to wrap specifically C++ with very different philosophies (template
                  > > heavy, minimal, Qt-based), pyrex (a Python "dialect" plus C-like
                  > > declarations to make it compilable to fast machine code), and others
                  > > yet.[/color]
                  >
                  > It is a common misconception that SIP is only really used to wrap
                  > Qt-based libraries, though that may be its main use in many projects:
                  >
                  > "SIP is a tool that makes it very easy to create Python bindings
                  > for C and C++ libraries. It was originally developed to create PyQt,
                  > the Python bindings for the Qt toolkit, but can be used to create
                  > bindings for any C or C++ library."[/color]

                  Ah, I see that SIP 4 is much better -- I was more experienced with 3.*,
                  which, what between lack of docs, no support for C, etc, was less
                  suitable for such a general role. If SIP 4 can be installed quite
                  independently of Qt, then it's indeed grown to the stature you mention.


                  Alex

                  Comment

                  • P Boy

                    #10
                    Re: Python and C

                    > Has anyone yet written a program to grab C struct declaration from the .h[color=blue]
                    > to produce code like
                    >
                    > # Overlay configuration
                    > class OverlayStoreCon fig(ctypes.Stru cture):
                    > _fields_ = [('FormatVersion ', ctypes.c_ulong) ,
                    > ('VolumeSize', ctypes.c_longlo ng),
                    > ('NumSegments', ctypes.c_ulong) ,
                    > ('FreeSegments' , ctypes.c_ulong) ,
                    > ('SegmentSize', ctypes.c_ulong) ,
                    > ('MaxVolumes', ctypes.c_ulong) ,
                    > ('NumVolumes', ctypes.c_ulong) ,
                    > ('MaxLevels', ctypes.c_ushort ),
                    > ('VolumeDescArr ay', pVolumeDesc),
                    > ]
                    > ?[/color]

                    http://starship.python.net/crew/thel...s/codegen.html says it can
                    be done. However, it requires MSVC 7.1 and I have not yet install it
                    since I already have MSVC (6,7,8) on my PC.

                    My header file is relatively small. The manual translation exercise
                    also allowed me to understand more details about ctypes.

                    Comment

                    • Terry Reedy

                      #11
                      Re: Python and C


                      "P Boy" <prettyboy988-google@yahoo.co m> wrote in message
                      news:1142047621 .068618.176350@ j52g2000cwj.goo glegroups.com.. .[color=blue][color=green]
                      >> Has anyone yet written a program to grab C struct declaration from the
                      >> .h
                      >> to produce code like
                      >>
                      >> # Overlay configuration
                      >> class OverlayStoreCon fig(ctypes.Stru cture):
                      >> _fields_ = [('FormatVersion ', ctypes.c_ulong) ,
                      >> ('VolumeSize', ctypes.c_longlo ng),[/color][/color]
                      etc[color=blue]
                      >
                      > http://starship.python.net/crew/thel...s/codegen.html says it can
                      > be done. However, it requires MSVC 7.1 and I have not yet install it
                      > since I already have MSVC (6,7,8) on my PC.[/color]

                      Or vc6. Neither of which most people have, especially those not using Swig
                      because they don't do C. Ok, we need program entirely in Python. Possible
                      exercise for someone.
                      [color=blue]
                      > My header file is relatively small. The manual translation exercise
                      > also allowed me to understand more details about ctypes.[/color]

                      Sure, the first time I would do such also.

                      tjr



                      Comment

                      • Thomas Heller

                        #12
                        Re: Python and C

                        Terry Reedy wrote:[color=blue]
                        > "P Boy" <prettyboy988-google@yahoo.co m> wrote in message
                        > news:1142047621 .068618.176350@ j52g2000cwj.goo glegroups.com.. .[color=green][color=darkred]
                        >>> Has anyone yet written a program to grab C struct declaration from the
                        >>> .h
                        >>> to produce code like
                        >>>
                        >>> # Overlay configuration
                        >>> class OverlayStoreCon fig(ctypes.Stru cture):
                        >>> _fields_ = [('FormatVersion ', ctypes.c_ulong) ,
                        >>> ('VolumeSize', ctypes.c_longlo ng),[/color][/color]
                        > etc[color=green]
                        >> http://starship.python.net/crew/thel...s/codegen.html says it can
                        >> be done. However, it requires MSVC 7.1 and I have not yet install it
                        >> since I already have MSVC (6,7,8) on my PC.[/color]
                        >
                        > Or vc6. Neither of which most people have, especially those not using Swig
                        > because they don't do C. Ok, we need program entirely in Python. Possible
                        > exercise for someone.
                        >[/color]

                        Alan Green apparently has got it to work with the free vctoolkit:




                        Thomas

                        Comment

                        Working...