file.search broken on python 2.6 with empty files
As of #29216 the file.search functionality was broken on python 2.6. From what I've found, changeing this from the if-else block to the try-except block broke it.
Correct output:
salt-call file.search /root/empty 'words'
local:
False
After the PR this stack trace happens:
salt-call file.search /root/empty 'words'
[ERROR ] An un-handled exception was caught by salt's global exception handler:
error: [Errno 22] Invalid argument
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
salt_call()
File "/usr/lib/python2.6/site-packages/salt/scripts.py", line 227, in salt_call
client.run()
File "/usr/lib/python2.6/site-packages/salt/cli/call.py", line 71, in run
caller.run()
File "/usr/lib/python2.6/site-packages/salt/cli/caller.py", line 236, in run
ret = self.call()
File "/usr/lib/python2.6/site-packages/salt/cli/caller.py", line 138, in call
ret['return'] = func(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/salt/modules/file.py", line 1832, in search
show_changes=False)
File "/usr/lib/python2.6/site-packages/salt/modules/file.py", line 1436, in replace
access=mmap.ACCESS_READ)
error: [Errno 22] Invalid argument
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
salt_call()
File "/usr/lib/python2.6/site-packages/salt/scripts.py", line 227, in salt_call
client.run()
File "/usr/lib/python2.6/site-packages/salt/cli/call.py", line 71, in run
caller.run()
File "/usr/lib/python2.6/site-packages/salt/cli/caller.py", line 236, in run
ret = self.call()
File "/usr/lib/python2.6/site-packages/salt/cli/caller.py", line 138, in call
ret['return'] = func(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/salt/modules/file.py", line 1832, in search
show_changes=False)
File "/usr/lib/python2.6/site-packages/salt/modules/file.py", line 1436, in replace
access=mmap.ACCESS_READ)
mmap.error: [Errno 22] Invalid argument
[root@li1179-228 ~]# salt-call file.search /root/test.txt 'new'
I tested this on the head of all the branches and it is broken on all of them.
ping @clan @lorengordon @cachedout
I don't have a system with python 2.6 handy. Is it choking on mmap.ACCESS_READ? That would be quite odd, the docs claim compatibility with 2.6... What is the output of python -c 'import mmap; print(mmap.ACCESS_READ)'?
@lorengordon
$ python -c 'import mmap; print(mmap.ACCESS_READ)'
1
Ok, that's fine. Can you try to create the mmap manually? My only guess is that on Linux mmap is throwing an exception other than ValueError, despite the docs saying Linux should be fine with empty files.
import mmap
with open('/root/test.txt', mode='rb') as fn_:
data = mmap.mmap(fn_.fileno(), 0, access=mmap.ACCESS_READ)
print(''.join(data))
data.close()
@lorengordon You're right, it doesn't throw a ValueError on python26.
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
mmap.error: [Errno 22] Invalid argument
However, it does throw a ValueError correctly on 27.
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: cannot mmap an empty file
Well, that's where the traceback is coming from, but I have no idea what argument is causing the exception. I won't be able to dig into it until tomorrow sometime, at the earliest.
@lorengordon I did a little digging and found that it's the 0 length that causes that traceback. Anything other than 0 throws a ValueError like expected.
>>> import mmap
>>> with open('/root/test.txt', mode='rb') as fn_:
... data = mmap.mmap(fn_.fileno(), 1, access=mmap.ACCESS_READ)
... print(''.join(data))
... data.close()
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: mmap length is greater than file size
>>> with open('/root/test.txt', mode='rb') as fn_:
... data = mmap.mmap(fn_.fileno(), 0, access=mmap.ACCESS_READ)
... print(''.join(data))
... data.close()
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
mmap.error: [Errno 22] Invalid argument
Ugh. 0 length means to map the size of the file. We don't really want to change that because the map will only contain the number of bytes we specify. I guess we could catch all exceptions, instead of catching the ValueError explicitly?
try:
r_data = mmap.mmap(r_file.fileno(),
0,
access=mmap.ACCESS_READ)
except:
r_data = ''.join(r_file)
Or otherwise try to catch both the ValueError and the odd mmap.error that is popping up in this case?
That could work. I'll try some things and see what seems to work best. I'll let you know what I find...
@lorengordon Ok, so catching all exceptions works as well as catching just the two. However, I think in cases like this it's best to be explicit in which exceptions we are catching.
I'll submit a PR with this fix and ping you on it.
I agree with being explicit about the exceptions. I just wasn't entirely sure how to reference the mmap.error exception. Thanks!
Yep. Thanks for your help!
Fix merged.