[Pyrex] Question about object creation

Martin Sjögren msjogren at gmail.com
Fri Aug 6 13:49:44 CEST 2004


Hello list.

I'm the author of pyopenssl (sf.net/projects/pyopenssl) and I've been
thinking of rewriting it in pyrex rather than continuing to maintain
the awkward C code I've got. I've been toying a bit with pyrex and I'm
impressed, it's really cool. :-)

I have a question, though. Wrapping openssl, I find myself doing some
evil tricks.

Example: I have types X509 and X509Name which wrap openssl's X509 and
X509_NAME structs. You can basically get an X509Name in two ways,
either by copying an existing object (i.e. a copy constructor) or by
calling get_issuer() or get_subject() on a X509 object. This means
that I need two different ways of creating X509Name objects, and I do
it by having a factory function that's inserted into the crypto
module's dictionary (note that these are old-style types that cannot
be inherited from) and an internal function that has much more freedom
to do as it pleases. Basically, it looks like this:

static Pyobject *
crypto_X509Name(PyObject *foo, PyObject *args) {
  crypto_X509NameObj *name;
  if (!PyArg_ParseTuple(args, "O!:X509Name", &crypto_X509Name_Type, &name))
    return NULL;
  return (PyObject *)crypto_X509Name_New(X509_NAME_dup(name->x509_name), 1);
}

crypto_X509NameObj *
crypto_X509Name_New(X509_NAME *name, int dealloc)
{
  ...
}

The dealloc argument controls whether the destructor for X509Name
should free the wrapped X509_NAME pointer or not. In the case of the
copy constructor (factory function) it should, but in the case of
get_subject() it shouldn't, so crypto_X509_get_subject() calls
crypto_X509Name_New with a 0 instead.

Was that understandable at all? Anyway, what I'm wondering is if I can
do this in pyrex, or if I need to redesign this mechanic from the
ground up... I wouldn't be surprised if this mechanic is buggy for
other reasons, so a redesign might be a good idea anyway. ;-)


Cheers,
Martin Sjögren




More information about the Pyrex mailing list