Created
May 11, 2013 20:39
-
-
Save markrwilliams/5561363 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import opcode | |
import struct | |
OPNAME = struct.Struct('B') | |
ARG = struct.Struct('H') | |
class Symbol(object): | |
def __init__(self): | |
caller_frame = sys._getframe(1) | |
ic = caller_frame.f_lasti | |
code = caller_frame.f_code.co_code | |
names = caller_frame.f_code.co_names | |
(op,) = OPNAME.unpack(code[ic]) | |
assert opcode.opname[op] == 'CALL_FUNCTION' | |
ic += (OPNAME.size + ARG.size) | |
(op,) = OPNAME.unpack(code[ic]) | |
if opcode.opname[op] != 'STORE_NAME': | |
raise RuntimeError("Must assign symbol a name!") | |
ic += OPNAME.size | |
(idx,) = ARG.unpack(code[ic:ic + ARG.size]) | |
self.name = names[idx] | |
def __repr__(self): | |
return "<Symbol '{}>".format(self.name) | |
MetaInfo = Symbol() | |
d = {MetaInfo: 1, 'a': 2} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment