comparison src/ex_cmds2.c @ 10722:7598ce51bf2a v8.0.0251

patch 8.0.0251: not easy to select Python 2 or 3 commit https://github.com/vim/vim/commit/f42dd3c3901ea0ba38e67a616aea9953cae81b8d Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jan 28 16:06:38 2017 +0100 patch 8.0.0251: not easy to select Python 2 or 3 Problem: It is not so easy to write a script that works with both Python 2 and Python 3, even when the Python code works with both. Solution: Add 'pyxversion', :pyx, etc. (Marc Weber, Ken Takata)
author Christian Brabandt <cb@256bit.org>
date Sat, 28 Jan 2017 16:15:04 +0100
parents 0bf064e7917a
children 437cf0fe2138
comparison
equal deleted inserted replaced
10721:9177c4f6a229 10722:7598ce51bf2a
3673 { 3673 {
3674 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL); 3674 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
3675 } 3675 }
3676 #endif 3676 #endif
3677 3677
3678 #if defined(FEAT_PYTHON3) || defined(FEAT_PYTHON) || defined(PROTO)
3679
3680 # if (defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
3681 /*
3682 * Detect Python 3 or 2, and initialize 'pyxversion'.
3683 */
3684 void
3685 init_pyxversion(void)
3686 {
3687 if (p_pyx == 0)
3688 {
3689 if (python3_enabled(FALSE))
3690 p_pyx = 3;
3691 else if (python_enabled(FALSE))
3692 p_pyx = 2;
3693 }
3694 }
3695 # endif
3696
3697 /*
3698 * Does a file contain one of the following strings at the beginning of any
3699 * line?
3700 * "#!(any string)python2" => returns 2
3701 * "#!(any string)python3" => returns 3
3702 * "# requires python 2.x" => returns 2
3703 * "# requires python 3.x" => returns 3
3704 * otherwise return 0.
3705 */
3706 static int
3707 requires_py_version(char_u *filename)
3708 {
3709 FILE *file;
3710 int requires_py_version = 0;
3711 int i, lines;
3712
3713 lines = (int)p_mls;
3714 if (lines < 0)
3715 lines = 5;
3716
3717 file = mch_fopen((char *)filename, "r");
3718 if (file != NULL)
3719 {
3720 for (i = 0; i < lines; i++)
3721 {
3722 if (vim_fgets(IObuff, IOSIZE, file))
3723 break;
3724 if (i == 0 && IObuff[0] == '#' && IObuff[1] == '!')
3725 {
3726 /* Check shebang. */
3727 if (strstr((char *)IObuff + 2, "python2") != NULL)
3728 {
3729 requires_py_version = 2;
3730 break;
3731 }
3732 if (strstr((char *)IObuff + 2, "python3") != NULL)
3733 {
3734 requires_py_version = 3;
3735 break;
3736 }
3737 }
3738 IObuff[21] = '\0';
3739 if (STRCMP("# requires python 2.x", IObuff) == 0)
3740 {
3741 requires_py_version = 2;
3742 break;
3743 }
3744 if (STRCMP("# requires python 3.x", IObuff) == 0)
3745 {
3746 requires_py_version = 3;
3747 break;
3748 }
3749 }
3750 fclose(file);
3751 }
3752 return requires_py_version;
3753 }
3754
3755
3756 /*
3757 * Source a python file using the requested python version.
3758 */
3759 static void
3760 source_pyx_file(exarg_T *eap, char_u *fname)
3761 {
3762 exarg_T ex;
3763 int v = requires_py_version(fname);
3764
3765 # if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
3766 init_pyxversion();
3767 # endif
3768 if (v == 0)
3769 {
3770 # if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
3771 /* user didn't choose a preference, 'pyx' is used */
3772 v = p_pyx;
3773 # elif defined(FEAT_PYTHON)
3774 v = 2;
3775 # elif defined(FEAT_PYTHON3)
3776 v = 3;
3777 # endif
3778 }
3779
3780 /*
3781 * now source, if required python version is not supported show
3782 * unobtrusive message.
3783 */
3784 if (eap == NULL)
3785 vim_memset(&ex, 0, sizeof(ex));
3786 else
3787 ex = *eap;
3788 ex.arg = fname;
3789 ex.cmd = (char_u *)(v == 2 ? "pyfile" : "pyfile3");
3790
3791 if (v == 2)
3792 {
3793 # ifdef FEAT_PYTHON
3794 ex_pyfile(&ex);
3795 # else
3796 vim_snprintf((char *)IObuff, IOSIZE,
3797 _("W20: Required python version 2.x not supported, ignoring file: %s"),
3798 fname);
3799 MSG(IObuff);
3800 # endif
3801 return;
3802 }
3803 else
3804 {
3805 # ifdef FEAT_PYTHON3
3806 ex_py3file(&ex);
3807 # else
3808 vim_snprintf((char *)IObuff, IOSIZE,
3809 _("W21: Required python version 3.x not supported, ignoring file: %s"),
3810 fname);
3811 MSG(IObuff);
3812 # endif
3813 return;
3814 }
3815 }
3816
3817 /*
3818 * ":pyxfile {fname}"
3819 */
3820 void
3821 ex_pyxfile(exarg_T *eap)
3822 {
3823 source_pyx_file(eap, eap->arg);
3824 }
3825
3826 /*
3827 * ":pyx"
3828 */
3829 void
3830 ex_pyx(exarg_T *eap)
3831 {
3832 # if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
3833 init_pyxversion();
3834 if (p_pyx == 2)
3835 ex_python(eap);
3836 else
3837 ex_py3(eap);
3838 # elif defined(FEAT_PYTHON)
3839 ex_python(eap);
3840 # elif defined(FEAT_PYTHON3)
3841 ex_py3(eap);
3842 # endif
3843 }
3844
3845 /*
3846 * ":pyxdo"
3847 */
3848 void
3849 ex_pyxdo(exarg_T *eap)
3850 {
3851 # if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
3852 init_pyxversion();
3853 if (p_pyx == 2)
3854 ex_pydo(eap);
3855 else
3856 ex_py3do(eap);
3857 # elif defined(FEAT_PYTHON)
3858 ex_pydo(eap);
3859 # elif defined(FEAT_PYTHON3)
3860 ex_py3do(eap);
3861 # endif
3862 }
3863
3864 #endif
3865
3678 /* 3866 /*
3679 * ":source {fname}" 3867 * ":source {fname}"
3680 */ 3868 */
3681 void 3869 void
3682 ex_source(exarg_T *eap) 3870 ex_source(exarg_T *eap)