ArtOfWarfare ArtOfWarfare - 1 year ago 138
Python Question

Set a Read-Only Attribute in Python?

Given how dynamic Python is, I'll be shocked if this isn't somehow possible:

I would like to change the implementation of

sys.stdout.write
.

I got the idea from this answer to another question of mine: https://stackoverflow.com/a/24492990/901641

I tried to simply write this:

original_stdoutWrite = sys.stdout.write

def new_stdoutWrite(*a, **kw):
original_stdoutWrite("The new one was called! ")
original_stdoutWrite(*a, **kw)

sys.stdout.write = new_stdoutWrite


But it tells me
AttributeError: 'file' object attribute 'write' is read-only
.

This is a nice attempt to keep me from doing something potentially (probably) stupid, but I'd really like to go ahead and do it anyways. I suspect the interpreter has some kind of lookup table its using that I can modify, but I couldn't find anything like that on Google.
__setattr__
didn't work, either - it returned the exact same error about the attribute being read-only.

I'm specifically looking for a Python 2.7 solution, if that's important, although there's no reason to resist throwing in answers that work for other versions since I suspect other people in the future will look here with similar questions regarding other versions.

Answer Source

Despite its dynamicity, Python does not allow monkey-patching built-in types such as file. It even prevents you to do so by modifying the __dict__ of such a type — the __dict__ property returns the dict wrapped in a read-only proxy, so both assignment to file.write and to file.__dict__['write'] fail. And for at least two good reasons:

  1. the C code expects the file built-in type to correspond to the PyFile type structure, and file.write to the PyFile_Write() function used internally.

  2. Python implements caching of attribute access on types to speed up method lookup and instance method creation. This cache would be broken if it were allowed to directly assign to type dicts.

Monkey-patching is of course allowed for classes implemented in Python which can handle dynamic modifications just fine.

However... if you really know what you are doing, you can use the low-level APIs such as ctypes to hook into the implementation and get to the type dict. For example:

# WARNING: do NOT attempt this in production code!

import ctypes

def magic_get_dict(o):
    # find address of dict whose offset is stored in the type
    dict_addr = id(o) + type(o).__dictoffset__

    # retrieve the dict object itself
    dict_ptr = ctypes.cast(dict_addr, ctypes.POINTER(ctypes.py_object))
    return dict_ptr.contents.value

def magic_flush_mro_cache():
    ctypes.PyDLL(None).PyType_Modified(ctypes.py_object(object))

# monkey-patch file.write
dct = magic_get_dict(file)
dct['write'] = lambda f, s, orig_write=file.write: orig_write(f, '42')

# flush the method cache for the monkey-patch to take effect
magic_flush_mro_cache()

# magic!
import sys
sys.stdout.write('hello world\n')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download