base85: switch to policy importer
authorYuya Nishihara <yuya@tcha.org>
Sat, 13 Aug 2016 12:08:23 +0900
changeset 32368 008d37c4d783
parent 32367 a9c71d578a1c
child 32369 3b88a7fa97d8
base85: switch to policy importer
contrib/check-py3-compat.py
contrib/import-checker.py
contrib/wix/dist.wxs
mercurial/__init__.py
mercurial/base85.c
mercurial/cext/base85.c
mercurial/debugcommands.py
mercurial/util.py
setup.py
--- a/contrib/check-py3-compat.py	Fri Aug 12 11:35:17 2016 +0900
+++ b/contrib/check-py3-compat.py	Sat Aug 13 12:08:23 2016 +0900
@@ -17,7 +17,6 @@
 
 # Modules that have both Python and C implementations.
 _dualmodules = (
-    'base85.py',
     'bdiff.py',
     'diffhelpers.py',
     'mpatch.py',
--- a/contrib/import-checker.py	Fri Aug 12 11:35:17 2016 +0900
+++ b/contrib/import-checker.py	Sat Aug 13 12:08:23 2016 +0900
@@ -26,7 +26,6 @@
 
 # Modules that have both Python and C implementations.
 _dualmodules = (
-    'base85.py',
     'bdiff.py',
     'diffhelpers.py',
     'mpatch.py',
--- a/contrib/wix/dist.wxs	Fri Aug 12 11:35:17 2016 +0900
+++ b/contrib/wix/dist.wxs	Sat Aug 13 12:08:23 2016 +0900
@@ -12,7 +12,7 @@
       <Directory Id="libdir" Name="lib" FileSource="$(var.SourceDir)/lib">
         <Component Id="libOutput" Guid="$(var.lib.guid)" Win64='$(var.IsX64)'>
           <File Name="library.zip" KeyPath="yes" />
-          <File Name="mercurial.base85.pyd" />
+          <File Name="mercurial.cext.base85.pyd" />
           <File Name="mercurial.bdiff.pyd" />
           <File Name="mercurial.diffhelpers.pyd" />
           <File Name="mercurial.mpatch.pyd" />
--- a/mercurial/__init__.py	Fri Aug 12 11:35:17 2016 +0900
+++ b/mercurial/__init__.py	Sat Aug 13 12:08:23 2016 +0900
@@ -23,7 +23,6 @@
 # Modules that have both Python and C implementations. See also the
 # set of .py files under mercurial/pure/.
 _dualmodules = {
-    'mercurial.base85',
     'mercurial.bdiff',
     'mercurial.diffhelpers',
     'mercurial.mpatch',
--- a/mercurial/base85.c	Fri Aug 12 11:35:17 2016 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,189 +0,0 @@
-/*
- base85 codec
-
- Copyright 2006 Brendan Cully <brendan@kublai.com>
-
- This software may be used and distributed according to the terms of
- the GNU General Public License, incorporated herein by reference.
-
- Largely based on git's implementation
-*/
-
-#define PY_SSIZE_T_CLEAN
-#include <Python.h>
-
-#include "util.h"
-
-static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-	"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
-static char b85dec[256];
-
-static void b85prep(void)
-{
-	unsigned i;
-
-	memset(b85dec, 0, sizeof(b85dec));
-	for (i = 0; i < sizeof(b85chars); i++)
-		b85dec[(int)(b85chars[i])] = i + 1;
-}
-
-static PyObject *b85encode(PyObject *self, PyObject *args)
-{
-	const unsigned char *text;
-	PyObject *out;
-	char *dst;
-	Py_ssize_t len, olen, i;
-	unsigned int acc, val, ch;
-	int pad = 0;
-
-	if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
-		return NULL;
-
-	if (pad)
-		olen = ((len + 3) / 4 * 5) - 3;
-	else {
-		olen = len % 4;
-		if (olen)
-			olen++;
-		olen += len / 4 * 5;
-	}
-	if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3)))
-		return NULL;
-
-	dst = PyBytes_AsString(out);
-
-	while (len) {
-		acc = 0;
-		for (i = 24; i >= 0; i -= 8) {
-			ch = *text++;
-			acc |= ch << i;
-			if (--len == 0)
-				break;
-		}
-		for (i = 4; i >= 0; i--) {
-			val = acc % 85;
-			acc /= 85;
-			dst[i] = b85chars[val];
-		}
-		dst += 5;
-	}
-
-	if (!pad)
-		_PyBytes_Resize(&out, olen);
-
-	return out;
-}
-
-static PyObject *b85decode(PyObject *self, PyObject *args)
-{
-	PyObject *out;
-	const char *text;
-	char *dst;
-	Py_ssize_t len, i, j, olen, cap;
-	int c;
-	unsigned int acc;
-
-	if (!PyArg_ParseTuple(args, "s#", &text, &len))
-		return NULL;
-
-	olen = len / 5 * 4;
-	i = len % 5;
-	if (i)
-		olen += i - 1;
-	if (!(out = PyBytes_FromStringAndSize(NULL, olen)))
-		return NULL;
-
-	dst = PyBytes_AsString(out);
-
-	i = 0;
-	while (i < len)
-	{
-		acc = 0;
-		cap = len - i - 1;
-		if (cap > 4)
-			cap = 4;
-		for (j = 0; j < cap; i++, j++)
-		{
-			c = b85dec[(int)*text++] - 1;
-			if (c < 0)
-				return PyErr_Format(
-					PyExc_ValueError,
-					"bad base85 character at position %d",
-					(int)i);
-			acc = acc * 85 + c;
-		}
-		if (i++ < len)
-		{
-			c = b85dec[(int)*text++] - 1;
-			if (c < 0)
-				return PyErr_Format(
-					PyExc_ValueError,
-					"bad base85 character at position %d",
-					(int)i);
-			/* overflow detection: 0xffffffff == "|NsC0",
-			 * "|NsC" == 0x03030303 */
-			if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c)
-				return PyErr_Format(
-					PyExc_ValueError,
-					"bad base85 sequence at position %d",
-					(int)i);
-			acc += c;
-		}
-
-		cap = olen < 4 ? olen : 4;
-		olen -= cap;
-		for (j = 0; j < 4 - cap; j++)
-			acc *= 85;
-		if (cap && cap < 4)
-			acc += 0xffffff >> (cap - 1) * 8;
-		for (j = 0; j < cap; j++)
-		{
-			acc = (acc << 8) | (acc >> 24);
-			*dst++ = acc;
-		}
-	}
-
-	return out;
-}
-
-static char base85_doc[] = "Base85 Data Encoding";
-
-static PyMethodDef methods[] = {
-	{"b85encode", b85encode, METH_VARARGS,
-	 "Encode text in base85.\n\n"
-	 "If the second parameter is true, pad the result to a multiple of "
-	 "five characters.\n"},
-	{"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
-	{NULL, NULL}
-};
-
-static const int version = 1;
-
-#ifdef IS_PY3K
-static struct PyModuleDef base85_module = {
-	PyModuleDef_HEAD_INIT,
-	"base85",
-	base85_doc,
-	-1,
-	methods
-};
-
-PyMODINIT_FUNC PyInit_base85(void)
-{
-	PyObject *m;
-	b85prep();
-
-	m = PyModule_Create(&base85_module);
-	PyModule_AddIntConstant(m, "version", version);
-	return m;
-}
-#else
-PyMODINIT_FUNC initbase85(void)
-{
-	PyObject *m;
-	m = Py_InitModule3("base85", methods, base85_doc);
-
-	b85prep();
-	PyModule_AddIntConstant(m, "version", version);
-}
-#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/cext/base85.c	Sat Aug 13 12:08:23 2016 +0900
@@ -0,0 +1,189 @@
+/*
+ base85 codec
+
+ Copyright 2006 Brendan Cully <brendan@kublai.com>
+
+ This software may be used and distributed according to the terms of
+ the GNU General Public License, incorporated herein by reference.
+
+ Largely based on git's implementation
+*/
+
+#define PY_SSIZE_T_CLEAN
+#include <Python.h>
+
+#include "util.h"
+
+static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+	"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
+static char b85dec[256];
+
+static void b85prep(void)
+{
+	unsigned i;
+
+	memset(b85dec, 0, sizeof(b85dec));
+	for (i = 0; i < sizeof(b85chars); i++)
+		b85dec[(int)(b85chars[i])] = i + 1;
+}
+
+static PyObject *b85encode(PyObject *self, PyObject *args)
+{
+	const unsigned char *text;
+	PyObject *out;
+	char *dst;
+	Py_ssize_t len, olen, i;
+	unsigned int acc, val, ch;
+	int pad = 0;
+
+	if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
+		return NULL;
+
+	if (pad)
+		olen = ((len + 3) / 4 * 5) - 3;
+	else {
+		olen = len % 4;
+		if (olen)
+			olen++;
+		olen += len / 4 * 5;
+	}
+	if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3)))
+		return NULL;
+
+	dst = PyBytes_AsString(out);
+
+	while (len) {
+		acc = 0;
+		for (i = 24; i >= 0; i -= 8) {
+			ch = *text++;
+			acc |= ch << i;
+			if (--len == 0)
+				break;
+		}
+		for (i = 4; i >= 0; i--) {
+			val = acc % 85;
+			acc /= 85;
+			dst[i] = b85chars[val];
+		}
+		dst += 5;
+	}
+
+	if (!pad)
+		_PyBytes_Resize(&out, olen);
+
+	return out;
+}
+
+static PyObject *b85decode(PyObject *self, PyObject *args)
+{
+	PyObject *out;
+	const char *text;
+	char *dst;
+	Py_ssize_t len, i, j, olen, cap;
+	int c;
+	unsigned int acc;
+
+	if (!PyArg_ParseTuple(args, "s#", &text, &len))
+		return NULL;
+
+	olen = len / 5 * 4;
+	i = len % 5;
+	if (i)
+		olen += i - 1;
+	if (!(out = PyBytes_FromStringAndSize(NULL, olen)))
+		return NULL;
+
+	dst = PyBytes_AsString(out);
+
+	i = 0;
+	while (i < len)
+	{
+		acc = 0;
+		cap = len - i - 1;
+		if (cap > 4)
+			cap = 4;
+		for (j = 0; j < cap; i++, j++)
+		{
+			c = b85dec[(int)*text++] - 1;
+			if (c < 0)
+				return PyErr_Format(
+					PyExc_ValueError,
+					"bad base85 character at position %d",
+					(int)i);
+			acc = acc * 85 + c;
+		}
+		if (i++ < len)
+		{
+			c = b85dec[(int)*text++] - 1;
+			if (c < 0)
+				return PyErr_Format(
+					PyExc_ValueError,
+					"bad base85 character at position %d",
+					(int)i);
+			/* overflow detection: 0xffffffff == "|NsC0",
+			 * "|NsC" == 0x03030303 */
+			if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c)
+				return PyErr_Format(
+					PyExc_ValueError,
+					"bad base85 sequence at position %d",
+					(int)i);
+			acc += c;
+		}
+
+		cap = olen < 4 ? olen : 4;
+		olen -= cap;
+		for (j = 0; j < 4 - cap; j++)
+			acc *= 85;
+		if (cap && cap < 4)
+			acc += 0xffffff >> (cap - 1) * 8;
+		for (j = 0; j < cap; j++)
+		{
+			acc = (acc << 8) | (acc >> 24);
+			*dst++ = acc;
+		}
+	}
+
+	return out;
+}
+
+static char base85_doc[] = "Base85 Data Encoding";
+
+static PyMethodDef methods[] = {
+	{"b85encode", b85encode, METH_VARARGS,
+	 "Encode text in base85.\n\n"
+	 "If the second parameter is true, pad the result to a multiple of "
+	 "five characters.\n"},
+	{"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
+	{NULL, NULL}
+};
+
+static const int version = 1;
+
+#ifdef IS_PY3K
+static struct PyModuleDef base85_module = {
+	PyModuleDef_HEAD_INIT,
+	"base85",
+	base85_doc,
+	-1,
+	methods
+};
+
+PyMODINIT_FUNC PyInit_base85(void)
+{
+	PyObject *m;
+	b85prep();
+
+	m = PyModule_Create(&base85_module);
+	PyModule_AddIntConstant(m, "version", version);
+	return m;
+}
+#else
+PyMODINIT_FUNC initbase85(void)
+{
+	PyObject *m;
+	m = Py_InitModule3("base85", methods, base85_doc);
+
+	b85prep();
+	PyModule_AddIntConstant(m, "version", version);
+}
+#endif
--- a/mercurial/debugcommands.py	Fri Aug 12 11:35:17 2016 +0900
+++ b/mercurial/debugcommands.py	Sat Aug 13 12:08:23 2016 +0900
@@ -1026,11 +1026,11 @@
         err = None
         try:
             from . import (
-                base85,
                 bdiff,
                 mpatch,
             )
             from .cext import (
+                base85,
                 osutil,
             )
             dir(bdiff), dir(mpatch), dir(base85), dir(osutil) # quiet pyflakes
--- a/mercurial/util.py	Fri Aug 12 11:35:17 2016 +0900
+++ b/mercurial/util.py	Sat Aug 13 12:08:23 2016 +0900
@@ -42,7 +42,6 @@
 import zlib
 
 from . import (
-    base85,
     encoding,
     error,
     i18n,
@@ -51,6 +50,7 @@
     pycompat,
 )
 
+base85 = policy.importmod(r'base85')
 osutil = policy.importmod(r'osutil')
 
 b85decode = base85.b85decode
--- a/setup.py	Fri Aug 12 11:35:17 2016 +0900
+++ b/setup.py	Sat Aug 13 12:08:23 2016 +0900
@@ -621,7 +621,7 @@
     osutil_ldflags += ['-framework', 'ApplicationServices']
 
 extmodules = [
-    Extension('mercurial.base85', ['mercurial/base85.c'],
+    Extension('mercurial.cext.base85', ['mercurial/cext/base85.c'],
               include_dirs=common_include_dirs,
               depends=common_depends),
     Extension('mercurial.bdiff', ['mercurial/bdiff.c',