从numpy.ndarray __getitem__返回一个超类

时间:2015-01-28 12:49:56

标签: python numpy pyopengl

我有一些通过PyOpenGL指针提供给我的内存。我使用此指针创建BufferMapping。这个类有一个unmap方法。我希望能够使用索引和所有常用的numpy方法来操作此数组,但我想防止从子代中取消内存,以便只有一个对象负责此函数。

因此,

BufferMapping.__getitem__需要返回MappedArray的实例,该实例未定义unmap。我怎么能这样做?

此外,还有其他方法可能最终使用BufferMapping的属性或其他内容意外创建ndarray的实例吗?

我有:

class MappedArray(ndarray):
    def __new__(cls, shape, dtype, buffer, gl_buffer, gl_offset, access):
        obj = ndarray.__new__(cls, shape, dtype, buffer)
        obj.gl_buffer = gl_buffer
        obj.offset = gl_offset
        obj.access = access
        return obj

    def __array_finalize__(self, obj):
        if obj is None:
            # in __new__, will set properties there
            return
        elif isinstance(obj, BufferMapping):
            # new-from-template (indexing), copy over properties
            self.gl_buffer = obj.gl_buffer
            self.offset = obj.offset
            self.access = obj.access
        else:
            # View casting, not allowed since we need to use a 
            # GL-provided memory location.
            raise TypeError("Cannot cast to a mapped buffer.")

    def flush(self):
        """Flushes buffer writes to the GL, so they can be used in
        rendering commands."""
        if self.access & GL.GL_MAP_FLUSH_EXPLICIT_BIT:
            length = self.dtype.itemsize * product(self.shape)
            with self.gl_buffer:
                GL.glFlushMappedBufferRange(self.gl_buffer.target, 
                                            self.offset, length)
        else:
            GL.glMemoryBarrier(GL.GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT)

class BufferMapping(MappedArray):
    def unmap(self):
        """Unmaps the buffer. The array *may not* be used after this is
        executed. This implicitly flushes the buffer.
        """
        with self.gl_buffer:
            GL.glUnmapBuffer(self.gl_buffer.target)
        self.unmap = lambda: None

    def __getitem__(self, idxs):
      # ?

0 个答案:

没有答案