Buffer.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. from __future__ import absolute_import
  2. from .Visitor import CythonTransform
  3. from .ModuleNode import ModuleNode
  4. from .Errors import CompileError
  5. from .UtilityCode import CythonUtilityCode
  6. from .Code import UtilityCode, TempitaUtilityCode
  7. from . import Options
  8. from . import Interpreter
  9. from . import PyrexTypes
  10. from . import Naming
  11. from . import Symtab
  12. def dedent(text, reindent=0):
  13. from textwrap import dedent
  14. text = dedent(text)
  15. if reindent > 0:
  16. indent = " " * reindent
  17. text = '\n'.join([indent + x for x in text.split('\n')])
  18. return text
  19. class IntroduceBufferAuxiliaryVars(CythonTransform):
  20. #
  21. # Entry point
  22. #
  23. buffers_exists = False
  24. using_memoryview = False
  25. def __call__(self, node):
  26. assert isinstance(node, ModuleNode)
  27. self.max_ndim = 0
  28. result = super(IntroduceBufferAuxiliaryVars, self).__call__(node)
  29. if self.buffers_exists:
  30. use_bufstruct_declare_code(node.scope)
  31. use_py2_buffer_functions(node.scope)
  32. return result
  33. #
  34. # Basic operations for transforms
  35. #
  36. def handle_scope(self, node, scope):
  37. # For all buffers, insert extra variables in the scope.
  38. # The variables are also accessible from the buffer_info
  39. # on the buffer entry
  40. scope_items = scope.entries.items()
  41. bufvars = [entry for name, entry in scope_items if entry.type.is_buffer]
  42. if len(bufvars) > 0:
  43. bufvars.sort(key=lambda entry: entry.name)
  44. self.buffers_exists = True
  45. memviewslicevars = [entry for name, entry in scope_items if entry.type.is_memoryviewslice]
  46. if len(memviewslicevars) > 0:
  47. self.buffers_exists = True
  48. for (name, entry) in scope_items:
  49. if name == 'memoryview' and isinstance(entry.utility_code_definition, CythonUtilityCode):
  50. self.using_memoryview = True
  51. break
  52. del scope_items
  53. if isinstance(node, ModuleNode) and len(bufvars) > 0:
  54. # for now...note that pos is wrong
  55. raise CompileError(node.pos, "Buffer vars not allowed in module scope")
  56. for entry in bufvars:
  57. if entry.type.dtype.is_ptr:
  58. raise CompileError(node.pos, "Buffers with pointer types not yet supported.")
  59. name = entry.name
  60. buftype = entry.type
  61. if buftype.ndim > Options.buffer_max_dims:
  62. raise CompileError(node.pos,
  63. "Buffer ndims exceeds Options.buffer_max_dims = %d" % Options.buffer_max_dims)
  64. if buftype.ndim > self.max_ndim:
  65. self.max_ndim = buftype.ndim
  66. # Declare auxiliary vars
  67. def decvar(type, prefix):
  68. cname = scope.mangle(prefix, name)
  69. aux_var = scope.declare_var(name=None, cname=cname,
  70. type=type, pos=node.pos)
  71. if entry.is_arg:
  72. aux_var.used = True # otherwise, NameNode will mark whether it is used
  73. return aux_var
  74. auxvars = ((PyrexTypes.c_pyx_buffer_nd_type, Naming.pybuffernd_prefix),
  75. (PyrexTypes.c_pyx_buffer_type, Naming.pybufferstruct_prefix))
  76. pybuffernd, rcbuffer = [decvar(type, prefix) for (type, prefix) in auxvars]
  77. entry.buffer_aux = Symtab.BufferAux(pybuffernd, rcbuffer)
  78. scope.buffer_entries = bufvars
  79. self.scope = scope
  80. def visit_ModuleNode(self, node):
  81. self.handle_scope(node, node.scope)
  82. self.visitchildren(node)
  83. return node
  84. def visit_FuncDefNode(self, node):
  85. self.handle_scope(node, node.local_scope)
  86. self.visitchildren(node)
  87. return node
  88. #
  89. # Analysis
  90. #
  91. buffer_options = ("dtype", "ndim", "mode", "negative_indices", "cast") # ordered!
  92. buffer_defaults = {"ndim": 1, "mode": "full", "negative_indices": True, "cast": False}
  93. buffer_positional_options_count = 1 # anything beyond this needs keyword argument
  94. ERR_BUF_OPTION_UNKNOWN = '"%s" is not a buffer option'
  95. ERR_BUF_TOO_MANY = 'Too many buffer options'
  96. ERR_BUF_DUP = '"%s" buffer option already supplied'
  97. ERR_BUF_MISSING = '"%s" missing'
  98. ERR_BUF_MODE = 'Only allowed buffer modes are: "c", "fortran", "full", "strided" (as a compile-time string)'
  99. ERR_BUF_NDIM = 'ndim must be a non-negative integer'
  100. ERR_BUF_DTYPE = 'dtype must be "object", numeric type or a struct'
  101. ERR_BUF_BOOL = '"%s" must be a boolean'
  102. def analyse_buffer_options(globalpos, env, posargs, dictargs, defaults=None, need_complete=True):
  103. """
  104. Must be called during type analysis, as analyse is called
  105. on the dtype argument.
  106. posargs and dictargs should consist of a list and a dict
  107. of tuples (value, pos). Defaults should be a dict of values.
  108. Returns a dict containing all the options a buffer can have and
  109. its value (with the positions stripped).
  110. """
  111. if defaults is None:
  112. defaults = buffer_defaults
  113. posargs, dictargs = Interpreter.interpret_compiletime_options(
  114. posargs, dictargs, type_env=env, type_args=(0, 'dtype'))
  115. if len(posargs) > buffer_positional_options_count:
  116. raise CompileError(posargs[-1][1], ERR_BUF_TOO_MANY)
  117. options = {}
  118. for name, (value, pos) in dictargs.items():
  119. if not name in buffer_options:
  120. raise CompileError(pos, ERR_BUF_OPTION_UNKNOWN % name)
  121. options[name] = value
  122. for name, (value, pos) in zip(buffer_options, posargs):
  123. if not name in buffer_options:
  124. raise CompileError(pos, ERR_BUF_OPTION_UNKNOWN % name)
  125. if name in options:
  126. raise CompileError(pos, ERR_BUF_DUP % name)
  127. options[name] = value
  128. # Check that they are all there and copy defaults
  129. for name in buffer_options:
  130. if not name in options:
  131. try:
  132. options[name] = defaults[name]
  133. except KeyError:
  134. if need_complete:
  135. raise CompileError(globalpos, ERR_BUF_MISSING % name)
  136. dtype = options.get("dtype")
  137. if dtype and dtype.is_extension_type:
  138. raise CompileError(globalpos, ERR_BUF_DTYPE)
  139. ndim = options.get("ndim")
  140. if ndim and (not isinstance(ndim, int) or ndim < 0):
  141. raise CompileError(globalpos, ERR_BUF_NDIM)
  142. mode = options.get("mode")
  143. if mode and not (mode in ('full', 'strided', 'c', 'fortran')):
  144. raise CompileError(globalpos, ERR_BUF_MODE)
  145. def assert_bool(name):
  146. x = options.get(name)
  147. if not isinstance(x, bool):
  148. raise CompileError(globalpos, ERR_BUF_BOOL % name)
  149. assert_bool('negative_indices')
  150. assert_bool('cast')
  151. return options
  152. #
  153. # Code generation
  154. #
  155. class BufferEntry(object):
  156. def __init__(self, entry):
  157. self.entry = entry
  158. self.type = entry.type
  159. self.cname = entry.buffer_aux.buflocal_nd_var.cname
  160. self.buf_ptr = "%s.rcbuffer->pybuffer.buf" % self.cname
  161. self.buf_ptr_type = entry.type.buffer_ptr_type
  162. self.init_attributes()
  163. def init_attributes(self):
  164. self.shape = self.get_buf_shapevars()
  165. self.strides = self.get_buf_stridevars()
  166. self.suboffsets = self.get_buf_suboffsetvars()
  167. def get_buf_suboffsetvars(self):
  168. return self._for_all_ndim("%s.diminfo[%d].suboffsets")
  169. def get_buf_stridevars(self):
  170. return self._for_all_ndim("%s.diminfo[%d].strides")
  171. def get_buf_shapevars(self):
  172. return self._for_all_ndim("%s.diminfo[%d].shape")
  173. def _for_all_ndim(self, s):
  174. return [s % (self.cname, i) for i in range(self.type.ndim)]
  175. def generate_buffer_lookup_code(self, code, index_cnames):
  176. # Create buffer lookup and return it
  177. # This is done via utility macros/inline functions, which vary
  178. # according to the access mode used.
  179. params = []
  180. nd = self.type.ndim
  181. mode = self.type.mode
  182. if mode == 'full':
  183. for i, s, o in zip(index_cnames,
  184. self.get_buf_stridevars(),
  185. self.get_buf_suboffsetvars()):
  186. params.append(i)
  187. params.append(s)
  188. params.append(o)
  189. funcname = "__Pyx_BufPtrFull%dd" % nd
  190. funcgen = buf_lookup_full_code
  191. else:
  192. if mode == 'strided':
  193. funcname = "__Pyx_BufPtrStrided%dd" % nd
  194. funcgen = buf_lookup_strided_code
  195. elif mode == 'c':
  196. funcname = "__Pyx_BufPtrCContig%dd" % nd
  197. funcgen = buf_lookup_c_code
  198. elif mode == 'fortran':
  199. funcname = "__Pyx_BufPtrFortranContig%dd" % nd
  200. funcgen = buf_lookup_fortran_code
  201. else:
  202. assert False
  203. for i, s in zip(index_cnames, self.get_buf_stridevars()):
  204. params.append(i)
  205. params.append(s)
  206. # Make sure the utility code is available
  207. if funcname not in code.globalstate.utility_codes:
  208. code.globalstate.utility_codes.add(funcname)
  209. protocode = code.globalstate['utility_code_proto']
  210. defcode = code.globalstate['utility_code_def']
  211. funcgen(protocode, defcode, name=funcname, nd=nd)
  212. buf_ptr_type_code = self.buf_ptr_type.empty_declaration_code()
  213. ptrcode = "%s(%s, %s, %s)" % (funcname, buf_ptr_type_code, self.buf_ptr,
  214. ", ".join(params))
  215. return ptrcode
  216. def get_flags(buffer_aux, buffer_type):
  217. flags = 'PyBUF_FORMAT'
  218. mode = buffer_type.mode
  219. if mode == 'full':
  220. flags += '| PyBUF_INDIRECT'
  221. elif mode == 'strided':
  222. flags += '| PyBUF_STRIDES'
  223. elif mode == 'c':
  224. flags += '| PyBUF_C_CONTIGUOUS'
  225. elif mode == 'fortran':
  226. flags += '| PyBUF_F_CONTIGUOUS'
  227. else:
  228. assert False
  229. if buffer_aux.writable_needed: flags += "| PyBUF_WRITABLE"
  230. return flags
  231. def used_buffer_aux_vars(entry):
  232. buffer_aux = entry.buffer_aux
  233. buffer_aux.buflocal_nd_var.used = True
  234. buffer_aux.rcbuf_var.used = True
  235. def put_unpack_buffer_aux_into_scope(buf_entry, code):
  236. # Generate code to copy the needed struct info into local
  237. # variables.
  238. buffer_aux, mode = buf_entry.buffer_aux, buf_entry.type.mode
  239. pybuffernd_struct = buffer_aux.buflocal_nd_var.cname
  240. fldnames = ['strides', 'shape']
  241. if mode == 'full':
  242. fldnames.append('suboffsets')
  243. ln = []
  244. for i in range(buf_entry.type.ndim):
  245. for fldname in fldnames:
  246. ln.append("%s.diminfo[%d].%s = %s.rcbuffer->pybuffer.%s[%d];" % \
  247. (pybuffernd_struct, i, fldname,
  248. pybuffernd_struct, fldname, i))
  249. code.putln(' '.join(ln))
  250. def put_init_vars(entry, code):
  251. bufaux = entry.buffer_aux
  252. pybuffernd_struct = bufaux.buflocal_nd_var.cname
  253. pybuffer_struct = bufaux.rcbuf_var.cname
  254. # init pybuffer_struct
  255. code.putln("%s.pybuffer.buf = NULL;" % pybuffer_struct)
  256. code.putln("%s.refcount = 0;" % pybuffer_struct)
  257. # init the buffer object
  258. # code.put_init_var_to_py_none(entry)
  259. # init the pybuffernd_struct
  260. code.putln("%s.data = NULL;" % pybuffernd_struct)
  261. code.putln("%s.rcbuffer = &%s;" % (pybuffernd_struct, pybuffer_struct))
  262. def put_acquire_arg_buffer(entry, code, pos):
  263. buffer_aux = entry.buffer_aux
  264. getbuffer = get_getbuffer_call(code, entry.cname, buffer_aux, entry.type)
  265. # Acquire any new buffer
  266. code.putln("{")
  267. code.putln("__Pyx_BufFmt_StackElem __pyx_stack[%d];" % entry.type.dtype.struct_nesting_depth())
  268. code.putln(code.error_goto_if("%s == -1" % getbuffer, pos))
  269. code.putln("}")
  270. # An exception raised in arg parsing cannot be caught, so no
  271. # need to care about the buffer then.
  272. put_unpack_buffer_aux_into_scope(entry, code)
  273. def put_release_buffer_code(code, entry):
  274. code.globalstate.use_utility_code(acquire_utility_code)
  275. code.putln("__Pyx_SafeReleaseBuffer(&%s.rcbuffer->pybuffer);" % entry.buffer_aux.buflocal_nd_var.cname)
  276. def get_getbuffer_call(code, obj_cname, buffer_aux, buffer_type):
  277. ndim = buffer_type.ndim
  278. cast = int(buffer_type.cast)
  279. flags = get_flags(buffer_aux, buffer_type)
  280. pybuffernd_struct = buffer_aux.buflocal_nd_var.cname
  281. dtype_typeinfo = get_type_information_cname(code, buffer_type.dtype)
  282. code.globalstate.use_utility_code(acquire_utility_code)
  283. return ("__Pyx_GetBufferAndValidate(&%(pybuffernd_struct)s.rcbuffer->pybuffer, "
  284. "(PyObject*)%(obj_cname)s, &%(dtype_typeinfo)s, %(flags)s, %(ndim)d, "
  285. "%(cast)d, __pyx_stack)" % locals())
  286. def put_assign_to_buffer(lhs_cname, rhs_cname, buf_entry,
  287. is_initialized, pos, code):
  288. """
  289. Generate code for reassigning a buffer variables. This only deals with getting
  290. the buffer auxiliary structure and variables set up correctly, the assignment
  291. itself and refcounting is the responsibility of the caller.
  292. However, the assignment operation may throw an exception so that the reassignment
  293. never happens.
  294. Depending on the circumstances there are two possible outcomes:
  295. - Old buffer released, new acquired, rhs assigned to lhs
  296. - Old buffer released, new acquired which fails, reaqcuire old lhs buffer
  297. (which may or may not succeed).
  298. """
  299. buffer_aux, buffer_type = buf_entry.buffer_aux, buf_entry.type
  300. pybuffernd_struct = buffer_aux.buflocal_nd_var.cname
  301. flags = get_flags(buffer_aux, buffer_type)
  302. code.putln("{") # Set up necessary stack for getbuffer
  303. code.putln("__Pyx_BufFmt_StackElem __pyx_stack[%d];" % buffer_type.dtype.struct_nesting_depth())
  304. getbuffer = get_getbuffer_call(code, "%s", buffer_aux, buffer_type) # fill in object below
  305. if is_initialized:
  306. # Release any existing buffer
  307. code.putln('__Pyx_SafeReleaseBuffer(&%s.rcbuffer->pybuffer);' % pybuffernd_struct)
  308. # Acquire
  309. retcode_cname = code.funcstate.allocate_temp(PyrexTypes.c_int_type, manage_ref=False)
  310. code.putln("%s = %s;" % (retcode_cname, getbuffer % rhs_cname))
  311. code.putln('if (%s) {' % (code.unlikely("%s < 0" % retcode_cname)))
  312. # If acquisition failed, attempt to reacquire the old buffer
  313. # before raising the exception. A failure of reacquisition
  314. # will cause the reacquisition exception to be reported, one
  315. # can consider working around this later.
  316. exc_temps = tuple(code.funcstate.allocate_temp(PyrexTypes.py_object_type, manage_ref=False)
  317. for _ in range(3))
  318. code.putln('PyErr_Fetch(&%s, &%s, &%s);' % exc_temps)
  319. code.putln('if (%s) {' % code.unlikely("%s == -1" % (getbuffer % lhs_cname)))
  320. code.putln('Py_XDECREF(%s); Py_XDECREF(%s); Py_XDECREF(%s);' % exc_temps) # Do not refnanny these!
  321. code.globalstate.use_utility_code(raise_buffer_fallback_code)
  322. code.putln('__Pyx_RaiseBufferFallbackError();')
  323. code.putln('} else {')
  324. code.putln('PyErr_Restore(%s, %s, %s);' % exc_temps)
  325. code.putln('}')
  326. code.putln('%s = %s = %s = 0;' % exc_temps)
  327. for t in exc_temps:
  328. code.funcstate.release_temp(t)
  329. code.putln('}')
  330. # Unpack indices
  331. put_unpack_buffer_aux_into_scope(buf_entry, code)
  332. code.putln(code.error_goto_if_neg(retcode_cname, pos))
  333. code.funcstate.release_temp(retcode_cname)
  334. else:
  335. # Our entry had no previous value, so set to None when acquisition fails.
  336. # In this case, auxiliary vars should be set up right in initialization to a zero-buffer,
  337. # so it suffices to set the buf field to NULL.
  338. code.putln('if (%s) {' % code.unlikely("%s == -1" % (getbuffer % rhs_cname)))
  339. code.putln('%s = %s; __Pyx_INCREF(Py_None); %s.rcbuffer->pybuffer.buf = NULL;' %
  340. (lhs_cname,
  341. PyrexTypes.typecast(buffer_type, PyrexTypes.py_object_type, "Py_None"),
  342. pybuffernd_struct))
  343. code.putln(code.error_goto(pos))
  344. code.put('} else {')
  345. # Unpack indices
  346. put_unpack_buffer_aux_into_scope(buf_entry, code)
  347. code.putln('}')
  348. code.putln("}") # Release stack
  349. def put_buffer_lookup_code(entry, index_signeds, index_cnames, directives,
  350. pos, code, negative_indices, in_nogil_context):
  351. """
  352. Generates code to process indices and calculate an offset into
  353. a buffer. Returns a C string which gives a pointer which can be
  354. read from or written to at will (it is an expression so caller should
  355. store it in a temporary if it is used more than once).
  356. As the bounds checking can have any number of combinations of unsigned
  357. arguments, smart optimizations etc. we insert it directly in the function
  358. body. The lookup however is delegated to a inline function that is instantiated
  359. once per ndim (lookup with suboffsets tend to get quite complicated).
  360. entry is a BufferEntry
  361. """
  362. negative_indices = directives['wraparound'] and negative_indices
  363. if directives['boundscheck']:
  364. # Check bounds and fix negative indices.
  365. # We allocate a temporary which is initialized to -1, meaning OK (!).
  366. # If an error occurs, the temp is set to the index dimension the
  367. # error is occurring at.
  368. failed_dim_temp = code.funcstate.allocate_temp(PyrexTypes.c_int_type, manage_ref=False)
  369. code.putln("%s = -1;" % failed_dim_temp)
  370. for dim, (signed, cname, shape) in enumerate(zip(index_signeds, index_cnames, entry.get_buf_shapevars())):
  371. if signed != 0:
  372. # not unsigned, deal with negative index
  373. code.putln("if (%s < 0) {" % cname)
  374. if negative_indices:
  375. code.putln("%s += %s;" % (cname, shape))
  376. code.putln("if (%s) %s = %d;" % (
  377. code.unlikely("%s < 0" % cname),
  378. failed_dim_temp, dim))
  379. else:
  380. code.putln("%s = %d;" % (failed_dim_temp, dim))
  381. code.put("} else ")
  382. # check bounds in positive direction
  383. if signed != 0:
  384. cast = ""
  385. else:
  386. cast = "(size_t)"
  387. code.putln("if (%s) %s = %d;" % (
  388. code.unlikely("%s >= %s%s" % (cname, cast, shape)),
  389. failed_dim_temp, dim))
  390. if in_nogil_context:
  391. code.globalstate.use_utility_code(raise_indexerror_nogil)
  392. func = '__Pyx_RaiseBufferIndexErrorNogil'
  393. else:
  394. code.globalstate.use_utility_code(raise_indexerror_code)
  395. func = '__Pyx_RaiseBufferIndexError'
  396. code.putln("if (%s) {" % code.unlikely("%s != -1" % failed_dim_temp))
  397. code.putln('%s(%s);' % (func, failed_dim_temp))
  398. code.putln(code.error_goto(pos))
  399. code.putln('}')
  400. code.funcstate.release_temp(failed_dim_temp)
  401. elif negative_indices:
  402. # Only fix negative indices.
  403. for signed, cname, shape in zip(index_signeds, index_cnames, entry.get_buf_shapevars()):
  404. if signed != 0:
  405. code.putln("if (%s < 0) %s += %s;" % (cname, cname, shape))
  406. return entry.generate_buffer_lookup_code(code, index_cnames)
  407. def use_bufstruct_declare_code(env):
  408. env.use_utility_code(buffer_struct_declare_code)
  409. def buf_lookup_full_code(proto, defin, name, nd):
  410. """
  411. Generates a buffer lookup function for the right number
  412. of dimensions. The function gives back a void* at the right location.
  413. """
  414. # _i_ndex, _s_tride, sub_o_ffset
  415. macroargs = ", ".join(["i%d, s%d, o%d" % (i, i, i) for i in range(nd)])
  416. proto.putln("#define %s(type, buf, %s) (type)(%s_imp(buf, %s))" % (name, macroargs, name, macroargs))
  417. funcargs = ", ".join(["Py_ssize_t i%d, Py_ssize_t s%d, Py_ssize_t o%d" % (i, i, i) for i in range(nd)])
  418. proto.putln("static CYTHON_INLINE void* %s_imp(void* buf, %s);" % (name, funcargs))
  419. defin.putln(dedent("""
  420. static CYTHON_INLINE void* %s_imp(void* buf, %s) {
  421. char* ptr = (char*)buf;
  422. """) % (name, funcargs) + "".join([dedent("""\
  423. ptr += s%d * i%d;
  424. if (o%d >= 0) ptr = *((char**)ptr) + o%d;
  425. """) % (i, i, i, i) for i in range(nd)]
  426. ) + "\nreturn ptr;\n}")
  427. def buf_lookup_strided_code(proto, defin, name, nd):
  428. """
  429. Generates a buffer lookup function for the right number
  430. of dimensions. The function gives back a void* at the right location.
  431. """
  432. # _i_ndex, _s_tride
  433. args = ", ".join(["i%d, s%d" % (i, i) for i in range(nd)])
  434. offset = " + ".join(["i%d * s%d" % (i, i) for i in range(nd)])
  435. proto.putln("#define %s(type, buf, %s) (type)((char*)buf + %s)" % (name, args, offset))
  436. def buf_lookup_c_code(proto, defin, name, nd):
  437. """
  438. Similar to strided lookup, but can assume that the last dimension
  439. doesn't need a multiplication as long as.
  440. Still we keep the same signature for now.
  441. """
  442. if nd == 1:
  443. proto.putln("#define %s(type, buf, i0, s0) ((type)buf + i0)" % name)
  444. else:
  445. args = ", ".join(["i%d, s%d" % (i, i) for i in range(nd)])
  446. offset = " + ".join(["i%d * s%d" % (i, i) for i in range(nd - 1)])
  447. proto.putln("#define %s(type, buf, %s) ((type)((char*)buf + %s) + i%d)" % (name, args, offset, nd - 1))
  448. def buf_lookup_fortran_code(proto, defin, name, nd):
  449. """
  450. Like C lookup, but the first index is optimized instead.
  451. """
  452. if nd == 1:
  453. proto.putln("#define %s(type, buf, i0, s0) ((type)buf + i0)" % name)
  454. else:
  455. args = ", ".join(["i%d, s%d" % (i, i) for i in range(nd)])
  456. offset = " + ".join(["i%d * s%d" % (i, i) for i in range(1, nd)])
  457. proto.putln("#define %s(type, buf, %s) ((type)((char*)buf + %s) + i%d)" % (name, args, offset, 0))
  458. def use_py2_buffer_functions(env):
  459. env.use_utility_code(GetAndReleaseBufferUtilityCode())
  460. class GetAndReleaseBufferUtilityCode(object):
  461. # Emulation of PyObject_GetBuffer and PyBuffer_Release for Python 2.
  462. # For >= 2.6 we do double mode -- use the new buffer interface on objects
  463. # which has the right tp_flags set, but emulation otherwise.
  464. requires = None
  465. is_cython_utility = False
  466. def __init__(self):
  467. pass
  468. def __eq__(self, other):
  469. return isinstance(other, GetAndReleaseBufferUtilityCode)
  470. def __hash__(self):
  471. return 24342342
  472. def get_tree(self, **kwargs): pass
  473. def put_code(self, output):
  474. code = output['utility_code_def']
  475. proto_code = output['utility_code_proto']
  476. env = output.module_node.scope
  477. cython_scope = env.context.cython_scope
  478. # Search all types for __getbuffer__ overloads
  479. types = []
  480. visited_scopes = set()
  481. def find_buffer_types(scope):
  482. if scope in visited_scopes:
  483. return
  484. visited_scopes.add(scope)
  485. for m in scope.cimported_modules:
  486. find_buffer_types(m)
  487. for e in scope.type_entries:
  488. if isinstance(e.utility_code_definition, CythonUtilityCode):
  489. continue
  490. t = e.type
  491. if t.is_extension_type:
  492. if scope is cython_scope and not e.used:
  493. continue
  494. release = get = None
  495. for x in t.scope.pyfunc_entries:
  496. if x.name == u"__getbuffer__": get = x.func_cname
  497. elif x.name == u"__releasebuffer__": release = x.func_cname
  498. if get:
  499. types.append((t.typeptr_cname, get, release))
  500. find_buffer_types(env)
  501. util_code = TempitaUtilityCode.load(
  502. "GetAndReleaseBuffer", from_file="Buffer.c",
  503. context=dict(types=types))
  504. proto = util_code.format_code(util_code.proto)
  505. impl = util_code.format_code(
  506. util_code.inject_string_constants(util_code.impl, output)[1])
  507. proto_code.putln(proto)
  508. code.putln(impl)
  509. def mangle_dtype_name(dtype):
  510. # Use prefixes to separate user defined types from builtins
  511. # (consider "typedef float unsigned_int")
  512. if dtype.is_pyobject:
  513. return "object"
  514. elif dtype.is_ptr:
  515. return "ptr"
  516. else:
  517. if dtype.is_typedef or dtype.is_struct_or_union:
  518. prefix = "nn_"
  519. else:
  520. prefix = ""
  521. return prefix + dtype.specialization_name()
  522. def get_type_information_cname(code, dtype, maxdepth=None):
  523. """
  524. Output the run-time type information (__Pyx_TypeInfo) for given dtype,
  525. and return the name of the type info struct.
  526. Structs with two floats of the same size are encoded as complex numbers.
  527. One can separate between complex numbers declared as struct or with native
  528. encoding by inspecting to see if the fields field of the type is
  529. filled in.
  530. """
  531. namesuffix = mangle_dtype_name(dtype)
  532. name = "__Pyx_TypeInfo_%s" % namesuffix
  533. structinfo_name = "__Pyx_StructFields_%s" % namesuffix
  534. if dtype.is_error: return "<error>"
  535. # It's critical that walking the type info doesn't use more stack
  536. # depth than dtype.struct_nesting_depth() returns, so use an assertion for this
  537. if maxdepth is None: maxdepth = dtype.struct_nesting_depth()
  538. if maxdepth <= 0:
  539. assert False
  540. if name not in code.globalstate.utility_codes:
  541. code.globalstate.utility_codes.add(name)
  542. typecode = code.globalstate['typeinfo']
  543. arraysizes = []
  544. if dtype.is_array:
  545. while dtype.is_array:
  546. arraysizes.append(dtype.size)
  547. dtype = dtype.base_type
  548. complex_possible = dtype.is_struct_or_union and dtype.can_be_complex()
  549. declcode = dtype.empty_declaration_code()
  550. if dtype.is_simple_buffer_dtype():
  551. structinfo_name = "NULL"
  552. elif dtype.is_struct:
  553. struct_scope = dtype.scope
  554. if dtype.is_const:
  555. struct_scope = struct_scope.const_base_type_scope
  556. # Must pre-call all used types in order not to recurse during utility code writing.
  557. fields = struct_scope.var_entries
  558. assert len(fields) > 0
  559. types = [get_type_information_cname(code, f.type, maxdepth - 1)
  560. for f in fields]
  561. typecode.putln("static __Pyx_StructField %s[] = {" % structinfo_name, safe=True)
  562. for f, typeinfo in zip(fields, types):
  563. typecode.putln(' {&%s, "%s", offsetof(%s, %s)},' %
  564. (typeinfo, f.name, dtype.empty_declaration_code(), f.cname), safe=True)
  565. typecode.putln(' {NULL, NULL, 0}', safe=True)
  566. typecode.putln("};", safe=True)
  567. else:
  568. assert False
  569. rep = str(dtype)
  570. flags = "0"
  571. is_unsigned = "0"
  572. if dtype is PyrexTypes.c_char_type:
  573. is_unsigned = "IS_UNSIGNED(%s)" % declcode
  574. typegroup = "'H'"
  575. elif dtype.is_int:
  576. is_unsigned = "IS_UNSIGNED(%s)" % declcode
  577. typegroup = "%s ? 'U' : 'I'" % is_unsigned
  578. elif complex_possible or dtype.is_complex:
  579. typegroup = "'C'"
  580. elif dtype.is_float:
  581. typegroup = "'R'"
  582. elif dtype.is_struct:
  583. typegroup = "'S'"
  584. if dtype.packed:
  585. flags = "__PYX_BUF_FLAGS_PACKED_STRUCT"
  586. elif dtype.is_pyobject:
  587. typegroup = "'O'"
  588. else:
  589. assert False, dtype
  590. typeinfo = ('static __Pyx_TypeInfo %s = '
  591. '{ "%s", %s, sizeof(%s), { %s }, %s, %s, %s, %s };')
  592. tup = (name, rep, structinfo_name, declcode,
  593. ', '.join([str(x) for x in arraysizes]) or '0', len(arraysizes),
  594. typegroup, is_unsigned, flags)
  595. typecode.putln(typeinfo % tup, safe=True)
  596. return name
  597. def load_buffer_utility(util_code_name, context=None, **kwargs):
  598. if context is None:
  599. return UtilityCode.load(util_code_name, "Buffer.c", **kwargs)
  600. else:
  601. return TempitaUtilityCode.load(util_code_name, "Buffer.c", context=context, **kwargs)
  602. context = dict(max_dims=Options.buffer_max_dims)
  603. buffer_struct_declare_code = load_buffer_utility("BufferStructDeclare", context=context)
  604. buffer_formats_declare_code = load_buffer_utility("BufferFormatStructs")
  605. # Utility function to set the right exception
  606. # The caller should immediately goto_error
  607. raise_indexerror_code = load_buffer_utility("BufferIndexError")
  608. raise_indexerror_nogil = load_buffer_utility("BufferIndexErrorNogil")
  609. raise_buffer_fallback_code = load_buffer_utility("BufferFallbackError")
  610. acquire_utility_code = load_buffer_utility("BufferGetAndValidate", context=context)
  611. buffer_format_check_code = load_buffer_utility("BufferFormatCheck", context=context)
  612. # See utility code BufferFormatFromTypeInfo
  613. _typeinfo_to_format_code = load_buffer_utility("TypeInfoToFormat")