mercurial/dicthelpers.py
author Chris Jerdonek <chris.jerdonek@gmail.com>
Tue, 26 Nov 2013 16:14:22 -0800
branchstable
changeset 20109 e57c532c3835
parent 18894 ed46c2b98b0d
permissions -rw-r--r--
parse_index2: fix crash on bad argument type (issue4110) Passing a non-string to parsers.parse_index2() causes Mercurial to crash instead of raising a TypeError (found on Mac OS X 10.8.5, Python 2.7.6): import mercurial.parsers as parsers parsers.parse_index2(0, 0) Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 parsers.so 0x000000010e071c59 _index_clearcaches + 73 (parsers.c:644) 1 parsers.so 0x000000010e06f2d5 index_dealloc + 21 (parsers.c:1767) 2 parsers.so 0x000000010e074e3b parse_index2 + 347 (parsers.c:1891) 3 org.python.python 0x000000010dda8b17 PyEval_EvalFrameEx + 9911 This happens because when arguments of the wrong type are passed to parsers.parse_index2(), indexType's initialization function index_init() in parsers.c leaves the indexObject instance in a state that indexType's destructor function index_dealloc() cannot handle. This patch moves enough of the indexObject initialization code inside index_init() from after the argument validation code to before it. This way, when bad arguments are passed to index_init(), the destructor doesn't crash and the existing code to raise a TypeError works. This patch also adds a test to check that a TypeError is raised.

# dicthelpers.py - helper routines for Python dicts
#
# Copyright 2013 Facebook
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

def diff(d1, d2, default=None):
    '''Return all key-value pairs that are different between d1 and d2.

    This includes keys that are present in one dict but not the other, and
    keys whose values are different. The return value is a dict with values
    being pairs of values from d1 and d2 respectively, and missing values
    treated as default, so if a value is missing from one dict and the same as
    default in the other, it will not be returned.'''
    res = {}
    if d1 is d2:
        # same dict, so diff is empty
        return res

    for k1, v1 in d1.iteritems():
        v2 = d2.get(k1, default)
        if v1 != v2:
            res[k1] = (v1, v2)

    for k2 in d2:
        if k2 not in d1:
            v2 = d2[k2]
            if v2 != default:
                res[k2] = (default, v2)

    return res

def join(d1, d2, default=None):
    '''Return all key-value pairs from both d1 and d2.

    This is akin to an outer join in relational algebra. The return value is a
    dict with values being pairs of values from d1 and d2 respectively, and
    missing values represented as default.'''
    res = {}

    for k1, v1 in d1.iteritems():
        if k1 in d2:
            res[k1] = (v1, d2[k1])
        else:
            res[k1] = (v1, default)

    if d1 is d2:
        return res

    for k2 in d2:
        if k2 not in d1:
            res[k2] = (default, d2[k2])

    return res