Last active
January 15, 2017 02:46
-
-
Save koron/90f6b56b73c211e059a3b8ab1db335a9 to your computer and use it in GitHub Desktop.
https://github.com/vim-jp/issues/issues/1008 patch to warn when python2 is used in Vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/runtime/doc/if_pyth.txt b/runtime/doc/if_pyth.txt | |
index 5929bcf..d226bf2 100644 | |
--- a/runtime/doc/if_pyth.txt | |
+++ b/runtime/doc/if_pyth.txt | |
@@ -811,5 +811,17 @@ dynamically, these has() calls will try to load them. If only one can be | |
loaded at a time, just checking if Python 2 or 3 are available will prevent | |
the other one from being available. | |
+ | |
+ *:py2usingfrom* | |
+Python 2.x will retire at 2020. (See https://pythonclock.org/). To help move | |
+Python 3, vim has a command to detect use of Python 2.x | |
+ | |
+To see which scripts are using python 2, use `:py2usingfrom` comand: > | |
+ :py2usingfrom | |
+ Python 2 is used from these files: | |
+ /path/to/your/scirpt/uses/python2a.vim | |
+ /path/to/your/scirpt/uses/python2b.vim | |
+ /path/to/your/scirpt/uses/python2c.vim | |
+< | |
============================================================================== | |
vim:tw=78:ts=8:ft=help:norl: | |
diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt | |
index 88dca60..7f4fc04 100644 | |
--- a/runtime/doc/options.txt | |
+++ b/runtime/doc/options.txt | |
@@ -5765,6 +5765,11 @@ A jump table for the options with a short description can be found at |Q_op|. | |
Insert mode completion. When zero as much space as available is used. | |
|ins-completion-menu|. | |
+ *'py2warn'* *'nopy2warn'* | |
+'py2warn' boolean (default on) | |
+ global | |
+ Show a warning at first use of Python 2.x. See |:py2usingfrom| also. | |
+ | |
*'pythondll'* | |
'pythondll' string (default depends on the build) | |
global | |
diff --git a/src/ex_cmds.h b/src/ex_cmds.h | |
index cb2fadc..bfad5ae 100644 | |
--- a/src/ex_cmds.h | |
+++ b/src/ex_cmds.h | |
@@ -1120,6 +1120,9 @@ EX(CMD_pydo, "pydo", ex_pydo, | |
EX(CMD_pyfile, "pyfile", ex_pyfile, | |
RANGE|FILE1|NEEDARG|CMDWIN, | |
ADDR_LINES), | |
+EX(CMD_py2usingfrom, "py2usingfrom", ex_py2usingfrom, | |
+ TRLBAR|CMDWIN, | |
+ ADDR_LINES), | |
EX(CMD_py3, "py3", ex_py3, | |
RANGE|EXTRA|NEEDARG|CMDWIN, | |
ADDR_LINES), | |
diff --git a/src/ex_docmd.c b/src/ex_docmd.c | |
index 295e539..61e709d 100644 | |
--- a/src/ex_docmd.c | |
+++ b/src/ex_docmd.c | |
@@ -282,6 +282,7 @@ static void ex_popup(exarg_T *eap); | |
# define ex_python ex_script_ni | |
# define ex_pydo ex_ni | |
# define ex_pyfile ex_ni | |
+# define ex_py2usingfrom ex_ni | |
#endif | |
#ifndef FEAT_PYTHON3 | |
# define ex_py3 ex_script_ni | |
diff --git a/src/globals.h b/src/globals.h | |
index 0b6abb0..d804d78 100644 | |
--- a/src/globals.h | |
+++ b/src/globals.h | |
@@ -1653,6 +1653,17 @@ EXTERN int disable_char_avail_for_testing INIT(= 0); | |
EXTERN int in_free_unref_items INIT(= FALSE); | |
#endif | |
+#ifdef FEAT_PYTHON | |
+/* | |
+ * State to indicate whether python2 is used and warned. | |
+ * | |
+ * 0 - not used. | |
+ * 1 - used but not warned. | |
+ * 2 - used and warned. | |
+ */ | |
+EXTERN int py2_used INIT(= 0); | |
+#endif | |
+ | |
#ifdef FEAT_TIMERS | |
EXTERN int did_add_timer INIT(= FALSE); | |
#endif | |
diff --git a/src/if_python.c b/src/if_python.c | |
index 622634d..ba6fe67 100644 | |
--- a/src/if_python.c | |
+++ b/src/if_python.c | |
@@ -828,6 +828,54 @@ typedef PySliceObject PySliceObject_T; | |
*/ | |
#include "if_py_both.h" | |
+static hashtab_T using_from; | |
+ | |
+ static void | |
+mark_py2_used(void) | |
+{ | |
+ if (py2_used == 0) | |
+ { | |
+ py2_used = 1; | |
+ hash_init(&using_from); | |
+ } | |
+ if (sourcing_name != NULL) | |
+ { | |
+ hash_T hash = hash_hash(sourcing_name); | |
+ hashitem_T *hi; | |
+ | |
+ hi = hash_lookup(&using_from, sourcing_name, hash); | |
+ if (!HASHITEM_EMPTY(hi)) | |
+ return; | |
+ hash_add_item(&using_from, hi, sourcing_name, hash); | |
+ } | |
+} | |
+ | |
+ void | |
+ex_py2usingfrom(exarg_T *eap) | |
+{ | |
+ hashtab_T *ht = &using_from; | |
+ hashitem_T *hi; | |
+ long todo; | |
+ | |
+ todo = (long)ht->ht_used; | |
+ if (todo == 0) | |
+ { | |
+ if (py2_used == 0) | |
+ msg((char_u *)"Python 2 isn't used yet."); | |
+ else | |
+ msg((char_u *)"Python 2 is used from command line."); | |
+ return; | |
+ } | |
+ msg((char_u *)"Python 2 is used from these files:"); | |
+ for (hi = ht->ht_array; todo > 0; ++hi) | |
+ { | |
+ if (!HASHITEM_EMPTY(hi)) | |
+ { | |
+ smsg((char_u *)" %s", hi->hi_key); | |
+ --todo; | |
+ } | |
+ } | |
+} | |
/****************************************************** | |
* Internal function prototypes. | |
@@ -878,6 +926,9 @@ python_end(void) | |
if (recurse != 0) | |
return; | |
+ if (py2_used != 0) | |
+ hash_clear_all(&using_from, 0); | |
+ | |
++recurse; | |
#ifdef DYNAMIC_PYTHON | |
@@ -1050,6 +1101,8 @@ DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg) | |
if (Python_Init()) | |
goto theend; | |
+ mark_py2_used(); | |
+ | |
init_range(arg); | |
Python_Release_Vim(); /* leave vim */ | |
diff --git a/src/main.c b/src/main.c | |
index f3c471a..a3a92e6 100644 | |
--- a/src/main.c | |
+++ b/src/main.c | |
@@ -1078,6 +1078,15 @@ main_loop( | |
* after insert mode finishes! */ | |
need_fileinfo = FALSE; | |
} | |
+#ifdef FEAT_PYTHON | |
+ if (py2_used == 1) | |
+ { | |
+ if (p_py2warn) | |
+ MSG_ATTR("Python 2 is used, check :py2usingfrom for details.", | |
+ hl_attr(HLF_W)); | |
+ py2_used = 2; | |
+ } | |
+#endif | |
} | |
/* Reset "got_int" now that we got back to the main loop. Except when | |
diff --git a/src/option.c b/src/option.c | |
index 6f9610d..67e075c 100644 | |
--- a/src/option.c | |
+++ b/src/option.c | |
@@ -2131,6 +2131,11 @@ static struct vimoption options[] = | |
(char_u *)NULL, PV_NONE, | |
#endif | |
{(char_u *)0L, (char_u *)0L} SCRIPTID_INIT}, | |
+#ifdef FEAT_PYTHON | |
+ {"py2warn", NULL, P_BOOL|P_VI_DEF, | |
+ (char_u *)&p_py2warn, PV_NONE, | |
+ {(char_u *)TRUE, (char_u *)0L} SCRIPTID_INIT}, | |
+#endif | |
#if defined(DYNAMIC_PYTHON3) | |
{"pythonthreedll", NULL, P_STRING|P_EXPAND|P_VI_DEF|P_SECURE, | |
(char_u *)&p_py3dll, PV_NONE, | |
diff --git a/src/option.h b/src/option.h | |
index 0ad2fef..91c0905 100644 | |
--- a/src/option.h | |
+++ b/src/option.h | |
@@ -524,6 +524,9 @@ EXTERN char_u *p_popt; /* 'printoptions' */ | |
EXTERN char_u *p_header; /* 'printheader' */ | |
#endif | |
EXTERN int p_prompt; /* 'prompt' */ | |
+#ifdef FEAT_PYTHON | |
+EXTERN int p_py2warn; /* 'py2warn' */ | |
+#endif | |
#ifdef FEAT_GUI | |
EXTERN char_u *p_guifont; /* 'guifont' */ | |
# ifdef FEAT_XFONTSET | |
diff --git a/src/proto/if_python.pro b/src/proto/if_python.pro | |
index 51054ca..5db6b87 100644 | |
--- a/src/proto/if_python.pro | |
+++ b/src/proto/if_python.pro | |
@@ -1,4 +1,5 @@ | |
/* if_python.c */ | |
+void ex_py2usingfrom(exarg_T *eap); | |
int python_enabled(int verbose); | |
void python_end(void); | |
int python_loaded(void); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment