summaryrefslogtreecommitdiffstats
path: root/parttype.c
blob: cbdae821dc42e95faf024fc674e15415038ef18c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * (C) 2022 Soleta Consulting S.L. <info@soleta.eu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Author: Jose M. Guisado <jguisado@soleta.eu>
 */


#include "fdisk.h"

static PyMemberDef PartType_members[] = {
	{ NULL }
};

static void PartType_dealloc(PartTypeObject *self)
{
	Py_TYPE(self)->tp_free((PyObject *) self);
}

static PyMethodDef PartType_methods[] = {
	{NULL}
};

static PyObject *PartType_get_name(PartTypeObject *self)
{
	return PyObjectResultStr(fdisk_parttype_get_name(self->type));
}

static PyObject *PartType_get_code(PartTypeObject *self)
{
	return PyLong_FromUnsignedLong(fdisk_parttype_get_code(self->type));
}

static PyGetSetDef PartType_getseters[] = {
	{"name",	(getter)PartType_get_name, NULL, "parttype human readable name", NULL},
	{"code",	(getter)PartType_get_code, NULL, "parttype DOS code", NULL},
	{NULL}
};

static PyObject *PartType_repr(PartTypeObject *self)
{
	return PyUnicode_FromFormat("<libfdisk.PartType object at %p, name=%s>",
			self, fdisk_parttype_get_name(self->type));
}

PyTypeObject PartTypeType = {
	PyVarObject_HEAD_INIT(NULL, 0)
	.tp_name = "libfdisk.PartType",
	.tp_basicsize = sizeof(PartTypeObject),
	.tp_dealloc = (destructor)PartType_dealloc,
	.tp_repr = (reprfunc) PartType_repr,
	.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
	/* TODO: .tp_doc = PartType_HELP, */
	.tp_methods = PartType_methods,
	.tp_members = PartType_members,
	.tp_getset = PartType_getseters,
};

PyObject *PyObjectResultPartType(struct fdisk_parttype *t)
{
        PartTypeObject *result;

        if (!t) {
                PyErr_SetString(PyExc_AssertionError, "lb assert failed");
                return NULL;
        }

        result = PyObject_New(PartTypeObject, &PartTypeType);
        if (!result) {
                PyErr_SetString(PyExc_MemoryError, "Couldn't allocate PartType object");
                return NULL;
        }

        result->type = t;
        return (PyObject *) result;
}

void PartType_AddModuleObject(PyObject *mod)
{
	if (PyType_Ready(&PartTypeType) < 0)
		return;

	Py_INCREF(&PartTypeType);
	PyModule_AddObject(mod, "PartType", (PyObject *)&PartTypeType);
}