Explorar el Código

relax the restrictions of pin name

wangweimin hace 3 años
padre
commit
d404dac7f9
Se han modificado 1 ficheros con 9 adiciones y 11 borrados
  1. 9 11
      pywebio/pin.py

+ 9 - 11
pywebio/pin.py

@@ -141,8 +141,6 @@ __all__ = ['put_input', 'put_textarea', 'put_select', 'put_checkbox', 'put_radio
 def check_name(name):
     assert all(i in _pin_name_chars for i in name), "pin `name` can only contain letters, digits, " \
                                                     "minus sign and underscore"
-    assert name != 'use_strict', "'use_strict' is a reserve name, can't use as pin widget name"
-    assert name != '_strict', "'_strict' is a reserve name, can't use as pin widget name"
 
 
 def _pin_output(single_input_return, scope, position):
@@ -254,28 +252,28 @@ class Pin_:
         Enable strict mode for getting pin widget value.
         An AssertionError will be raised when try to get value of pin widgets that are currently not in the page.
         """
-        self._strict = True
+        object.__setattr__(self, '_strict', True)
 
     def __getattr__(self, name):
         """__getattr__ is only invoked if the attribute wasn't found the usual ways"""
-        check_name(name)
-        return get_pin_value(name, self._strict)
+        if name.startswith('__'):
+            raise AttributeError('Pin object has no attribute %r' % name)
+        return self.__getitem__(name)
 
     def __getitem__(self, name):
-        return self.__getattr__(name)
+        check_name(name)
+        return get_pin_value(name, self._strict)
 
     def __setattr__(self, name, value):
         """
         __setattr__ will be invoked regardless of whether the attribute be found
         """
-        if name == '_strict':
-            return object.__setattr__(self, name, value)
-
+        assert name != 'use_strict', "'use_strict' is a reserve name, can't use as pin widget name"
         check_name(name)
-        send_msg('pin_update', spec=dict(name=name, attributes={"value": value}))
+        self.__setitem__(name, value)
 
     def __setitem__(self, name, value):
-        self.__setattr__(name, value)
+        send_msg('pin_update', spec=dict(name=name, attributes={"value": value}))
 
 
 # pin widgets value getter (and setter).