forked from pygame-community/pygame-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.c
2498 lines (2277 loc) · 66.9 KB
/
base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
pygame-ce - Python Game Library
Copyright (C) 2000-2001 Pete Shinners
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Pete Shinners
pete@shinners.org
*/
#define NO_PYGAME_C_API
#define PYGAMEAPI_BASE_INTERNAL
#include "pygame.h"
#include <signal.h>
#include "doc/pygame_doc.h"
#include "pgarrinter.h"
#include "pgcompat.h"
/* This file controls all the initialization of
* the module and the various SDL subsystems
*/
/*platform specific init stuff*/
#ifdef MS_WIN32 /*python gives us MS_WIN32*/
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>
extern int
SDL_RegisterApp(const char *, Uint32, void *);
#endif
#if defined(macintosh)
#if (!defined(__MWERKS__) && !TARGET_API_MAC_CARBON)
QDGlobals pg_qd;
#endif
#endif
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
#define PAI_MY_ENDIAN '<'
#define PAI_OTHER_ENDIAN '>'
#define BUF_OTHER_ENDIAN '>'
#else
#define PAI_MY_ENDIAN '>'
#define PAI_OTHER_ENDIAN '<'
#define BUF_OTHER_ENDIAN '<'
#endif
#define BUF_MY_ENDIAN '='
/* Extended array struct */
typedef struct pg_capsule_interface_s {
PyArrayInterface inter;
Py_intptr_t imem[1];
} pgCapsuleInterface;
/* Py_buffer internal data for an array interface/struct */
typedef struct pg_view_internals_s {
char format[4]; /* make 4 byte word sized */
Py_ssize_t imem[1];
} pgViewInternals;
/* Custom exceptions */
static PyObject *pgExc_BufferError = NULL;
/* Only one instance of the state per process. */
static PyObject *pg_quit_functions = NULL;
static int pg_is_init = 0;
static int pg_sdl_was_init = 0;
SDL_Window *pg_default_window = NULL;
pgSurfaceObject *pg_default_screen = NULL;
static int pg_env_blend_alpha_SDL2 = 0;
static void
pg_install_parachute(void);
static void
pg_uninstall_parachute(void);
static void
pg_atexit_quit(void);
static int
pgGetArrayStruct(PyObject *, PyObject **, PyArrayInterface **);
static PyObject *
pgArrayStruct_AsDict(PyArrayInterface *);
static PyObject *
pgBuffer_AsArrayInterface(Py_buffer *);
static PyObject *
pgBuffer_AsArrayStruct(Py_buffer *);
static int
_pg_buffer_is_byteswapped(Py_buffer *);
static void
pgBuffer_Release(pg_buffer *);
static int
pgObject_GetBuffer(PyObject *, pg_buffer *, int);
static inline PyObject *
pgObject_getRectHelper(PyObject *, PyObject *const *, Py_ssize_t, PyObject *,
char *);
static int
pgGetArrayInterface(PyObject **, PyObject *);
static int
pgArrayStruct_AsBuffer(pg_buffer *, PyObject *, PyArrayInterface *, int);
static int
_pg_arraystruct_as_buffer(Py_buffer *, PyObject *, PyArrayInterface *, int);
static int
_pg_arraystruct_to_format(char *, PyArrayInterface *, int);
static int
pgDict_AsBuffer(pg_buffer *, PyObject *, int);
static int
_pg_shape_check(PyObject *);
static int
_pg_typestr_check(PyObject *);
static int
_pg_strides_check(PyObject *);
static int
_pg_data_check(PyObject *);
static int
_pg_is_int_tuple(PyObject *);
static int
_pg_values_as_buffer(Py_buffer *, int, PyObject *, PyObject *, PyObject *,
PyObject *);
static int
_pg_int_tuple_as_ssize_arr(PyObject *, Py_ssize_t *);
static int
_pg_typestr_as_format(PyObject *, char *, Py_ssize_t *);
static PyObject *
pg_view_get_typestr_obj(Py_buffer *);
static PyObject *
pg_view_get_shape_obj(Py_buffer *);
static PyObject *
pg_view_get_strides_obj(Py_buffer *);
static PyObject *
pg_view_get_data_obj(Py_buffer *);
static char
_pg_as_arrayinter_typekind(Py_buffer *);
static char
_pg_as_arrayinter_byteorder(Py_buffer *);
static int
_pg_as_arrayinter_flags(Py_buffer *);
static pgCapsuleInterface *
_pg_new_capsuleinterface(Py_buffer *);
static void
_pg_capsule_PyMem_Free(PyObject *);
static PyObject *
_pg_shape_as_tuple(PyArrayInterface *);
static PyObject *
_pg_typekind_as_str(PyArrayInterface *);
static PyObject *
_pg_strides_as_tuple(PyArrayInterface *);
static PyObject *
_pg_data_as_tuple(PyArrayInterface *);
static PyObject *
pg_get_array_interface(PyObject *, PyObject *);
static void
_pg_release_buffer_array(Py_buffer *);
static void
_pg_release_buffer_generic(Py_buffer *);
static SDL_Window *
pg_GetDefaultWindow(void);
static void
pg_SetDefaultWindow(SDL_Window *);
static pgSurfaceObject *
pg_GetDefaultWindowSurface(void);
static void
pg_SetDefaultWindowSurface(pgSurfaceObject *);
static int
pg_EnvShouldBlendAlphaSDL2(void);
/* compare compiled to linked, raise python error on incompatibility */
static int
pg_CheckSDLVersions(void)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
int compiled = SDL_VERSION;
int linked = SDL_GetVersion();
#else
SDL_version compiled;
SDL_version linked;
SDL_VERSION(&compiled);
SDL_GetVersion(&linked);
#endif
/* only check the major version, in general major version is bumped for ABI
* incompatible changes */
if (PG_FIND_VNUM_MAJOR(compiled) != PG_FIND_VNUM_MAJOR(linked)) {
PyErr_Format(PyExc_RuntimeError,
"ABI incompatibility detected! SDL compiled with "
"%d.%d.%d, linked to %d.%d.%d (major versions should "
"have matched)",
PG_FIND_VNUM_MAJOR(compiled),
PG_FIND_VNUM_MINOR(compiled),
PG_FIND_VNUM_MICRO(compiled), PG_FIND_VNUM_MAJOR(linked),
PG_FIND_VNUM_MINOR(linked), PG_FIND_VNUM_MICRO(linked));
return 0;
}
/* Basically, this is compiled_version > linked_version case, which we
* don't allow */
if ((PG_FIND_VNUM_MINOR(linked) == PG_FIND_VNUM_MINOR(compiled) &&
PG_FIND_VNUM_MICRO(linked) < PG_FIND_VNUM_MICRO(compiled)) ||
PG_FIND_VNUM_MINOR(linked) < PG_FIND_VNUM_MINOR(compiled)) {
/* We do some ifdefs to support different SDL versions at compile time.
We use newer API only when available.
Downgrading via dynamic API probably breaks this.*/
PyErr_Format(PyExc_RuntimeError,
"Dynamic linking causes SDL downgrade! (compiled with "
"version %d.%d.%d, linked to %d.%d.%d)",
PG_FIND_VNUM_MAJOR(compiled),
PG_FIND_VNUM_MINOR(compiled),
PG_FIND_VNUM_MICRO(compiled), PG_FIND_VNUM_MAJOR(linked),
PG_FIND_VNUM_MINOR(linked), PG_FIND_VNUM_MICRO(linked));
return 0;
}
return 1;
}
void
pg_RegisterQuit(void (*func)(void))
{
if (!pg_quit_functions) {
pg_quit_functions = PyList_New(0);
if (!pg_quit_functions) {
return;
}
}
if (func) {
PyObject *obj = PyCapsule_New(func, "quit", NULL);
if (!obj) {
return;
}
/* There is no difference between success and error
for PyList_Append in this case */
(void)PyList_Append(pg_quit_functions, obj);
Py_DECREF(obj);
}
}
static PyObject *
pg_register_quit(PyObject *self, PyObject *value)
{
if (!pg_quit_functions) {
pg_quit_functions = PyList_New(0);
if (!pg_quit_functions) {
return NULL;
}
}
if (0 != PyList_Append(pg_quit_functions, value)) {
return NULL; /* Exception already set */
}
Py_RETURN_NONE;
}
/* init pygame modules, returns 1 if successful, 0 if fail, with PyErr set*/
static int
pg_mod_autoinit(const char *modname)
{
PyObject *module, *funcobj, *temp;
int ret = 0;
module = PyImport_ImportModule(modname);
if (!module)
return 0;
funcobj = PyObject_GetAttrString(module, "_internal_mod_init");
/* If we could not load _internal_mod_init, load init function */
if (!funcobj) {
PyErr_Clear();
funcobj = PyObject_GetAttrString(module, "init");
}
if (funcobj) {
temp = PyObject_CallNoArgs(funcobj);
if (temp) {
Py_DECREF(temp);
ret = 1;
}
}
Py_DECREF(module);
Py_XDECREF(funcobj);
return ret;
}
/* try to quit pygame modules, errors silenced */
static void
pg_mod_autoquit(const char *modname)
{
PyObject *module, *funcobj, *temp;
module = PyImport_ImportModule(modname);
if (!module) {
PyErr_Clear();
return;
}
funcobj = PyObject_GetAttrString(module, "_internal_mod_quit");
/* If we could not load _internal_mod_quit, load quit function */
if (!funcobj)
funcobj = PyObject_GetAttrString(module, "quit");
/* Silence errors */
if (PyErr_Occurred())
PyErr_Clear();
if (funcobj) {
temp = PyObject_CallNoArgs(funcobj);
Py_XDECREF(temp);
}
/* Silence errors */
if (PyErr_Occurred())
PyErr_Clear();
Py_DECREF(module);
Py_XDECREF(funcobj);
}
static PyObject *
pg_init(PyObject *self, PyObject *_null)
{
int i = 0, success = 0, fail = 0;
/* Put all the module names we want to init in this array */
const char *modnames[] = {
IMPPREFIX "display", /* Display first, this also inits event,time */
IMPPREFIX "joystick", IMPPREFIX "font", IMPPREFIX "freetype",
IMPPREFIX "mixer",
/* IMPPREFIX "_sdl2.controller", Is this required? Comment for now*/
NULL};
/*nice to initialize timer, so startup time will reflec pg_init() time*/
#if defined(WITH_THREAD) && !defined(MS_WIN32) && defined(SDL_INIT_EVENTTHREAD)
pg_sdl_was_init = SDL_Init(SDL_INIT_EVENTTHREAD | PG_INIT_TIMER |
PG_INIT_NOPARACHUTE) == 0;
#else
pg_sdl_was_init = SDL_Init(PG_INIT_TIMER | PG_INIT_NOPARACHUTE) == 0;
#endif
pg_env_blend_alpha_SDL2 = SDL_getenv("PYGAME_BLEND_ALPHA_SDL2") != NULL;
/* initialize all pygame modules */
for (i = 0; modnames[i]; i++) {
if (pg_mod_autoinit(modnames[i]))
success++;
else {
/* ImportError is neither counted as success nor failure */
if (!PyErr_ExceptionMatches(PyExc_ImportError))
fail++;
PyErr_Clear();
}
}
pg_is_init = 1;
return pg_tuple_couple_from_values_int(success, fail);
}
static void
pg_atexit_quit(void)
{
/* Maybe it is safe to call SDL_quit more than once after an SDL_Init,
but this is undocumented. So play it safe and only call after a
successful SDL_Init.
*/
if (pg_sdl_was_init) {
pg_sdl_was_init = 0;
SDL_Quit();
}
}
static PyObject *
pg_get_sdl_version(PyObject *self, PyObject *args, PyObject *kwargs)
{
int linked = 1; /* Default is linked version. */
#if SDL_VERSION_ATLEAST(3, 0, 0)
int version = SDL_VERSION;
#else
SDL_version version;
SDL_VERSION(&version);
#endif
static char *keywords[] = {"linked", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|p", keywords, &linked)) {
return NULL; /* Exception already set. */
}
if (linked) {
#if SDL_VERSION_ATLEAST(3, 0, 0)
version = SDL_GetVersion();
#else
SDL_GetVersion(&version);
#endif
}
return Py_BuildValue("iii", PG_FIND_VNUM_MAJOR(version),
PG_FIND_VNUM_MINOR(version),
PG_FIND_VNUM_MICRO(version));
}
static PyObject *
pg_get_sdl_byteorder(PyObject *self, PyObject *_null)
{
return PyLong_FromLong(SDL_BYTEORDER);
}
static void
_pg_quit(void)
{
Py_ssize_t num, i;
PyObject *quit, *privatefuncs, *temp;
/* Put all the module names we want to quit in this array */
const char *modnames[] = {
/* IMPPREFIX "_sdl2.controller", Is this required?, comment for now */
IMPPREFIX "mixer",
IMPPREFIX "freetype",
IMPPREFIX "font",
IMPPREFIX "joystick",
IMPPREFIX "display", /* Display last, this also quits event,time */
NULL};
if (pg_quit_functions) {
privatefuncs = pg_quit_functions;
pg_quit_functions = NULL;
pg_uninstall_parachute(); /* Is this done here, or can it be done
below? */
num = PyList_Size(privatefuncs);
/*quit funcs in reverse order*/
while (num--) {
quit = PyList_GET_ITEM(privatefuncs, num);
if (!quit) {
PyErr_Clear();
continue;
}
if (PyCallable_Check(quit)) {
temp = PyObject_CallNoArgs(quit);
if (temp)
Py_DECREF(temp);
else
PyErr_Clear();
}
else if (PyCapsule_CheckExact(quit)) {
void *ptr = PyCapsule_GetPointer(quit, "quit");
(*(void (*)(void))ptr)();
}
}
Py_DECREF(privatefuncs);
}
/* quit all pygame modules */
for (i = 0; modnames[i]; i++) {
pg_mod_autoquit(modnames[i]);
}
/* Because quit never errors */
if (PyErr_Occurred())
PyErr_Clear();
pg_is_init = 0;
/* Release the GIL here, because the timer thread cleanups should happen
* without deadlocking. */
Py_BEGIN_ALLOW_THREADS;
pg_atexit_quit();
Py_END_ALLOW_THREADS;
}
static PyObject *
pg_quit(PyObject *self, PyObject *_null)
{
_pg_quit();
Py_RETURN_NONE;
}
static PyObject *
pg_base_get_init(PyObject *self, PyObject *_null)
{
return PyBool_FromLong(pg_is_init);
}
/* internal C API utility functions */
static int
pg_IntFromObj(PyObject *obj, int *val)
{
if (PyFloat_Check(obj)) {
/* Python3.8 complains with deprecation warnings if we pass
* floats to PyLong_AsLong.
*/
*val = (int)PyFloat_AS_DOUBLE(obj);
}
else {
*val = PyLong_AsLong(obj);
if (*val == -1 && PyErr_Occurred()) {
PyErr_Clear();
return 0;
}
}
return 1;
}
static int
pg_IntFromObjIndex(PyObject *obj, int _index, int *val)
{
int result = 0;
PyObject *item = PySequence_GetItem(obj, _index);
if (!item) {
PyErr_Clear();
return 0;
}
result = pg_IntFromObj(item, val);
Py_DECREF(item);
return result;
}
static int
pg_TwoIntsFromObj(PyObject *obj, int *val1, int *val2)
{
// First, lets check the size. This returns -1 if invalid and may set an
// error.
Py_ssize_t obj_size = PySequence_Size(obj);
// If the object is a tuple of one element, try that one element.
if (obj_size == 1 && PyTuple_Check(obj)) {
return pg_TwoIntsFromObj(PyTuple_GET_ITEM(obj, 0), val1, val2);
}
// Otherwise lets make sure this is a legit sequence and has two elements.
// Some objects can passing PySequence_Size but fail PySequence_Check
// (like sets)
if (obj_size != 2 || !PySequence_Check(obj)) {
PyErr_Clear(); // Clear the potential error from PySequence_Size
return 0;
}
// Now we can extract the items, using this macro because we know
// obj is a PySequence.
PyObject *item1 = PySequence_ITEM(obj, 0);
PyObject *item2 = PySequence_ITEM(obj, 1);
// If either item is NULL lets get out of here
if (item1 == NULL || item2 == NULL) {
Py_XDECREF(item1);
Py_XDECREF(item2);
PyErr_Clear();
return 0;
}
// Fastest way to extract numbers I tested (in Python 3.13) is to extract
// Python floats as doubles with the below macro, and get everything else
// through PyLong_AsLong, using C casting to turn into the final type.
if (PyFloat_Check(item1)) {
*val1 = (int)PyFloat_AS_DOUBLE(item1);
}
else {
*val1 = PyLong_AsLong(item1);
}
if (PyFloat_Check(item2)) {
*val2 = (int)PyFloat_AS_DOUBLE(item2);
}
else {
*val2 = PyLong_AsLong(item2);
}
// This catches the case where either of the PyLong_AsLong's failed
if ((*val1 == -1 || *val2 == -1) && PyErr_Occurred()) {
PyErr_Clear();
Py_DECREF(item1);
Py_DECREF(item2);
return 0;
}
Py_DECREF(item1);
Py_DECREF(item2);
return 1;
}
static int
pg_FloatFromObj(PyObject *obj, float *val)
{
if (PyFloat_Check(obj)) {
*val = (float)PyFloat_AS_DOUBLE(obj);
}
else {
*val = (float)PyLong_AsLong(obj);
if (*val == -1.0f && PyErr_Occurred()) {
PyErr_Clear();
return 0;
}
}
return 1;
}
static int
pg_FloatFromObjIndex(PyObject *obj, int _index, float *val)
{
int result = 0;
PyObject *item = PySequence_GetItem(obj, _index);
if (!item) {
PyErr_Clear();
return 0;
}
result = pg_FloatFromObj(item, val);
Py_DECREF(item);
return result;
}
static int
pg_TwoFloatsFromObj(PyObject *obj, float *val1, float *val2)
{
// First, lets check the size. This returns -1 if invalid and may set an
// error.
Py_ssize_t obj_size = PySequence_Size(obj);
// If the object is a tuple of one element, try that one element.
if (obj_size == 1 && PyTuple_Check(obj)) {
return pg_TwoFloatsFromObj(PyTuple_GET_ITEM(obj, 0), val1, val2);
}
// Otherwise lets make sure this is a legit sequence and has two elements.
// Some objects can passing PySequence_Size but fail PySequence_Check
// (like sets)
if (obj_size != 2 || !PySequence_Check(obj)) {
PyErr_Clear(); // Clear the potential error from PySequence_Size
return 0;
}
// Now we can extract the items, using this macro because we know
// obj is a PySequence.
PyObject *item1 = PySequence_ITEM(obj, 0);
PyObject *item2 = PySequence_ITEM(obj, 1);
// If either item is NULL lets get out of here
if (item1 == NULL || item2 == NULL) {
Py_XDECREF(item1);
Py_XDECREF(item2);
PyErr_Clear();
return 0;
}
// Fastest way to extract numbers I tested (in Python 3.13) is to extract
// Python floats as doubles with the below macro, and get everything else
// through PyLong_AsLong, using C casting to turn into the final type.
if (PyFloat_Check(item1)) {
*val1 = (float)PyFloat_AS_DOUBLE(item1);
}
else {
*val1 = (float)PyLong_AsLong(item1);
}
if (PyFloat_Check(item2)) {
*val2 = (float)PyFloat_AS_DOUBLE(item2);
}
else {
*val2 = (float)PyLong_AsLong(item2);
}
// This catches the case where either of the PyLong_AsLong's failed
if ((*val1 == -1.0f || *val2 == -1.0f) && PyErr_Occurred()) {
PyErr_Clear();
Py_DECREF(item1);
Py_DECREF(item2);
return 0;
}
Py_DECREF(item1);
Py_DECREF(item2);
return 1;
}
static inline int
pg_DoubleFromObj(PyObject *obj, double *val)
{
if (PyFloat_Check(obj)) {
*val = PyFloat_AS_DOUBLE(obj);
return 1;
}
*val = (double)PyLong_AsLong(obj);
if (PyErr_Occurred()) {
PyErr_Clear();
return 0;
}
return 1;
}
/*Assumes obj is a Sequence, internal or conscious use only*/
static inline int
_pg_DoubleFromObjIndex(PyObject *obj, int index, double *val)
{
int result = 0;
PyObject *item = PySequence_ITEM(obj, index);
if (!item) {
PyErr_Clear();
return 0;
}
result = pg_DoubleFromObj(item, val);
Py_DECREF(item);
return result;
}
static inline int
pg_TwoDoublesFromObj(PyObject *obj, double *val1, double *val2)
{
Py_ssize_t length;
/*Faster path for tuples and lists*/
if (pgSequenceFast_Check(obj)) {
length = PySequence_Fast_GET_SIZE(obj);
PyObject **f_arr = PySequence_Fast_ITEMS(obj);
if (length == 2) {
if (!pg_DoubleFromObj(f_arr[0], val1) ||
!pg_DoubleFromObj(f_arr[1], val2)) {
return 0;
}
}
else if (length == 1) {
/* Handle case of ((x, y), ) 'nested sequence' */
return pg_TwoDoublesFromObj(f_arr[0], val1, val2);
}
else {
return 0;
}
}
else if (PySequence_Check(obj)) {
length = PySequence_Length(obj);
if (length == 2) {
if (!_pg_DoubleFromObjIndex(obj, 0, val1)) {
return 0;
}
if (!_pg_DoubleFromObjIndex(obj, 1, val2)) {
return 0;
}
}
else if (length == 1 && !PyUnicode_Check(obj)) {
/* Handle case of ((x, y), ) 'nested sequence' */
PyObject *tmp = PySequence_ITEM(obj, 0);
int ret = pg_TwoDoublesFromObj(tmp, val1, val2);
Py_DECREF(tmp);
return ret;
}
else {
PyErr_Clear();
return 0;
}
}
else {
return 0;
}
return 1;
}
static inline int
pg_TwoDoublesFromFastcallArgs(PyObject *const *args, Py_ssize_t nargs,
double *val1, double *val2)
{
if (nargs == 1 && pg_TwoDoublesFromObj(args[0], val1, val2)) {
return 1;
}
else if (nargs == 2 && pg_DoubleFromObj(args[0], val1) &&
pg_DoubleFromObj(args[1], val2)) {
return 1;
}
return 0;
}
static int
pg_UintFromObj(PyObject *obj, Uint32 *val)
{
if (PyNumber_Check(obj)) {
PyObject *longobj;
if (!(longobj = PyNumber_Long(obj))) {
PyErr_Clear();
return 0;
}
*val = (Uint32)PyLong_AsUnsignedLong(longobj);
Py_DECREF(longobj);
if (PyErr_Occurred()) {
PyErr_Clear();
return 0;
}
return 1;
}
return 0;
}
static int
pg_UintFromObjIndex(PyObject *obj, int _index, Uint32 *val)
{
int result = 0;
PyObject *item = PySequence_GetItem(obj, _index);
if (!item) {
PyErr_Clear();
return 0;
}
result = pg_UintFromObj(item, val);
Py_DECREF(item);
return result;
}
/* You probably want to use the pg_RGBAFromObjEx function instead of this. */
static int
pg_RGBAFromObj(PyObject *obj, Uint8 *RGBA)
{
Py_ssize_t length;
Uint32 val;
if (PyTuple_Check(obj) && PyTuple_Size(obj) == 1) {
return pg_RGBAFromObj(PyTuple_GET_ITEM(obj, 0), RGBA);
}
if (!PySequence_Check(obj)) {
return 0;
}
length = PySequence_Length(obj);
if (length < 3 || length > 4) {
return 0;
}
if (!pg_UintFromObjIndex(obj, 0, &val) || val > 255) {
return 0;
}
RGBA[0] = (Uint8)val;
if (!pg_UintFromObjIndex(obj, 1, &val) || val > 255) {
return 0;
}
RGBA[1] = (Uint8)val;
if (!pg_UintFromObjIndex(obj, 2, &val) || val > 255) {
return 0;
}
RGBA[2] = (Uint8)val;
if (length == 4) {
if (!pg_UintFromObjIndex(obj, 3, &val) || val > 255) {
return 0;
}
RGBA[3] = (Uint8)val;
}
else {
RGBA[3] = (Uint8)255;
}
return 1;
}
static PyObject *
pg_get_error(PyObject *self, PyObject *_null)
{
return PyUnicode_FromString(SDL_GetError());
}
static PyObject *
pg_set_error(PyObject *s, PyObject *args)
{
char *errstring = NULL;
#if defined(PYPY_VERSION)
if (!PyArg_ParseTuple(args, "es", "UTF-8", &errstring))
return NULL;
SDL_SetError("%s", errstring);
PyMem_Free(errstring);
#else
if (!PyArg_ParseTuple(args, "s", &errstring)) {
return NULL;
}
SDL_SetError("%s", errstring);
#endif
Py_RETURN_NONE;
}
/*array interface*/
static int
pgGetArrayStruct(PyObject *obj, PyObject **cobj_p, PyArrayInterface **inter_p)
{
PyObject *cobj = PyObject_GetAttrString(obj, "__array_struct__");
PyArrayInterface *inter = NULL;
if (cobj == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
PyErr_SetString(PyExc_ValueError, "no C-struct array interface");
}
return -1;
}
if (PyCapsule_IsValid(cobj, NULL)) {
inter = (PyArrayInterface *)PyCapsule_GetPointer(cobj, NULL);
}
if (inter == NULL || inter->two != 2 /* conditional or */) {
Py_DECREF(cobj);
PyErr_SetString(PyExc_ValueError, "invalid array interface");
return -1;
}
*cobj_p = cobj;
*inter_p = inter;
return 0;
}
static PyObject *
pgArrayStruct_AsDict(PyArrayInterface *inter_p)
{
PyObject *dictobj = Py_BuildValue("{sisNsNsNsN}", "version", (int)3,
"typestr", _pg_typekind_as_str(inter_p),
"shape", _pg_shape_as_tuple(inter_p),
"strides", _pg_strides_as_tuple(inter_p),
"data", _pg_data_as_tuple(inter_p));
if (!dictobj) {
return 0;
}
if (inter_p->flags & PAI_ARR_HAS_DESCR) {
if (!inter_p->descr) {
Py_DECREF(dictobj);
PyErr_SetString(PyExc_ValueError,
"Array struct has descr flag set"
" but no descriptor");
return 0;
}
if (PyDict_SetItemString(dictobj, "descr", inter_p->descr)) {
Py_DECREF(dictobj);
return 0;
}
}
return dictobj;
}
static PyObject *
pgBuffer_AsArrayInterface(Py_buffer *view_p)
{
return Py_BuildValue("{sisNsNsNsN}", "version", (int)3, "typestr",
pg_view_get_typestr_obj(view_p), "shape",
pg_view_get_shape_obj(view_p), "strides",
pg_view_get_strides_obj(view_p), "data",
pg_view_get_data_obj(view_p));
}
static PyObject *
pgBuffer_AsArrayStruct(Py_buffer *view_p)
{
void *cinter_p = _pg_new_capsuleinterface(view_p);
PyObject *capsule;
if (!cinter_p) {
return 0;
}
capsule = PyCapsule_New(cinter_p, 0, _pg_capsule_PyMem_Free);
if (!capsule) {
PyMem_Free(cinter_p);
return 0;
}
return capsule;
}
static pgCapsuleInterface *
_pg_new_capsuleinterface(Py_buffer *view_p)
{
int ndim = view_p->ndim;
Py_ssize_t cinter_size;
pgCapsuleInterface *cinter_p;
int i;
cinter_size =
(sizeof(pgCapsuleInterface) + sizeof(Py_intptr_t) * (2 * ndim - 1));
cinter_p = (pgCapsuleInterface *)PyMem_Malloc(cinter_size);
if (!cinter_p) {
PyErr_NoMemory();
return 0;
}
cinter_p->inter.two = 2;
cinter_p->inter.nd = ndim;
cinter_p->inter.typekind = _pg_as_arrayinter_typekind(view_p);
cinter_p->inter.itemsize = (int)view_p->itemsize;
cinter_p->inter.flags = _pg_as_arrayinter_flags(view_p);
if (view_p->shape) {
cinter_p->inter.shape = cinter_p->imem;
for (i = 0; i < ndim; ++i) {
cinter_p->inter.shape[i] = (Py_intptr_t)view_p->shape[i];