Top Banner
Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll
69

Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

May 23, 2018

Download

Documents

vanminh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

ctypes

Direct access to happiness.dll

Page 2: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Who is this guy

PyOpenGL lead developer(other stuff too)

OpenGLContext, SimpleParse, StarPy, TTFQuery, BasicProperty, PyDispatcher

(and a consultant on VoIPand other stuff)

Page 3: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

What I've done lately

Rewrote PyOpenGL from SWIG to ctypes

(Why?)

Page 4: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Python will be faster

Page 5: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Because it must

The language is fine

We need speed to expand intonew areas (e.g. games)

Page 6: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Or PyPy will replace it

Or IronPython

Or OLPC-Python

Or Parrot

Or something else...

Page 7: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

(And no...)

We really don't care

about a new print syntax

Page 8: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

As Python accellerates

We can think about replacing C

(Particularly C extensions)

Page 9: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Extensions were about

Speed

(zoom, zoom)

Page 10: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

But also

Pre-written libraries of code

Page 11: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

When sPython

~= sC

(Get working on this peoples)

Page 12: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

We still need

Access to pre-written libraries of code

(From pure Python)

Page 13: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Lucky we already have it

It's been around for years

Page 14: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Hacker's ctypes(old school)

>>> import ctypes>>> happiness = ctypes.cdll.LoadLibrary( './happiness.so')>>> happiness.hello( 'Hello world %i\n', 42 )Hello world 4215 # return value

Page 15: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Yawn

(In a mind blowingly “cool” sort of way)

Page 16: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

That is so 2004

Just a hacker's toy

Page 17: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

But it was COOL

We could poke deep in the machine

Twiddle random bits

Make things happen

Page 18: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Um, we said “Yawn”

Hacker's backwater for years

(No-one really cared)

Page 19: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

What's different?

Standard library inclusion

Page 20: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

What's different?

Automated code generation

Page 21: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

What's different?

PyPy support

Page 22: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

What's different?

Numpy Support

Page 23: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

What's different?

Bigger projects possible

Page 24: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Bigger, you say?

Comtypes

Pygame-ctypes

Pyglet

PyOpenGL

Page 25: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

PyOpenGL Scale

2189 C functions

3475 constant definitions

262 extension modules

6 possible core versions

Page 26: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Where we came from

1.x Manual Wrapping

2.x SWIG Wrapping with custom distutils

Page 27: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

1.x died years ago

Manual wrapping way too time consuming

(This is where I came into the picture, life support for a dying project)

Page 28: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Really

No one wants to code in C

Page 29: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

The promised land

SWIG

(Tarn decided to use this to rewrite the project)

Page 30: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Macro headaches

Level upon levels of macro expansionSWIG typemaps, SWIG macros, macros, macro-

expanded utility libraries(Easy to write, extremely difficult to maintain)

Page 31: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

C compilation problems

Complex build process

Edit/compile/run cycle of 20 minutes+

Togl build procedure constantly broken

No compiler on one platform

Page 32: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Developer fatigue

No one wanted to do the day-to-day stuff

(Because it was such a pain)

Page 33: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Few joined, none stayed

Figuring out how to start was way too hard

Page 34: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

So I was going to dump PyOpenGL

Wasn't enough fun to spend my free time on it

Page 35: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Won't someone think of the users?

70+ downloads a day

(outside the distributions or applications)

Page 36: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Won't someone think of the users?

Hundreds, maybe thousands of applications

(Science, extension systems, graphic libraries)

Page 37: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

We should try ctypes

(Rene suggested it IIRC)

Page 38: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

First tests

Could create C-like API easily with custom (hacky) auto-generation

Page 39: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

But it wasn't compatible

So I ignored it for a while

(And things got worse)

Page 40: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Second tests

Not “can we wrap OpenGL”

“Can we create PyOpenGL”

Page 41: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

The goal

Fully compatible with PyOpenGL 2.x(reasonably compatible)

With half a dozen new fixes/features

Full extension coverage

Page 42: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

A few bugs early on

AMD64 platform issues, mostly

Page 43: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

But we could make it work

So we did

Page 44: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Array handling

3 different array systems (now pluggable)

Fairly trivial, it turns out(Even easier now)

Page 45: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

No high-level automation

No automated type/name matching

(Yay, wrote it in Python)

Page 46: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Library loading

Platform specific (no big deal)

Needs to be available (hmm)Needs to be dynamic library

Page 47: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

No C++

We don't care, others will

Page 48: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Macro problems

We don't have a lot of them

Just hacked around them in Python

Page 49: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Speed problems

2-5x slower

(Delicious irony of slowing down to speed up)

Page 50: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Documentation

Not as extensive as you'd want

Page 51: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

What we got

Page 52: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

No C coding

No one wants to code in C

Page 53: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Lower barrier to entry

Hack from day one

Page 54: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Easier to contribute

Developer's “hacks” integrate easily

Page 55: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Easier to install

easy_install PyOpenGL

Page 56: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Easier to build

(do nothing)

Page 57: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Easier to debug

Walk through the whole wrapping process

Page 58: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

More coverage

Core library 1.3 through 2.0 (automatic)

All registered extensions

Silly little regex script creates them(Pyglet guys have a

more advanced wrapper)

Page 59: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

New features

Pluggable data-format support

Optional logged operation

Run-time binding (dll substitution)

Page 60: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Compatible

Some users don't even realise it's a new technology, it's just “3.0”

Page 61: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

We're future-facing again

Type inferencing ready

Page 62: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

We're future-facing again

PyPy ready

Page 63: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

We're future-facing again

Numpy compatible

(pluggable data-types throughout)

Page 64: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

I'm motivated again

Development is fun

Page 65: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Why ctypes?

You have libraries in C you want to use

(Loadable libraries without many macros)

Page 66: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Why ctypes?

You want to code in Python

(No-one wants to code in C)

Page 67: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Why ctypes?

You want to avoid usershaving to compile your code

(Drop a pure-Python .egg)

Page 68: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

You want to code Python

Because Python is fun

(and no-one wants to code in C)

Page 69: Direct access to happiness - Mike Fletcher · Mike C. Fletcher – VRPlumber Consulting Inc. ctypes Direct access to happiness.dll

Mike C. Fletcher – VRPlumber Consulting Inc.

Why ctypes?

Because you want to load happiness.dll

into your namespace