}
static PyObject* Key_export(KeyObject* self, PyObject* args) {
- PyObject* file = NULL;
+ pakfire_key_export_mode_t mode = PAKFIRE_KEY_EXPORT_MODE_PUBLIC;
+ PyObject* object = NULL;
int secret = 0;
- if (!PyArg_ParseTuple(args, "O|p", &file, &secret))
+ if (!PyArg_ParseTuple(args, "|p", &secret))
return NULL;
- // Get a file descriptor
- int fd = PyObject_AsFileDescriptor(file);
- if (fd < 0)
- return NULL;
+ char* data = NULL;
+ size_t data_length = 0;
- // Convert to FILE*
- FILE* f = fdopen(fd, "w");
+ // Create buffer to write the key to
+ FILE* f = open_memstream(&data, &data_length);
if (!f) {
PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
+ goto ERROR;
}
- pakfire_key_export_mode_t mode;
if (secret)
mode = PAKFIRE_KEY_EXPORT_MODE_PRIVATE;
- else
- mode = PAKFIRE_KEY_EXPORT_MODE_PUBLIC;
// Export the key
int r = pakfire_key_export(self->key, f, mode);
if (r) {
PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
+ goto ERROR;
}
- Py_RETURN_NONE;
+ object = PyUnicode_FromStringAndSize(data, data_length);
+
+ERROR:
+ if (f)
+ fclose(f);
+ if (data)
+ free(data);
+
+ return object;
}
static PyObject* Key_sign(KeyObject* self, PyObject* args, PyObject* kwargs) {
static PyObject* Pakfire_import_key(PakfireObject* self, PyObject* args) {
struct pakfire_key* key = NULL;
PyObject* object = NULL;
- PyObject* file = NULL;
+ char* data = NULL;
+ Py_ssize_t data_length = 0;
int r;
// Parse arguments
- if (!PyArg_ParseTuple(args, "O", &file))
+ if (!PyArg_ParseTuple(args, "s#", &data, &data_length))
return NULL;
- // Treat the object as a file
- FILE* f = PyObject_AsFileHandle(file, "r");
+ // Map the object
+ FILE* f = fmemopen(data, data_length, "r");
if (!f)
return NULL;