Debian Codegraph @

src/pygame_sdl2/rwobject.pyx

raw
1 # Copyright 2014 Tom Rothamel <[email protected]>
2 #
3 # This software is provided 'as-is', without any express or implied
4 # warranty. In no event will the authors be held liable for any damages
5 # arising from the use of this software.
6 #
7 # Permission is granted to anyone to use this software for any purpose,
8 # including commercial applications, and to alter it and redistribute it
9 # freely, subject to the following restrictions:
10 #
11 # 1. The origin of this software must not be misrepresented; you must not
12 # claim that you wrote the original software. If you use this software
13 # in a product, an acknowledgment in the product documentation would be
14 # appreciated but is not required.
15 # 2. Altered source versions must be plainly marked as such, and must not be
16 # misrepresented as being the original software.
17 # 3. This notice may not be removed or altered from any source distribution.
18
19 from sdl2 cimport *
20 from cpython.ref cimport Py_INCREF, Py_DECREF
21 from cpython.buffer cimport PyObject_CheckBuffer, PyObject_GetBuffer, PyBuffer_Release, PyBUF_CONTIG, PyBUF_CONTIG_RO
22 from libc.string cimport memcpy
23 from libc.stdio cimport FILE, fopen, fclose, fseek, ftell, fread, SEEK_SET, SEEK_CUR, SEEK_END
24 from libc.stdlib cimport calloc, free
25 from libc.stdint cimport uintptr_t
26
27 from pygame_sdl2.compat import file_type, bytes_, unicode_
28
29 import sys
30 import io
31
32 # The fsencoding.
33 fsencoding = sys.getfilesystemencoding() or "utf-8"
34
35 cdef extern from "SDL.h" nogil:
36 Sint64 SDL_RWtell(SDL_RWops* context)
37
38 Sint64 SDL_RWseek(SDL_RWops* context,
39 Sint64 offset,
40 int whence)
41
42 size_t SDL_RWread(SDL_RWops* context,
43 void* ptr,
44 size_t size,
45 size_t maxnum)
46
47 size_t SDL_RWwrite(SDL_RWops* context,
48 const void* ptr,
49 size_t size,
50 size_t num)
51
52 int SDL_RWclose(SDL_RWops* context)
53
54 SDL_RWops* SDL_RWFromFile(const char *file,
55 const char *mode)
56
57 cdef extern from "python_threads.h":
58 void init_python_threads()
59
60 cdef set_error(e):
61 cdef char *msg
62 e = str(e)
63 msg = <char *> e
64 SDL_SetError("%s", msg)
65
66 cdef Sint64 python_size(SDL_RWops *context) noexcept with gil:
67 f = <object> context.hidden.unknown.data1
68
69 try:
70 cur = f.tell()
71 f.seek(0, 2)
72 rv = f.tell()
73 f.seek(cur, 0)
74 except Exception as e:
75 return -1
76
77 return rv
78
79 cdef Sint64 python_seek(SDL_RWops *context, Sint64 seek, int whence) noexcept with gil:
80 f = <object> context.hidden.unknown.data1
81
82 try:
83 f.seek(seek, whence)
84 rv = f.tell()
85 except Exception as e:
86 set_error(e)
87 return -1
88
89 return rv
90
91 cdef size_t python_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum) noexcept with gil:
92 f = <object> context.hidden.unknown.data1
93
94 try:
95 data = f.read(size * maxnum)
96 except Exception as e:
97 set_error(e)
98 return -1
99
100 memcpy(ptr, <void *><char *> data, len(data))
101 return len(data)
102
103 cdef size_t python_write(SDL_RWops *context, const void *ptr, size_t size, size_t maxnum) noexcept with gil:
104 f = <object> context.hidden.unknown.data1
105 data = (<char *> ptr)[:size * maxnum]
106
107 try:
108 f.write(data)
109 except Exception as e:
110 set_error(e)
111 return -1
112
113 return len(data)
114
115 cdef int python_close(SDL_RWops *context) noexcept with gil:
116 if context != NULL:
117 if context.hidden.unknown.data1 != NULL:
118 f = <object> context.hidden.unknown.data1
119
120 try:
121 f.close()
122 except Exception as e:
123 set_error(e)
124 return -1
125
126 Py_DECREF(f)
127
128 context.hidden.unknown.data1 = NULL
129 SDL_FreeRW(context)
130 return 0
131
132 cdef struct SubFile:
133 SDL_RWops *rw
134 Sint64 base
135 Sint64 length
136 Sint64 tell
137
138 cdef Sint64 subfile_size(SDL_RWops *context) noexcept nogil:
139 cdef SubFile *sf = <SubFile *> context.hidden.unknown.data1
140 return sf.length
141
142 cdef Sint64 subfile_seek(SDL_RWops *context, Sint64 seek, int whence) noexcept nogil:
143 cdef SubFile *sf = <SubFile *> context.hidden.unknown.data1
144
145 if whence == RW_SEEK_SET:
146 sf.tell = SDL_RWseek(sf.rw, seek + sf.base, RW_SEEK_SET) - sf.base
147 elif whence == RW_SEEK_CUR:
148 sf.tell = SDL_RWseek(sf.rw, seek, RW_SEEK_CUR) - sf.base
149 elif whence == RW_SEEK_END:
150 sf.tell = SDL_RWseek(sf.rw, sf.base + sf.length + seek, RW_SEEK_SET) - sf.base
151
152 return sf.tell
153
154 cdef size_t subfile_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum) noexcept nogil:
155 cdef SubFile *sf = <SubFile *> context.hidden.unknown.data1
156
157 cdef Sint64 left = sf.length - sf.tell
158 cdef size_t rv
159
160 if size * maxnum > left:
161 maxnum = left // size
162
163 if maxnum == 0:
164 return 0
165
166 rv = SDL_RWread(sf.rw, ptr, size, maxnum)
167
168 if rv > 0:
169 sf.tell += size * rv
170
171 return rv
172
173 cdef int subfile_close(SDL_RWops *context) noexcept nogil:
174 cdef SubFile *sf
175
176 if context != NULL:
177 sf = <SubFile *> context.hidden.unknown.data1
178 if sf.rw != NULL:
179 SDL_RWclose(sf.rw)
180 if sf != NULL:
181 free(sf)
182 context.hidden.unknown.data1 = NULL
183 SDL_FreeRW(context)
184
185 return 0
186
187
188 cdef struct SplitFile:
189 SDL_RWops *a
190 SDL_RWops *b
191 Sint64 split
192 Sint64 tell
193
194 cdef Sint64 splitfile_size(SDL_RWops *context) noexcept nogil:
195 cdef SplitFile *sf = <SplitFile *> context.hidden.unknown.data1
196 cdef Sint64 rv
197
198 return SDL_RWsize(sf.a) + SDL_RWsize(sf.b)
199
200 cdef Sint64 splitfile_seek(SDL_RWops *context, Sint64 seek, int whence) noexcept nogil:
201 cdef SplitFile *sf = <SplitFile *> context.hidden.unknown.data1
202 cdef Sint64 rv
203
204 if whence == RW_SEEK_SET:
205 sf.tell = seek
206 elif whence == RW_SEEK_CUR:
207 sf.tell += seek
208 elif whence == RW_SEEK_END:
209 sf.tell = splitfile_size(context) + seek
210
211 if sf.tell < sf.split:
212 rv = SDL_RWseek(sf.a, sf.tell, RW_SEEK_SET)
213 SDL_RWseek(sf.b, 0, RW_SEEK_SET)
214 else:
215 SDL_RWseek(sf.a, sf.split, RW_SEEK_SET)
216 rv = SDL_RWseek(sf.b, sf.tell - sf.split, RW_SEEK_SET)
217
218 if rv < 0:
219 return rv
220 else:
221 return sf.tell
222
223 cdef size_t splitfile_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum) noexcept nogil:
224 cdef SplitFile *sf = <SplitFile *> context.hidden.unknown.data1
225 cdef Sint64 left = splitfile_size(context) - sf.tell
226 cdef size_t rv
227
228 cdef size_t total_read
229 cdef size_t left_read
230 cdef size_t right_read
231 cdef size_t ret
232
233 total_read = size * maxnum
234
235 left_read = min(total_read, sf.split - sf.tell)
236 left_read = max(left_read, 0)
237
238 if left_read > 0:
239 left_read = SDL_RWread(sf.a, ptr, 1, left_read)
240 if left_read < 0:
241 return left_read
242
243 right_read = total_read - left_read
244
245 if right_read > 0:
246 right_read = SDL_RWread(sf.b, <char *> ptr + left_read, 1, right_read)
247 if right_read < 0:
248 return right_read
249
250 sf.tell += left_read + right_read
251
252 return (left_read + right_read) // size
253
254 cdef int splitfile_close(SDL_RWops *context) noexcept nogil:
255 cdef SplitFile *sf
256
257 if context != NULL:
258 sf = <SplitFile *> context.hidden.unknown.data1
259 if sf.a != NULL:
260 SDL_RWclose(sf.a)
261 if sf.b != NULL:
262 SDL_RWclose(sf.b)
263 if sf != NULL:
264 free(sf)
265 context.hidden.unknown.data1 = NULL
266 SDL_FreeRW(context)
267
268 return 0
269
270
271 cdef struct BufFile:
272 Py_buffer view
273 Uint8 *base
274 Uint8 *here
275 Uint8 *stop
276
277 cdef Sint64 buffile_size(SDL_RWops *context) noexcept nogil:
278 cdef BufFile *bf = <BufFile *> context.hidden.unknown.data1
279
280 return bf.stop - bf.base
281
282 cdef Sint64 buffile_seek(SDL_RWops *context, Sint64 offset, int whence) noexcept nogil:
283 cdef BufFile *bf = <BufFile *> context.hidden.unknown.data1
284
285 cdef Uint8 *newpos
286
287 if whence == RW_SEEK_SET:
288 newpos = bf.base + offset
289 elif whence == RW_SEEK_CUR:
290 newpos = bf.here + offset
291 elif whence == RW_SEEK_END:
292 newpos = bf.stop + offset
293 else:
294 with gil:
295 set_error("Unknown value for 'whence'")
296 return -1
297 if newpos < bf.base:
298 newpos = bf.base
299 if newpos > bf.stop:
300 newpos = bf.stop
301 bf.here = newpos
302
303 return bf.here - bf.base
304
305 cdef size_t buffile_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum) noexcept nogil:
306 cdef BufFile *bf = <BufFile *> context.hidden.unknown.data1
307 cdef size_t total_bytes = 0
308 cdef size_t mem_available = 0
309
310 total_bytes = maxnum * size
311 if (maxnum == 0) or (size == 0) or ((total_bytes // maxnum) != size):
312 return 0
313
314 mem_available = bf.stop - bf.here
315 if total_bytes > mem_available:
316 total_bytes = mem_available
317
318 SDL_memcpy(ptr, bf.here, total_bytes)
319 bf.here += total_bytes
320
321 return (total_bytes // size)
322
323 cdef size_t buffile_write(SDL_RWops *context, const void *ptr, size_t size, size_t num) noexcept nogil:
324 cdef BufFile *bf = <BufFile *> context.hidden.unknown.data1
325
326 if bf.view.readonly != 0:
327 return 0
328
329 if (bf.here + (num * size)) > bf.stop:
330 num = (bf.stop - bf.here) // size
331 SDL_memcpy(bf.here, ptr, num * size)
332 bf.here += num * size
333
334 return num
335
336 cdef int buffile_close(SDL_RWops *context) noexcept with gil:
337 cdef BufFile *bf
338
339 if context != NULL:
340 bf = <BufFile *> context.hidden.unknown.data1
341 if bf != NULL:
342 PyBuffer_Release(&bf.view)
343 free(bf)
344 bf = NULL
345 SDL_FreeRW(context)
346
347 return 0
348
349
350 cdef SDL_RWops *to_rwops(filelike, mode="rb", base=None, length=None) except NULL:
351 """
352 This accepts, in order:
353
354 * An io.BufferedIOBase object, which is unwrapped to get the
355 underlying file object. (Which is then processed as below.)
356
357 * A RWopsIO object, which is closed and the underlying SDL_RWops
358 object is returned.
359
360 * A filename, which is opened.
361
362 * An object with the name, base, and length attributes. The name is
363 interpreted as a filename, and the base and length are used to
364 create a subfile.
365
366 * An object with a name fileld. The name is interpreted as a filename.
367 and opened. The object will be closed.
368
369 * An object that supports the buffer protocol.
370
371 * A file-like object, which is wrapped in a Python file-like object
372
373 It returns an SDL_RWops object, or NULL on error.
374 """
375
376 cdef FILE *f
377 cdef SubFile *sf
378 cdef SDL_RWops *rv
379 cdef SDL_RWops *rw
380 cdef char *cname
381 cdef char *cmode
382
383 cdef RWopsIOImpl rwopsio
384
385 if not isinstance(mode, bytes_):
386 mode = mode.encode("ascii")
387
388 name = filelike
389
390 # Handle turning BufferedIOBase and RWopsIO objects into their underlying
391 # objects.
392 try:
393 while True:
394 filelike = filelike.raw
395 except AttributeError:
396 pass
397
398 if isinstance(filelike, RWopsIOImpl):
399 rwopsio = <RWopsIOImpl> filelike
400 if not rwopsio.ops:
401 raise ValueError("I/O on closed file.")
402
403 rv = rwopsio.ops
404 rwopsio.ops = NULL
405 return rv
406
407 if isinstance(filelike, (file_type, io.IOBase)) and mode == "rb":
408 name = getattr(filelike, "name", None)
409
410 # Try to open as a file.
411 if isinstance(name, bytes_):
412 name = name.decode(fsencoding)
413 elif isinstance(name, unicode_):
414 pass
415 else:
416 name = None
417
418 if (mode == b"rb") and (name is not None):
419
420 dname = name.encode("utf-8")
421 cname = dname
422 cmode = mode
423
424 with nogil:
425 rv = SDL_RWFromFile(cname, cmode)
426
427 if rv == NULL:
428 raise IOError("Could not open {!r}: {}".format(name, SDL_GetError()))
429
430 if base is None and length is None:
431 try:
432 base = filelike.base
433 length = filelike.length
434 except AttributeError:
435 pass
436
437 if base is not None and length is not None:
438
439 # If we have these properties, we're either an APK asset or a Ren'Py-style
440 # subfile, so use an optimized path.
441
442 rw = rv
443
444 SDL_RWseek(rw, base, RW_SEEK_SET);
445
446 sf = <SubFile *> calloc(sizeof(SubFile), 1)
447 sf.rw = rw
448 sf.base = base
449 sf.length = length
450 sf.tell = 0
451
452 rv = SDL_AllocRW()
453 rv.size = subfile_size
454 rv.seek = subfile_seek
455 rv.read = subfile_read
456 rv.write = NULL
457 rv.close = subfile_close
458 rv.type = 0
459 rv.hidden.unknown.data1 = <void *> sf
460
461 try:
462 filelike.close()
463 except:
464 pass
465
466 return rv
467
468 if not (hasattr(filelike, "read") or hasattr(filelike, "write")):
469 raise IOError("{!r} is not a filename or file-like object.".format(filelike))
470
471 Py_INCREF(filelike)
472
473 rv = SDL_AllocRW()
474 rv.size = python_size
475 rv.seek = python_seek
476 rv.read = python_read
477 rv.write = python_write
478 rv.close = python_close
479 rv.type = 0
480 rv.hidden.unknown.data1 = <void *> filelike
481
482 return rv
483
484
485 whence_mapping = {
486 io.SEEK_SET : RW_SEEK_SET,
487 io.SEEK_CUR : RW_SEEK_CUR,
488 io.SEEK_END : RW_SEEK_END,
489 }
490
491 cdef class RWopsIOImpl:
492 """
493 This wraps an SDL_RWops object in a Python file-like object.
494 """
495
496 cdef SDL_RWops *ops
497 cdef public object name
498 cdef public object base
499 cdef public object length
500
501 def __dealloc__(self):
502 if self.ops != NULL:
503 SDL_RWclose(self.ops)
504 self.ops = NULL
505
506 def __init__(self, filelike, mode="rb", base=None, length=None, name=None):
507 """
508 Creates a new RWopsIO object. All parameter are passed to to_rwops
509 to create the SDL_RWops object.
510 """
511
512 if name is not None:
513 self.name = name
514 elif isinstance(filelike, basestring):
515 self.name = filelike
516 else:
517 self.name = getattr(filelike, "name", name)
518
519 self.base = base
520 self.length = length
521
522 if filelike is not None:
523 self.ops = to_rwops(filelike, mode, base, length)
524 else:
525 self.ops = NULL
526
527 def close(self):
528
529 # A closed file may be closed again.
530
531 if self.ops:
532 SDL_RWclose(self.ops)
533 self.ops = NULL
534
535 def is_closed(self):
536 return not self.ops
537
538 def seek(self, long long offset, whence=0):
539 cdef int whence_rw
540 cdef long long rv
541
542 if not self.ops:
543 raise ValueError("I/O operation on closed file.")
544
545 whence_rw = whence_mapping.get(whence, RW_SEEK_SET)
546
547 with nogil:
548 rv = SDL_RWseek(self.ops, offset, whence_rw)
549
550 if rv < 0:
551 raise IOError("Could not seek: {}".format(SDL_GetError()))
552
553 return rv
554
555 def tell(self):
556 cdef long long rv
557
558 with nogil:
559 rv = SDL_RWtell(self.ops)
560
561 return rv
562
563 def readinto(self, b):
564 cdef Py_buffer view
565 cdef long long rv = 0
566
567 if not self.ops:
568 raise ValueError("I/O operation on closed file.")
569
570 if not PyObject_CheckBuffer(b):
571 raise ValueError("Passed in object does not support buffer protocol")
572
573 try:
574 PyObject_GetBuffer(b, &view, PyBUF_CONTIG)
575
576 with nogil:
577 rv = SDL_RWread(self.ops, view.buf, 1, view.len)
578 finally:
579 PyBuffer_Release(&view)
580
581 if rv < 0:
582 raise IOError("Could not read: {}".format(SDL_GetError()))
583
584 return rv
585
586 def write(self, b):
587 cdef Py_buffer view
588 cdef long long rv = 0
589
590 if not self.ops:
591 raise ValueError("I/O operation on closed file.")
592
593 if not PyObject_CheckBuffer(b):
594 raise ValueError("Passed in object does not support buffer protocol")
595
596 try:
597 PyObject_GetBuffer(b, &view, PyBUF_CONTIG_RO)
598 with nogil:
599 rv = SDL_RWwrite(self.ops, view.buf, 1, view.len)
600 finally:
601 PyBuffer_Release(&view)
602
603 if rv < 0:
604 raise IOError("Could not write: {}".format(SDL_GetError()))
605
606 return rv
607
608
609 cdef api SDL_RWops *RWopsFromPython(filelike) except NULL:
610 return to_rwops(filelike, "rb", None, None)
611
612
613 class RWopsIO(io.RawIOBase):
614
615 def __init__(self, filelike, mode='rb', base=None, length=None, name=None):
616 """
617 Creates a new RWopsIO object. All parameter are passed to to_rwops
618 to create the SDL_RWops object.
619 """
620
621 io.RawIOBase.__init__(self)
622
623 self.raw = RWopsIOImpl(filelike, mode=mode, base=base, length=length, name=name)
624
625 self.close = self.raw.close
626 self.seek = self.raw.seek
627 self.tell = self.raw.tell
628 self.write = self.raw.write
629 self.readinto = self.raw.readinto
630
631 def __repr__(self):
632 if self.raw.base is not None:
633 return "<RWopsIO {!r} base={!r} length={!r}>".format(self.raw.name, self.raw.base, self.raw.length)
634 else:
635 return "<RWopsIO {!r}>".format(self.raw.name)
636
637 # Implemented class: io.IOBase
638
639 # close is taken from RWopsIOImpl.
640
641 @property
642 def closed(self):
643 return self.raw.is_closed()
644
645 def fileno(self):
646 raise OSError()
647
648 # inherited flush is used
649
650 # inherited isatty is used
651
652 def readable(self):
653 return True
654
655 # inherited readline is used
656
657 # inherited readlines is used
658
659 # seek is taken from RWopsIOImpl.
660
661 def seekable(self):
662 return True
663
664 # tell is taken from RWopsIOImpl.
665
666 def truncate(self, size=None):
667 raise OSError()
668
669 def writable(self):
670 return True
671
672 # inherited writelines is used
673
674 # inherited __del__ is used
675
676 # Implemented class: io.RawIOBase
677
678 # inherited read is used
679
680 # inherited readall is used
681
682 # readinto is taken from RWopsIOImpl.
683
684 # write is taken from RWopsIOImpl.
685
686 @staticmethod
687 def from_buffer(buffer, mode="rb", name=None):
688 """
689 Creates a new RWopsIO object from a buffer.
690 """
691
692 cdef BufFile *bf
693 cdef SDL_RWops *rw
694
695 if not PyObject_CheckBuffer(buffer):
696 raise ValueError("Passed in object does not support buffer protocol")
697
698 bf = <BufFile *> calloc(sizeof(BufFile), 1)
699 if bf == NULL:
700 raise MemoryError()
701
702 if PyObject_GetBuffer(buffer, &bf.view, PyBUF_CONTIG_RO) < 0:
703 free(bf)
704 raise ValueError("Could not get buffer.")
705
706 bf.base = <Uint8 *> bf.view.buf
707 bf.here = bf.base
708 bf.stop = bf.base + bf.view.len
709
710 rw = SDL_AllocRW()
711 rw.size = buffile_size
712 rw.seek = buffile_seek
713 rw.read = buffile_read
714 rw.write = buffile_write
715 rw.close = buffile_close
716 rw.type = 0
717 rw.hidden.unknown.data1 = <void *> bf
718
719 rv = RWopsIO(None, name=name)
720 (<RWopsIOImpl> rv.raw).ops = rw
721 return rv
722
723 @staticmethod
724 def from_split(a, b, name=None):
725 """
726 Creates a new RWopsIO object from two other RWopsIO objects,
727 representing the concatenation of the two.
728 """
729
730 cdef SplitFile *sf
731 cdef SDL_RWops *rw
732
733 sf = <SplitFile *> calloc(sizeof(SplitFile), 1)
734 if sf == NULL:
735 raise MemoryError()
736
737 sf.a = to_rwops(a)
738 sf.b = to_rwops(b)
739 sf.split = SDL_RWsize(sf.a)
740 sf.tell = 0
741
742 rw = SDL_AllocRW()
743 rw.size = splitfile_size
744 rw.seek = splitfile_seek
745 rw.read = splitfile_read
746 rw.write = NULL
747 rw.close = splitfile_close
748 rw.type = 0
749 rw.hidden.unknown.data1 = <void *> sf
750
751 rv = RWopsIO(None, name=name)
752 (<RWopsIOImpl> rv.raw).ops = rw
753 return rv
754
755
756
757 init_python_threads()