From 3731a3fe1af6accb199d73d018405a7a83235ac6 Mon Sep 17 00:00:00 2001 From: Alex Siryi Date: Sat, 28 Jan 2017 15:05:13 +0200 Subject: [PATCH] =?UTF-8?q?Added=20simple=20encapsulation:=20Javascript=20?= =?UTF-8?q?no=20longer=20can=20access=20python=20object=20properties=20sta?= =?UTF-8?q?rting=20with=20=E2=80=9C=5F=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Wrapper.cpp | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Wrapper.cpp b/src/Wrapper.cpp index e3d318e..725fc63 100644 --- a/src/Wrapper.cpp +++ b/src/Wrapper.cpp @@ -272,6 +272,12 @@ void CPythonObject::NamedGetter(v8::Local prop, const v8::PropertyCa v8::String::Utf8Value name(prop); if (PyGen_Check(obj.ptr())) CALLBACK_RETURN(v8::Undefined(info.GetIsolate())); + + if (name.length() && **name == '_') + { + // return None for every attribute starting with "_" + CALLBACK_RETURN(v8::Handle()); + } PyObject *value = ::PyObject_GetAttrString(obj.ptr(), *name); @@ -330,6 +336,13 @@ void CPythonObject::NamedSetter(v8::Local prop, v8::Local py::object obj = CJavascriptObject::Wrap(info.Holder()); v8::String::Utf8Value name(prop); + + if (name.length() && **name == '_') + { + // return None for every attribute starting with "_" + CALLBACK_RETURN(value); + } + py::object newval = CJavascriptObject::Wrap(value); bool found = 1 == ::PyObject_HasAttrString(obj.ptr(), *name); @@ -390,9 +403,19 @@ void CPythonObject::NamedQuery(v8::Local prop, const v8::PropertyCal py::object obj = CJavascriptObject::Wrap(info.Holder()); v8::String::Utf8Value name(prop); - - bool exists = PyGen_Check(obj.ptr()) || ::PyObject_HasAttrString(obj.ptr(), *name) || - (::PyMapping_Check(obj.ptr()) && ::PyMapping_HasKeyString(obj.ptr(), *name)); + + bool exists; + + if (name.length() && **name == '_') + { + // return None for every attribute starting with "_" + exists = false; + } + else + { + exists = PyGen_Check(obj.ptr()) || ::PyObject_HasAttrString(obj.ptr(), *name) || + (::PyMapping_Check(obj.ptr()) && ::PyMapping_HasKeyString(obj.ptr(), *name)); + } if (exists) CALLBACK_RETURN(v8::Integer::New(info.GetIsolate(), v8::None)); @@ -410,6 +433,10 @@ void CPythonObject::NamedDeleter(v8::Local prop, const v8::PropertyC py::object obj = CJavascriptObject::Wrap(info.Holder()); v8::String::Utf8Value name(prop); + if (name.length() && **name == '_') + { + CALLBACK_RETURN(false); + } if (!::PyObject_HasAttrString(obj.ptr(), *name) && ::PyMapping_Check(obj.ptr()) && @@ -499,7 +526,7 @@ void CPythonObject::NamedEnumerator(const v8::PropertyCallbackInfo& i // FIXME Are there any methods to avoid such a dirty work? - if (name.startswith("__") && name.endswith("__")) + if (name.startswith("_")) continue; }