Check whether an array is read-only

I am using mmaped arrays, and sometimes they are opened as read-only.

i’d like to know, at run-time, whether an array is read-only, and if so make a copy so I can have a temporary modifiable version of it. Is there a function in julia to check ‘read-onlyness’, or if it can be written to? I can catch a read-only exception but I thought that maybe there was a better alternative.

I found isreadonly() but that applies to streams.

thanks!

perhaps something like

issettable{T}(x::T) = method_exists(setindex!, Tuple{T, eltype(T), Int})

? you might have to tweak the last argument. A trait that tests for this could be useful.

My understanding was that memory mapped arrays were of type Array. So a setindex! method always exists.

I haven’t heard of these being read only, however. Could you elaborate as to how/when that happens?

If a read-only file is mmapped, then resulting Array pages are read-only.

So, you could implement isreadonly(a::Array) if there is some way to detect whether pointer(a) is in a protected page. (I suppose you could try writing a byte and catching a segfault; I don’t know if there is a better way.)

Thanks for your answers.

I guess that then it would be easier to try to write to the first element and catch an exception. But that’s what I was trying to avoid.

@andyferris this is an example of how to try that behavior:

  1. Find a file on disk, fileName = "/somewhere/file.txt"
  2. In julia, do: f = open(fileName,"r"); myVec = Mmap.mmap(f, Vector{UInt8}, (1,), 0)
  3. Now you can try to write to it, you’ll get an exception: myVec[1] = 0;