Skip to content

Commit a66da09

Browse files
committed
Replace basestring with six.string_types
On Python 3, calling `setup_logging()` in `boxsdk.util.log` would always raise NameError: name 'basestring' is not defined On Python 3, this prevented `LoggingNetwork` from being initialized without passing an existing `Logger`. Replace this usage of `basestring` with the equivalent `six.string_types`. Add unit test coverage of `boxsdk.util.log`. Bump version to 1.3.2.
1 parent 636cba0 commit a66da09

File tree

6 files changed

+86
-6
lines changed

6 files changed

+86
-6
lines changed

HISTORY.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ Release History
66
Upcoming
77
++++++++
88

9+
1.3.2
10+
++++++++++++++++++
11+
12+
- Fix ``boxsdk.util.log.setup_logging()`` on Python 3.
13+
14+
1.3.1 (2015-11-06)
15+
++++++++++++++++++
16+
17+
- Add requests-toolbelt to setup.py (it was accidentally missing from 1.3.0).
18+
919
1.3.0 (2015-11-05)
1020
++++++++++++++++++
1121

@@ -32,11 +42,11 @@ Upcoming
3242
++++++++++++++++++
3343

3444
- Added support for Box Developer Edition. This includes JWT auth (auth as enterprise or as app user),
35-
and `create_user` functionality.
45+
and ``create_user`` functionality.
3646
- Added support for setting shared link expiration dates.
3747
- Added support for setting shared link permissions.
3848
- Added support for 'As-User' requests. See https://box-content.readme.io/#as-user-1
39-
- Improved support for accessing shared items. Items returned from the `client.get_shared_item` method will
49+
- Improved support for accessing shared items. Items returned from the ``client.get_shared_item`` method will
4050
remember the shared link (and the optionally provided shared link password) so methods called on the returned
4151
items will be properly authorized.
4252

@@ -71,7 +81,7 @@ Upcoming
7181
1.1.3 (2015-03-26)
7282
++++++++++++++++++
7383

74-
- Added support for the /shared_items endpoint. `client.get_shared_item` can be used to get information about
84+
- Added support for the /shared_items endpoint. ``client.get_shared_item`` can be used to get information about
7585
a shared link. See https://developers.box.com/docs/#shared-items
7686

7787
1.1.2 (2015-03-20)

boxsdk/util/log.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import logging
55
import sys
66

7+
from six import string_types
8+
79

810
def setup_logging(stream_or_file=None, debug=False, name=None):
911
"""
@@ -28,7 +30,7 @@ def setup_logging(stream_or_file=None, debug=False, name=None):
2830
:class:`Logger`
2931
"""
3032
logger = logging.getLogger(name)
31-
if isinstance(stream_or_file, basestring):
33+
if isinstance(stream_or_file, string_types):
3234
handler = logging.FileHandler(stream_or_file, mode='w')
3335
else:
3436
handler = logging.StreamHandler(stream_or_file or sys.stdout)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def main():
6161
install_requires.append('ordereddict>=1.1')
6262
setup(
6363
name='boxsdk',
64-
version='1.3.1',
64+
version='1.3.2',
6565
description='Official Box Python SDK',
6666
long_description=open(join(base_dir, 'README.rst')).read(),
6767
author='Box',

test/unit/network/test_logging_network.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ def test_logging_network_calls_setup_logging_if_logger_is_none():
1616
assert network.logger is setup_logging.return_value
1717

1818

19+
def test_logging_network_can_be_initialized_if_logger_is_none():
20+
with patch('logging.getLogger') as get_logger:
21+
get_logger.return_value = Mock(Logger)
22+
network = LoggingNetwork()
23+
assert network.logger is get_logger.return_value
24+
get_logger.assert_called_once_with(LoggingNetwork.LOGGER_NAME)
25+
26+
1927
def test_logging_network_does_not_call_setup_logging_if_logger_is_not_none():
2028
logger = Mock(Logger)
2129
with patch.object(logging_network, 'setup_logging') as setup_logging:

test/unit/util/test_log.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# coding: utf-8
2+
3+
from __future__ import absolute_import, unicode_literals
4+
5+
import io
6+
import logging
7+
8+
from mock import mock_open, patch, Mock
9+
import pytest
10+
from six import string_types
11+
12+
import boxsdk.util.log
13+
14+
15+
_MOCK_FILEPATH = '/home/user/boxsdk.log'
16+
_MOCK_LOG_NAME = 'boxsdk'
17+
18+
19+
@pytest.fixture(params=[io.StringIO(), _MOCK_FILEPATH])
20+
def stream_or_file(request):
21+
return request.param
22+
23+
24+
@pytest.fixture(params=[False, True])
25+
def debug(request):
26+
return request.param
27+
28+
29+
@pytest.fixture
30+
def expected_log_level(debug):
31+
return logging.DEBUG if debug else logging.INFO
32+
33+
34+
@pytest.fixture(params=[_MOCK_LOG_NAME, None])
35+
def name(request):
36+
return request.param
37+
38+
39+
@pytest.fixture
40+
def mock_logger():
41+
return Mock(logging.Logger)
42+
43+
44+
def test_setup_logging(stream_or_file, debug, expected_log_level, name, mock_logger):
45+
mock_file_open = mock_open()
46+
47+
with patch('logging.getLogger') as get_logger:
48+
with patch('logging.open', mock_file_open, create=True):
49+
get_logger.return_value = mock_logger
50+
assert boxsdk.util.log.setup_logging(stream_or_file, debug=debug, name=name) == mock_logger
51+
get_logger.assert_called_once_with(name)
52+
53+
assert mock_logger.addHandler.call_count == 1
54+
assert isinstance(mock_logger.addHandler.call_args[0][0], logging.Handler)
55+
mock_logger.setLevel.assert_called_once_with(expected_log_level)
56+
57+
if isinstance(stream_or_file, string_types):
58+
assert mock_file_open.call_count == 1
59+
assert mock_file_open.call_args[0][:2] == (stream_or_file, 'w') # Python 3 passes additional args.

tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ deps =
4141
[testenv:pylint]
4242
commands =
4343
pylint --rcfile=.pylintrc boxsdk
44-
pylint --rcfile=.pylintrc test
44+
# pylint:disable W0621(redefined-outer-name) - Using py.test fixtures always breaks this rule.
45+
pylint --rcfile=.pylintrc test -d W0621
4546
deps = -rrequirements-dev.txt
4647

4748
[testenv:coverage]

0 commit comments

Comments
 (0)