jenkins-bot has submitted this change and it was merged.
Change subject: [FIX] Diff: Support Python < 2.7.2 ......................................................................
[FIX] Diff: Support Python < 2.7.2
The function named '_format_range_unified' was introduced in 2.7.2: https://hg.python.org/cpython/file/8527427914a2/Lib/difflib.py#l1147 This adds a backported version of so that Python 2.6, 2.7.0 and 2.7.1 are supported
It also fixes a problem with non-indexed placeholders for format which aren't supported in Python 2.6.
Bug: T76276 Change-Id: I2f2799f434d3a0782999f94a307c9a081a004014 --- A pywikibot/backports.py M pywikibot/diff.py 2 files changed, 83 insertions(+), 3 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/backports.py b/pywikibot/backports.py new file mode 100644 index 0000000..55712da --- /dev/null +++ b/pywikibot/backports.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- +""" +This module contains backports to support older Python versions. + +They contain the backported code originally developed for Python. It is +therefore distributed under the PSF license, as follows: + +PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 +-------------------------------------------- +1. This LICENSE AGREEMENT is between the Python Software Foundation +("PSF"), and the Individual or Organization ("Licensee") accessing and +otherwise using this software ("Python") in source or binary form and +its associated documentation. + +2. Subject to the terms and conditions of this License Agreement, PSF hereby +grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce, +analyze, test, perform and/or display publicly, prepare derivative works, +distribute, and otherwise use Python alone or in any derivative version, +provided, however, that PSF's License Agreement and PSF's notice of copyright, +i.e., "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, +2011, 2012, 2013, 2014 Python Software Foundation; All Rights Reserved" are +retained in Python alone or in any derivative version prepared by Licensee. + +3. In the event Licensee prepares a derivative work that is based on +or incorporates Python or any part thereof, and wants to make +the derivative work available to others as provided herein, then +Licensee hereby agrees to include in any such work a brief summary of +the changes made to Python. + +4. PSF is making Python available to Licensee on an "AS IS" +basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR +IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND +DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS +FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT +INFRINGE ANY THIRD PARTY RIGHTS. + +5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON +FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS +A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, +OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. + +6. This License Agreement will automatically terminate upon a material +breach of its terms and conditions. + +7. Nothing in this License Agreement shall be deemed to create any +relationship of agency, partnership, or joint venture between PSF and +Licensee. This License Agreement does not grant permission to use PSF +trademarks or trade name in a trademark sense to endorse or promote +products or services of Licensee, or any third party. + +8. By copying, installing or otherwise using Python, Licensee +agrees to be bound by the terms and conditions of this License +Agreement. +""" +# +# (C) Python Software Foundation, 2001-2014 +# (C) with modifications from Pywikibot team, 2014 +# +# Distributed under the terms of the PSF license. +# + + +def format_range_unified(start, stop): + """ + Convert range to the "ed" format. + + Copied from C{difflib._format_range_unified()} which was introduced in + Python 2.7.2. + + @see: https://hg.python.org/cpython/file/8527427914a2/Lib/difflib.py#l1147 + """ + # Per the diff spec at http://www.unix.org/single_unix_specification/ + beginning = start + 1 # lines start numbering with one + length = stop - start + if length == 1: + return '{0}'.format(beginning) + if not length: + beginning -= 1 # empty ranges begin at line just before the range + return '{0},{1}'.format(beginning, length) diff --git a/pywikibot/diff.py b/pywikibot/diff.py index 7773b7e..a93a90b 100644 --- a/pywikibot/diff.py +++ b/pywikibot/diff.py @@ -16,6 +16,7 @@ from itertools import izip_longest as zip_longest
import pywikibot +from pywikibot.backports import format_range_unified # introduced in 2.7.2
class Hunk(object): @@ -65,9 +66,9 @@
def get_header(self): """Provide header of unified diff.""" - a_rng = difflib._format_range_unified(*self.a_rng) - b_rng = difflib._format_range_unified(*self.b_rng) - return '@@ -{} +{} @@{}'.format(a_rng, b_rng, '\n') + a_rng = format_range_unified(*self.a_rng) + b_rng = format_range_unified(*self.b_rng) + return '@@ -{0} +{1} @@\n'.format(a_rng, b_rng)
def create_diff(self): """Generator of diff text for this hunk, without formatting."""