# Check whether an array is read-only

**URL:** <https://discourse.julialang.org/t/check-whether-an-array-is-read-only/2949>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 29, 2017, 1:55pm UTC](https://discourse.julialang.org/t/check-whether-an-array-is-read-only/2949 "2017-03-29T13:55:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjb](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@cjb](https://discourse.julialang.org/u/cjb)\
**Post date:** [March 29, 2017, 1:55pm UTC](https://discourse.julialang.org/t/check-whether-an-array-is-read-only/2949/1 "2017-03-29T13:55:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 30, 2017, 9:43am UTC](https://discourse.julialang.org/t/check-whether-an-array-is-read-only/2949/2 "2017-03-30T09:43:09Z")

</div>

perhaps something like

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

```

? you might have to tweak the last argument. A [trait](http://trait) that tests for this could be useful.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [March 30, 2017, 10:52am UTC](https://discourse.julialang.org/t/check-whether-an-array-is-read-only/2949/3 "2017-03-30T10:52:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 30, 2017, 2:31pm UTC](https://discourse.julialang.org/t/check-whether-an-array-is-read-only/2949/4 "2017-03-30T14:31:51Z")

</div>

If a read-only file is mmapped, then resulting Array [pages are read-only](https://github.com/JuliaLang/julia/blob/6659b59fee30232c77cefbba1848b958041f5c61/base/mmap.jl#L117-L137).

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.)

---

<div class="post-metadata">

**Author:** ![cjb](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@cjb](https://discourse.julialang.org/u/cjb)\
**Post date:** [March 30, 2017, 3:08pm UTC](https://discourse.julialang.org/t/check-whether-an-array-is-read-only/2949/5 "2017-03-30T15:08:31Z")

</div>

Thanks for your answers.

> [@stevengj](#):
>
> 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.)

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;`
