anatoly techtonik
anatoly techtonik

Reputation: 20569

Trace functions that are called on Python strings

Is it possible in Python to trace and filter functions that are called on strings during program run? I want to add sys.setdefaultencoding("utf-8") application, and I want to set some guards to predict potential problems with misusing standard functions (like len, for example), to process such strings.

Upvotes: 1

Views: 94

Answers (1)

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76611

You can replace the builtin:

import __builtin__

real_len = __builtin__.len

def checked_len(s):
    ... do extra checks ...
    return real_len(s)

__builtin__.len = checked_len

Upvotes: 2

Related Questions