Re: [PyOpenGL-Devel] Python 3- strings or bytes interface
Brought to you by:
mcfletch
|
From: Mike C. F. <mcf...@vr...> - 2012-04-04 15:44:55
|
On 12-04-04 07:23 AM, Rob Reilink wrote:
> Hi,
>
> I've noticed that under Python3, PyOpenGL inconsistently expects 'str'
> and 'bytes' objects as function arguments. For some functions, I'd
> expect to use 'str' while in the current implementation 'bytes' is used.
>
> E.g. in glGetUniformlocation, the name of the uniform is to be
> specified as a 'bytes' object, while I would expect to use 'str' since
> it is a name. Also, shaders.compileShader takes a 'str' for the shader
> code
>
> Similarly, extensions.hasGLExtension() expects a 'str' object for the
> extension name, but extension.AVAILABLE_GL_EXTENSIONS is a list of
> 'bytes'.
>
> Of course, for arguments dealing with binary data (e.g. glTexImage2D),
> a 'bytes' object is to be used.
>
> Apart from the actual implementation, has there been any thought on
> how to expose things like uniform names to the user?
My first reaction would be to do this:
if isinstance( arg, unicode):
arg = arg.encode( 'latin-1' )
in the wrapper (the as_8_bit() hack has been added to the extensions
module, for instance), that is, for each argument which is current str,
make the argument capable of accepting a unicode argument... as for
producing them... I'm hesitant to convert the 8-bit values (which is
internally what OpenGL is specifying; GLchar * is an 8-bit value) to
unicode arbitrarily, as now code that uses formal, correct, byte-strings
is going to crash when it tries to interact with the generated unicode
values.
Everything in OpenGL is binary data. Everything has an expressly defined
binary representation, and that includes byte-strings. Anything I do
here to paper over the difference is, I expect, going to come back to
byte us in the future. Someone is going to do a glGetActiveUniform() in
my_unicode_shader and have it blow up on a unicode/bytes disagreement if
I convert on return, or is going to do glGetActiveUniform() in
my_bytes_shader if I don't, but I expect that the number of problems
with glGetUniform( 'myuniform' ) will be substantial.
Basically I don't have a good solution. Either we create an API
inconsistency between Python 2 and Python 3 (returning "str" in both,
though they are different types), or we make Python 3 users explicitly
deal with the return-type of the GLchar* calls and/or use byte-strings
everywhere.
Enjoy,
Mike
--
________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com
|