t=io.open("/dev/random","r")
--This will print "r".
print(io.getmode(t))
--And probably this would also work but I haven't tested it: t:getmode()
I tried that last line and it didn't work. I expected that the metatable of file handles (all of them have the same metatable right?) would be resolving any missing functions called upon those handles by looking into the io table. But it looks like this is not the case. Does anyone know why when I add methods to the io table, they don't get "added" to file handles like filehandle:myossumfunction(). Conversely, why does this not raise an error:
io.close=nil
h=io.open("something","r")
h:close()
I thought that I could remove methods from tables by setting them to nil in order to create sandboxed versions of the basic tables. But being able to call close as a method on the file handle pretty much ruins that. Then how do you properly sandbox io functions? Do I have to wrap io.open to store internally (in an upvalue) the actual handles and return some other object to the user that lacks the original :read :close etc methods?