How do I apply a monkeypatch to GAE?

2019-07-19 17:55发布

Can you tell me how I apply this patch to google app engine as in where to put it? Thank you

def _user_init(self, email=None, _auth_domain=None,
             _user_id=None, federated_identity=None, federated_provider=None):
  if not _auth_domain:
    _auth_domain = os.environ.get('AUTH_DOMAIN')
  assert _auth_domain

  if email is None and federated_identity is None:
    email = os.environ.get('USER_EMAIL', email)
    _user_id = os.environ.get('USER_ID', _user_id)
    federated_identity = os.environ.get('FEDERATED_IDENTITY',
                                        federated_identity)
    federated_provider = os.environ.get('FEDERATED_PROVIDER',
                                        federated_provider)

  if not email and not federated_identity:
    raise UserNotFoundError

  self.__email = email
  self.__federated_identity = federated_identity
  self.__federated_provider = federated_provider
  self.__auth_domain = _auth_domain
  self.__user_id = _user_id or None

users.User.__init__ = _user_init

3条回答
小情绪 Triste *
2楼-- · 2019-07-19 18:31

Just use it as-is: Put that code in a module that gets imported before you use the relevant User module or datastore functionality. I included the relevant line to patch the code (the last line) with the patch itself.

查看更多
对你真心纯属浪费
3楼-- · 2019-07-19 18:37

I think, this belongs to some application as a grep within the appengine sdk, for 'federated_identity' does not result any clues. BTW, you should be doing the same. Grep (or WinGrep) for terms like 'federated' to see if this partial patch can be applied to any source.

Thanks for the updated link. The patch can be added to the file google/appengine/api/users.py

You might just need to add the last line: users.User.__init__ = _user_init

I could figure this out after checking the latest code in the svn.

查看更多
一纸荒年 Trace。
4楼-- · 2019-07-19 18:54

Overriding the constructor like this is not safe. If the internal implementation of the Users API changes in production, your application could break without warning.

What are you trying to accomplish here? I don't see any custom logic; it looks like you've just copied the constructor from the SDK verbatim. If you need to add custom logic, try subclassing UserProperty and/or wrapping the users API calls instead.

查看更多
登录 后发表回答