Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Use NullHandler to scope user algorithm code logging #2325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/source/whatsnew/1.3.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ None
Bug Fixes
~~~~~~~~~

None
Now uses `NullHandler` to block logs from the user's algorithm from
being sent to stdout or stderr unless otherwise specified.

Performance
~~~~~~~~~~~
Expand Down
21 changes: 15 additions & 6 deletions zipline/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ def __init__(self,
def noop(*args, **kwargs):
pass

def unlog_method(fn):
def unlogged_method(*args, **kwargs):
with logbook.NullHandler().applicationbound():
fn(*args, **kwargs)
if fn is None:
return fn
else:
return unlogged_method

if self.algoscript is not None:
unexpected_api_methods = set()
if initialize is not None:
Expand All @@ -358,15 +367,15 @@ def noop(*args, **kwargs):
if algo_filename is None:
algo_filename = '<string>'
code = compile(self.algoscript, algo_filename, 'exec')
exec_(code, self.namespace)
unlog_method(exec_)(code, self.namespace)

self._initialize = self.namespace.get('initialize', noop)
self._handle_data = self.namespace.get('handle_data', noop)
self._before_trading_start = self.namespace.get(
self._initialize = unlog_method(self.namespace.get('initialize', noop))
self._handle_data = unlog_method(self.namespace.get('handle_data', noop))
self._before_trading_start = unlog_method(self.namespace.get(
'before_trading_start',
)
))
# Optional analyze function, gets called after run
self._analyze = self.namespace.get('analyze')
self._analyze = unlog_method(self.namespace.get('analyze'))

else:
self._initialize = initialize or (lambda self: None)
Expand Down
5 changes: 4 additions & 1 deletion zipline/utils/run_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import sys
import warnings
from logbook import NullHandler

try:
from pygments import highlight
Expand Down Expand Up @@ -237,6 +238,7 @@ def load_extensions(default, extensions, strict, environ, reload=False):
reload : bool, optional
Reload any extensions that have already been loaded.
"""

if default:
default_extension_path = pth.default_extension(environ=environ)
pth.ensure_file(default_extension_path)
Expand All @@ -252,7 +254,8 @@ def load_extensions(default, extensions, strict, environ, reload=False):
if ext.endswith('.py'):
with open(ext) as f:
ns = {}
six.exec_(compile(f.read(), ext, 'exec'), ns, ns)
with NullHandler.applicationbound():
six.exec_(compile(f.read(), ext, 'exec'), ns, ns)
else:
__import__(ext)
except Exception as e:
Expand Down