> > (If anyones interested, Ive been experimenting with
> allowing file-like
> > objects to choose their own character encoding when a unicode string
> > is printed)
>
> Cool. How does bytecodehacks help, out of curiosity?
The regular print converts stuff using str() - which will raise an exception
in a unicode object that contains non-ascii characters.
Python 2.0 adds two new instructions in support of the new sytax that lets
you
specify the file to which stuff gets printed (print >> file, stuff). Im
converting
those instructions into method calls into the file object.
def hook_print_to(function):
func = Function(function)
cs = func.func_code.co_code
i = 0
while i < len(cs):
if cs[i].__class__ == PRINT_ITEM_TO:
cs[i:i+1] =
[LOAD_ATTR('print_item_to'),ROT_TWO(),CALL_FUNCTION(1),POP_TOP()]
i=i+4
elif cs[i].__class__ == PRINT_NEWLINE_TO:
cs[i:i+1] =
[LOAD_ATTR('print_newline_to'),CALL_FUNCTION(0),POP_TOP()]
i=i+1
else:
i=i+1
return func.make_function()
|