Ctx.log.info in __init__

Hi,

I have a very weird error with the following code:

from mitmproxy import *
from mitmproxy.addons import *

class Dummy:
  def __init__(self):
    print("print")
    ctx.log.info("log")

def start():
  return Dummy()

This is what I get:

$ mitmdump --script "problem.py" 2>&1 | head -n 30
Loading script: problem.py
print
log
Script error: TypeError: 'module' object is not callable

Script error: Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/addons/script.py", line 75, in scriptenv
    yield
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/addons/script.py", line 161, in run
    return func(*args, **kwargs)
TypeError: 'module' object is not callable

During handling of the above exception, another exception occurred:

TypeError: 'module' object is not callable

Script error: Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/addons/script.py", line 75, in scriptenv
    yield
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/addons/script.py", line 161, in run
    return func(*args, **kwargs)
TypeError: 'module' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/addons/script.py", line 75, in scriptenv
    yield
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/addons/script.py", line 161, in run
    return func(*args, **kwargs)

The wird thing is that if I remove the ctx.log.info line, there is no exception. So somehow, this line causes an exception after doing what it’s supposed to do…

(I am using this because I’m making a “combiner” thing to combine several objects implementing start(), stop(). The way it works is that it registers things into ctx.master.addons on start() and removes them on done(). But since done() isn’t called when the script is reloaded, in the start() of my combiner, I store the instance in some global variable, and I call its done in __init__ if it’s still there. And since I log everything, I used log in __init__() and that’s how I got that problem)

Thank you in advance for your help.

The problem are your wildcard imports - if you replace them with just

from mitmproxy import ctx

things work as expected. I believe the problem is that from mitmproxy import * imports the log module, which coincidentally has the same name as the log event handler. As you do some logging, we try to invoke the log handler, which fails because it’s the module and not an event handler.