From 4d71acd5747b58baa844ab1b7cc17fe6aa6ff691 Mon Sep 17 00:00:00 2001 From: Google Code Exporter Date: Fri, 11 Dec 2015 05:01:31 -0500 Subject: [PATCH] Migrating wiki contents from Google Code --- CompilingExtensionModules.md | 81 ++++++++++++++++++++++ CompilingStandaloneExecutables.md | 111 ++++++++++++++++++++++++++++++ GeneratingWrappers.md | 57 +++++++++++++++ ProjectHome.md | 6 ++ UnderstandingRpython.md | 13 ++++ 5 files changed, 268 insertions(+) create mode 100644 CompilingExtensionModules.md create mode 100644 CompilingStandaloneExecutables.md create mode 100644 GeneratingWrappers.md create mode 100644 ProjectHome.md create mode 100644 UnderstandingRpython.md diff --git a/CompilingExtensionModules.md b/CompilingExtensionModules.md new file mode 100644 index 0000000..b63de54 --- /dev/null +++ b/CompilingExtensionModules.md @@ -0,0 +1,81 @@ +#caching CPython extension modules. + +# Introduction # + +It is easier to migrate code to RPython in stages, and keeping the rest of your code regular Python. Two decorators let your mark functions and objects to be compiled and cached on demand. You can set the cache location at the top of your script. If there is no cached module the first time the script runs, RPythonic will look for GCC and compile a new module and load it at runtime. If GCC is not found, the program can continue to run as long as your RPython code is not using RFFI. + +If you need to distributing pre-compiled modules to clients without GCC. You can just copy the module from your local cache and include it with your script. Note that the cached module can be used with all Python versions because it is loaded by ctypes. + + +# Overview # + * import rpythonic + * decorate functions and objects + * call rpythonic.cache() + +## Example1 - Functions ## +![http://4.bp.blogspot.com/-kclx9Uhcbzs/TfHzFFsMhjI/AAAAAAAAAHk/fkTk1UKhCo4/s1600/Screenshot-10.png](http://4.bp.blogspot.com/-kclx9Uhcbzs/TfHzFFsMhjI/AAAAAAAAAHk/fkTk1UKhCo4/s1600/Screenshot-10.png) + +``` +import rpythonic +rpythonic.set_pypy_root( '../../pypy' ) +################################ +rpy = rpythonic.RPython() + +@rpy.bind() # declare arg types is optional if, +def add( a=1, b=1000 ): # keyword defaults are given + return a+b + +@rpy.bind(a=float, b=float) +def sub( a, b ): + return a-b + +rpy.cache('test1', refresh=1) # only compiles if cache is dirty +``` + +## Example2 - Shared Objects ## +![http://1.bp.blogspot.com/-2FjJyK3v9YM/TfHzFaVMvEI/AAAAAAAAAHs/rXxehIq9T9I/s1600/Screenshot-11.png](http://1.bp.blogspot.com/-2FjJyK3v9YM/TfHzFaVMvEI/AAAAAAAAAHs/rXxehIq9T9I/s1600/Screenshot-11.png) + +``` +import rpythonic +rpythonic.set_pypy_root( '../../pypy' ) +rpy = rpythonic.RPython() + +@rpy.object +class MyRpyObject(object): + def __init__(self, x=.0, y=.0, z=.0): + self.x = x; self.y = y; self.z = z + def add( self, x=.0,y=.0,z=.0 ): self.x += x; self.y += y; self.z += z + def sum( self ): return self.x + self.y + self.z + def show( self ): print '<%s %s %s>' %(self.x, self.y, self.z) + +rpy.cache('test2', refresh=1) + +o = MyRpyObject() +print o.x +o.add( 1,2,3 ) +print o.x +o.show() + +lock = thread.allocate_lock() + +GO = True +def ctypes_thread(): + print( 'thread start' ) + while GO: + lock.acquire() + o.add( 0.1, 0.2, 0.3 ) + lock.release() + print( 'thread exit' ) + + +thread.start_new_thread( ctypes_thread, () ) + +for i in range(10000*10): + # getting values from _pointer.contents is thread safe even without locks + print o.x, o.y, o.z + + lock.acquire() + o.show() # not thread safe + lock.release() + +``` \ No newline at end of file diff --git a/CompilingStandaloneExecutables.md b/CompilingStandaloneExecutables.md new file mode 100644 index 0000000..8fbc1ed --- /dev/null +++ b/CompilingStandaloneExecutables.md @@ -0,0 +1,111 @@ +#compiling standalone executables for Linux and Android. + +# Introduction # + +It is a quick process to compile a standalone for Linux, just download the latest PyPy source code, set the path in your script, and run the example below. + +Compiling standalone's for Android is more complex and still experimental; you will need to download the Android SDK and NDK, get the PyPy source code and modify it. + + +# Overview # + * import rpythonic + * create a RPython instance, and set the target type + * decorate the entry-point as standalone + * call rpy.compile() + + +## Example - OpenGL SDL ## +``` +import rpythonic +rpythonic.set_pypy_root( '../../pypy' ) +rpy = rpythonic.RPython( 'linux' ) +gl = rpy.rimport( 'openGL', namespace=globals() ) +glu = rpy.rimport( 'openGLU', namespace=globals() ) +sdl = rpy.rimport( 'SDL' ) + +def reset_viewport( width, height ): + ratio = float( width ) / float( height ) + glViewport(0, 0, width, height) + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + #/* Set our perspective */ + gluPerspective(45.0, ratio, 0.1, 100.0); + + #/* Make sure we're chaning the model view and not the projection */ + glMatrixMode(GL_MODELVIEW); + + #/* Reset The View */ + glLoadIdentity(); + +@rpy.standalone +def myentrypoint(): + vec3_t = rffi.CFixedArray( rffi.DOUBLE, 3 ) + vec4_t = rffi.CFixedArray( rffi.DOUBLE, 4 ) + colors_t = rffi.CFixedArray( vec4_t, 4 ) + verts_t = rffi.CFixedArray( vec3_t, 4 ) + + colors = lltype.malloc( colors_t, flavor='raw' ) + verts = lltype.malloc( verts_t, flavor='raw' ) + for i in range(4): + colors[i][0] = 1.0 + if i==0: verts[i][1] = 1.0 + elif i==1: verts[i][0] = -1.0; verts[i][1] = -1.0 + elif i==2: verts[i][0] = 1.0; verts[i][1] = -1.0 + + + width = 320; height = 240 + sdl.SDL_Init( sdl.SDL_INIT_VIDEO ) + sdl.SDL_SetVideoMode( width, height, 32, sdl.SDL_GL_ACCELERATED_VISUAL )#SDL_GL_DOUBLEBUFFER ) + sdl.SDL_WM_SetCaption( 'hello world', None ) + + #/* Enable smooth shading */ + glShadeModel(GL_SMOOTH); + + #/* Set the background black */ + glClearColor(0.0, 0.0, 0.0, 0.0); + + #/* Depth buffer setup */ + glClearDepth(1.0); + + #/* Enables Depth Testing */ + glEnable(GL_DEPTH_TEST); + + #/* The Type Of Depth Test To Do */ + glDepthFunc(GL_LEQUAL); + #/* Really Nice Perspective Calculations */ + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + + reset_viewport( width, height ) + + for i in range( 1000 ): + sdl.SDL_PumpEvents() + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) + + #/* Enable COLOR and VERTEX arrays */ + glEnableClientState(GL_COLOR_ARRAY); + glEnableClientState(GL_VERTEX_ARRAY); + + #/* Setup pointers to COLOR and VERTEX arrays */ + #glColorPointer(4, GL_FLOAT, 0, colors); + #glVertexPointer(3, GL_FLOAT, 0, verts); + glColorPointer(4, GL_DOUBLE, 0, colors); + glVertexPointer(3, GL_DOUBLE, 0, verts); + + #/* Move Left 1.5 Units And Into The Screen 6.0 */ + glLoadIdentity(); + glTranslatef(-1.5, 0.0, -6.0); + glDrawArrays(GL_TRIANGLES, 0, 3); + + + sdl.SDL_GL_SwapBuffers() + time.sleep(0.01) + + sdl.SDL_Quit() + + +package = rpy.compile() +package.test() +package.save( './openGLES-test1' ) +``` \ No newline at end of file diff --git a/GeneratingWrappers.md b/GeneratingWrappers.md new file mode 100644 index 0000000..54c52cb --- /dev/null +++ b/GeneratingWrappers.md @@ -0,0 +1,57 @@ +#generating wrappers to C libraries. + +# Introduction # + +Its likely you will need to work with several C libraries in your project. Writing wrappers by hand is very slow, error prone, and tedious. Other older wrapper generators are usually based on GccXML, which is no longer maintained. RPythonic integrates with the latest PyCParser to generate ctypes and RFFI bindings. The ctypes bindings will generate an object oriented interface, and work like many hand-written wrappers hiding the ugly ctypes details as much as possible. The process is very simple, just a single function call, rpythonic.wrap( 'name', 'path/to/myheader.h' ) + + +![http://2.bp.blogspot.com/-oVoPWCvKERU/TfHykGDmzUI/AAAAAAAAAHc/iNJ-9cMoh5M/s1600/Screenshot-9.png](http://2.bp.blogspot.com/-oVoPWCvKERU/TfHykGDmzUI/AAAAAAAAAHc/iNJ-9cMoh5M/s1600/Screenshot-9.png) + +# Overview # + * import rpythonic + * call rpythonic.wrap( 'name', 'path/to/header.h' ) + +## Simple Example ## +``` +mod = rpythonic.load( 'SDL' ) +if not mod: # if not found in cache, generate new wrapper + rpythonic.wrap( + 'SDL', + header='/usr/include/SDL/SDL.h', + ctype=True, # defaults to true + rffi=True, # generate RFFI wrappers, defaults to false + ) + +``` + + + +## Mix Custom Wrapper Code Example ## +``` +mod = rpythonic.load( 'ode' ) +if not mod: + defines = ['dDOUBLE'] + footer = ''' +### ode headers sometimes define a return type as a pointer when it might be a vec3 or vec4 ### +Vector3 = ctypes.c_double * 3 +Vector4 = ctypes.c_double * 4 +def _ode_convert_to_vector3_( pointer=None ): + v = ctypes.cast( pointer, ctypes.POINTER(Vector3) ) + return v.contents[0], v.contents[1], v.contents[2] +def _ode_convert_to_vector4_( pointer=None ): + v = ctypes.cast( pointer, ctypes.POINTER(Vector4) ) + return v.contents[0], v.contents[1], v.contents[2], v.contents[3] + +for func in ( dBodyGetPosition, dBodyGetRotation, dBodyGetLinearVel, dBodyGetAngularVel, dBodyGetForce, dBodyGetTorque ): + func.return_wrapper = _ode_convert_to_vector3_ + func.object_oriented = True + +for func in ( dBodyGetQuaternion, ): + func.return_wrapper = _ode_convert_to_vector4_ + func.object_oriented = True +########### end of manual patch ######### + ''' + rpythonic.wrap( 'ode', header='/usr/include/ode/ode.h', + defines=defines, rffi=True, ctypes_footer=footer + ) +``` \ No newline at end of file diff --git a/ProjectHome.md b/ProjectHome.md new file mode 100644 index 0000000..008e73f --- /dev/null +++ b/ProjectHome.md @@ -0,0 +1,6 @@ +RPythonic is a frontend for using [RPython](http://codespeak.net/pypy/dist/pypy/doc/coding-guide.html#restricted-python) (the translation toolchain of PyPy), it simplifies: wrapper generation, compiling standalone programs and Python extension modules. It also generates wrappers for C libraries using ctypes that can be loaded by CPython2, CPython3, and PyPy. Rpythonic also aims to advance the Rpython language by removing some of its limitations, and making it more useful as a general purpose language. + +## Main Features ## + * compiling RPython standalone executables + * compiling RPython extension modules (for Python) + * automatic integration with C libraries using ctypes generated wrappers \ No newline at end of file diff --git a/UnderstandingRpython.md b/UnderstandingRpython.md new file mode 100644 index 0000000..14fdd79 --- /dev/null +++ b/UnderstandingRpython.md @@ -0,0 +1,13 @@ +#dive into Rpython + +# Dave Beazley's talk from January 12, 2012 # + + + +# Brett's Quick Intro # + + + +# Dave Beazley's talk from February 9, 2012 # + + \ No newline at end of file