PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/interpreter/function.py

https://bitbucket.org/pypy/pypy/
Python | 675 lines | 659 code | 7 blank | 9 comment | 5 complexity | 3c35b66d3569ecd2657fb6d749432e49 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. """
  2. Function objects.
  3. In PyPy there is no difference between built-in and user-defined function
  4. objects; the difference lies in the code object found in their func_code
  5. attribute.
  6. """
  7. from rpython.rlib.unroll import unrolling_iterable
  8. from pypy.interpreter.baseobjspace import W_Root
  9. from pypy.interpreter.error import OperationError, oefmt
  10. from pypy.interpreter.eval import Code
  11. from pypy.interpreter.argument import Arguments
  12. from rpython.rlib import jit
  13. from rpython.rlib.rarithmetic import LONG_BIT
  14. from rpython.rlib.rbigint import rbigint
  15. funccallunrolling = unrolling_iterable(range(4))
  16. @jit.elidable_promote()
  17. def _get_immutable_code(func):
  18. assert not func.can_change_code
  19. return func.code
  20. class Function(W_Root):
  21. """A function is a code object captured with some environment:
  22. an object space, a dictionary of globals, default arguments,
  23. and an arbitrary 'closure' passed to the code object."""
  24. can_change_code = True
  25. _immutable_fields_ = ['code?',
  26. 'w_func_globals?',
  27. 'closure?[*]',
  28. 'defs_w?[*]',
  29. 'name?']
  30. def __init__(self, space, code, w_globals=None, defs_w=[], closure=None,
  31. forcename=None):
  32. self.space = space
  33. self.name = forcename or code.co_name
  34. self.w_doc = None # lazily read from code.getdocstring()
  35. self.code = code # Code instance
  36. self.w_func_globals = w_globals # the globals dictionary
  37. self.closure = closure # normally, list of Cell instances or None
  38. self.defs_w = defs_w
  39. self.w_func_dict = None # filled out below if needed
  40. self.w_module = None
  41. def __repr__(self):
  42. # return "function %s.%s" % (self.space, self.name)
  43. # maybe we want this shorter:
  44. name = getattr(self, 'name', None)
  45. if not isinstance(name, str):
  46. name = '?'
  47. return "<%s %s>" % (self.__class__.__name__, name)
  48. def call_args(self, args):
  49. # delegate activation to code
  50. w_res = self.getcode().funcrun(self, args)
  51. assert isinstance(w_res, W_Root)
  52. return w_res
  53. def call_obj_args(self, w_obj, args):
  54. # delegate activation to code
  55. w_res = self.getcode().funcrun_obj(self, w_obj, args)
  56. assert isinstance(w_res, W_Root)
  57. return w_res
  58. def getcode(self):
  59. if jit.we_are_jitted():
  60. if not self.can_change_code:
  61. return _get_immutable_code(self)
  62. return jit.promote(self.code)
  63. return self.code
  64. def funccall(self, *args_w): # speed hack
  65. from pypy.interpreter import gateway
  66. from pypy.interpreter.pycode import PyCode
  67. code = self.getcode() # hook for the jit
  68. nargs = len(args_w)
  69. fast_natural_arity = code.fast_natural_arity
  70. if nargs == fast_natural_arity:
  71. if nargs == 0:
  72. assert isinstance(code, gateway.BuiltinCode0)
  73. return code.fastcall_0(self.space, self)
  74. elif nargs == 1:
  75. assert isinstance(code, gateway.BuiltinCode1)
  76. return code.fastcall_1(self.space, self, args_w[0])
  77. elif nargs == 2:
  78. assert isinstance(code, gateway.BuiltinCode2)
  79. return code.fastcall_2(self.space, self, args_w[0], args_w[1])
  80. elif nargs == 3:
  81. assert isinstance(code, gateway.BuiltinCode3)
  82. return code.fastcall_3(self.space, self, args_w[0],
  83. args_w[1], args_w[2])
  84. elif nargs == 4:
  85. assert isinstance(code, gateway.BuiltinCode4)
  86. return code.fastcall_4(self.space, self, args_w[0],
  87. args_w[1], args_w[2], args_w[3])
  88. elif (nargs | PyCode.FLATPYCALL) == fast_natural_arity:
  89. assert isinstance(code, PyCode)
  90. if nargs < 5:
  91. new_frame = self.space.createframe(code, self.w_func_globals,
  92. self)
  93. for i in funccallunrolling:
  94. if i < nargs:
  95. new_frame.locals_cells_stack_w[i] = args_w[i]
  96. return new_frame.run()
  97. elif nargs >= 1 and fast_natural_arity == Code.PASSTHROUGHARGS1:
  98. assert isinstance(code, gateway.BuiltinCodePassThroughArguments1)
  99. return code.funcrun_obj(self, args_w[0],
  100. Arguments(self.space,
  101. list(args_w[1:])))
  102. return self.call_args(Arguments(self.space, list(args_w)))
  103. def funccall_valuestack(self, nargs, frame): # speed hack
  104. from pypy.interpreter import gateway
  105. from pypy.interpreter.pycode import PyCode
  106. code = self.getcode() # hook for the jit
  107. #
  108. if (jit.we_are_jitted() and code is self.space._code_of_sys_exc_info
  109. and nargs == 0):
  110. from pypy.module.sys.vm import exc_info_direct
  111. return exc_info_direct(self.space, frame)
  112. #
  113. fast_natural_arity = code.fast_natural_arity
  114. if nargs == fast_natural_arity:
  115. if nargs == 0:
  116. assert isinstance(code, gateway.BuiltinCode0)
  117. return code.fastcall_0(self.space, self)
  118. elif nargs == 1:
  119. assert isinstance(code, gateway.BuiltinCode1)
  120. return code.fastcall_1(self.space, self, frame.peekvalue(0))
  121. elif nargs == 2:
  122. assert isinstance(code, gateway.BuiltinCode2)
  123. return code.fastcall_2(self.space, self, frame.peekvalue(1),
  124. frame.peekvalue(0))
  125. elif nargs == 3:
  126. assert isinstance(code, gateway.BuiltinCode3)
  127. return code.fastcall_3(self.space, self, frame.peekvalue(2),
  128. frame.peekvalue(1), frame.peekvalue(0))
  129. elif nargs == 4:
  130. assert isinstance(code, gateway.BuiltinCode4)
  131. return code.fastcall_4(self.space, self, frame.peekvalue(3),
  132. frame.peekvalue(2), frame.peekvalue(1),
  133. frame.peekvalue(0))
  134. elif (nargs | Code.FLATPYCALL) == fast_natural_arity:
  135. assert isinstance(code, PyCode)
  136. return self._flat_pycall(code, nargs, frame)
  137. elif fast_natural_arity & Code.FLATPYCALL:
  138. natural_arity = fast_natural_arity & 0xff
  139. if natural_arity > nargs >= natural_arity - len(self.defs_w):
  140. assert isinstance(code, PyCode)
  141. return self._flat_pycall_defaults(code, nargs, frame,
  142. natural_arity - nargs)
  143. elif fast_natural_arity == Code.PASSTHROUGHARGS1 and nargs >= 1:
  144. assert isinstance(code, gateway.BuiltinCodePassThroughArguments1)
  145. w_obj = frame.peekvalue(nargs-1)
  146. args = frame.make_arguments(nargs-1)
  147. return code.funcrun_obj(self, w_obj, args)
  148. args = frame.make_arguments(nargs)
  149. return self.call_args(args)
  150. @jit.unroll_safe
  151. def _flat_pycall(self, code, nargs, frame):
  152. # code is a PyCode
  153. new_frame = self.space.createframe(code, self.w_func_globals,
  154. self)
  155. for i in xrange(nargs):
  156. w_arg = frame.peekvalue(nargs-1-i)
  157. new_frame.locals_cells_stack_w[i] = w_arg
  158. return new_frame.run()
  159. @jit.unroll_safe
  160. def _flat_pycall_defaults(self, code, nargs, frame, defs_to_load):
  161. # code is a PyCode
  162. new_frame = self.space.createframe(code, self.w_func_globals,
  163. self)
  164. for i in xrange(nargs):
  165. w_arg = frame.peekvalue(nargs-1-i)
  166. new_frame.locals_cells_stack_w[i] = w_arg
  167. ndefs = len(self.defs_w)
  168. start = ndefs - defs_to_load
  169. i = nargs
  170. for j in xrange(start, ndefs):
  171. new_frame.locals_cells_stack_w[i] = self.defs_w[j]
  172. i += 1
  173. return new_frame.run()
  174. def getdict(self, space):
  175. if self.w_func_dict is None:
  176. self.w_func_dict = space.newdict(instance=True)
  177. return self.w_func_dict
  178. def setdict(self, space, w_dict):
  179. if not space.isinstance_w(w_dict, space.w_dict):
  180. raise oefmt(space.w_TypeError,
  181. "setting function's dictionary to a non-dict")
  182. self.w_func_dict = w_dict
  183. def descr_function__new__(space, w_subtype, w_code, w_globals,
  184. w_name=None, w_argdefs=None, w_closure=None):
  185. code = space.interp_w(Code, w_code)
  186. if not space.isinstance_w(w_globals, space.w_dict):
  187. raise oefmt(space.w_TypeError, "expected dict")
  188. if not space.is_none(w_name):
  189. name = space.str_w(w_name)
  190. else:
  191. name = None
  192. if not space.is_none(w_argdefs):
  193. defs_w = space.fixedview(w_argdefs)
  194. else:
  195. defs_w = []
  196. nfreevars = 0
  197. from pypy.interpreter.pycode import PyCode
  198. if isinstance(code, PyCode):
  199. nfreevars = len(code.co_freevars)
  200. if space.is_none(w_closure) and nfreevars == 0:
  201. closure = None
  202. elif not space.is_w(space.type(w_closure), space.w_tuple):
  203. raise oefmt(space.w_TypeError, "invalid closure")
  204. else:
  205. from pypy.interpreter.nestedscope import Cell
  206. closure_w = space.unpackiterable(w_closure)
  207. n = len(closure_w)
  208. if nfreevars == 0:
  209. raise oefmt(space.w_ValueError, "no closure needed")
  210. elif nfreevars != n:
  211. raise oefmt(space.w_ValueError, "closure is wrong size")
  212. closure = [space.interp_w(Cell, w_cell) for w_cell in closure_w]
  213. func = space.allocate_instance(Function, w_subtype)
  214. Function.__init__(func, space, code, w_globals, defs_w, closure, name)
  215. return space.wrap(func)
  216. def descr_function_call(self, __args__):
  217. return self.call_args(__args__)
  218. def descr_function_repr(self):
  219. return self.getrepr(self.space, 'function %s' % (self.name,))
  220. # delicate
  221. _all = {'': None}
  222. def _cleanup_(self):
  223. from pypy.interpreter.gateway import BuiltinCode
  224. if isinstance(self.code, BuiltinCode):
  225. # we have been seen by other means so rtyping should not choke
  226. # on us
  227. identifier = self.code.identifier
  228. previous = Function._all.get(identifier, self)
  229. assert previous is self, (
  230. "duplicate function ids with identifier=%r: %r and %r" % (
  231. identifier, previous, self))
  232. self.add_to_table()
  233. return False
  234. def add_to_table(self):
  235. Function._all[self.code.identifier] = self
  236. def find(identifier):
  237. return Function._all[identifier]
  238. find = staticmethod(find)
  239. def descr_function__reduce__(self, space):
  240. from pypy.interpreter.gateway import BuiltinCode
  241. from pypy.interpreter.mixedmodule import MixedModule
  242. w_mod = space.getbuiltinmodule('_pickle_support')
  243. mod = space.interp_w(MixedModule, w_mod)
  244. code = self.code
  245. if isinstance(code, BuiltinCode):
  246. new_inst = mod.get('builtin_function')
  247. return space.newtuple([new_inst,
  248. space.newtuple([space.wrap(code.identifier)])])
  249. new_inst = mod.get('func_new')
  250. w = space.wrap
  251. if self.closure is None:
  252. w_closure = space.w_None
  253. else:
  254. w_closure = space.newtuple([w(cell) for cell in self.closure])
  255. if self.w_doc is None:
  256. w_doc = space.w_None
  257. else:
  258. w_doc = self.w_doc
  259. if self.w_func_globals is None:
  260. w_func_globals = space.w_None
  261. else:
  262. w_func_globals = self.w_func_globals
  263. if self.w_func_dict is None:
  264. w_func_dict = space.w_None
  265. else:
  266. w_func_dict = self.w_func_dict
  267. nt = space.newtuple
  268. tup_base = []
  269. tup_state = [
  270. w(self.name),
  271. w_doc,
  272. w(self.code),
  273. w_func_globals,
  274. w_closure,
  275. nt(self.defs_w),
  276. w_func_dict,
  277. self.w_module,
  278. ]
  279. return nt([new_inst, nt(tup_base), nt(tup_state)])
  280. def descr_function__setstate__(self, space, w_args):
  281. args_w = space.unpackiterable(w_args)
  282. try:
  283. (w_name, w_doc, w_code, w_func_globals, w_closure, w_defs,
  284. w_func_dict, w_module) = args_w
  285. except ValueError:
  286. # wrong args
  287. raise oefmt(space.w_ValueError,
  288. "Wrong arguments to function.__setstate__")
  289. self.space = space
  290. self.name = space.str_w(w_name)
  291. self.code = space.interp_w(Code, w_code)
  292. if not space.is_w(w_closure, space.w_None):
  293. from pypy.interpreter.nestedscope import Cell
  294. closure_w = space.unpackiterable(w_closure)
  295. self.closure = [space.interp_w(Cell, w_cell) for w_cell in closure_w]
  296. else:
  297. self.closure = None
  298. if space.is_w(w_doc, space.w_None):
  299. w_doc = None
  300. self.w_doc = w_doc
  301. if space.is_w(w_func_globals, space.w_None):
  302. w_func_globals = None
  303. self.w_func_globals = w_func_globals
  304. if space.is_w(w_func_dict, space.w_None):
  305. w_func_dict = None
  306. self.w_func_dict = w_func_dict
  307. self.defs_w = space.fixedview(w_defs)
  308. self.w_module = w_module
  309. def fget_func_defaults(self, space):
  310. values_w = self.defs_w
  311. # the `None in values_w` check here is to ensure that interp-level
  312. # functions with a default of None do not get their defaults
  313. # exposed at applevel
  314. if not values_w or None in values_w:
  315. return space.w_None
  316. return space.newtuple(values_w)
  317. def fset_func_defaults(self, space, w_defaults):
  318. if space.is_w(w_defaults, space.w_None):
  319. self.defs_w = []
  320. return
  321. if not space.isinstance_w(w_defaults, space.w_tuple):
  322. raise oefmt(space.w_TypeError,
  323. "func_defaults must be set to a tuple object or None")
  324. self.defs_w = space.fixedview(w_defaults)
  325. def fdel_func_defaults(self, space):
  326. self.defs_w = []
  327. def fget_func_doc(self, space):
  328. if self.w_doc is None:
  329. self.w_doc = self.code.getdocstring(space)
  330. return self.w_doc
  331. def fset_func_doc(self, space, w_doc):
  332. self.w_doc = w_doc
  333. def fget_func_name(self, space):
  334. return space.wrap(self.name)
  335. def fset_func_name(self, space, w_name):
  336. if space.isinstance_w(w_name, space.w_str):
  337. self.name = space.str_w(w_name)
  338. else:
  339. raise oefmt(space.w_TypeError,
  340. "__name__ must be set to a string object")
  341. def fdel_func_doc(self, space):
  342. self.w_doc = space.w_None
  343. def fget___module__(self, space):
  344. if self.w_module is None:
  345. if self.w_func_globals is not None and not space.is_w(self.w_func_globals, space.w_None):
  346. self.w_module = space.call_method(self.w_func_globals, "get", space.wrap("__name__"))
  347. else:
  348. self.w_module = space.w_None
  349. return self.w_module
  350. def fset___module__(self, space, w_module):
  351. self.w_module = w_module
  352. def fdel___module__(self, space):
  353. self.w_module = space.w_None
  354. def fget_func_code(self, space):
  355. return space.wrap(self.code)
  356. def fset_func_code(self, space, w_code):
  357. from pypy.interpreter.pycode import PyCode
  358. if not self.can_change_code:
  359. raise oefmt(space.w_TypeError,
  360. "Cannot change code attribute of builtin functions")
  361. code = space.interp_w(Code, w_code)
  362. closure_len = 0
  363. if self.closure:
  364. closure_len = len(self.closure)
  365. if isinstance(code, PyCode) and closure_len != len(code.co_freevars):
  366. raise oefmt(space.w_ValueError,
  367. "%N() requires a code object with %d free vars, not "
  368. "%d", self, closure_len, len(code.co_freevars))
  369. self.fget_func_doc(space) # see test_issue1293
  370. self.code = code
  371. def fget_func_closure(self, space):
  372. if self.closure is not None:
  373. w_res = space.newtuple([space.wrap(i) for i in self.closure])
  374. else:
  375. w_res = space.w_None
  376. return w_res
  377. def descr_function_get(space, w_function, w_obj, w_cls=None):
  378. """functionobject.__get__(obj[, type]) -> method"""
  379. # this is not defined as a method on Function because it's generally
  380. # useful logic: w_function can be any callable. It is used by Method too.
  381. asking_for_bound = (space.is_none(w_cls) or
  382. not space.is_w(w_obj, space.w_None) or
  383. space.is_w(w_cls, space.type(space.w_None)))
  384. if asking_for_bound:
  385. return space.wrap(Method(space, w_function, w_obj, w_cls))
  386. else:
  387. return space.wrap(Method(space, w_function, None, w_cls))
  388. class Method(W_Root):
  389. """A method is a function bound to a specific instance or class."""
  390. _immutable_fields_ = ['w_function', 'w_instance', 'w_class']
  391. def __init__(self, space, w_function, w_instance, w_class):
  392. self.space = space
  393. self.w_function = w_function
  394. self.w_instance = w_instance # or None
  395. if w_class is None:
  396. w_class = space.w_None
  397. self.w_class = w_class # possibly space.w_None
  398. def descr_method__new__(space, w_subtype, w_function, w_instance,
  399. w_class=None):
  400. if space.is_w(w_instance, space.w_None):
  401. w_instance = None
  402. if w_instance is None and space.is_none(w_class):
  403. raise oefmt(space.w_TypeError, "unbound methods must have class")
  404. method = space.allocate_instance(Method, w_subtype)
  405. Method.__init__(method, space, w_function, w_instance, w_class)
  406. return space.wrap(method)
  407. def __repr__(self):
  408. if self.w_instance:
  409. pre = "bound"
  410. else:
  411. pre = "unbound"
  412. return "%s method %s" % (pre, self.w_function.getname(self.space))
  413. def call_args(self, args):
  414. space = self.space
  415. if self.w_instance is not None:
  416. # bound method
  417. return space.call_obj_args(self.w_function, self.w_instance, args)
  418. # unbound method
  419. w_firstarg = args.firstarg()
  420. if w_firstarg is not None and (
  421. space.abstract_isinstance_w(w_firstarg, self.w_class)):
  422. pass # ok
  423. else:
  424. clsdescr = self.w_class.getname(space)
  425. if clsdescr and clsdescr != '?':
  426. clsdescr += " instance"
  427. else:
  428. clsdescr = "instance"
  429. if w_firstarg is None:
  430. instdescr = "nothing"
  431. else:
  432. instname = space.abstract_getclass(w_firstarg).getname(space)
  433. if instname and instname != '?':
  434. instdescr = instname + " instance"
  435. else:
  436. instdescr = "instance"
  437. raise oefmt(space.w_TypeError,
  438. "unbound method %N() must be called with %s as first "
  439. "argument (got %s instead)", self, clsdescr, instdescr)
  440. return space.call_args(self.w_function, args)
  441. def descr_method_get(self, w_obj, w_cls=None):
  442. space = self.space
  443. if self.w_instance is not None:
  444. return space.wrap(self) # already bound
  445. else:
  446. # only allow binding to a more specific class than before
  447. if (w_cls is not None and
  448. not space.is_w(w_cls, space.w_None) and
  449. not space.abstract_issubclass_w(w_cls, self.w_class,
  450. allow_override=True)):
  451. return space.wrap(self) # subclass test failed
  452. else:
  453. return descr_function_get(space, self.w_function, w_obj, w_cls)
  454. def descr_method_call(self, __args__):
  455. return self.call_args(__args__)
  456. def descr_method_repr(self):
  457. space = self.space
  458. name = self.w_function.getname(self.space)
  459. # XXX do we handle all cases sanely here?
  460. if space.is_w(self.w_class, space.w_None):
  461. w_class = space.type(self.w_instance)
  462. else:
  463. w_class = self.w_class
  464. typename = w_class.getname(self.space)
  465. if self.w_instance is None:
  466. s = "<unbound method %s.%s>" % (typename, name)
  467. return space.wrap(s)
  468. else:
  469. objrepr = space.str_w(space.repr(self.w_instance))
  470. s = '<bound method %s.%s of %s>' % (typename, name, objrepr)
  471. return space.wrap(s)
  472. def descr_method_getattribute(self, w_attr):
  473. space = self.space
  474. if space.str_w(w_attr) != '__doc__':
  475. try:
  476. return space.call_method(space.w_object, '__getattribute__',
  477. space.wrap(self), w_attr)
  478. except OperationError as e:
  479. if not e.match(space, space.w_AttributeError):
  480. raise
  481. # fall-back to the attribute of the underlying 'im_func'
  482. return space.getattr(self.w_function, w_attr)
  483. def descr_method_eq(self, w_other):
  484. space = self.space
  485. if not isinstance(w_other, Method):
  486. return space.w_NotImplemented
  487. if self.w_instance is None:
  488. if w_other.w_instance is not None:
  489. return space.w_False
  490. else:
  491. if w_other.w_instance is None:
  492. return space.w_False
  493. if not space.eq_w(self.w_instance, w_other.w_instance):
  494. return space.w_False
  495. return space.eq(self.w_function, w_other.w_function)
  496. def is_w(self, space, other):
  497. if not isinstance(other, Method):
  498. return False
  499. return (self.w_instance is other.w_instance and
  500. self.w_function is other.w_function and
  501. self.w_class is other.w_class)
  502. def immutable_unique_id(self, space):
  503. from pypy.objspace.std.util import IDTAG_METHOD as tag
  504. from pypy.objspace.std.util import IDTAG_SHIFT
  505. if self.w_instance is not None:
  506. id = space.bigint_w(space.id(self.w_instance))
  507. id = id.lshift(LONG_BIT)
  508. else:
  509. id = rbigint.fromint(0)
  510. id = id.or_(space.bigint_w(space.id(self.w_function)))
  511. id = id.lshift(LONG_BIT).or_(space.bigint_w(space.id(self.w_class)))
  512. id = id.lshift(IDTAG_SHIFT).int_or_(tag)
  513. return space.newlong_from_rbigint(id)
  514. def descr_method_hash(self):
  515. space = self.space
  516. w_result = space.hash(self.w_function)
  517. if self.w_instance is not None:
  518. w_result = space.xor(w_result, space.hash(self.w_instance))
  519. return w_result
  520. def descr_method__reduce__(self, space):
  521. from pypy.interpreter.mixedmodule import MixedModule
  522. from pypy.interpreter.gateway import BuiltinCode
  523. w_mod = space.getbuiltinmodule('_pickle_support')
  524. mod = space.interp_w(MixedModule, w_mod)
  525. new_inst = mod.get('method_new')
  526. w_instance = self.w_instance or space.w_None
  527. w_function = self.w_function
  528. if (isinstance(w_function, Function) and
  529. isinstance(w_function.code, BuiltinCode)):
  530. new_inst = mod.get('builtin_method_new')
  531. if space.is_w(w_instance, space.w_None):
  532. tup = [self.w_class, space.wrap(w_function.name)]
  533. else:
  534. tup = [w_instance, space.wrap(w_function.name)]
  535. elif space.is_w(self.w_class, space.w_None):
  536. tup = [self.w_function, w_instance]
  537. else:
  538. tup = [self.w_function, w_instance, self.w_class]
  539. return space.newtuple([new_inst, space.newtuple(tup)])
  540. class StaticMethod(W_Root):
  541. """The staticmethod objects."""
  542. _immutable_fields_ = ['w_function']
  543. def __init__(self, w_function):
  544. self.w_function = w_function
  545. def descr_staticmethod_get(self, w_obj, w_cls=None):
  546. """staticmethod(x).__get__(obj[, type]) -> x"""
  547. return self.w_function
  548. def descr_staticmethod__new__(space, w_subtype, w_function):
  549. instance = space.allocate_instance(StaticMethod, w_subtype)
  550. instance.__init__(w_function)
  551. return space.wrap(instance)
  552. class ClassMethod(W_Root):
  553. """The classmethod objects."""
  554. _immutable_fields_ = ['w_function']
  555. def __init__(self, w_function):
  556. self.w_function = w_function
  557. def descr_classmethod_get(self, space, w_obj, w_klass=None):
  558. if space.is_none(w_klass):
  559. w_klass = space.type(w_obj)
  560. return space.wrap(Method(space, self.w_function, w_klass,
  561. space.type(w_klass)))
  562. def descr_classmethod__new__(space, w_subtype, w_function):
  563. instance = space.allocate_instance(ClassMethod, w_subtype)
  564. instance.__init__(w_function)
  565. return space.wrap(instance)
  566. class FunctionWithFixedCode(Function):
  567. can_change_code = False
  568. class BuiltinFunction(Function):
  569. can_change_code = False
  570. def __init__(self, func):
  571. assert isinstance(func, Function)
  572. Function.__init__(self, func.space, func.code, func.w_func_globals,
  573. func.defs_w, func.closure, func.name)
  574. self.w_doc = func.w_doc
  575. self.w_func_dict = func.w_func_dict
  576. self.w_module = func.w_module
  577. def descr_builtinfunction__new__(space, w_subtype):
  578. raise oefmt(space.w_TypeError,
  579. "cannot create 'builtin_function' instances")
  580. def descr_function_repr(self):
  581. return self.space.wrap('<built-in function %s>' % (self.name,))
  582. def is_builtin_code(w_func):
  583. from pypy.interpreter.gateway import BuiltinCode
  584. if isinstance(w_func, Method):
  585. w_func = w_func.w_function
  586. if isinstance(w_func, Function):
  587. code = w_func.getcode()
  588. else:
  589. code = None
  590. return isinstance(code, BuiltinCode)