Skip to content

Commit e2a5181

Browse files
author
Christopher Doris
committed
getattr/setattr on julia objects does not do property lookup if attr starts and ends with double underscore
1 parent d9562d0 commit e2a5181

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/cpython/juliaany.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ pyjlany_getattro(xo::PyPtr, ko::PyPtr) = begin
2020
else
2121
return ro
2222
end
23-
# Now try to get the corresponding property
23+
# Convert attribute to a string
2424
x = PyJuliaValue_GetValue(xo)
2525
k = PyUnicode_AsString(ko)
2626
isempty(k) && PyErr_IsSet() && return PyNULL
27+
# If has double leading and trailing underscore, do not allow
28+
if length(k) > 4 && startswith(k, "__") && endswith(k, "__")
29+
PyErr_SetString(PyExc_AttributeError(), "'$(PyType_Name(Py_Type(xo)))' object has no attribute '$k'")
30+
return PyNULL
31+
end
32+
# Look up a property on the Julia object
2733
k = pyjl_attr_py2jl(k)
2834
@pyjltry begin
2935
v = getproperty(x, Symbol(k))
@@ -48,10 +54,16 @@ pyjlany_setattro(xo::PyPtr, ko::PyPtr, vo::PyPtr) = begin
4854
PyErr_SetString(PyExc_TypeError(), "attribute deletion not supported")
4955
return Cint(-1)
5056
end
51-
# Now try to set the corresponding property
57+
# Convert attribute to a string
5258
x = PyJuliaValue_GetValue(xo)
5359
k = PyUnicode_AsString(ko)
5460
isempty(k) && PyErr_IsSet() && return Cint(-1)
61+
# If has double leading and trailing underscore, do not allow
62+
if length(k) > 4 && startswith(k, "__") && endswith(k, "__")
63+
PyErr_SetString(PyExc_AttributeError(), "'$(PyType_Name(Py_Type(xo)))' object has no attribute '$k'")
64+
return PyNULL
65+
end
66+
# Look up a property on the Julia object
5567
k = pyjl_attr_py2jl(k)
5668
@pyjltry begin
5769
V = propertytype(x, Symbol(k))

src/cpython/juliaraw.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ pyjlraw_getattro(xo::PyPtr, ko::PyPtr) = begin
2323
else
2424
return ro
2525
end
26-
# Now try to get the corresponding property
26+
# Convert attribute to a string
2727
x = PyJuliaValue_GetValue(xo)
2828
k = PyUnicode_AsString(ko)
2929
isempty(k) && PyErr_IsSet() && return PyNULL
30+
# If has double leading and trailing underscore, do not allow
31+
if length(k) > 4 && startswith(k, "__") && endswith(k, "__")
32+
PyErr_SetString(PyExc_AttributeError(), "'$(PyType_Name(Py_Type(xo)))' object has no attribute '$k'")
33+
return PyNULL
34+
end
35+
# Look up a property on the Julia object
3036
k = pyjl_attr_py2jl(k)
3137
@pyjltry PyJuliaRawValue_New(getproperty(x, Symbol(k))) PyNULL
3238
end
@@ -39,10 +45,16 @@ pyjlraw_setattro(xo::PyPtr, ko::PyPtr, vo::PyPtr) = begin
3945
else
4046
return ro
4147
end
42-
# Now try to set the corresponding property
48+
# Convert attribute to a string
4349
x = PyJuliaValue_GetValue(xo)
4450
k = PyUnicode_AsString(ko)
4551
isempty(k) && PyErr_IsSet() && return Cint(-1)
52+
# If has double leading and trailing underscore, do not allow
53+
if length(k) > 4 && startswith(k, "__") && endswith(k, "__")
54+
PyErr_SetString(PyExc_AttributeError(), "'$(PyType_Name(Py_Type(xo)))' object has no attribute '$k'")
55+
return PyNULL
56+
end
57+
# Look up a property on the Julia object
4658
k = pyjl_attr_py2jl(k)
4759
ism1(PyObject_Convert(vo, Any)) && return Cint(-1)
4860
v = takeresult(Any)

0 commit comments

Comments
 (0)