Issue #286 resolved

For Python 3.4, Color arithmetic does not argument type checking

Lenard Lindstrom
created an issue
Python 3.4.3 (default, Oct 14 2015, 20:33:09) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>> c = pygame.Color('grey50')
>>> c
(127, 127, 127, 255)
>>> c + 100
(128, 127, 127, 255)
>>> 100 + c
(128, 127, 127, 255)
>>> c + None
(128, 127, 127, 255)
>>> c + pygame.Color(100, 0, 0, 0)
(227, 127, 127, 255)
>>> 

For Python 2.7, an exception is raised as expected:

Python 2.7.6 (default, Jun 22 2015, 18:00:18) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>> c = pygame.Color('grey50')
>>> c
(127, 127, 127, 255)
>>> c + 100
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'pygame.Color' and 'int'
>>> 100 + c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'pygame.Color'
>>> 

The Color type relies on coercion for type checking in arithmetic operations. This works in Python 2.x, but coercion was removed from Python 3.x. So each binop Color arithmetic slot function has to do its own argument type checking.

Comments (5)

  1. Lenard Lindstrom reporter

    Resolve Issue #286

    In type pygame.Color, for compatibility with Python 3.x, remove the nb_coersion slot function. For binary arithmetic operations, any type checking and conversion is now handled within the binary operation slot functions themselves. This will also help with Issue #280, allowing arithmetic on Color subclasses to return a result in the same subclass.

    → <<cset 5525f3e30956>>

  2. sirkne

    In case you weren't aware, the bugfix changeset 5525f3e30956 causes the build to fail with the following error (on OSX, 3.5):

    building 'pygame.color' extension
    gcc -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch x86_64 -Ddarwin -D_THREAD_SAFE -DENABLE_NEWBUF=1 -I/usr/X11R6/include -I/usr/local/include/SDL -I/mc/envs/_build/include/python3.5m -c src/color.c -o build/temp.macosx-10.5-x86_64-3.5/src/color.o
    src/color.c:316:5: error: use of undeclared identifier 'Py_TPFLAGS_CHECKTYPES'
        COLOR_TPFLAGS,
        ^
    src/color.c:276:23: note: expanded from macro 'COLOR_TPFLAGS'
    #define COLOR_TPFLAGS COLOR_TPFLAGS_COMMON
                          ^
    src/color.c:272:49: note: expanded from macro 'COLOR_TPFLAGS_COMMON'
        (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES)
                                                    ^
    1 error generated.
    error: command 'gcc' failed with exit status 1
    

    If you'd like this in a separate issue, just let me know.

  3. Lenard Lindstrom reporter

    Thanks sirkne. A definition of Py_TPFLAGS_CHECKTYPES somehow disappeared in my first commit. I have added it in a follow-up commit, and Pygame now builds for Python 3.5.

  4. Log in to comment