Opening a text file with no xattr gives "Operation not permitted" under macOS Catalina and Python 2

452 views Asked by At

I've been having trouble getting Python (embarassingly, Python 2 in this case) to open some files on my machine, running macOS Catalina 10.15.6. I've boiled down the issue down to the following. I have two apparently identical text files except with different xattr. Python is willing to open one and not the other. Here's my code:

import os, subprocess

for filename in ["file1.txt", "file2.txt"]:
    path = os.path.join(os.path.expanduser("~/Downloads/"+filename))
    assert os.path.isfile(path)
    print(filename, " attr:", oct(os.stat(path).st_mode & 0o777))
    print(filename, "xattr:", subprocess.check_output(["xattr", path]).strip())
    try:
        with open(path) as infile:
            print("Succeeded for file", filename)
    except IOError as ioe:
        print(ioe.args[1], "for", filename)

Here's the output:

file1.txt  attr: 0777
file1.txt xattr: com.apple.macl
Succeeded for file file1.txt
file2.txt  attr: 0777
file2.txt xattr: 
Operation not permitted for file2.txt

You might think that the lack of xattr on file2.txt is convincing my computer that file2.txt is poison and cannot be trusted, but Emacs is perfectly happy to open it.

"What if you remove the com.apple.macl from file1.txt?", you ask. Well it turns out that macOS will not allow you to do that, but there's a workaround. And if I do this I do then lose the ability to read file1.txt in Python as well.

The files that I can't read were created with no malice--for example, files created in Emacs get this treatment. I'm wondering:

  1. What's going on here? Are extended attributes really to blame?
  2. How can I get Python to read these benign files which other program on my computer are happy to read?
0

There are 0 answers