This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mahmoud
Recipients mahmoud
Date 2012-01-15.00:33:10
SpamBayes Score 0.0
Marked as misclassified No
Message-id <1326587591.5.0.14837599409.issue13787@psf.upfronthosting.co.za>
In-reply-to
Content
On Python 3.1.4, attempting to create a code object will apparently result in a TypeError (must be str, not tuple), even when you're creating a code object from another, working code object:

# co_what.py
def foo():
    return 'bar'

co = foo.__code__

co_copy = type(co)(co.co_argcount,
                   co.co_kwonlyargcount,
                   co.co_nlocals,
                   co.co_stacksize,
                   co.co_flags,
                   co.co_code,
                   co.co_consts,
                   co.co_names,
                   co.co_varnames,
                   co.co_freevars,
                   co.co_cellvars,
                   co.co_filename,
                   co.co_name,
                   co.co_firstlineno,
                   co.co_lnotab)
# EOF
$ python3 co_what.py
Traceback (most recent call last):
  File "co_what.py", line 20, in <module>
    co.co_lnotab)
TypeError: must be str, not tuple

Looking at the PyCode_New function, all the arguments look correctly matched up according to the signature in my Python 3.1.4 build source (looks identical to the trunk source):

# Objects/codeobject.c

PyCode_New(int argcount, int kwonlyargcount,
           int nlocals, int stacksize, int flags,
           PyObject *code, PyObject *consts, PyObject *names,
           PyObject *varnames, PyObject *freevars, PyObject *cellvars,
           PyObject *filename, PyObject *name, int firstlineno,
           PyObject *lnotab)
{
    PyCodeObject *co;
    Py_ssize_t i;

    /* Check argument types */
    if (argcount < 0 || nlocals < 0 ||
        code == NULL ||
        consts == NULL || !PyTuple_Check(consts) ||
        names == NULL || !PyTuple_Check(names) ||
        varnames == NULL || !PyTuple_Check(varnames) ||
        freevars == NULL || !PyTuple_Check(freevars) ||
        cellvars == NULL || !PyTuple_Check(cellvars) ||
        name == NULL || !PyUnicode_Check(name) ||
        filename == NULL || !PyUnicode_Check(filename) ||
        lnotab == NULL || !PyBytes_Check(lnotab) ||
        !PyObject_CheckReadBuffer(code)) {
        PyErr_BadInternalCall();
        return NULL;
    }

And, for the record, this same behavior works just fine in the equivalent Python 2.
History
Date User Action Args
2012-01-15 00:33:11mahmoudsetrecipients: + mahmoud
2012-01-15 00:33:11mahmoudsetmessageid: <1326587591.5.0.14837599409.issue13787@psf.upfronthosting.co.za>
2012-01-15 00:33:10mahmoudlinkissue13787 messages
2012-01-15 00:33:10mahmoudcreate