/* * ":py3" */ void ex_py3(exarg_T *eap) { char_u *script; script = script_get(eap, eap->arg); if (!eap->skip) { if (script == NULL) DoPy3Command(eap, (char *)eap->arg); else DoPy3Command(eap, (char *)script); } vim_free(script); }
/* * ":py3file" */ void ex_py3file(exarg_T *eap) { static char buffer[BUFFER_SIZE]; const char *file; char *p; int i; /* Have to do it like this. PyRun_SimpleFile requires you to pass a * stdio file pointer, but Vim and the Python DLL are compiled with * different options under Windows, meaning that stdio pointers aren't * compatible between the two. Yuk. * * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec')) * * Using bytes so that Python can detect the source encoding as it normally * does. The doc does not say "compile" accept bytes, though. * * We need to escape any backslashes or single quotes in the file name, so that * Python won't mangle the file name. */ strcpy(buffer, "exec(compile(open('"); p = buffer + 19; /* size of "exec(compile(open('" */ for (i=0; i<2; ++i) { file = (char *)eap->arg; while (*file && p < buffer + (BUFFER_SIZE - 3)) { if (*file == '\\' || *file == '\'') *p++ = '\\'; *p++ = *file++; } /* If we didn't finish the file name, we hit a buffer overflow */ if (*file != '\0') return; if (i==0) { strcpy(p,"','rb').read(),'"); p += 16; } else { strcpy(p,"','exec'))"); p += 10; } } /* Execute the file */ DoPy3Command(eap, buffer); }