Ver código fonte

Drop python 2 compatibility in C extensions.

Jelmer Vernooij 4 anos atrás
pai
commit
b7f7c6b181
3 arquivos alterados com 33 adições e 131 exclusões
  1. 13 53
      dulwich/_diff_tree.c
  2. 7 33
      dulwich/_objects.c
  3. 13 45
      dulwich/_pack.c

+ 13 - 53
dulwich/_diff_tree.c

@@ -26,25 +26,6 @@
 typedef unsigned short mode_t;
 #endif
 
-#if PY_MAJOR_VERSION < 3
-typedef long Py_hash_t;
-#endif
-
-#if PY_MAJOR_VERSION >= 3
-#define PyInt_FromLong PyLong_FromLong
-#define PyInt_AsLong PyLong_AsLong
-#define PyInt_AS_LONG PyLong_AS_LONG
-#define PyString_AS_STRING PyBytes_AS_STRING
-#define PyString_AsStringAndSize PyBytes_AsStringAndSize
-#define PyString_Check PyBytes_Check
-#define PyString_CheckExact PyBytes_CheckExact
-#define PyString_FromStringAndSize PyBytes_FromStringAndSize
-#define PyString_FromString PyBytes_FromString
-#define PyString_GET_SIZE PyBytes_GET_SIZE
-#define PyString_Size PyBytes_Size
-#define _PyString_Join _PyBytes_Join
-#endif
-
 static PyObject *tree_entry_cls = NULL, *null_entry = NULL,
 	*defaultdict_cls = NULL, *int_cls = NULL;
 static int block_size;
@@ -118,7 +99,7 @@ static PyObject **tree_entries(char *path, Py_ssize_t path_len, PyObject *tree,
 		if (!sha)
 			goto error;
 		name = PyTuple_GET_ITEM(old_entry, 0);
-		name_len = PyString_Size(name);
+		name_len = PyBytes_Size(name);
 		if (PyErr_Occurred())
 			goto error;
 
@@ -133,18 +114,13 @@ static PyObject **tree_entries(char *path, Py_ssize_t path_len, PyObject *tree,
 		if (path_len) {
 			memcpy(new_path, path, path_len);
 			new_path[path_len] = '/';
-			memcpy(new_path + path_len + 1, PyString_AS_STRING(name), name_len);
+			memcpy(new_path + path_len + 1, PyBytes_AS_STRING(name), name_len);
 		} else {
-			memcpy(new_path, PyString_AS_STRING(name), name_len);
+			memcpy(new_path, PyBytes_AS_STRING(name), name_len);
 		}
 
-#if PY_MAJOR_VERSION >= 3
 		result[i] = PyObject_CallFunction(tree_entry_cls, "y#OO", new_path,
 			new_path_len, PyTuple_GET_ITEM(old_entry, 1), sha);
-#else
-		result[i] = PyObject_CallFunction(tree_entry_cls, "s#OO", new_path,
-			new_path_len, PyTuple_GET_ITEM(old_entry, 1), sha);
-#endif
 		PyMem_Free(new_path);
 		if (!result[i]) {
 			goto error;
@@ -172,7 +148,7 @@ static int entry_path_cmp(PyObject *entry1, PyObject *entry2)
 	if (!path1)
 		goto done;
 
-	if (!PyString_Check(path1)) {
+	if (!PyBytes_Check(path1)) {
 		PyErr_SetString(PyExc_TypeError, "path is not a (byte)string");
 		goto done;
 	}
@@ -181,12 +157,12 @@ static int entry_path_cmp(PyObject *entry1, PyObject *entry2)
 	if (!path2)
 		goto done;
 
-	if (!PyString_Check(path2)) {
+	if (!PyBytes_Check(path2)) {
 		PyErr_SetString(PyExc_TypeError, "path is not a (byte)string");
 		goto done;
 	}
 
-	result = strcmp(PyString_AS_STRING(path1), PyString_AS_STRING(path2));
+	result = strcmp(PyBytes_AS_STRING(path1), PyBytes_AS_STRING(path2));
 
 done:
 	Py_XDECREF(path1);
@@ -202,11 +178,7 @@ static PyObject *py_merge_entries(PyObject *self, PyObject *args)
 	char *path_str;
 	int cmp;
 
-#if PY_MAJOR_VERSION >= 3
 	if (!PyArg_ParseTuple(args, "y#OO", &path_str, &path_len, &tree1, &tree2))
-#else
-	if (!PyArg_ParseTuple(args, "s#OO", &path_str, &path_len, &tree1, &tree2))
-#endif
 		return NULL;
 
 	entries1 = tree_entries(path_str, path_len, tree1, &n1);
@@ -291,7 +263,7 @@ static PyObject *py_is_tree(PyObject *self, PyObject *args)
 		result = Py_False;
 		Py_INCREF(result);
 	} else {
-		lmode = PyInt_AsLong(mode);
+		lmode = PyLong_AsLong(mode);
 		if (lmode == -1 && PyErr_Occurred()) {
 			Py_DECREF(mode);
 			return NULL;
@@ -310,13 +282,13 @@ static Py_hash_t add_hash(PyObject *get, PyObject *set, char *str, int n)
 
 	/* It would be nice to hash without copying str into a PyString, but that
 	 * isn't exposed by the API. */
-	str_obj = PyString_FromStringAndSize(str, n);
+	str_obj = PyBytes_FromStringAndSize(str, n);
 	if (!str_obj)
 		goto error;
 	hash = PyObject_Hash(str_obj);
 	if (hash == -1)
 		goto error;
-	hash_obj = PyInt_FromLong(hash);
+	hash_obj = PyLong_FromLong(hash);
 	if (!hash_obj)
 		goto error;
 
@@ -324,7 +296,7 @@ static Py_hash_t add_hash(PyObject *get, PyObject *set, char *str, int n)
 	if (!value)
 		goto error;
 	set_value = PyObject_CallFunction(set, "(Ol)", hash_obj,
-		PyInt_AS_LONG(value) + n);
+		PyLong_AS_LONG(value) + n);
 	if (!set_value)
 		goto error;
 
@@ -377,11 +349,11 @@ static PyObject *py_count_blocks(PyObject *self, PyObject *args)
 
 	for (i = 0; i < num_chunks; i++) {
 		chunk = PyList_GET_ITEM(chunks, i);
-		if (!PyString_Check(chunk)) {
+		if (!PyBytes_Check(chunk)) {
 			PyErr_SetString(PyExc_TypeError, "chunk is not a string");
 			goto error;
 		}
-		if (PyString_AsStringAndSize(chunk, &chunk_str, &chunk_len) == -1)
+		if (PyBytes_AsStringAndSize(chunk, &chunk_str, &chunk_len) == -1)
 			goto error;
 
 		for (j = 0; j < chunk_len; j++) {
@@ -425,7 +397,6 @@ moduleinit(void)
 	PyObject *m, *objects_mod = NULL, *diff_tree_mod = NULL;
 	PyObject *block_size_obj = NULL;
 
-#if PY_MAJOR_VERSION >= 3
 	static struct PyModuleDef moduledef = {
 		PyModuleDef_HEAD_INIT,
 		"_diff_tree",         /* m_name */
@@ -438,9 +409,6 @@ moduleinit(void)
 		NULL,                 /* m_free */
 	};
 	m = PyModule_Create(&moduledef);
-#else
-	m = Py_InitModule("_diff_tree", py_diff_tree_methods);
-#endif
 	if (!m)
 		goto error;
 
@@ -464,7 +432,7 @@ moduleinit(void)
 	block_size_obj = PyObject_GetAttrString(diff_tree_mod, "_BLOCK_SIZE");
 	if (!block_size_obj)
 		goto error;
-	block_size = (int)PyInt_AsLong(block_size_obj);
+	block_size = (int)PyLong_AsLong(block_size_obj);
 
 	if (PyErr_Occurred())
 		goto error;
@@ -495,16 +463,8 @@ error:
 	return NULL;
 }
 
-#if PY_MAJOR_VERSION >= 3
 PyMODINIT_FUNC
 PyInit__diff_tree(void)
 {
 	return moduleinit();
 }
-#else
-PyMODINIT_FUNC
-init_diff_tree(void)
-{
-	moduleinit();
-}
-#endif

+ 7 - 33
dulwich/_objects.c

@@ -23,15 +23,6 @@
 #include <stdlib.h>
 #include <sys/stat.h>
 
-#if PY_MAJOR_VERSION >= 3
-#define PyInt_Check(obj) 0
-#define PyInt_CheckExact(obj) 0
-#define PyInt_AsLong PyLong_AsLong
-#define PyString_AS_STRING PyBytes_AS_STRING
-#define PyString_Check PyBytes_Check
-#define PyString_FromStringAndSize PyBytes_FromStringAndSize
-#endif
-
 #if defined(__MINGW32_VERSION) || defined(__APPLE__)
 size_t rep_strnlen(char *text, size_t maxlen);
 size_t rep_strnlen(char *text, size_t maxlen)
@@ -56,7 +47,7 @@ static PyObject *sha_to_pyhex(const unsigned char *sha)
 		hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
 	}
 
-	return PyString_FromStringAndSize(hexsha, 40);
+	return PyBytes_FromStringAndSize(hexsha, 40);
 }
 
 static PyObject *py_parse_tree(PyObject *self, PyObject *args, PyObject *kw)
@@ -67,13 +58,8 @@ static PyObject *py_parse_tree(PyObject *self, PyObject *args, PyObject *kw)
 	PyObject *ret, *item, *name, *sha, *py_strict = NULL;
 	static char *kwlist[] = {"text", "strict", NULL};
 
-#if PY_MAJOR_VERSION >= 3
 	if (!PyArg_ParseTupleAndKeywords(args, kw, "y#|O", kwlist,
 	                                 &text, &len, &py_strict))
-#else
-	if (!PyArg_ParseTupleAndKeywords(args, kw, "s#|O", kwlist,
-	                                 &text, &len, &py_strict))
-#endif
 		return NULL;
 	strict = py_strict ?  PyObject_IsTrue(py_strict) : 0;
 	/* TODO: currently this returns a list; if memory usage is a concern,
@@ -100,7 +86,7 @@ static PyObject *py_parse_tree(PyObject *self, PyObject *args, PyObject *kw)
 		}
 		text++;
 		namelen = strnlen(text, len - (text - start));
-		name = PyString_FromStringAndSize(text, namelen);
+		name = PyBytes_FromStringAndSize(text, namelen);
 		if (name == NULL) {
 			Py_DECREF(ret);
 			return NULL;
@@ -207,7 +193,7 @@ static PyObject *py_sorted_tree_items(PyObject *self, PyObject *args)
 	}
 
 	while (PyDict_Next(entries, &pos, &key, &value)) {
-		if (!PyString_Check(key)) {
+		if (!PyBytes_Check(key)) {
 			PyErr_SetString(PyExc_TypeError, "Name is not a string");
 			goto error;
 		}
@@ -218,18 +204,18 @@ static PyObject *py_sorted_tree_items(PyObject *self, PyObject *args)
 		}
 
 		py_mode = PyTuple_GET_ITEM(value, 0);
-		if (!PyInt_Check(py_mode) && !PyLong_Check(py_mode)) {
+		if (!PyLong_Check(py_mode)) {
 			PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
 			goto error;
 		}
 
 		py_sha = PyTuple_GET_ITEM(value, 1);
-		if (!PyString_Check(py_sha)) {
+		if (!PyBytes_Check(py_sha)) {
 			PyErr_SetString(PyExc_TypeError, "SHA is not a string");
 			goto error;
 		}
-		qsort_entries[n].name = PyString_AS_STRING(key);
-		qsort_entries[n].mode = PyInt_AsLong(py_mode);
+		qsort_entries[n].name = PyBytes_AS_STRING(key);
+		qsort_entries[n].mode = PyLong_AsLong(py_mode);
 
 		qsort_entries[n].tuple = PyObject_CallFunctionObjArgs(
 		                tree_entry_cls, key, py_mode, py_sha, NULL);
@@ -272,7 +258,6 @@ moduleinit(void)
 {
 	PyObject *m, *objects_mod, *errors_mod;
 
-#if PY_MAJOR_VERSION >= 3
 	static struct PyModuleDef moduledef = {
 		PyModuleDef_HEAD_INIT,
 		"_objects",         /* m_name */
@@ -285,9 +270,6 @@ moduleinit(void)
 		NULL,               /* m_free */
 	};
 	m = PyModule_Create(&moduledef);
-#else
-	m = Py_InitModule3("_objects", py_objects_methods, NULL);
-#endif
 	if (m == NULL) {
 		return NULL;
 	}
@@ -320,16 +302,8 @@ moduleinit(void)
 	return m;
 }
 
-#if PY_MAJOR_VERSION >= 3
 PyMODINIT_FUNC
 PyInit__objects(void)
 {
 	return moduleinit();
 }
-#else
-PyMODINIT_FUNC
-init_objects(void)
-{
-	moduleinit();
-}
-#endif

+ 13 - 45
dulwich/_pack.c

@@ -22,27 +22,14 @@
 #include <Python.h>
 #include <stdint.h>
 
-#if PY_MAJOR_VERSION >= 3
-#define PyInt_FromLong PyLong_FromLong
-#define PyString_AS_STRING PyBytes_AS_STRING
-#define PyString_AS_STRING PyBytes_AS_STRING
-#define PyString_Check PyBytes_Check
-#define PyString_CheckExact PyBytes_CheckExact
-#define PyString_FromStringAndSize PyBytes_FromStringAndSize
-#define PyString_FromString PyBytes_FromString
-#define PyString_GET_SIZE PyBytes_GET_SIZE
-#define PyString_Size PyBytes_Size
-#define _PyString_Join _PyBytes_Join
-#endif
-
 static PyObject *PyExc_ApplyDeltaError = NULL;
 
 static int py_is_sha(PyObject *sha)
 {
-	if (!PyString_CheckExact(sha))
+	if (!PyBytes_CheckExact(sha))
 		return 0;
 
-	if (PyString_Size(sha) != 20)
+	if (PyBytes_Size(sha) != 20)
 		return 0;
 
 	return 1;
@@ -67,18 +54,18 @@ static size_t get_delta_header_size(uint8_t *delta, size_t *index, size_t length
 static PyObject *py_chunked_as_string(PyObject *py_buf)
 {
 	if (PyList_Check(py_buf)) {
-		PyObject *sep = PyString_FromString("");
+		PyObject *sep = PyBytes_FromString("");
 		if (sep == NULL) {
 			PyErr_NoMemory();
 			return NULL;
 		}
-		py_buf = _PyString_Join(sep, py_buf);
+		py_buf = _PyBytes_Join(sep, py_buf);
 		Py_DECREF(sep);
 		if (py_buf == NULL) {
 			PyErr_NoMemory();
 			return NULL;
 		}
-	} else if (PyString_Check(py_buf)) {
+	} else if (PyBytes_Check(py_buf)) {
 		Py_INCREF(py_buf);
 	} else {
 		PyErr_SetString(PyExc_TypeError,
@@ -111,11 +98,11 @@ static PyObject *py_apply_delta(PyObject *self, PyObject *args)
 		return NULL;
 	}
 
-	src_buf = (uint8_t *)PyString_AS_STRING(py_src_buf);
-	src_buf_len = (size_t)PyString_GET_SIZE(py_src_buf);
+	src_buf = (uint8_t *)PyBytes_AS_STRING(py_src_buf);
+	src_buf_len = (size_t)PyBytes_GET_SIZE(py_src_buf);
 
-	delta = (uint8_t *)PyString_AS_STRING(py_delta);
-	delta_len = (size_t)PyString_GET_SIZE(py_delta);
+	delta = (uint8_t *)PyBytes_AS_STRING(py_delta);
+	delta_len = (size_t)PyBytes_GET_SIZE(py_delta);
 
 	index = 0;
 	src_size = get_delta_header_size(delta, &index, delta_len);
@@ -127,14 +114,14 @@ static PyObject *py_apply_delta(PyObject *self, PyObject *args)
 		return NULL;
 	}
 	dest_size = get_delta_header_size(delta, &index, delta_len);
-	ret = PyString_FromStringAndSize(NULL, dest_size);
+	ret = PyBytes_FromStringAndSize(NULL, dest_size);
 	if (ret == NULL) {
 		PyErr_NoMemory();
 		Py_DECREF(py_src_buf);
 		Py_DECREF(py_delta);
 		return NULL;
 	}
-	out = (uint8_t *)PyString_AS_STRING(ret);
+	out = (uint8_t *)PyBytes_AS_STRING(ret);
 	while (index < delta_len) {
 		uint8_t cmd = delta[index];
 		index++;
@@ -208,13 +195,8 @@ static PyObject *py_bisect_find_sha(PyObject *self, PyObject *args)
 	char *sha;
 	Py_ssize_t sha_len;
 	int start, end;
-#if PY_MAJOR_VERSION >= 3
 	if (!PyArg_ParseTuple(args, "iiy#O", &start, &end,
 			      &sha, &sha_len, &unpack_name))
-#else
-	if (!PyArg_ParseTuple(args, "iis#O", &start, &end,
-			      &sha, &sha_len, &unpack_name))
-#endif
 		return NULL;
 
 	if (sha_len != 20) {
@@ -239,14 +221,14 @@ static PyObject *py_bisect_find_sha(PyObject *self, PyObject *args)
 			Py_DECREF(file_sha);
 			return NULL;
 		}
-		cmp = memcmp(PyString_AS_STRING(file_sha), sha, 20);
+		cmp = memcmp(PyBytes_AS_STRING(file_sha), sha, 20);
 		Py_DECREF(file_sha);
 		if (cmp < 0)
 			start = i + 1;
 		else if (cmp > 0)
 			end = i - 1;
 		else {
-			return PyInt_FromLong(i);
+			return PyLong_FromLong(i);
 		}
 	}
 	Py_RETURN_NONE;
@@ -265,7 +247,6 @@ moduleinit(void)
 	PyObject *m;
 	PyObject *errors_module;
 
-#if PY_MAJOR_VERSION >= 3
 	static struct PyModuleDef moduledef = {
 	  PyModuleDef_HEAD_INIT,
 	  "_pack",         /* m_name */
@@ -277,7 +258,6 @@ moduleinit(void)
 	  NULL,            /* m_clear*/
 	  NULL,            /* m_free */
 	};
-#endif
 
 	errors_module = PyImport_ImportModule("dulwich.errors");
 	if (errors_module == NULL)
@@ -288,27 +268,15 @@ moduleinit(void)
 	if (PyExc_ApplyDeltaError == NULL)
 		return NULL;
 
-#if PY_MAJOR_VERSION >= 3
 	m = PyModule_Create(&moduledef);
-#else
-	m = Py_InitModule3("_pack", py_pack_methods, NULL);
-#endif
 	if (m == NULL)
 		return NULL;
 
 	return m;
 }
 
-#if PY_MAJOR_VERSION >= 3
 PyMODINIT_FUNC
 PyInit__pack(void)
 {
 	return moduleinit();
 }
-#else
-PyMODINIT_FUNC
-init_pack(void)
-{
-	moduleinit();
-}
-#endif