_strings.py :  » Game-2D-3D » PyOpenGL » PyOpenGL-3.0.1 » OpenGL » arrays » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » PyOpenGL 
PyOpenGL » PyOpenGL 3.0.1 » OpenGL » arrays » _strings.py
"""Run-time calculation of offset into Python string structure

Does a scan to find the digits of pi in a string structure
in order to produce an offset that can be used to produce 
data-pointers from Python strings.

Porting note:
    
    Currently this uses id( str a ) to get the base address
    of the Python string.  Python implementations where id( a )
    is *not* the memory address of the string will not work!
"""
import ctypes
PI_DIGITS = '31415926535897931'

def calculateOffset( ):
    """Calculates the data-pointer offset for strings
    
    This does a sequential scan for 100 bytes from the id
    of a string to find special data-value stored in the
    string (the digits of PI).  It produces a dataPointer
    function which adds that offset to the id of the 
    passed strings.
    """
    finalOffset = None
    a = PI_DIGITS
    # XXX NOT portable across Python implmentations!!!
    initial = id(a)
    targetType = ctypes.POINTER( ctypes.c_char )
    for offset in range( 100 ):
        vector = ctypes.cast( initial+offset,targetType )
        allMatched = True
        for index,digit in enumerate( a ):
            if vector[index] != digit:
                allMatched = False
                break
        if allMatched:
            finalOffset = offset
            break
    if finalOffset is not None:
        def dataPointer( data ):
            """Return the data-pointer from the array using calculated offset
            
            data -- a Python string
            
            Returns the raw data-pointer to the internal buffer of the passed string
            """
            if not isinstance( data, str ):
                raise TypeError(
                    """This function can only handle Python strings!  Got %s"""%(
                        type(data),
                    )
                )
            return id(data) + finalOffset
        # just for later reference...
        dataPointer.offset = finalOffset 
        return dataPointer
    raise RuntimeError(
        """Unable to determine dataPointer offset for strings!"""
    )

dataPointer = calculateOffset()

if __name__ == "__main__":
    a  = 'this'
    print id(a), dataPointer( a ), dataPointer(a) - id(a)
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.