Commit ac480a3e authored by Vincent Pelletier's avatar Vincent Pelletier

Copy ExternalMethod.__call__ entirely to prepare for further patching.

Also, use from ... import * to make verbatim original code work.
Also, use original code's coding style for unaltered lines.
parent 1a39a60e
......@@ -11,14 +11,14 @@
#
##############################################################################
from Products.ExternalMethod.ExternalMethod import ExternalMethod
from Products.ExternalMethod.ExternalMethod import *
from App.Extensions import FuncCode, getObject
if 1:
def getFunction(self, reload=False, f=None):
"""
Patch to get ZODB Component Extension function if available, otherwise
fallback on filesystem Extension
Patch2: do not use hasattr.
"""
if f is None:
import erp5.component.extension
......@@ -28,21 +28,41 @@ if 1:
except AttributeError:
f = getObject(self._module, self._function, reload)
# From ExternalMethod.getFunction
ff = getattr(f, 'im_func', f)
self._v_func_defaults = ff.func_defaults
self._v_func_code = FuncCode(ff, f is not ff)
self._v_f = f
self._v_func_defaults = ff.func_defaults
self._v_func_code = FuncCode(ff,f is not ff)
self._v_f=f
return f
ExternalMethod.getFunction = getFunction
ExternalMethod__call__ = ExternalMethod.__call__
def __call__(self, *args, **kw):
"""
Patch to call ZODB Component Extension, by trying first to import ZODB
Component Extension if available, otherwise fallback on filesystem
Extension
"""Call an ExternalMethod
Calling an External Method is roughly equivalent to calling
the original actual function from Python. Positional and
keyword parameters can be passed as usual. Note however that
unlike the case of a normal Python method, the "self" argument
must be passed explicitly. An exception to this rule is made
if:
- The supplied number of arguments is one less than the
required number of arguments, and
- The name of the function\'s first argument is 'self'.
In this case, the URL parent of the object is supplied as the
first argument.
Monkey patches:
- call ZODB Component Extension, by trying first to import ZODB
Component Extension if available, otherwise fallback on filesystem
Extension
- access volatile attribute safely
"""
try:
f = getattr(__import__('erp5.component.extension.' + self._module,
......@@ -51,28 +71,41 @@ if 1:
self._function)
except (ImportError, AttributeError):
return ExternalMethod__call__(self, *args, **kw)
import Globals # for data
filePath = self.filepath()
if filePath==None:
raise RuntimeError,\
"external method could not be called " \
"because it is None"
if not os.path.exists(filePath):
raise RuntimeError,\
"external method could not be called " \
"because the file does not exist"
if Globals.DevelopmentMode:
self.reloadIfChanged()
f = None
_v_f = getattr(self, '_v_f', None)
if not _v_f or (f and f is not _v_f):
f = self.getFunction(f=f)
else:
_v_f = getattr(self, '_v_f', None)
if not _v_f or f is not _v_f:
f = self.getFunction(f=f)
f = _v_f
# From ExternalMethod.__call__
__traceback_info__ = args, kw, self._v_func_defaults
__traceback_info__=args, kw, self._v_func_defaults
try: return f(*args, **kw)
except TypeError, v:
tb=sys.exc_info()[2]
try:
return f(*args, **kw)
except TypeError, v:
import sys
tb = sys.exc_info()[2]
try:
if ((self._v_func_code.co_argcount -
len(self._v_func_defaults or ()) - 1 == len(args)) and
self._v_func_code.co_varnames[0] == 'self'):
return f(self.aq_parent.this(), *args, **kw)
raise TypeError, v, tb
finally:
tb = None
if ((self._v_func_code.co_argcount-
len(self._v_func_defaults or ()) - 1 == len(args))
and self._v_func_code.co_varnames[0]=='self'):
return f(self.aq_parent.this(), *args, **kw)
raise TypeError, v, tb
finally: tb=None
ExternalMethod.__call__ = __call__
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment