Skip to content

Instantly share code, notes, and snippets.

@o11c
Created May 7, 2013 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save o11c/5536409 to your computer and use it in GitHub Desktop.
Save o11c/5536409 to your computer and use it in GitHub Desktop.
fragment of core.py
class Type:
def __new__(cls, raw, context):
''' Deeply magic wrapper that constructs a Type from a C Type*.
It will always return the same object as long as it exists,
which will actually be an instance of a subclass.
'''
assert isinstance(raw, _core.Type)
assert isinstance(context, Context)
assert cls == Type # subclasses must override it
if not raw:
return None
raw_ptr = _c.pointer_value(raw)
try:
return context.type_cache[raw_ptr]
except KeyError:
pass
kind = _core.GetTypeKind(raw)
self = object.__new__(Type.kind_type_map[kind])
context.type_cache[raw_ptr] = self
self._raw = raw
self._context = context
return self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment