mercurial/cext/parsers.c
author Augie Fackler <augie@google.com>
Wed, 19 Dec 2018 23:51:02 -0500
changeset 41011 55d6d0ff703b
parent 40708 f2342483f7a6
child 41016 5c68b617ba24
permissions -rw-r--r--
parsers: remove long-dead parse_manifest method We haven't used this in years, I think it's fine to ditch it now. We had previously kept it around to ease bisecting with built extensions, but these days we've got a better versioning scheme anyway. Noticed this method kicking around while looking in parsers.so for likely fuzzing targets. Differential Revision: https://phab.mercurial-scm.org/D5459
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6389
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     1
/*
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     2
 parsers.c - efficient content parsing
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     3
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     4
 Copyright 2008 Matt Mackall <mpm@selenic.com> and others
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     5
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     6
 This software may be used and distributed according to the terms of
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     7
 the GNU General Public License, incorporated herein by reference.
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     8
*/
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     9
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    10
#include <Python.h>
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    11
#include <ctype.h>
17356
511dfb34b412 parsers: fix an integer size warning issued by clang
Bryan O'Sullivan <bryano@fb.com>
parents: 17353
diff changeset
    12
#include <stddef.h>
6389
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    13
#include <string.h>
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    14
34438
b90e8da190da cext: reorder #include
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33926
diff changeset
    15
#include "bitmanipulation.h"
33758
0f4ac3b6dee4 cext: factor out header for charencode.c
Yuya Nishihara <yuya@tcha.org>
parents: 33757
diff changeset
    16
#include "charencode.h"
11361
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
    17
#include "util.h"
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
    18
30112
9b6ff0f940ed parsers: move PyInt aliasing out of util.h
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30103
diff changeset
    19
#ifdef IS_PY3K
9b6ff0f940ed parsers: move PyInt aliasing out of util.h
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30103
diff changeset
    20
/* The mapping of Python types is meant to be temporary to get Python
9b6ff0f940ed parsers: move PyInt aliasing out of util.h
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30103
diff changeset
    21
 * 3 to compile. We should remove this once Python 3 support is fully
9b6ff0f940ed parsers: move PyInt aliasing out of util.h
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30103
diff changeset
    22
 * supported and proper types are used in the extensions themselves. */
30169
5f7151e6de85 parsers: alias more PyInt* symbols on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30112
diff changeset
    23
#define PyInt_Check PyLong_Check
30112
9b6ff0f940ed parsers: move PyInt aliasing out of util.h
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30103
diff changeset
    24
#define PyInt_FromLong PyLong_FromLong
30169
5f7151e6de85 parsers: alias more PyInt* symbols on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30112
diff changeset
    25
#define PyInt_FromSsize_t PyLong_FromSsize_t
30112
9b6ff0f940ed parsers: move PyInt aliasing out of util.h
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30103
diff changeset
    26
#define PyInt_AsLong PyLong_AsLong
9b6ff0f940ed parsers: move PyInt aliasing out of util.h
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30103
diff changeset
    27
#endif
9b6ff0f940ed parsers: move PyInt aliasing out of util.h
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30103
diff changeset
    28
32386
7640584e697c cext: mark constant variables
Yuya Nishihara <yuya@tcha.org>
parents: 32384
diff changeset
    29
static const char *const versionerrortext = "Python minor version mismatch";
20742
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
    30
25584
72b2711f12ea parsers: add an API to create a new presized dict
Siddharth Agarwal <sid0@fb.com>
parents: 25583
diff changeset
    31
static PyObject *dict_new_presized(PyObject *self, PyObject *args)
72b2711f12ea parsers: add an API to create a new presized dict
Siddharth Agarwal <sid0@fb.com>
parents: 25583
diff changeset
    32
{
72b2711f12ea parsers: add an API to create a new presized dict
Siddharth Agarwal <sid0@fb.com>
parents: 25583
diff changeset
    33
	Py_ssize_t expected_size;
72b2711f12ea parsers: add an API to create a new presized dict
Siddharth Agarwal <sid0@fb.com>
parents: 25583
diff changeset
    34
72b2711f12ea parsers: add an API to create a new presized dict
Siddharth Agarwal <sid0@fb.com>
parents: 25583
diff changeset
    35
	if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size))
72b2711f12ea parsers: add an API to create a new presized dict
Siddharth Agarwal <sid0@fb.com>
parents: 25583
diff changeset
    36
		return NULL;
72b2711f12ea parsers: add an API to create a new presized dict
Siddharth Agarwal <sid0@fb.com>
parents: 25583
diff changeset
    37
72b2711f12ea parsers: add an API to create a new presized dict
Siddharth Agarwal <sid0@fb.com>
parents: 25583
diff changeset
    38
	return _dict_new_presized(expected_size);
72b2711f12ea parsers: add an API to create a new presized dict
Siddharth Agarwal <sid0@fb.com>
parents: 25583
diff changeset
    39
}
72b2711f12ea parsers: add an API to create a new presized dict
Siddharth Agarwal <sid0@fb.com>
parents: 25583
diff changeset
    40
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    41
static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode,
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
    42
                                                       int size, int mtime)
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    43
{
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
    44
	dirstateTupleObject *t =
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
    45
	    PyObject_New(dirstateTupleObject, &dirstateTupleType);
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    46
	if (!t)
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    47
		return NULL;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    48
	t->state = state;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    49
	t->mode = mode;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    50
	t->size = size;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    51
	t->mtime = mtime;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    52
	return t;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    53
}
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    54
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    55
static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args,
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
    56
                                    PyObject *kwds)
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    57
{
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    58
	/* We do all the initialization here and not a tp_init function because
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    59
	 * dirstate_tuple is immutable. */
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    60
	dirstateTupleObject *t;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    61
	char state;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    62
	int size, mode, mtime;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    63
	if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime))
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    64
		return NULL;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    65
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    66
	t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1);
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    67
	if (!t)
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    68
		return NULL;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    69
	t->state = state;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    70
	t->mode = mode;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    71
	t->size = size;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    72
	t->mtime = mtime;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    73
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    74
	return (PyObject *)t;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    75
}
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    76
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    77
static void dirstate_tuple_dealloc(PyObject *o)
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    78
{
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    79
	PyObject_Del(o);
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    80
}
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    81
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    82
static Py_ssize_t dirstate_tuple_length(PyObject *o)
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    83
{
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    84
	return 4;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    85
}
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    86
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    87
static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i)
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    88
{
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    89
	dirstateTupleObject *t = (dirstateTupleObject *)o;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    90
	switch (i) {
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    91
	case 0:
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    92
		return PyBytes_FromStringAndSize(&t->state, 1);
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    93
	case 1:
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    94
		return PyInt_FromLong(t->mode);
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    95
	case 2:
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    96
		return PyInt_FromLong(t->size);
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    97
	case 3:
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    98
		return PyInt_FromLong(t->mtime);
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
    99
	default:
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   100
		PyErr_SetString(PyExc_IndexError, "index out of range");
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   101
		return NULL;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   102
	}
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   103
}
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   104
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   105
static PySequenceMethods dirstate_tuple_sq = {
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   106
    dirstate_tuple_length, /* sq_length */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   107
    0,                     /* sq_concat */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   108
    0,                     /* sq_repeat */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   109
    dirstate_tuple_item,   /* sq_item */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   110
    0,                     /* sq_ass_item */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   111
    0,                     /* sq_contains */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   112
    0,                     /* sq_inplace_concat */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   113
    0                      /* sq_inplace_repeat */
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   114
};
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   115
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   116
PyTypeObject dirstateTupleType = {
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   117
    PyVarObject_HEAD_INIT(NULL, 0)      /* header */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   118
    "dirstate_tuple",                   /* tp_name */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   119
    sizeof(dirstateTupleObject),        /* tp_basicsize */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   120
    0,                                  /* tp_itemsize */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   121
    (destructor)dirstate_tuple_dealloc, /* tp_dealloc */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   122
    0,                                  /* tp_print */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   123
    0,                                  /* tp_getattr */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   124
    0,                                  /* tp_setattr */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   125
    0,                                  /* tp_compare */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   126
    0,                                  /* tp_repr */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   127
    0,                                  /* tp_as_number */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   128
    &dirstate_tuple_sq,                 /* tp_as_sequence */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   129
    0,                                  /* tp_as_mapping */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   130
    0,                                  /* tp_hash  */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   131
    0,                                  /* tp_call */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   132
    0,                                  /* tp_str */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   133
    0,                                  /* tp_getattro */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   134
    0,                                  /* tp_setattro */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   135
    0,                                  /* tp_as_buffer */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   136
    Py_TPFLAGS_DEFAULT,                 /* tp_flags */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   137
    "dirstate tuple",                   /* tp_doc */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   138
    0,                                  /* tp_traverse */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   139
    0,                                  /* tp_clear */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   140
    0,                                  /* tp_richcompare */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   141
    0,                                  /* tp_weaklistoffset */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   142
    0,                                  /* tp_iter */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   143
    0,                                  /* tp_iternext */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   144
    0,                                  /* tp_methods */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   145
    0,                                  /* tp_members */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   146
    0,                                  /* tp_getset */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   147
    0,                                  /* tp_base */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   148
    0,                                  /* tp_dict */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   149
    0,                                  /* tp_descr_get */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   150
    0,                                  /* tp_descr_set */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   151
    0,                                  /* tp_dictoffset */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   152
    0,                                  /* tp_init */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   153
    0,                                  /* tp_alloc */
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   154
    dirstate_tuple_new,                 /* tp_new */
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   155
};
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   156
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   157
static PyObject *parse_dirstate(PyObject *self, PyObject *args)
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   158
{
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   159
	PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   160
	PyObject *fname = NULL, *cname = NULL, *entry = NULL;
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 20109
diff changeset
   161
	char state, *cur, *str, *cpos;
19725
5e25d71a58cc parsers: state is a char, not an int
Bryan O'Sullivan <bryano@fb.com>
parents: 19718
diff changeset
   162
	int mode, size, mtime;
22403
41e9d58ec56f parsers: avoid signed/unsigned comparison mismatch
Henrik Stuart <hg@hstuart.dk>
parents: 22402
diff changeset
   163
	unsigned int flen, len, pos = 40;
41e9d58ec56f parsers: avoid signed/unsigned comparison mismatch
Henrik Stuart <hg@hstuart.dk>
parents: 22402
diff changeset
   164
	int readlen;
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   165
36620
186c6df3a373 py3: bulk-replace 'const char*' format specifier passed to PyArg_ParseTuple*()
Yuya Nishihara <yuya@tcha.org>
parents: 36619
diff changeset
   166
	if (!PyArg_ParseTuple(
186c6df3a373 py3: bulk-replace 'const char*' format specifier passed to PyArg_ParseTuple*()
Yuya Nishihara <yuya@tcha.org>
parents: 36619
diff changeset
   167
	        args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"),
186c6df3a373 py3: bulk-replace 'const char*' format specifier passed to PyArg_ParseTuple*()
Yuya Nishihara <yuya@tcha.org>
parents: 36619
diff changeset
   168
	        &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen))
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   169
		goto quit;
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   170
22403
41e9d58ec56f parsers: avoid signed/unsigned comparison mismatch
Henrik Stuart <hg@hstuart.dk>
parents: 22402
diff changeset
   171
	len = readlen;
41e9d58ec56f parsers: avoid signed/unsigned comparison mismatch
Henrik Stuart <hg@hstuart.dk>
parents: 22402
diff changeset
   172
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   173
	/* read parents */
26052
b970418bbafe parsers: set exception when there's too little string data to extract parents
Augie Fackler <augie@google.com>
parents: 26051
diff changeset
   174
	if (len < 40) {
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   175
		PyErr_SetString(PyExc_ValueError,
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   176
		                "too little data for parents");
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   177
		goto quit;
26052
b970418bbafe parsers: set exception when there's too little string data to extract parents
Augie Fackler <augie@google.com>
parents: 26051
diff changeset
   178
	}
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   179
36619
1f8c3fadbb8e py3: bulk-replace bytes format specifier passed to Py_BuildValue()
Yuya Nishihara <yuya@tcha.org>
parents: 35309
diff changeset
   180
	parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, 20, str + 20, 20);
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   181
	if (!parents)
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   182
		goto quit;
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   183
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   184
	/* read filenames */
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 20109
diff changeset
   185
	while (pos >= 40 && pos < len) {
27226
f5e8cb813a4d parsers: fix parse_dirstate to check len before unpacking header (issue4979)
Yuya Nishihara <yuya@tcha.org>
parents: 26872
diff changeset
   186
		if (pos + 17 > len) {
f5e8cb813a4d parsers: fix parse_dirstate to check len before unpacking header (issue4979)
Yuya Nishihara <yuya@tcha.org>
parents: 26872
diff changeset
   187
			PyErr_SetString(PyExc_ValueError,
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   188
			                "overflow in dirstate");
27226
f5e8cb813a4d parsers: fix parse_dirstate to check len before unpacking header (issue4979)
Yuya Nishihara <yuya@tcha.org>
parents: 26872
diff changeset
   189
			goto quit;
f5e8cb813a4d parsers: fix parse_dirstate to check len before unpacking header (issue4979)
Yuya Nishihara <yuya@tcha.org>
parents: 26872
diff changeset
   190
		}
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 20109
diff changeset
   191
		cur = str + pos;
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   192
		/* unpack header */
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   193
		state = *cur;
16437
d126a0d16856 util.h: replace ntohl/htonl with get/putbe32
Matt Mackall <mpm@selenic.com>
parents: 16414
diff changeset
   194
		mode = getbe32(cur + 1);
d126a0d16856 util.h: replace ntohl/htonl with get/putbe32
Matt Mackall <mpm@selenic.com>
parents: 16414
diff changeset
   195
		size = getbe32(cur + 5);
d126a0d16856 util.h: replace ntohl/htonl with get/putbe32
Matt Mackall <mpm@selenic.com>
parents: 16414
diff changeset
   196
		mtime = getbe32(cur + 9);
d126a0d16856 util.h: replace ntohl/htonl with get/putbe32
Matt Mackall <mpm@selenic.com>
parents: 16414
diff changeset
   197
		flen = getbe32(cur + 13);
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 20109
diff changeset
   198
		pos += 17;
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   199
		cur += 17;
20316
40f08c31844c parsers: fix 'unsigned expression is always true' warning (issue4142)
David Soria Parra <davidsp@fb.com>
parents: 20169
diff changeset
   200
		if (flen > len - pos) {
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   201
			PyErr_SetString(PyExc_ValueError,
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   202
			                "overflow in dirstate");
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   203
			goto quit;
7174
4da87407b845 parsers.c: fix integer overflows
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7168
diff changeset
   204
		}
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   205
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   206
		entry =
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   207
		    (PyObject *)make_dirstate_tuple(state, mode, size, mtime);
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   208
		cpos = memchr(cur, 0, flen);
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   209
		if (cpos) {
11361
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
   210
			fname = PyBytes_FromStringAndSize(cur, cpos - cur);
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   211
			cname = PyBytes_FromStringAndSize(
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   212
			    cpos + 1, flen - (cpos - cur) - 1);
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   213
			if (!fname || !cname ||
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   214
			    PyDict_SetItem(cmap, fname, cname) == -1 ||
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   215
			    PyDict_SetItem(dmap, fname, entry) == -1)
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   216
				goto quit;
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   217
			Py_DECREF(cname);
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   218
		} else {
11361
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
   219
			fname = PyBytes_FromStringAndSize(cur, flen);
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   220
			if (!fname || PyDict_SetItem(dmap, fname, entry) == -1)
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   221
				goto quit;
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   222
		}
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   223
		Py_DECREF(fname);
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   224
		Py_DECREF(entry);
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   225
		fname = cname = entry = NULL;
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 20109
diff changeset
   226
		pos += flen;
7093
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   227
	}
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   228
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   229
	ret = parents;
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   230
	Py_INCREF(ret);
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   231
quit:
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   232
	Py_XDECREF(fname);
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   233
	Py_XDECREF(cname);
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   234
	Py_XDECREF(entry);
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   235
	Py_XDECREF(parents);
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   236
	return ret;
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   237
}
16bafcebd3d1 dirstate: C parsing extension
Matt Mackall <mpm@selenic.com>
parents: 7092
diff changeset
   238
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   239
/*
31278
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   240
 * Build a set of non-normal and other parent entries from the dirstate dmap
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   241
 */
34440
7ed0750c71a1 cext: wrap before brace for functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34438
diff changeset
   242
static PyObject *nonnormalotherparententries(PyObject *self, PyObject *args)
7ed0750c71a1 cext: wrap before brace for functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34438
diff changeset
   243
{
31278
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   244
	PyObject *dmap, *fname, *v;
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   245
	PyObject *nonnset = NULL, *otherpset = NULL, *result = NULL;
27592
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   246
	Py_ssize_t pos;
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   247
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   248
	if (!PyArg_ParseTuple(args, "O!:nonnormalentries", &PyDict_Type, &dmap))
27592
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   249
		goto bail;
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   250
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   251
	nonnset = PySet_New(NULL);
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   252
	if (nonnset == NULL)
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   253
		goto bail;
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   254
31278
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   255
	otherpset = PySet_New(NULL);
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   256
	if (otherpset == NULL)
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   257
		goto bail;
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   258
27592
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   259
	pos = 0;
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   260
	while (PyDict_Next(dmap, &pos, &fname, &v)) {
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   261
		dirstateTupleObject *t;
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   262
		if (!dirstate_tuple_check(v)) {
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   263
			PyErr_SetString(PyExc_TypeError,
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   264
			                "expected a dirstate tuple");
27592
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   265
			goto bail;
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   266
		}
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   267
		t = (dirstateTupleObject *)v;
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   268
31278
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   269
		if (t->state == 'n' && t->size == -2) {
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   270
			if (PySet_Add(otherpset, fname) == -1) {
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   271
				goto bail;
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   272
			}
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   273
		}
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   274
27592
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   275
		if (t->state == 'n' && t->mtime != -1)
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   276
			continue;
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   277
		if (PySet_Add(nonnset, fname) == -1)
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   278
			goto bail;
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   279
	}
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   280
31278
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   281
	result = Py_BuildValue("(OO)", nonnset, otherpset);
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   282
	if (result == NULL)
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   283
		goto bail;
31291
fffd1abb1337 parsers: avoid leak of nonnset and otherpset
Augie Fackler <augie@google.com>
parents: 31278
diff changeset
   284
	Py_DECREF(nonnset);
fffd1abb1337 parsers: avoid leak of nonnset and otherpset
Augie Fackler <augie@google.com>
parents: 31278
diff changeset
   285
	Py_DECREF(otherpset);
31278
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   286
	return result;
27592
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   287
bail:
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   288
	Py_XDECREF(nonnset);
31278
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   289
	Py_XDECREF(otherpset);
1c97a91a18dc dirstate: track otherparent files same as nonnormal
Durham Goode <durham@fb.com>
parents: 30577
diff changeset
   290
	Py_XDECREF(result);
27592
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   291
	return NULL;
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   292
}
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   293
7c9eb2927879 dirstate: add a C implementation for nonnormalentries
Laurent Charignon <lcharignon@fb.com>
parents: 27410
diff changeset
   294
/*
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   295
 * Efficiently pack a dirstate object into its on-disk format.
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   296
 */
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   297
static PyObject *pack_dirstate(PyObject *self, PyObject *args)
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   298
{
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   299
	PyObject *packobj = NULL;
21806
05bd2667df4d pack_dirstate: in C version, for invalidation set dict to what we write to disk
Siddharth Agarwal <sid0@fb.com>
parents: 21730
diff changeset
   300
	PyObject *map, *copymap, *pl, *mtime_unset = NULL;
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   301
	Py_ssize_t nbytes, pos, l;
23946
f3e94aa6e182 parsers: don't leak a tuple in pack_dirstate
Augie Fackler <augie@google.com>
parents: 23945
diff changeset
   302
	PyObject *k, *v = NULL, *pn;
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   303
	char *p, *s;
26630
3111b45a2bbf parsers: make pack_dirstate take now in integer for consistency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26591
diff changeset
   304
	int now;
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   305
39422
adacefb0b7ea dirstate: use tuple interface to fix leak in pack_dirstate()
Yuya Nishihara <yuya@tcha.org>
parents: 37968
diff changeset
   306
	if (!PyArg_ParseTuple(args, "O!O!O!i:pack_dirstate", &PyDict_Type, &map,
adacefb0b7ea dirstate: use tuple interface to fix leak in pack_dirstate()
Yuya Nishihara <yuya@tcha.org>
parents: 37968
diff changeset
   307
	                      &PyDict_Type, &copymap, &PyTuple_Type, &pl, &now))
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   308
		return NULL;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   309
39422
adacefb0b7ea dirstate: use tuple interface to fix leak in pack_dirstate()
Yuya Nishihara <yuya@tcha.org>
parents: 37968
diff changeset
   310
	if (PyTuple_Size(pl) != 2) {
adacefb0b7ea dirstate: use tuple interface to fix leak in pack_dirstate()
Yuya Nishihara <yuya@tcha.org>
parents: 37968
diff changeset
   311
		PyErr_SetString(PyExc_TypeError, "expected 2-element tuple");
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   312
		return NULL;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   313
	}
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   314
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   315
	/* Figure out how much we need to allocate. */
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   316
	for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   317
		PyObject *c;
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   318
		if (!PyBytes_Check(k)) {
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   319
			PyErr_SetString(PyExc_TypeError, "expected string key");
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   320
			goto bail;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   321
		}
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   322
		nbytes += PyBytes_GET_SIZE(k) + 17;
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   323
		c = PyDict_GetItem(copymap, k);
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   324
		if (c) {
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   325
			if (!PyBytes_Check(c)) {
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   326
				PyErr_SetString(PyExc_TypeError,
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   327
				                "expected string key");
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   328
				goto bail;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   329
			}
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   330
			nbytes += PyBytes_GET_SIZE(c) + 1;
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   331
		}
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   332
	}
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   333
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   334
	packobj = PyBytes_FromStringAndSize(NULL, nbytes);
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   335
	if (packobj == NULL)
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   336
		goto bail;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   337
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   338
	p = PyBytes_AS_STRING(packobj);
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   339
39422
adacefb0b7ea dirstate: use tuple interface to fix leak in pack_dirstate()
Yuya Nishihara <yuya@tcha.org>
parents: 37968
diff changeset
   340
	pn = PyTuple_GET_ITEM(pl, 0);
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   341
	if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   342
		PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   343
		goto bail;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   344
	}
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   345
	memcpy(p, s, l);
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   346
	p += 20;
39422
adacefb0b7ea dirstate: use tuple interface to fix leak in pack_dirstate()
Yuya Nishihara <yuya@tcha.org>
parents: 37968
diff changeset
   347
	pn = PyTuple_GET_ITEM(pl, 1);
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   348
	if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   349
		PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   350
		goto bail;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   351
	}
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   352
	memcpy(p, s, l);
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   353
	p += 20;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   354
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   355
	for (pos = 0; PyDict_Next(map, &pos, &k, &v);) {
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   356
		dirstateTupleObject *tuple;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   357
		char state;
26774
04ab2348efd1 parsers: correct type of temporary variables for dirstate tuple fields
Yuya Nishihara <yuya@tcha.org>
parents: 26630
diff changeset
   358
		int mode, size, mtime;
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   359
		Py_ssize_t len, l;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   360
		PyObject *o;
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   361
		char *t;
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   362
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   363
		if (!dirstate_tuple_check(v)) {
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   364
			PyErr_SetString(PyExc_TypeError,
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   365
			                "expected a dirstate tuple");
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   366
			goto bail;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   367
		}
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   368
		tuple = (dirstateTupleObject *)v;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   369
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   370
		state = tuple->state;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   371
		mode = tuple->mode;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   372
		size = tuple->size;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   373
		mtime = tuple->mtime;
26630
3111b45a2bbf parsers: make pack_dirstate take now in integer for consistency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26591
diff changeset
   374
		if (state == 'n' && mtime == now) {
18567
194e63c1ccb9 dirstate: move pure python dirstate packing to pure/parsers.py
Siddharth Agarwal <sid0@fb.com>
parents: 18504
diff changeset
   375
			/* See pure/parsers.py:pack_dirstate for why we do
194e63c1ccb9 dirstate: move pure python dirstate packing to pure/parsers.py
Siddharth Agarwal <sid0@fb.com>
parents: 18504
diff changeset
   376
			 * this. */
21806
05bd2667df4d pack_dirstate: in C version, for invalidation set dict to what we write to disk
Siddharth Agarwal <sid0@fb.com>
parents: 21730
diff changeset
   377
			mtime = -1;
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   378
			mtime_unset = (PyObject *)make_dirstate_tuple(
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   379
			    state, mode, size, mtime);
21806
05bd2667df4d pack_dirstate: in C version, for invalidation set dict to what we write to disk
Siddharth Agarwal <sid0@fb.com>
parents: 21730
diff changeset
   380
			if (!mtime_unset)
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   381
				goto bail;
21806
05bd2667df4d pack_dirstate: in C version, for invalidation set dict to what we write to disk
Siddharth Agarwal <sid0@fb.com>
parents: 21730
diff changeset
   382
			if (PyDict_SetItem(map, k, mtime_unset) == -1)
05bd2667df4d pack_dirstate: in C version, for invalidation set dict to what we write to disk
Siddharth Agarwal <sid0@fb.com>
parents: 21730
diff changeset
   383
				goto bail;
05bd2667df4d pack_dirstate: in C version, for invalidation set dict to what we write to disk
Siddharth Agarwal <sid0@fb.com>
parents: 21730
diff changeset
   384
			Py_DECREF(mtime_unset);
05bd2667df4d pack_dirstate: in C version, for invalidation set dict to what we write to disk
Siddharth Agarwal <sid0@fb.com>
parents: 21730
diff changeset
   385
			mtime_unset = NULL;
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   386
		}
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   387
		*p++ = state;
26774
04ab2348efd1 parsers: correct type of temporary variables for dirstate tuple fields
Yuya Nishihara <yuya@tcha.org>
parents: 26630
diff changeset
   388
		putbe32((uint32_t)mode, p);
04ab2348efd1 parsers: correct type of temporary variables for dirstate tuple fields
Yuya Nishihara <yuya@tcha.org>
parents: 26630
diff changeset
   389
		putbe32((uint32_t)size, p + 4);
04ab2348efd1 parsers: correct type of temporary variables for dirstate tuple fields
Yuya Nishihara <yuya@tcha.org>
parents: 26630
diff changeset
   390
		putbe32((uint32_t)mtime, p + 8);
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   391
		t = p + 12;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   392
		p += 16;
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   393
		len = PyBytes_GET_SIZE(k);
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   394
		memcpy(p, PyBytes_AS_STRING(k), len);
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   395
		p += len;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   396
		o = PyDict_GetItem(copymap, k);
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   397
		if (o) {
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   398
			*p++ = '\0';
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   399
			l = PyBytes_GET_SIZE(o);
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   400
			memcpy(p, PyBytes_AS_STRING(o), l);
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   401
			p += l;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   402
			len += l + 1;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   403
		}
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   404
		putbe32((uint32_t)len, t);
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   405
	}
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   406
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   407
	pos = p - PyBytes_AS_STRING(packobj);
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   408
	if (pos != nbytes) {
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   409
		PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   410
		             (long)pos, (long)nbytes);
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   411
		goto bail;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   412
	}
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   413
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   414
	return packobj;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   415
bail:
21806
05bd2667df4d pack_dirstate: in C version, for invalidation set dict to what we write to disk
Siddharth Agarwal <sid0@fb.com>
parents: 21730
diff changeset
   416
	Py_XDECREF(mtime_unset);
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   417
	Py_XDECREF(packobj);
23946
f3e94aa6e182 parsers: don't leak a tuple in pack_dirstate
Augie Fackler <augie@google.com>
parents: 23945
diff changeset
   418
	Py_XDECREF(v);
16955
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   419
	return NULL;
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   420
}
92e1c64ba0d4 parsers: add a C function to pack the dirstate
Bryan O'Sullivan <bryano@fb.com>
parents: 16863
diff changeset
   421
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   422
#define BUMPED_FIX 1
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   423
#define USING_SHA_256 2
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   424
#define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1)
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   425
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   426
static PyObject *readshas(const char *source, unsigned char num,
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   427
                          Py_ssize_t hashwidth)
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   428
{
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   429
	int i;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   430
	PyObject *list = PyTuple_New(num);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   431
	if (list == NULL) {
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   432
		return NULL;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   433
	}
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   434
	for (i = 0; i < num; i++) {
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   435
		PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth);
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   436
		if (hash == NULL) {
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   437
			Py_DECREF(list);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   438
			return NULL;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   439
		}
26213
4d6cdea33f37 parsers: use PyTuple_SET_ITEM() to fill new marker tuples
Yuya Nishihara <yuya@tcha.org>
parents: 26107
diff changeset
   440
		PyTuple_SET_ITEM(list, i, hash);
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   441
		source += hashwidth;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   442
	}
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   443
	return list;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   444
}
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   445
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   446
static PyObject *fm1readmarker(const char *databegin, const char *dataend,
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   447
                               uint32_t *msize)
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   448
{
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   449
	const char *data = databegin;
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   450
	const char *meta;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   451
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   452
	double mtime;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   453
	int16_t tz;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   454
	uint16_t flags;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   455
	unsigned char nsuccs, nparents, nmetadata;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   456
	Py_ssize_t hashwidth = 20;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   457
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   458
	PyObject *prec = NULL, *parents = NULL, *succs = NULL;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   459
	PyObject *metadata = NULL, *ret = NULL;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   460
	int i;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   461
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   462
	if (data + FM1_HEADER_SIZE > dataend) {
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   463
		goto overflow;
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   464
	}
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   465
24019
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   466
	*msize = getbe32(data);
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   467
	data += 4;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   468
	mtime = getbefloat64(data);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   469
	data += 8;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   470
	tz = getbeint16(data);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   471
	data += 2;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   472
	flags = getbeuint16(data);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   473
	data += 2;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   474
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   475
	if (flags & USING_SHA_256) {
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   476
		hashwidth = 32;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   477
	}
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   478
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   479
	nsuccs = (unsigned char)(*data++);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   480
	nparents = (unsigned char)(*data++);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   481
	nmetadata = (unsigned char)(*data++);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   482
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   483
	if (databegin + *msize > dataend) {
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   484
		goto overflow;
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   485
	}
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   486
	dataend = databegin + *msize; /* narrow down to marker size */
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   487
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   488
	if (data + hashwidth > dataend) {
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   489
		goto overflow;
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   490
	}
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   491
	prec = PyBytes_FromStringAndSize(data, hashwidth);
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   492
	data += hashwidth;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   493
	if (prec == NULL) {
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   494
		goto bail;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   495
	}
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   496
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   497
	if (data + nsuccs * hashwidth > dataend) {
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   498
		goto overflow;
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   499
	}
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   500
	succs = readshas(data, nsuccs, hashwidth);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   501
	if (succs == NULL) {
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   502
		goto bail;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   503
	}
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   504
	data += nsuccs * hashwidth;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   505
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   506
	if (nparents == 1 || nparents == 2) {
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   507
		if (data + nparents * hashwidth > dataend) {
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   508
			goto overflow;
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   509
		}
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   510
		parents = readshas(data, nparents, hashwidth);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   511
		if (parents == NULL) {
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   512
			goto bail;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   513
		}
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   514
		data += nparents * hashwidth;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   515
	} else {
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   516
		parents = Py_None;
31426
43a7dfbead0c parsers: handle refcounting of "parents" consistently
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31292
diff changeset
   517
		Py_INCREF(parents);
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   518
	}
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   519
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   520
	if (data + 2 * nmetadata > dataend) {
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   521
		goto overflow;
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   522
	}
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   523
	meta = data + (2 * nmetadata);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   524
	metadata = PyTuple_New(nmetadata);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   525
	if (metadata == NULL) {
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   526
		goto bail;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   527
	}
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   528
	for (i = 0; i < nmetadata; i++) {
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   529
		PyObject *tmp, *left = NULL, *right = NULL;
26590
473a63c45394 parsers: read sizes of metadata pair of obsolete marker at once
Yuya Nishihara <yuya@tcha.org>
parents: 26214
diff changeset
   530
		Py_ssize_t leftsize = (unsigned char)(*data++);
473a63c45394 parsers: read sizes of metadata pair of obsolete marker at once
Yuya Nishihara <yuya@tcha.org>
parents: 26214
diff changeset
   531
		Py_ssize_t rightsize = (unsigned char)(*data++);
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   532
		if (meta + leftsize + rightsize > dataend) {
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   533
			goto overflow;
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   534
		}
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   535
		left = PyBytes_FromStringAndSize(meta, leftsize);
26590
473a63c45394 parsers: read sizes of metadata pair of obsolete marker at once
Yuya Nishihara <yuya@tcha.org>
parents: 26214
diff changeset
   536
		meta += leftsize;
30100
c5afe5531709 parsers: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30090
diff changeset
   537
		right = PyBytes_FromStringAndSize(meta, rightsize);
26590
473a63c45394 parsers: read sizes of metadata pair of obsolete marker at once
Yuya Nishihara <yuya@tcha.org>
parents: 26214
diff changeset
   538
		meta += rightsize;
26214
46605888faf3 parsers: use PyTuple_New and SET_ITEM to construct metadata pair of markers
Yuya Nishihara <yuya@tcha.org>
parents: 26213
diff changeset
   539
		tmp = PyTuple_New(2);
46605888faf3 parsers: use PyTuple_New and SET_ITEM to construct metadata pair of markers
Yuya Nishihara <yuya@tcha.org>
parents: 26213
diff changeset
   540
		if (!left || !right || !tmp) {
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   541
			Py_XDECREF(left);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   542
			Py_XDECREF(right);
26214
46605888faf3 parsers: use PyTuple_New and SET_ITEM to construct metadata pair of markers
Yuya Nishihara <yuya@tcha.org>
parents: 26213
diff changeset
   543
			Py_XDECREF(tmp);
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   544
			goto bail;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   545
		}
26214
46605888faf3 parsers: use PyTuple_New and SET_ITEM to construct metadata pair of markers
Yuya Nishihara <yuya@tcha.org>
parents: 26213
diff changeset
   546
		PyTuple_SET_ITEM(tmp, 0, left);
46605888faf3 parsers: use PyTuple_New and SET_ITEM to construct metadata pair of markers
Yuya Nishihara <yuya@tcha.org>
parents: 26213
diff changeset
   547
		PyTuple_SET_ITEM(tmp, 1, right);
26213
4d6cdea33f37 parsers: use PyTuple_SET_ITEM() to fill new marker tuples
Yuya Nishihara <yuya@tcha.org>
parents: 26107
diff changeset
   548
		PyTuple_SET_ITEM(metadata, i, tmp);
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   549
	}
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   550
	ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime,
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   551
	                    (int)tz * 60, parents);
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   552
	goto bail; /* return successfully */
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   553
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   554
overflow:
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   555
	PyErr_SetString(PyExc_ValueError, "overflow in obsstore");
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   556
bail:
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   557
	Py_XDECREF(prec);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   558
	Py_XDECREF(succs);
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   559
	Py_XDECREF(metadata);
31426
43a7dfbead0c parsers: handle refcounting of "parents" consistently
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31292
diff changeset
   560
	Py_XDECREF(parents);
24017
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   561
	return ret;
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   562
}
72c9b5ae7278 parsers: add fm1readmarker
Augie Fackler <augie@google.com>
parents: 24004
diff changeset
   563
34440
7ed0750c71a1 cext: wrap before brace for functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34438
diff changeset
   564
static PyObject *fm1readmarkers(PyObject *self, PyObject *args)
7ed0750c71a1 cext: wrap before brace for functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34438
diff changeset
   565
{
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   566
	const char *data, *dataend;
26872
ce03e72837c6 parsers: fix width of datalen variable in fm1readmarkers
Yuya Nishihara <yuya@tcha.org>
parents: 26775
diff changeset
   567
	int datalen;
26107
50582df9d7a7 parsers: fix two cases of unsigned long instead of Py_ssize_t
Augie Fackler <augie@google.com>
parents: 26098
diff changeset
   568
	Py_ssize_t offset, stop;
24019
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   569
	PyObject *markers = NULL;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   570
36620
186c6df3a373 py3: bulk-replace 'const char*' format specifier passed to PyArg_ParseTuple*()
Yuya Nishihara <yuya@tcha.org>
parents: 36619
diff changeset
   571
	if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen,
186c6df3a373 py3: bulk-replace 'const char*' format specifier passed to PyArg_ParseTuple*()
Yuya Nishihara <yuya@tcha.org>
parents: 36619
diff changeset
   572
	                      &offset, &stop)) {
24019
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   573
		return NULL;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   574
	}
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   575
	dataend = data + datalen;
24019
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   576
	data += offset;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   577
	markers = PyList_New(0);
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   578
	if (!markers) {
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   579
		return NULL;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   580
	}
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   581
	while (offset < stop) {
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   582
		uint32_t msize;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   583
		int error;
26591
042344313939 parsers: fix infinite loop or out-of-bound read in fm1readmarkers (issue4888)
Yuya Nishihara <yuya@tcha.org>
parents: 26590
diff changeset
   584
		PyObject *record = fm1readmarker(data, dataend, &msize);
24019
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   585
		if (!record) {
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   586
			goto bail;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   587
		}
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   588
		error = PyList_Append(markers, record);
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   589
		Py_DECREF(record);
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   590
		if (error) {
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   591
			goto bail;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   592
		}
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   593
		data += msize;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   594
		offset += msize;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   595
	}
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   596
	return markers;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   597
bail:
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   598
	Py_DECREF(markers);
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   599
	return NULL;
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   600
}
26fbf07482b2 _fm1readmarkers: generate list in C
Martin von Zweigbergk <martinvonz@google.com>
parents: 24017
diff changeset
   601
6389
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   602
static char parsers_doc[] = "Efficient content parsing.";
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   603
17606
318fb32b980e pathencode: new C module with fast encodedir() function
Adrian Buehlmann <adrian@cadifra.com>
parents: 17356
diff changeset
   604
PyObject *encodedir(PyObject *self, PyObject *args);
17616
9535a0dc41f2 store: implement fncache basic path encoding in C
Bryan O'Sullivan <bryano@fb.com>
parents: 17606
diff changeset
   605
PyObject *pathencode(PyObject *self, PyObject *args);
18430
0459c6555f69 store: implement lowerencode in C
Bryan O'Sullivan <bryano@fb.com>
parents: 17616
diff changeset
   606
PyObject *lowerencode(PyObject *self, PyObject *args);
32378
7d0c69505a66 cext: extract revlog/index parsing code to own C file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32372
diff changeset
   607
PyObject *parse_index2(PyObject *self, PyObject *args);
17606
318fb32b980e pathencode: new C module with fast encodedir() function
Adrian Buehlmann <adrian@cadifra.com>
parents: 17356
diff changeset
   608
6389
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   609
static PyMethodDef methods[] = {
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   610
    {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   611
    {"nonnormalotherparententries", nonnormalotherparententries, METH_VARARGS,
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   612
     "create a set containing non-normal and other parent entries of given "
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   613
     "dirstate\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   614
    {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   615
    {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   616
    {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   617
    {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   618
    {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   619
    {"dict_new_presized", dict_new_presized, METH_VARARGS,
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   620
     "construct a dict with an expected size\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   621
    {"make_file_foldmap", make_file_foldmap, METH_VARARGS,
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   622
     "make file foldmap\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   623
    {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS,
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   624
     "escape a UTF-8 byte string to JSON (fast path)\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   625
    {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   626
    {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   627
    {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   628
    {"fm1readmarkers", fm1readmarkers, METH_VARARGS,
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   629
     "parse v1 obsolete markers\n"},
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   630
    {NULL, NULL}};
6389
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   631
18900
02ee846b246a scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents: 18567
diff changeset
   632
void dirs_module_init(PyObject *mod);
24214
a5f1bccd2996 manifest.c: new extension code to lazily parse manifests
Augie Fackler <augie@google.com>
parents: 24032
diff changeset
   633
void manifest_module_init(PyObject *mod);
32378
7d0c69505a66 cext: extract revlog/index parsing code to own C file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32372
diff changeset
   634
void revlog_module_init(PyObject *mod);
18900
02ee846b246a scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents: 18567
diff changeset
   635
40708
f2342483f7a6 cext: increment the `parsers` module version
Boris Feld <boris.feld@octobus.net>
parents: 39452
diff changeset
   636
static const int version = 12;
32360
af3ef002395d parsers: add version to help detect breaking binary changes
Jun Wu <quark@fb.com>
parents: 31470
diff changeset
   637
16363
2cdd7e63211b parsers: incrementally parse the revlog index in C
Bryan O'Sullivan <bryano@fb.com>
parents: 15033
diff changeset
   638
static void module_init(PyObject *mod)
2cdd7e63211b parsers: incrementally parse the revlog index in C
Bryan O'Sullivan <bryano@fb.com>
parents: 15033
diff changeset
   639
{
32360
af3ef002395d parsers: add version to help detect breaking binary changes
Jun Wu <quark@fb.com>
parents: 31470
diff changeset
   640
	PyModule_AddIntConstant(mod, "version", version);
af3ef002395d parsers: add version to help detect breaking binary changes
Jun Wu <quark@fb.com>
parents: 31470
diff changeset
   641
20742
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   642
	/* This module constant has two purposes.  First, it lets us unit test
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   643
	 * the ImportError raised without hard-coding any error text.  This
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   644
	 * means we can change the text in the future without breaking tests,
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   645
	 * even across changesets without a recompile.  Second, its presence
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   646
	 * can be used to determine whether the version-checking logic is
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   647
	 * present, which also helps in testing across changesets without a
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   648
	 * recompile.  Note that this means the pure-Python version of parsers
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   649
	 * should not have this module constant. */
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   650
	PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext);
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   651
18900
02ee846b246a scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents: 18567
diff changeset
   652
	dirs_module_init(mod);
24214
a5f1bccd2996 manifest.c: new extension code to lazily parse manifests
Augie Fackler <augie@google.com>
parents: 24032
diff changeset
   653
	manifest_module_init(mod);
32378
7d0c69505a66 cext: extract revlog/index parsing code to own C file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32372
diff changeset
   654
	revlog_module_init(mod);
18900
02ee846b246a scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents: 18567
diff changeset
   655
32384
2e5a476b2e46 cext: move back finalization of dirstateTupleType where it should be
Yuya Nishihara <yuya@tcha.org>
parents: 32378
diff changeset
   656
	if (PyType_Ready(&dirstateTupleType) < 0)
2e5a476b2e46 cext: move back finalization of dirstateTupleType where it should be
Yuya Nishihara <yuya@tcha.org>
parents: 32378
diff changeset
   657
		return;
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   658
	Py_INCREF(&dirstateTupleType);
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 21807
diff changeset
   659
	PyModule_AddObject(mod, "dirstatetuple",
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   660
	                   (PyObject *)&dirstateTupleType);
16363
2cdd7e63211b parsers: incrementally parse the revlog index in C
Bryan O'Sullivan <bryano@fb.com>
parents: 15033
diff changeset
   661
}
2cdd7e63211b parsers: incrementally parse the revlog index in C
Bryan O'Sullivan <bryano@fb.com>
parents: 15033
diff changeset
   662
20742
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   663
static int check_python_version(void)
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   664
{
23943
5fb44983a696 parsers: don't leak references to sys et al in check_python_version
Augie Fackler <augie@google.com>
parents: 23942
diff changeset
   665
	PyObject *sys = PyImport_ImportModule("sys"), *ver;
5fb44983a696 parsers: don't leak references to sys et al in check_python_version
Augie Fackler <augie@google.com>
parents: 23942
diff changeset
   666
	long hexversion;
5fb44983a696 parsers: don't leak references to sys et al in check_python_version
Augie Fackler <augie@google.com>
parents: 23942
diff changeset
   667
	if (!sys)
5fb44983a696 parsers: don't leak references to sys et al in check_python_version
Augie Fackler <augie@google.com>
parents: 23942
diff changeset
   668
		return -1;
5fb44983a696 parsers: don't leak references to sys et al in check_python_version
Augie Fackler <augie@google.com>
parents: 23942
diff changeset
   669
	ver = PyObject_GetAttrString(sys, "hexversion");
5fb44983a696 parsers: don't leak references to sys et al in check_python_version
Augie Fackler <augie@google.com>
parents: 23942
diff changeset
   670
	Py_DECREF(sys);
5fb44983a696 parsers: don't leak references to sys et al in check_python_version
Augie Fackler <augie@google.com>
parents: 23942
diff changeset
   671
	if (!ver)
5fb44983a696 parsers: don't leak references to sys et al in check_python_version
Augie Fackler <augie@google.com>
parents: 23942
diff changeset
   672
		return -1;
5fb44983a696 parsers: don't leak references to sys et al in check_python_version
Augie Fackler <augie@google.com>
parents: 23942
diff changeset
   673
	hexversion = PyInt_AsLong(ver);
5fb44983a696 parsers: don't leak references to sys et al in check_python_version
Augie Fackler <augie@google.com>
parents: 23942
diff changeset
   674
	Py_DECREF(ver);
20742
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   675
	/* sys.hexversion is a 32-bit number by default, so the -1 case
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   676
	 * should only occur in unusual circumstances (e.g. if sys.hexversion
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   677
	 * is manually set to an invalid value). */
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   678
	if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) {
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   679
		PyErr_Format(PyExc_ImportError,
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   680
		             "%s: The Mercurial extension "
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   681
		             "modules were compiled with Python " PY_VERSION
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   682
		             ", but "
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   683
		             "Mercurial is currently using Python with "
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   684
		             "sys.hexversion=%ld: "
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   685
		             "Python %s\n at: %s",
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   686
		             versionerrortext, hexversion, Py_GetVersion(),
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   687
		             Py_GetProgramFullPath());
20742
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   688
		return -1;
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   689
	}
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   690
	return 0;
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   691
}
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   692
11361
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
   693
#ifdef IS_PY3K
34862
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   694
static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers",
d92dc725223b parsers: allow clang-format here
Augie Fackler <augie@google.com>
parents: 34861
diff changeset
   695
                                            parsers_doc, -1, methods};
11361
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
   696
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
   697
PyMODINIT_FUNC PyInit_parsers(void)
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
   698
{
20797
e286ab22e461 parsers: fix compiler errors on MSVC 2008
Matt Harbison <matt_harbison@yahoo.com>
parents: 20742
diff changeset
   699
	PyObject *mod;
e286ab22e461 parsers: fix compiler errors on MSVC 2008
Matt Harbison <matt_harbison@yahoo.com>
parents: 20742
diff changeset
   700
20742
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   701
	if (check_python_version() == -1)
30090
8abe9264c73a parsers: return NULL from PyInit_parsers on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29444
diff changeset
   702
		return NULL;
20797
e286ab22e461 parsers: fix compiler errors on MSVC 2008
Matt Harbison <matt_harbison@yahoo.com>
parents: 20742
diff changeset
   703
	mod = PyModule_Create(&parsers_module);
16363
2cdd7e63211b parsers: incrementally parse the revlog index in C
Bryan O'Sullivan <bryano@fb.com>
parents: 15033
diff changeset
   704
	module_init(mod);
2cdd7e63211b parsers: incrementally parse the revlog index in C
Bryan O'Sullivan <bryano@fb.com>
parents: 15033
diff changeset
   705
	return mod;
11361
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
   706
}
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
   707
#else
6389
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   708
PyMODINIT_FUNC initparsers(void)
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   709
{
20797
e286ab22e461 parsers: fix compiler errors on MSVC 2008
Matt Harbison <matt_harbison@yahoo.com>
parents: 20742
diff changeset
   710
	PyObject *mod;
e286ab22e461 parsers: fix compiler errors on MSVC 2008
Matt Harbison <matt_harbison@yahoo.com>
parents: 20742
diff changeset
   711
20742
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   712
	if (check_python_version() == -1)
3681de20b0a7 parsers: fail fast if Python has wrong minor version (issue4110)
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 20555
diff changeset
   713
		return;
20797
e286ab22e461 parsers: fix compiler errors on MSVC 2008
Matt Harbison <matt_harbison@yahoo.com>
parents: 20742
diff changeset
   714
	mod = Py_InitModule3("parsers", methods, parsers_doc);
16363
2cdd7e63211b parsers: incrementally parse the revlog index in C
Bryan O'Sullivan <bryano@fb.com>
parents: 15033
diff changeset
   715
	module_init(mod);
6389
0231f763ebc8 manifest: improve parsing performance by 8x via a new C extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   716
}
11361
3de3d670d2b6 parsers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10449
diff changeset
   717
#endif