[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Strange (uncommon) behaviour of Font.render()



The first 3 arguments are positional.  If this were in pure python, then background="" would have the same effect as just passing None.   The font module is programed in C.  If you look at font.c you'll find

font_render (PyObject* self, PyObject* args)
{
    TTF_Font* font = PyFont_AsFont (self);
    int aa;
    PyObject* text, *final;
    PyObject* fg_rgba_obj, *bg_rgba_obj = NULL;
    Uint8 rgba[4];
    SDL_Surface* surf;
    SDL_Color foreg, backg;
    int just_return;
    just_return = 0;

    if (!PyArg_ParseTuple (args, "OiO|O", &text, &aa, &fg_rgba_obj,
                           &bg_rgba_obj))
        return NULL;

    if (!RGBAFromColorObj (fg_rgba_obj, rgba))
        return RAISE (PyExc_TypeError, "Invalid foreground RGBA argument");
    foreg.r = rgba[0];
    foreg.g = rgba[1];
    foreg.b = rgba[2];
    if (bg_rgba_obj)
    {
        if (!RGBAFromColorObj (bg_rgba_obj, rgba))
            return RAISE (PyExc_TypeError, "Invalid background RGBA argument");
        backg.r = rgba[0];
        backg.g = rgba[1];
        backg.b = rgba[2];
        backg.unused = 0;
    }
    else
   {
        backg.r = 0;
        backg.g = 0;
        backg.b = 0;
        backg.unused = 0;
    }
 <snip>

While I haven't traced through all the code, it seems clear that if you pass a parameter for the background, the function will check if it's a valid RGBA argument, and raise a TypeError if it isn't.

I have found similar things in pygame before, although I can't recall the details.  

In any event, if you don't want a background apramter, leave it out; None won't work whatever you do.

Saul

On Sun, Jan 9, 2011 at 4:29 PM, Floris van Manen <vm@xxxxxxxxxxxxx> wrote:

On Jan 9, 2011, at 23:08, Gregor Lingl wrote:

>> then try
>>
>> text = font.render("Hello world!", 1, (255, 255, 0), background=""> >>
> Is there any reason, why this could work differently?


Named arguments can be presented in any order.
If there are more optional arguments in between, you'd get incorrect assignments if you leave the name out