Plexnet gadgets - espians

Plexnet gadgets

From espians

Jump to: navigation, search

This page documents bits of interesting, mainly Python, code that have been produced in coming up with Espra alpha 1.

Chainable Stateful Coroutines (disco)

These are basically generators that have internal state. They act like miniature classes in a way.

@worker()
def cat(self):	 
   """A demonstration ``cat`` disco worker.
      Yields out what it gets in.
   """
   while True: 
      value = yield self.get()
      yield value

As you can see, cat has a simple state storage ("self"), an input interface ("yield self.get()"), and an output interface ("yield value"). The idea is that you can chain the coroutines together, like in unix piping:

counter(2, 10) | cat() | square() | sum()

Capabilities (capabilities)

How do you restrict the user's environment so that they can't write to the filesystem? Tav came up with a system that didn't quite work, so he asked on python-dev and got given a beautiful recipe that he was able to hash out into a working secure system. Obviously the only way to test such a thing is to try to get people to crack it; but so far it's proving impregnable.

The problem with passing open('test.txt', 'r') around is...

# First system opens a file, and passes it to another system
f = open('test.txt', 'r')
# Second system can reopen the file in write mode!
type(f)('test.txt', 'w')

Even with a closure-based system you can get the file type by inspecting func_closure[0].cell_contents. So Tav used the recipe to come up with safe.py. You do "from safe import FileReader", and your environment can no longer write to the disc.

>>> from safe import FileReader
>>> f = FileReader('test.txt')
>>> f.getter.func_closure
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'func_closure'
>>> license
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.5/site.py", line 291, in __repr__
    self.__setup()
  File "/usr/local/lib/python2.5/site.py", line 277, in __setup
    fp = file(filename, "rU")
NameError: global name 'file' is not defined
>>> 

Note that this merely demonstrates the core capability. Obviously it's not a good thing to lack even the ability to import all the time, so this is useful for when you want to make sure that code is executed in a restricted environment—when it's been passed, for example, from an external unverifiably secure source.

Custom Importing (pimp)

Not sure what the benefits of pimp are.