From 3abad6ea155a7f6e138e1de3ac5428177bfb0254 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Thu, 22 Sep 2022 23:46:52 +0900 Subject: [PATCH 1/2] drop python2 --- autoload/fuzzycomt.c | 54 ++++++++++++++++++++++++++++---------------- autoload/fuzzycomt.h | 14 ++++++------ autoload/matcher.vim | 4 ++-- autoload/setup.py | 2 +- install_windows.bat | 4 ++-- 5 files changed, 47 insertions(+), 31 deletions(-) diff --git a/autoload/fuzzycomt.c b/autoload/fuzzycomt.c index ec48124..45b00be 100644 --- a/autoload/fuzzycomt.c +++ b/autoload/fuzzycomt.c @@ -83,10 +83,12 @@ int ctrlp_comp_alpha(const void *a, const void *b) { matchobj_t a_val = *(matchobj_t *)a; matchobj_t b_val = *(matchobj_t *)b; - char *a_p = PyString_AsString(a_val.str); - long a_len = PyString_Size(a_val.str); - char *b_p = PyString_AsString(b_val.str); - long b_len = PyString_Size(b_val.str); + const char *a_p = PyUnicode_AsUTF8(a_val.str); + Py_ssize_t a_len = 0; + PyUnicode_AsUTF8AndSize(a_val.str, &a_len); + const char *b_p = PyUnicode_AsUTF8(b_val.str); + Py_ssize_t b_len = 0; + PyUnicode_AsUTF8AndSize(b_val.str, &b_len); int order = 0; if (a_len > b_len) { @@ -236,7 +238,7 @@ PyObject* ctrlp_fuzzycomt_match(PyObject* self, PyObject* args) { return 0; } - if (PyString_Check(abbrev) != 1) { + if (PyUnicode_Check(abbrev) != 1) { PyErr_SetString(PyExc_TypeError,"expected a string"); return 0; } @@ -247,7 +249,9 @@ PyObject* ctrlp_fuzzycomt_match(PyObject* self, PyObject* args) { limit = PyList_Size(paths); } - if ( PyString_Size(abbrev) == 0) { + Py_ssize_t len = 0; + PyUnicode_AsUTF8AndSize(abbrev, &len); + if (len == 0) { // if string is empty - just return first (:param limit) lines PyObject *initlist; @@ -304,7 +308,7 @@ PyObject* ctrlp_fuzzycomt_sorted_match_list(PyObject* self, PyObject* args) { return 0; } - if (PyString_Check(abbrev) != 1) { + if (PyUnicode_Check(abbrev) != 1) { PyErr_SetString(PyExc_TypeError,"expected a string"); return 0; } @@ -315,7 +319,9 @@ PyObject* ctrlp_fuzzycomt_sorted_match_list(PyObject* self, PyObject* args) { limit = PyList_Size(paths); } - if ( PyString_Size(abbrev) == 0) { + Py_ssize_t len = 0; + PyUnicode_AsUTF8AndSize(abbrev, &len); + if (len == 0) { // if string is empty - just return first (:param limit) lines PyObject *initlist; @@ -352,16 +358,16 @@ PyObject* ctrlp_fuzzycomt_sorted_match_list(PyObject* self, PyObject* args) { matchobj_t ctrlp_find_match(PyObject* str, PyObject* abbrev, char *mmode) { - long i, max; + size_t i, max; double score; matchobj_t returnobj; // Make a copy of input string to replace all backslashes. - // We need to create a copy because PyString_AsString returns + // We need to create a copy because PyUnicode_AsUTF8 returns // string that must not be changed. // We will free() it later char *temp_string; - temp_string = strduplicate(PyString_AsString(str)); + temp_string = strduplicate(PyUnicode_AsUTF8(str)); // Replace all backslashes for (i = 0; i < strlen(temp_string); i++) { @@ -377,11 +383,14 @@ matchobj_t ctrlp_find_match(PyObject* str, PyObject* abbrev, char *mmode) m.haystack_len = strlen(m.haystack_p); } else { - m.haystack_p = temp_string; - m.haystack_len = PyString_Size(str); + m.haystack_p = temp_string; + m.haystack_len = 0; + PyUnicode_AsUTF8AndSize(str, &m.haystack_len); } - m.needle_p = PyString_AsString(abbrev); - m.needle_len = PyString_Size(abbrev); + m.needle_p = PyUnicode_AsUTF8(abbrev); + m.needle_len = 0; + PyUnicode_AsUTF8AndSize(abbrev, &m.needle_len); + m.max_score_per_char = (1.0 / m.haystack_len + 1.0 / m.needle_len) / 2; m.dot_file = 0; @@ -392,7 +401,7 @@ matchobj_t ctrlp_find_match(PyObject* str, PyObject* abbrev, char *mmode) if (m.needle_len == 0) { // filter out dot files - for (i = 0; i < m.haystack_len; i++) { + for (i = 0; i < (size_t) m.haystack_len; i++) { char c = m.haystack_p[i]; if (c == '.' && (i == 0 || m.haystack_p[i - 1] == '/')) { score = 0.0; @@ -433,8 +442,15 @@ static PyMethodDef fuzzycomt_funcs[] = { {NULL} }; -PyMODINIT_FUNC initfuzzycomt() +static struct PyModuleDef fuzzycomt_module = { + PyModuleDef_HEAD_INIT, + "fuzzycomt", + "", + -1, + fuzzycomt_funcs, +}; + +PyMODINIT_FUNC PyInit_fuzzycomt() { - Py_InitModule3("fuzzycomt", fuzzycomt_funcs, - "Fuzzy matching module"); + return PyModule_Create(&fuzzycomt_module); } diff --git a/autoload/fuzzycomt.h b/autoload/fuzzycomt.h index 55d4ded..c345a40 100644 --- a/autoload/fuzzycomt.h +++ b/autoload/fuzzycomt.h @@ -34,13 +34,13 @@ typedef struct { } matchobj_t; typedef struct { - char *haystack_p; // pointer to string to be searched - long haystack_len; // length of same - char *needle_p; // pointer to search string (abbreviation) - long needle_len; // length of same - double max_score_per_char; - int dot_file; // boolean: true if str is a dot-file - double *memo; // memoization + char *haystack_p; // pointer to string to be searched + Py_ssize_t haystack_len; // length of same + const char *needle_p; // pointer to search string (abbreviation) + Py_ssize_t needle_len; // length of same + double max_score_per_char; + int dot_file; // boolean: true if str is a dot-file + double *memo; // memoization } matchinfo_t; matchobj_t ctrlp_find_match(PyObject* str, PyObject* abbrev, char *mmode); diff --git a/autoload/matcher.vim b/autoload/matcher.vim index a55bd34..7509d55 100644 --- a/autoload/matcher.vim +++ b/autoload/matcher.vim @@ -23,7 +23,7 @@ else endif let s:script_folder_path = escape( expand( ':p:h' ), '\' ) -python << ImportEOF +python3 << ImportEOF import sys, os, vim sys.path.insert( 0, os.path.abspath( vim.eval('s:script_folder_path' ) ) ) import fuzzycomt @@ -40,7 +40,7 @@ fu! s:matchfname(item, pat) endf fu! s:cmatcher(lines, input, limit, mmode, ispath, crfile) -python << EOF +python3 << EOF lines = vim.eval('a:lines') searchinp = vim.eval('a:input') limit = int(vim.eval('a:limit')) diff --git a/autoload/setup.py b/autoload/setup.py index 324e5af..d5684a6 100644 --- a/autoload/setup.py +++ b/autoload/setup.py @@ -1,4 +1,4 @@ -from distutils.core import setup, Extension +from setuptools import setup, Extension import os, platform if os.name == 'nt' and platform.architecture()[0] == '64bit': diff --git a/install_windows.bat b/install_windows.bat index 293cc11..7efad25 100644 --- a/install_windows.bat +++ b/install_windows.bat @@ -2,6 +2,6 @@ pushd autoload python setup.py build -c mingw32 pushd build\lib* -xcopy fuzzycomt.pyd ..\..\ +copy fuzzycomt*.pyd ..\..\fuzzycomt.pyd +popd popd -popd \ No newline at end of file From 385c8d02398dbb328b1a943a94e7109fe6473a08 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Thu, 22 Sep 2022 14:48:23 +0000 Subject: [PATCH 2/2] drop python2 --- install.sh | 46 ++-------------------------------------------- 1 file changed, 2 insertions(+), 44 deletions(-) diff --git a/install.sh b/install.sh index 2fcd3dc..48daf52 100755 --- a/install.sh +++ b/install.sh @@ -1,47 +1,5 @@ #!/usr/bin/env sh -chkPython2() -{ - cmd=$1 - ret=$($cmd -V 2>&1) - case "$ret" in - "Python 2."*) - return 0 - ;; - *) - return 1 - ;; - esac -} - -findPython2() -{ - cmd_list="python python2 python27 python2.7 python26 python2.6" - for cmd in $cmd_list; do - if chkPython2 $cmd; then - found_python=$cmd - break - fi - done - - if [ "$found_python" = "" ]; then - echo "cannot find python2 automatically" >&2 - while true; do - read -p "please input your python 2 command: " cmd - if chkPython2 "$cmd"; then - found_python=$cmd - break - fi - echo "verify [$cmd] with -V failed" >&2 - done - fi - - echo $found_python -} - -python=$(findPython2) -echo "find python2 -> $python" - cd autoload -$python setup.py build -cp build/lib*/fuzzycomt.so . +python setup.py build +cp build/lib*/fuzzycomt*.so fuzzycomt.so