From 285fe5c96685615cf41e4c835df477ec8a39d445 Mon Sep 17 00:00:00 2001 From: Bryan Davis Date: Sun, 21 Aug 2016 12:21:57 -0600 Subject: [PATCH 1/3] validate_netmask: ensure 32 bit expansion Ensure that the bit string representation of a netmask is the full 32 bits before validating the left most bits are 1s. See #18 --- iptools/ipv4.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/iptools/ipv4.py b/iptools/ipv4.py index c3d0127..08f199d 100644 --- a/iptools/ipv4.py +++ b/iptools/ipv4.py @@ -285,6 +285,10 @@ def validate_netmask(s): True >>> validate_netmask('128.0.0.1') False + >>> validate_netmask('1.255.255.0') + False + >>> validate_netmask('0.255.255.0') + False :param s: String to validate as a dotted-quad notation netmask. @@ -293,7 +297,8 @@ def validate_netmask(s): :raises: TypeError """ if validate_ip(s): - mask = bin(ip2network(s))[2:] + # Convert to binary string, strip '0b' prefix, 0 pad to 32 bits + mask = bin(ip2network(s))[2:].zfill(32) # all left most bits must be 1, all right most must be 0 seen0 = False for c in mask: From 51b9e84c72e2dd077d8ff4995e9389b1b4614185 Mon Sep 17 00:00:00 2001 From: Bryan Davis Date: Sun, 21 Aug 2016 12:25:55 -0600 Subject: [PATCH 2/3] Move imports before exports --- iptools/__init__.py | 15 +++++----- iptools/ipv4.py | 71 ++++++++++++++++++++++----------------------- iptools/ipv6.py | 8 ++--- 3 files changed, 44 insertions(+), 50 deletions(-) diff --git a/iptools/__init__.py b/iptools/__init__.py index 608ea4b..181f845 100644 --- a/iptools/__init__.py +++ b/iptools/__init__.py @@ -22,13 +22,6 @@ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. -__version__ = '0.7.0-dev' - -__all__ = ( - 'IpRange', - 'IpRangeList', -) - # sniff for python2.x / python3k compatibility "fixes' try: @@ -51,10 +44,16 @@ def next(iterable): Sequence = object # end compatibility "fixes' - from . import ipv4 from . import ipv6 +__version__ = '0.7.0-dev' + +__all__ = ( + 'IpRange', + 'IpRangeList', +) + def _address2long(address): """ diff --git a/iptools/ipv4.py b/iptools/ipv4.py index 08f199d..3243482 100644 --- a/iptools/ipv4.py +++ b/iptools/ipv4.py @@ -23,6 +23,40 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. +import re + +# sniff for python2.x / python3k compatibility "fixes' +try: + basestring = basestring +except NameError: + # 'basestring' is undefined, must be python3k + basestring = str + +try: + bin = bin +except NameError: + # builtin bin function doesn't exist + def bin(x): + """ + From http://code.activestate.com/recipes/219300/#c7 + """ + if x < 0: + return '-' + bin(-x) + out = [] + if x == 0: + out.append('0') + while x > 0: + out.append('01'[x & 1]) + x >>= 1 + pass + try: + return '0b' + ''.join(reversed(out)) + except NameError: + out.reverse() + return '0b' + ''.join(out) + # end bin +# end compatibility "fixes' + __all__ = ( 'cidr2block', 'hex2ip', @@ -60,43 +94,6 @@ 'TEST_NET_3', ) - -import re - - -# sniff for python2.x / python3k compatibility "fixes' -try: - basestring = basestring -except NameError: - # 'basestring' is undefined, must be python3k - basestring = str - -try: - bin = bin -except NameError: - # builtin bin function doesn't exist - def bin(x): - """ - From http://code.activestate.com/recipes/219300/#c7 - """ - if x < 0: - return '-' + bin(-x) - out = [] - if x == 0: - out.append('0') - while x > 0: - out.append('01'[x & 1]) - x >>= 1 - pass - try: - return '0b' + ''.join(reversed(out)) - except NameError: - out.reverse() - return '0b' + ''.join(out) - # end bin -# end compatibility "fixes' - - #: Regex for validating an IPv4 address _DOTTED_QUAD_RE = re.compile(r'^(\d{1,3}\.){0,3}\d{1,3}$') diff --git a/iptools/ipv6.py b/iptools/ipv6.py index 9fab832..97d00ee 100644 --- a/iptools/ipv6.py +++ b/iptools/ipv6.py @@ -23,6 +23,9 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. +import re +from . import ipv4 + __all__ = ( 'cidr2block', 'ip2long', @@ -52,11 +55,6 @@ 'UNSPECIFIED_ADDRESS', ) - -import re -from . import ipv4 - - #: Regex for validating an IPv6 in hex notation _HEX_RE = re.compile(r'^([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}$') From 041628d6d5860d39825302c4b2a6f68aec7014b2 Mon Sep 17 00:00:00 2001 From: Bryan Davis Date: Sun, 21 Aug 2016 14:08:12 -0600 Subject: [PATCH 3/3] Travis: use tox for testing * Update the Travis CI testing configuration to use tox. * Drop testing of Python 2.6, 3.2, 3.3 * Add Python 3.4, 3.5, pypy3 --- .gitignore | 13 +++++++------ .travis.yml | 20 +++++++++++++------- setup.cfg | 5 +++-- tox.ini | 8 ++++++++ 4 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 tox.ini diff --git a/.gitignore b/.gitignore index 71b79d3..b11bdd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ +*.egg-info *.pyc -.coverage -.venv -build -dist -extras -iptools.egg-info +/.coverage +/.tox/ +/.venv/ +/build/ +/dist/ +/extras/ setuptools-*.egg diff --git a/.travis.yml b/.travis.yml index eada3f2..70c1d3c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,22 @@ language: python python: - - "2.6" - "2.7" - - "3.2" - - "3.3" + - "3.4" + - "3.5" - "pypy" + - "pypy3" +sudo: false +matrix: + fast_finish: true + install: - - pip install . - - pip install -r tests/requirements.txt + - pip install wheel tox-travis + - python setup.py install bdist_wheel + - pip install ./dist/iptools-*.whl script: - - flake8 - - nosetests + - tox + - tox --installpkg ./dist/iptools-*.whl + notifications: email: - travis-ci+python-iptools@bd808.com diff --git a/setup.cfg b/setup.cfg index ec2d4e2..85d3e9c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,18 +1,19 @@ [nosetests] +verbosity=2 detailed-errors=1 with-coverage=1 with-doctest=1 cover-package=iptools cover-html=1 cover-html-dir=docs/_build/cover +cover-branches=1 [flake8] -ignore=F821,E402 count=1 show-pep8=1 show-source=1 statistics=1 -exclude=build,dist,docs,*.egg,*.egg-info +exclude=.tox,.venv,build,dist,docs,*.egg,*.egg-info [wheel] universal = 1 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..a99d698 --- /dev/null +++ b/tox.ini @@ -0,0 +1,8 @@ +[tox] +envlist = py27, py34, py35, pypy, pypy3 + +[testenv] +deps = -r{toxinidir}/tests/requirements.txt +commands = + flake8 + nosetests