Skip to content

Commit 57747f6

Browse files
committed
add test for threadsafe_call exception and allow logging in tests
1 parent 4be9207 commit 57747f6

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

neovim/__init__.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
__all__ = ('tcp_session', 'socket_session', 'stdio_session', 'child_session',
1717
'start_host', 'autocmd', 'command', 'encoding', 'function',
1818
'plugin', 'rpc_export', 'Host', 'DecodeHook', 'Nvim',
19-
'SessionHook', 'shutdown_hook', 'attach')
19+
'SessionHook', 'shutdown_hook', 'attach', 'setup_logging')
2020

2121

2222
def start_host(session=None):
@@ -43,23 +43,8 @@ def start_host(session=None):
4343
if not plugins:
4444
sys.exit('must specify at least one plugin as argument')
4545

46-
logger = logging.getLogger(__name__)
47-
if 'NVIM_PYTHON_LOG_FILE' in os.environ:
48-
logfile = (os.environ['NVIM_PYTHON_LOG_FILE'].strip() +
49-
'_' + str(os.getpid()))
50-
handler = logging.FileHandler(logfile, 'w')
51-
handler.formatter = logging.Formatter(
52-
'%(asctime)s [%(levelname)s @ '
53-
'%(filename)s:%(funcName)s:%(lineno)s] %(process)s - %(message)s')
54-
logging.root.addHandler(handler)
55-
level = logging.INFO
56-
if 'NVIM_PYTHON_LOG_LEVEL' in os.environ:
57-
l = getattr(logging,
58-
os.environ['NVIM_PYTHON_LOG_LEVEL'].strip(),
59-
level)
60-
if isinstance(l, int):
61-
level = l
62-
logger.setLevel(level)
46+
setup_logging()
47+
6348
if not session:
6449
session = stdio_session()
6550
host = Host(Nvim.from_session(session))
@@ -95,6 +80,27 @@ def attach(session_type, address=None, port=None, path=None, argv=None):
9580
return Nvim.from_session(session)
9681

9782

83+
def setup_logging():
84+
"""Setup logging according to environment variables."""
85+
logger = logging.getLogger(__name__)
86+
if 'NVIM_PYTHON_LOG_FILE' in os.environ:
87+
logfile = (os.environ['NVIM_PYTHON_LOG_FILE'].strip() +
88+
'_' + str(os.getpid()))
89+
handler = logging.FileHandler(logfile, 'w')
90+
handler.formatter = logging.Formatter(
91+
'%(asctime)s [%(levelname)s @ '
92+
'%(filename)s:%(funcName)s:%(lineno)s] %(process)s - %(message)s')
93+
logging.root.addHandler(handler)
94+
level = logging.INFO
95+
if 'NVIM_PYTHON_LOG_LEVEL' in os.environ:
96+
l = getattr(logging,
97+
os.environ['NVIM_PYTHON_LOG_LEVEL'].strip(),
98+
level)
99+
if isinstance(l, int):
100+
level = l
101+
logger.setLevel(level)
102+
103+
98104
# Required for python 2.6
99105
class NullHandler(logging.Handler):
100106
def emit(self, record):

test/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from nose.tools import eq_ as eq
88

9+
neovim.setup_logging()
910

1011
if 'NVIM_CHILD_ARGV' in os.environ:
1112
vim = neovim.attach('child', argv=json.loads(os.environ['NVIM_CHILD_ARGV']))
@@ -17,7 +18,6 @@
1718
# with Python2
1819
vim = vim.with_hook(neovim.DecodeHook())
1920

20-
2121
cleanup_func = ''':function BeforeEachTest()
2222
set all&
2323
redir => groups

test/test_concurrency.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ def test_interrupt_from_another_thread():
99
timer = Timer(0.5, lambda: session.threadsafe_call(lambda: session.stop()))
1010
timer.start()
1111
eq(vim.session.next_message(), None)
12+
13+
14+
@with_setup(setup=cleanup)
15+
def test_exception_in_threadsafe_call():
16+
# an exception in a threadsafe_call shouldn't crash the entire host
17+
vim.session.threadsafe_call(lambda: [vim.eval("3"), undefined_variable])
18+
vim.session.threadsafe_call(lambda: vim.session.stop())
19+
vim.session.run(None, None)

0 commit comments

Comments
 (0)