Low level I/O: because I could

eryk sun eryksun at gmail.com
Wed May 10 23:49:58 EDT 2017


On Wed, May 10, 2017 at 10:30 PM, Rob Gaddi
<rgaddi at highlandtechnology.invalid> wrote:
> Sorry, but I'm just too proud of this.
>
> Given that you have:
>
>   class RegisterLayout(ctypes.Structure):
>     ...yadayadayada...
>
> You can then:
>
>   fh = os.open('/dev/devicethingy', os.O_RDWR)
>   mm = mmap.mmap(fh, ctypes.sizeof(RegisterLayout))
>   registers = RegisterLayout.from_buffer(mm)
>
> And it just works.  Behaves exactly the same way memory-mapping that struct
> in C would.  Sure the accesses take dict lookups, and that definitely slows
> you down a bit.  If you REALLY really needed that speed you'd be writing C.
> But it works.

To clarify, the dict lookup here is to bind the CField data descriptor
from the class dict. It isn't using the instance dict.



More information about the Python-list mailing list