Mihnea Giurgea
Mihnea Giurgea

Reputation: 447

How to avoid overriding attributes of system libraries in Python?

Say we have a buggy piece of code that accidentally overrides an attribute of some system library, like this:

import socket
socket.error = 'some other object'

Is there any way to prevent this behavior, or to find the code responsible for doing that?

I tried making a property out of socket.error, but that did not work:

def fget(self):
    return socket.error

def fset(self, value):
    raise SystemError('You cannot alter this attribute.')

# From now on, settings socket.error = x should raise an error
socket.error = property(fget, fset)

Upvotes: 1

Views: 154

Answers (1)

unutbu
unutbu

Reputation: 880089

You could (temporarily) stick this in your code:

class Bridge(object):
    def __init__(self, module):
        self.__dict__['module'] = module
    def __getattr__(self, attr):
        return getattr(self.module, attr)
    def __setattr__(self, attr, val):
        raise TypeError('{a!r} can not be set'.format(a=attr))

import sys
import socket
socket = sys.modules['socket'] = Bridge(socket)

Then setting attributes on socket (at least in the most usual ways) would raise an exception. The traceback will allow you to locate the error.

print(socket.error)
setattr(socket, 'error', 'blah')

yields

% test.py
<class 'socket.error'>
Traceback (most recent call last):
  File "/home/unutbu/pybin/test.py", line 15, in <module>
    setattr(socket, 'error', 'blah')
  File "/home/unutbu/pybin/test.py", line 10, in __setattr__
    raise TypeError('{a!r} can not be set'.format(a=attr))
TypeError: 'error' can not be set

Upvotes: 6

Related Questions