caijialiang created AMBARI-25997:
------------------------------------

             Summary: Improvement and Bug Fix in ambari_server.properties.py
                 Key: AMBARI-25997
                 URL: https://issues.apache.org/jira/browse/AMBARI-25997
             Project: Ambari
          Issue Type: Bug
    Affects Versions: 2.7.7, 2.8.0
            Reporter: caijialiang


File Location: ambari-server/src/main/python/ambari_server/properties.py

Problem Description:
1. The `__getattr__` method is used for dynamic proxying of methods. In the 
case where `hasattr(self._props, name)` is false indicating the corresponding 
method or attribute cannot be found, an exception needs to be thrown. If not, 
it may confuse the user and obstruct code debugging.

2. The class `Properties` proxies the `has_key` method to the `dict` object 
through `__getattr__`, which is acceptable: `Properties().has_key("x")`. 
However, for the operation `"x" in Properties()`, it ends up in an infinite 
hang, never returning. Therefore, a `__contains__` method needs to be 
implemented to keep consistency with the logic of `has_key`, i.e., to determine 
whether the object is in `_props`:

```python
def __contains__(self, key):
   return key in self._props
```
This modification prevents the infinite hang problem from occurring. 

To reproduce the hang issue, we can make a simplified version of the 
`Properties` class for testing, as follows:

```python
#!/usr/bin/env python

import os
import re
import time

class Properties(object):
   def __init__(self, props=None):
       self._props = {}
       self._origprops = {}
       self._keymap = {}

   def get_property(self, key):
       return self._props.get(key, '')

   def propertyNames(self):
       return self._props.keys()

   def getPropertyDict(self):
       return self._props

   def __getitem__(self, name):
       return self.get_property(name)

   def __getattr__(self, name):
       try:
           return self.__dict__[name]
       except KeyError:
           if hasattr(self._props, name):
               return getattr(self._props, name)
           else:
               raise NotImplementedError("The method '{}' is not 
implemented.".format(name))

   def __contains__(self, key):
       return  key in self._props

if __name__ == "__main__":
   p = Properties()
   "a" in p
```
Upon running this program with either Python 3 or Python 2 python, it will 
indefinitely hang, never returning.  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to