Skip to content

Instantly share code, notes, and snippets.

@Tinche
Created April 20, 2017 22:49
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 Tinche/678a2f97f656421ab65338ea678df354 to your computer and use it in GitHub Desktop.
Save Tinche/678a2f97f656421ab65338ea678df354 to your computer and use it in GitHub Desktop.
from cpython.object cimport descrgetfunc, Py_TYPE
cdef class FrozenSlotDescriptor(object):
"""
Wraps the normal Python slot descriptor, disabling write access.
"""
cdef descrgetfunc get
cdef object descriptor
def __cinit__(self, orig):
self.descriptor = orig
self.get = Py_TYPE(orig).tp_descr_get
def __get__(self, obj, type):
return self.get(self.descriptor, obj, type)
def __set__(self, obj, value):
raise AttributeError('This attribute is frozen.')
def __delete__(self, obj):
raise AttributeError('This attribute is frozen.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment