[Python-3000] Strange code in bsddb that doesn't work in Python 3.0

Amaury Forgeot d'Arc amauryfa at gmail.com
Tue Jul 22 00:39:54 CEST 2008


2008/7/21 Jesus Cea <jcea at jcea.es>:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Trying to find why I can't import bsddb in python3.0, I have found that
> this code fails:
>
> """
> ~    /* Some magic to make DBNotFoundError and DBKeyEmptyError derive
> ~     * from both DBError and KeyError, since the API only supports
> ~     * using one base class. */
> ~    PyDict_SetItemString(d, "KeyError", PyExc_KeyError);
> ~    PyRun_String("class DBNotFoundError(DBError, KeyError): pass\n"
> ~                 "class DBKeyEmptyError(DBError, KeyError): pass",
> ~                 Py_file_input, d, d);
> ~    DBNotFoundError = PyDict_GetItemString(d, "DBNotFoundError");
> ~    DBKeyEmptyError = PyDict_GetItemString(d, "DBKeyEmptyError");
> ~    PyDict_DelItemString(d, "KeyError");
> """
>
> "Some magic" comment is a bit scary :-), although the code seems fairly
> obvious.
>
> This code works in Python 2.x, but fails in 3.0.
>
> The line that fails is "PyRun_String()". The error is: "ImportError:
> __build_class__ not found".
>
> :-?
>
> Ideas welcomed!
>
> The code should work in python2.[3-6] and python3.0. Conditional
> compilation can be used.
>
> PS: Googling around, I see that Python 2.5 allows a tuple being used as
> base for exception creation.  Is this available in Python3.0?. Searching
> for "PyErr_NewException" in 3.0 documentation online shows no results.
>
> Ok, the index show the function. It should work.
>
> I'm still interested in knowing WHY the original code fails.

I suppose you just copied the _bsddb.c file from trunk, right? Here is
the problem.
The current version of _bsddb.c in the py3k branch contains the lines:
    {
	    PyObject *builtin_mod = PyImport_ImportModule("builtins");
	    PyDict_SetItemString(d, "__builtins__", builtin_mod);
    }
And indeed __build_class__ is in the __builtins__ module, and seems to
be needed by any "class" statement
(to implement new metaclass semantics I guess)
PyErr_NewException is better in any case.

But didn't you try svnmerge? there are other py3k changes you may want to keep.

-- 
Amaury Forgeot d'Arc


More information about the Python-3000 mailing list