0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 8.2.2343: Vim9: return type of readfile() is any

Problem:    Vim9: return type of readfile() is any.
Solution:   Add readblob() so that readfile() can be expected to always
            return a list of strings. (closes #7671)
This commit is contained in:
Bram Moolenaar 2021-01-13 20:38:03 +01:00
parent 9145846b6a
commit c423ad77ed
7 changed files with 74 additions and 10 deletions

View File

@ -1,4 +1,4 @@
*eval.txt* For Vim version 8.2. Last change: 2021 Jan 10 *eval.txt* For Vim version 8.2. Last change: 2021 Jan 13
VIM REFERENCE MANUAL by Bram Moolenaar VIM REFERENCE MANUAL by Bram Moolenaar
@ -2775,6 +2775,7 @@ pyxeval({expr}) any evaluate |python_x| expression
rand([{expr}]) Number get pseudo-random number rand([{expr}]) Number get pseudo-random number
range({expr} [, {max} [, {stride}]]) range({expr} [, {max} [, {stride}]])
List items from {expr} to {max} List items from {expr} to {max}
readblob({fname}) Blob read a |Blob| from {fname}
readdir({dir} [, {expr} [, {dict}]]) readdir({dir} [, {expr} [, {dict}]])
List file names in {dir} selected by {expr} List file names in {dir} selected by {expr}
readdirex({dir} [, {expr} [, {dict}]]) readdirex({dir} [, {expr} [, {dict}]])
@ -8265,6 +8266,14 @@ rand([{expr}]) *rand()* *random*
:echo rand(seed) :echo rand(seed)
:echo rand(seed) % 16 " random number 0 - 15 :echo rand(seed) % 16 " random number 0 - 15
< <
readblob({fname}) *readblob()*
Read file {fname} in binary mode and return a |Blob|.
When the file can't be opened an error message is given and
the result is an empty |Blob|.
Also see |readfile()| and |writefile()|.
readdir({directory} [, {expr} [, {dict}]]) *readdir()* readdir({directory} [, {expr} [, {dict}]]) *readdir()*
Return a list with file and directory names in {directory}. Return a list with file and directory names in {directory}.
You can also use |glob()| if you don't need to do complicated You can also use |glob()| if you don't need to do complicated
@ -8379,6 +8388,7 @@ readdirex({directory} [, {expr} [, {dict}]]) *readdirex()*
Can also be used as a |method|: > Can also be used as a |method|: >
GetDirName()->readdirex() GetDirName()->readdirex()
< <
*readfile()* *readfile()*
readfile({fname} [, {type} [, {max}]]) readfile({fname} [, {type} [, {max}]])
Read file {fname} and return a |List|, each line of the file Read file {fname} and return a |List|, each line of the file
@ -8390,8 +8400,6 @@ readfile({fname} [, {type} [, {max}]])
- When the last line ends in a NL an extra empty list item is - When the last line ends in a NL an extra empty list item is
added. added.
- No CR characters are removed. - No CR characters are removed.
When {type} contains "B" a |Blob| is returned with the binary
data of the file unmodified.
Otherwise: Otherwise:
- CR characters that appear before a NL are removed. - CR characters that appear before a NL are removed.
- Whether the last line ends in a NL or not does not matter. - Whether the last line ends in a NL or not does not matter.
@ -8409,6 +8417,9 @@ readfile({fname} [, {type} [, {max}]])
Note that without {max} the whole file is read into memory. Note that without {max} the whole file is read into memory.
Also note that there is no recognition of encoding. Read a Also note that there is no recognition of encoding. Read a
file into a buffer if you need to. file into a buffer if you need to.
Deprecated (use |readblob()| instead): When {type} contains
"B" a |Blob| is returned with the binary data of the file
unmodified.
When the file can't be opened an error message is given and When the file can't be opened an error message is given and
the result is an empty list. the result is an empty list.
Also see |writefile()|. Also see |writefile()|.
@ -11295,9 +11306,11 @@ win_execute({id}, {command} [, {silent}]) *win_execute()*
call win_execute(winid, 'set syntax=python') call win_execute(winid, 'set syntax=python')
< Doing the same with `setwinvar()` would not trigger < Doing the same with `setwinvar()` would not trigger
autocommands and not actually show syntax highlighting. autocommands and not actually show syntax highlighting.
*E994* *E994*
Not all commands are allowed in popup windows. Not all commands are allowed in popup windows.
When window {id} does not exist then no error is given. When window {id} does not exist then no error is given and
an empty string is returned.
Can also be used as a |method|, the base is passed as the Can also be used as a |method|, the base is passed as the
second argument: > second argument: >

View File

@ -820,6 +820,7 @@ System functions and manipulation of files:
setenv() set an environment variable setenv() set an environment variable
hostname() name of the system hostname() name of the system
readfile() read a file into a List of lines readfile() read a file into a List of lines
readblob() read a file into a Blob
readdir() get a List of file names in a directory readdir() get a List of file names in a directory
readdirex() get a List of file information in a directory readdirex() get a List of file information in a directory
writefile() write a List of lines or Blob into a file writefile() write a List of lines or Blob into a file

View File

@ -1344,12 +1344,14 @@ static funcentry_T global_functions[] =
ret_number, f_rand}, ret_number, f_rand},
{"range", 1, 3, FEARG_1, NULL, {"range", 1, 3, FEARG_1, NULL,
ret_list_number, f_range}, ret_list_number, f_range},
{"readblob", 1, 1, FEARG_1, NULL,
ret_blob, f_readblob},
{"readdir", 1, 3, FEARG_1, NULL, {"readdir", 1, 3, FEARG_1, NULL,
ret_list_string, f_readdir}, ret_list_string, f_readdir},
{"readdirex", 1, 3, FEARG_1, NULL, {"readdirex", 1, 3, FEARG_1, NULL,
ret_list_dict_any, f_readdirex}, ret_list_dict_any, f_readdirex},
{"readfile", 1, 3, FEARG_1, NULL, {"readfile", 1, 3, FEARG_1, NULL,
ret_any, f_readfile}, ret_list_string, f_readfile},
{"reduce", 2, 3, FEARG_1, NULL, {"reduce", 2, 3, FEARG_1, NULL,
ret_any, f_reduce}, ret_any, f_reduce},
{"reg_executing", 0, 0, 0, NULL, {"reg_executing", 0, 0, 0, NULL,

View File

@ -1640,11 +1640,11 @@ f_readdirex(typval_T *argvars, typval_T *rettv)
/* /*
* "readfile()" function * "readfile()" function
*/ */
void static void
f_readfile(typval_T *argvars, typval_T *rettv) read_file_or_blob(typval_T *argvars, typval_T *rettv, int always_blob)
{ {
int binary = FALSE; int binary = FALSE;
int blob = FALSE; int blob = always_blob;
int failed = FALSE; int failed = FALSE;
char_u *fname; char_u *fname;
FILE *fd; FILE *fd;
@ -1796,7 +1796,8 @@ f_readfile(typval_T *argvars, typval_T *rettv)
if (dest < buf) if (dest < buf)
{ {
adjust_prevlen = (int)(buf - dest); // must be 1 or 2 // must be 1 or 2
adjust_prevlen = (int)(buf - dest);
dest = buf; dest = buf;
} }
if (readlen > p - buf + 1) if (readlen > p - buf + 1)
@ -1866,6 +1867,24 @@ f_readfile(typval_T *argvars, typval_T *rettv)
fclose(fd); fclose(fd);
} }
/*
* "readblob()" function
*/
void
f_readblob(typval_T *argvars, typval_T *rettv)
{
read_file_or_blob(argvars, rettv, TRUE);
}
/*
* "readfile()" function
*/
void
f_readfile(typval_T *argvars, typval_T *rettv)
{
read_file_or_blob(argvars, rettv, FALSE);
}
/* /*
* "resolve()" function * "resolve()" function
*/ */

View File

@ -1,5 +1,6 @@
/* filepath.c */ /* filepath.c */
int modify_fname(char_u *src, int tilde_file, int *usedlen, char_u **fnamep, char_u **bufp, int *fnamelen); int modify_fname(char_u *src, int tilde_file, int *usedlen, char_u **fnamep, char_u **bufp, int *fnamelen);
void shorten_dir(char_u *str);
void f_chdir(typval_T *argvars, typval_T *rettv); void f_chdir(typval_T *argvars, typval_T *rettv);
void f_delete(typval_T *argvars, typval_T *rettv); void f_delete(typval_T *argvars, typval_T *rettv);
void f_executable(typval_T *argvars, typval_T *rettv); void f_executable(typval_T *argvars, typval_T *rettv);
@ -21,10 +22,10 @@ void f_glob2regpat(typval_T *argvars, typval_T *rettv);
void f_globpath(typval_T *argvars, typval_T *rettv); void f_globpath(typval_T *argvars, typval_T *rettv);
void f_isdirectory(typval_T *argvars, typval_T *rettv); void f_isdirectory(typval_T *argvars, typval_T *rettv);
void f_mkdir(typval_T *argvars, typval_T *rettv); void f_mkdir(typval_T *argvars, typval_T *rettv);
void shorten_dir(char_u *str);
void f_pathshorten(typval_T *argvars, typval_T *rettv); void f_pathshorten(typval_T *argvars, typval_T *rettv);
void f_readdir(typval_T *argvars, typval_T *rettv); void f_readdir(typval_T *argvars, typval_T *rettv);
void f_readdirex(typval_T *argvars, typval_T *rettv); void f_readdirex(typval_T *argvars, typval_T *rettv);
void f_readblob(typval_T *argvars, typval_T *rettv);
void f_readfile(typval_T *argvars, typval_T *rettv); void f_readfile(typval_T *argvars, typval_T *rettv);
void f_resolve(typval_T *argvars, typval_T *rettv); void f_resolve(typval_T *argvars, typval_T *rettv);
void f_tempname(typval_T *argvars, typval_T *rettv); void f_tempname(typval_T *argvars, typval_T *rettv);

View File

@ -623,6 +623,32 @@ def Test_readdir()
eval expand('sautest')->readdirex((e) => e.name[0] !=# '.') eval expand('sautest')->readdirex((e) => e.name[0] !=# '.')
enddef enddef
def Test_readblob()
var blob = 0z12341234
writefile(blob, 'Xreadblob')
var read: blob = readblob('Xreadblob')
assert_equal(blob, read)
var lines =<< trim END
var read: list<string> = readblob('Xreadblob')
END
CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<string> but got blob', 1)
delete('Xreadblob')
enddef
def Test_readfile()
var text = ['aaa', 'bbb', 'ccc']
writefile(text, 'Xreadfile')
var read: list<string> = readfile('Xreadfile')
assert_equal(text, read)
var lines =<< trim END
var read: dict<string> = readfile('Xreadfile')
END
CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected dict<string> but got list<string>', 1)
delete('Xreadfile')
enddef
def Test_remove_return_type() def Test_remove_return_type()
var l = remove({one: [1, 2], two: [3, 4]}, 'one') var l = remove({one: [1, 2], two: [3, 4]}, 'one')
var res = 0 var res = 0

View File

@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
2343,
/**/ /**/
2342, 2342,
/**/ /**/