Further help with Object Caching

Brent Fulgham bfulgham at debian.org
Mon Feb 28 23:15:25 EST 2000


I must be missing something obvious.  I seem to be able to throw
objects into strings without a problem, but they don't come out
of the Cache alive :-)

I whittled it further, to the point where it doesn't seem that
the Cache is the issue, but rather the way I am storing objects.

The following pseudo-code snippet shows the problem (taken
from my program with the logging methods removed for clarity)
(Note that the variable 'code' is a valid PyCodeObject*)

============================================================

// Create a cacheable code object by writing it to a string,
// then converting the string to a char*
PyObject* codeString;
PyObject* test;
PyObject* test2;
char* cacheable;
char* store2;
int length;

codeString = PyMarshal_WriteObjectToString((PyObject*)code);
cacheable = PyString_AsString(codeString);
length = PyObject_Length(codeString);

// Test 1 -- Works
test = PyMarshal_ReadObjectFromString((char*)cacheable, length);
if (test == NULL)
{
    // Log error message
}

// Test 2 -- Fails
store2 = (char*)malloc(length * sizeof(char))
strncpy(store2, cacheable, length);

// Use copy to build object
test2 = PyMarshal_ReadObjectFromString((char*)store2, length);
if (test2 == NULL)
{
    // Throw tantrum
}

=====================================================

The 'test2' case fails.  The first thing that comes to mind is
that the "length" I am getting from PyObject_Length is wrong.
Perhaps there is header or trailer data that is being
included that should not, or information that is missing.

Can anyone help shed some light on this?

Thanks,

-Brent





More information about the Python-list mailing list