Skip to content

Commit cda5afc

Browse files
authored
Merge pull request #174 from hoefling/py2-cleanup
Remove Python 2 compatibility code
2 parents f6a574f + 5055be6 commit cda5afc

File tree

7 files changed

+22
-138
lines changed

7 files changed

+22
-138
lines changed

src/constants.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static PyObject* PyXmlSec_Transform__str__(PyObject* self) {
2727
else
2828
snprintf(buf, sizeof(buf), "%s, None", transform->id->name);
2929

30-
return PyString_FromString(buf);
30+
return PyUnicode_FromString(buf);
3131
}
3232

3333
// __repr__ method
@@ -38,18 +38,18 @@ static PyObject* PyXmlSec_Transform__repr__(PyObject* self) {
3838
snprintf(buf, sizeof(buf), "__Transform('%s', '%s', %d)", transform->id->name, transform->id->href, transform->id->usage);
3939
else
4040
snprintf(buf, sizeof(buf), "__Transform('%s', None, %d)", transform->id->name, transform->id->usage);
41-
return PyString_FromString(buf);
41+
return PyUnicode_FromString(buf);
4242
}
4343

4444
static const char PyXmlSec_TransformNameGet__doc__[] = "The transform's name.";
4545
static PyObject* PyXmlSec_TransformNameGet(PyXmlSec_Transform* self, void* closure) {
46-
return PyString_FromString((const char*)self->id->name);
46+
return PyUnicode_FromString((const char*)self->id->name);
4747
}
4848

4949
static const char PyXmlSec_TransformHrefGet__doc__[] = "The transform's identification string (href).";
5050
static PyObject* PyXmlSec_TransformHrefGet(PyXmlSec_Transform* self, void* closure) {
5151
if (self->id->href != NULL)
52-
return PyString_FromString((const char*)self->id->href);
52+
return PyUnicode_FromString((const char*)self->id->href);
5353
Py_RETURN_NONE;
5454
}
5555

@@ -149,7 +149,7 @@ static PyObject* PyXmlSec_KeyData__str__(PyObject* self) {
149149
snprintf(buf, sizeof(buf), "%s, %s", keydata->id->name, keydata->id->href);
150150
else
151151
snprintf(buf, sizeof(buf), "%s, None", keydata->id->name);
152-
return PyString_FromString(buf);
152+
return PyUnicode_FromString(buf);
153153
}
154154

155155
// __repr__ method
@@ -160,18 +160,18 @@ static PyObject* PyXmlSec_KeyData__repr__(PyObject* self) {
160160
snprintf(buf, sizeof(buf), "__KeyData('%s', '%s')", keydata->id->name, keydata->id->href);
161161
else
162162
snprintf(buf, sizeof(buf), "__KeyData('%s', None)", keydata->id->name);
163-
return PyString_FromString(buf);
163+
return PyUnicode_FromString(buf);
164164
}
165165

166166
static const char PyXmlSec_KeyDataNameGet__doc__[] = "The key data's name.";
167167
static PyObject* PyXmlSec_KeyDataNameGet(PyXmlSec_KeyData* self, void* closure) {
168-
return PyString_FromString((const char*)self->id->name);
168+
return PyUnicode_FromString((const char*)self->id->name);
169169
}
170170

171171
static const char PyXmlSec_KeyDataHrefGet__doc__[] = "The key data's identification string (href).";
172172
static PyObject* PyXmlSec_KeyDataHrefGet(PyXmlSec_KeyData* self, void* closure) {
173173
if (self->id->href != NULL)
174-
return PyString_FromString((const char*)self->id->href);
174+
return PyUnicode_FromString((const char*)self->id->href);
175175
Py_RETURN_NONE;
176176
}
177177

@@ -245,15 +245,13 @@ static PyObject* PyXmlSec_KeyDataNew(xmlSecKeyDataId id) {
245245
return (PyObject*)keydata;
246246
}
247247

248-
#ifdef PY3K
249248
static PyModuleDef PyXmlSec_ConstantsModule =
250249
{
251250
PyModuleDef_HEAD_INIT,
252251
STRINGIFY(MODULE_NAME) ".constants",
253252
PYXMLSEC_CONSTANTS_DOC,
254253
-1, NULL, NULL, NULL, NULL, NULL
255254
};
256-
#endif // PY3K
257255

258256
// initialize constants module and registers it base package
259257
int PyXmlSec_ConstantsModule_Init(PyObject* package) {
@@ -267,12 +265,7 @@ int PyXmlSec_ConstantsModule_Init(PyObject* package) {
267265
PyObject* keyDataTypeCls = NULL;
268266
PyObject* tmp = NULL;
269267

270-
#ifdef PY3K
271268
constants = PyModule_Create(&PyXmlSec_ConstantsModule);
272-
#else
273-
constants = Py_InitModule3(STRINGIFY(MODULE_NAME) ".constants", NULL, PYXMLSEC_CONSTANTS_DOC);
274-
Py_XINCREF(constants);
275-
#endif
276269

277270
if (!constants) return -1;
278271

@@ -292,7 +285,7 @@ int PyXmlSec_ConstantsModule_Init(PyObject* package) {
292285
#undef PYXMLSEC_ADD_INT_CONSTANT
293286

294287
#define PYXMLSEC_DECLARE_NAMESPACE(var, name) \
295-
if (!(var = PyCreateDummyObject(name))) goto ON_FAIL; \
288+
if (!(var = PyModule_New(name))) goto ON_FAIL; \
296289
if (PyModule_AddObject(package, name, var) < 0) goto ON_FAIL; \
297290
Py_INCREF(var); // add object steels reference
298291

@@ -308,7 +301,7 @@ int PyXmlSec_ConstantsModule_Init(PyObject* package) {
308301

309302

310303
#define PYXMLSEC_ADD_NS_CONSTANT(name, lname) \
311-
tmp = PyString_FromString((const char*)(JOIN(xmlSec, name))); \
304+
tmp = PyUnicode_FromString((const char*)(JOIN(xmlSec, name))); \
312305
PYXMLSEC_ADD_CONSTANT(nsCls, name, lname);
313306

314307
// namespaces
@@ -334,7 +327,7 @@ int PyXmlSec_ConstantsModule_Init(PyObject* package) {
334327

335328

336329
#define PYXMLSEC_ADD_ENC_CONSTANT(name, lname) \
337-
tmp = PyString_FromString((const char*)(JOIN(xmlSec, name))); \
330+
tmp = PyUnicode_FromString((const char*)(JOIN(xmlSec, name))); \
338331
PYXMLSEC_ADD_CONSTANT(encryptionTypeCls, name, lname);
339332

340333
// encryption type
@@ -349,7 +342,7 @@ int PyXmlSec_ConstantsModule_Init(PyObject* package) {
349342

350343

351344
#define PYXMLSEC_ADD_NODE_CONSTANT(name, lname) \
352-
tmp = PyString_FromString((const char*)(JOIN(xmlSec, name))); \
345+
tmp = PyUnicode_FromString((const char*)(JOIN(xmlSec, name))); \
353346
PYXMLSEC_ADD_CONSTANT(nodeCls, name, lname);
354347

355348
// node

src/keys.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static PyObject* PyXmlSec_KeyFromBinaryFile(PyObject* self, PyObject* args, PyOb
249249

250250
PYXMLSEC_DEBUG("load symmetric key - start");
251251
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!O&:from_binary_file", kwlist,
252-
PyXmlSec_KeyDataType, &keydata, PyString_FSConverter, &filepath))
252+
PyXmlSec_KeyDataType, &keydata, PyUnicode_FSConverter, &filepath))
253253
{
254254
goto ON_FAIL;
255255
}
@@ -436,7 +436,7 @@ static PyObject* PyXmlSec_KeyNameGet(PyObject* self, void* closure) {
436436
}
437437
cname = (const char*)xmlSecKeyGetName(key->handle);
438438
if (cname != NULL) {
439-
return PyString_FromString(cname);
439+
return PyUnicode_FromString(cname);
440440
}
441441
Py_RETURN_NONE;
442442
}
@@ -460,7 +460,7 @@ static int PyXmlSec_KeyNameSet(PyObject* self, PyObject* value, void* closure) {
460460
return 0;
461461
}
462462

463-
name = PyString_AsString(value);
463+
name = PyUnicode_AsUTF8(value);
464464
if (name == NULL) return -1;
465465

466466
if (xmlSecKeySetName(key->handle, XSTR(name)) < 0) {
@@ -698,7 +698,7 @@ static PyObject* PyXmlSec_KeysManagerLoadCert(PyObject* self, PyObject* args, Py
698698

699699
PYXMLSEC_DEBUGF("%p: load cert - start", self);
700700
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&II:load_cert", kwlist,
701-
PyString_FSConverter, &filepath, &format, &type)) {
701+
PyUnicode_FSConverter, &filepath, &format, &type)) {
702702
goto ON_FAIL;
703703
}
704704

src/main.c

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ int PyXmlSec_EncModule_Init(PyObject* package);
203203
// templates management
204204
int PyXmlSec_TemplateModule_Init(PyObject* package);
205205

206-
#ifdef PY3K
207-
208206
static int PyXmlSec_PyClear(PyObject *self) {
209207
PyXmlSec_Free(free_mode);
210208
return 0;
@@ -225,54 +223,12 @@ static PyModuleDef PyXmlSecModule = {
225223

226224
#define PYENTRY_FUNC_NAME JOIN(PyInit_, MODULE_NAME)
227225
#define PY_MOD_RETURN(m) return m
228-
#else // PY3K
229-
#define PYENTRY_FUNC_NAME JOIN(init, MODULE_NAME)
230-
#define PY_MOD_RETURN(m) return
231-
232-
static void PyXmlSec_PyModuleGuard__del__(PyObject* self)
233-
{
234-
PyXmlSec_Free(free_mode);
235-
Py_TYPE(self)->tp_free(self);
236-
}
237-
238-
// we need guard to free resources on module unload
239-
typedef struct {
240-
PyObject_HEAD
241-
} PyXmlSec_PyModuleGuard;
242-
243-
static PyTypeObject PyXmlSec_PyModuleGuardType = {
244-
PyVarObject_HEAD_INIT(NULL, 0)
245-
STRINGIFY(MODULE_NAME) "__Guard", /* tp_name */
246-
sizeof(PyXmlSec_PyModuleGuard), /* tp_basicsize */
247-
0, /* tp_itemsize */
248-
PyXmlSec_PyModuleGuard__del__, /* tp_dealloc */
249-
0, /* tp_print */
250-
0, /* tp_getattr */
251-
0, /* tp_setattr */
252-
0, /* tp_reserved */
253-
0, /* tp_repr */
254-
0, /* tp_as_number */
255-
0, /* tp_as_sequence */
256-
0, /* tp_as_mapping */
257-
0, /* tp_hash */
258-
0, /* tp_call */
259-
0, /* tp_str */
260-
0, /* tp_getattro */
261-
0, /* tp_setattro */
262-
0, /* tp_as_buffer */
263-
Py_TPFLAGS_DEFAULT, /* tp_flags */
264-
};
265-
#endif // PY3K
266226

267227
PyMODINIT_FUNC
268228
PYENTRY_FUNC_NAME(void)
269229
{
270230
PyObject *module = NULL;
271-
#ifdef PY3K
272231
module = PyModule_Create(&PyXmlSecModule);
273-
#else
274-
module = Py_InitModule3(STRINGIFY(MODULE_NAME), PyXmlSec_MainMethods, MODULE_DOC);
275-
#endif
276232
if (!module) {
277233
PY_MOD_RETURN(NULL); /* this really should never happen */
278234
}
@@ -294,13 +250,6 @@ PYENTRY_FUNC_NAME(void)
294250
if (PyXmlSec_EncModule_Init(module) < 0) goto ON_FAIL;
295251
if (PyXmlSec_TemplateModule_Init(module) < 0) goto ON_FAIL;
296252

297-
#ifndef PY3K
298-
if (PyType_Ready(&PyXmlSec_PyModuleGuardType) < 0) goto ON_FAIL;
299-
PYXMLSEC_DEBUGF("%p", &PyXmlSec_PyModuleGuardType);
300-
// added guard to free resources on module unload, this should be called after last
301-
if (PyModule_AddObject(module, "__guard", _PyObject_New(&PyXmlSec_PyModuleGuardType)) < 0) goto ON_FAIL;
302-
#endif
303-
304253
PY_MOD_RETURN(module);
305254
ON_FAIL:
306255
PY_MOD_RETURN(NULL);

src/platform.h

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,50 +35,6 @@ typedef int Py_ssize_t;
3535
#define PY_SSIZE_T_MIN INT_MIN
3636
#endif
3737

38-
#if PY_MAJOR_VERSION >= 3
39-
#define PY3K 1
40-
#define PyString_Check PyUnicode_Check
41-
#define PyString_FromStringAndSize PyUnicode_FromStringAndSize
42-
43-
#define PyString_FromString PyUnicode_FromString
44-
45-
#define PyString_AsString PyUnicode_AsUTF8
46-
#define PyString_AsUtf8AndSize PyUnicode_AsUTF8AndSize
47-
48-
#define PyCreateDummyObject PyModule_New
49-
50-
#define PyString_FSConverter PyUnicode_FSConverter
51-
#else // PY3K
52-
53-
#define PyBytes_Check PyString_Check
54-
#define PyBytes_FromStringAndSize PyString_FromStringAndSize
55-
56-
#define PyBytes_AsString PyString_AsString
57-
#define PyBytes_AsStringAndSize PyString_AsStringAndSize
58-
59-
static inline char* PyString_AsUtf8AndSize(PyObject *obj, Py_ssize_t* length) {
60-
char* buffer = NULL;
61-
return (PyString_AsStringAndSize(obj, &buffer, length) < 0) ? (char*)(0) : buffer;
62-
}
63-
64-
static inline PyObject* PyCreateDummyObject(const char* name) {
65-
PyObject* tmp = Py_InitModule(name, NULL);
66-
Py_INCREF(tmp);
67-
return tmp;
68-
}
69-
70-
static inline int PyString_FSConverter(PyObject* o, PyObject** p) {
71-
if (o == NULL) {
72-
return 0;
73-
}
74-
75-
Py_INCREF(o);
76-
*p = o;
77-
return 1;
78-
}
79-
80-
#endif // PYTHON3
81-
8238
static inline char* PyBytes_AsStringAndSize2(PyObject *obj, Py_ssize_t* length) {
8339
char* buffer = NULL;
8440
return ((PyBytes_AsStringAndSize(obj, &buffer, length) < 0) ? (char*)(0) : buffer);

src/template.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -766,10 +766,10 @@ static PyObject* PyXmlSec_TemplateTransformAddC14NInclNamespaces(PyObject* self,
766766
goto ON_FAIL;
767767
}
768768
if (PyList_Check(prefixes) || PyTuple_Check(prefixes)) {
769-
sep = PyString_FromString(" ");
769+
sep = PyUnicode_FromString(" ");
770770
prefixes = PyObject_CallMethod(sep, "join", "O", prefixes);
771771
Py_DECREF(sep);
772-
} else if (PyString_Check(prefixes)) {
772+
} else if (PyUnicode_Check(prefixes)) {
773773
Py_INCREF(prefixes);
774774
} else {
775775
PyErr_SetString(PyExc_TypeError, "expected instance of str or list of str");
@@ -781,7 +781,7 @@ static PyObject* PyXmlSec_TemplateTransformAddC14NInclNamespaces(PyObject* self,
781781
}
782782

783783

784-
c_prefixes = PyString_AsString(prefixes);
784+
c_prefixes = PyUnicode_AsUTF8(prefixes);
785785
Py_BEGIN_ALLOW_THREADS;
786786
res = xmlSecTmplTransformAddC14NInclNamespaces(node->_c_node, XSTR(c_prefixes));
787787
Py_END_ALLOW_THREADS;
@@ -918,7 +918,6 @@ static PyMethodDef PyXmlSec_TemplateMethods[] = {
918918
{NULL, NULL} /* sentinel */
919919
};
920920

921-
#ifdef PY3K
922921
static PyModuleDef PyXmlSec_TemplateModule =
923922
{
924923
PyModuleDef_HEAD_INIT,
@@ -931,15 +930,9 @@ static PyModuleDef PyXmlSec_TemplateModule =
931930
NULL, /* m_clear */
932931
NULL, /* m_free */
933932
};
934-
#endif // PY3K
935933

936934
int PyXmlSec_TemplateModule_Init(PyObject* package) {
937-
#ifdef PY3K
938935
PyObject* template = PyModule_Create(&PyXmlSec_TemplateModule);
939-
#else
940-
PyObject* template = Py_InitModule3(STRINGIFY(MODULE_NAME) ".template", PyXmlSec_TemplateMethods, PYXMLSEC_TEMPLATES_DOC);
941-
Py_XINCREF(template);
942-
#endif
943936

944937
if (!template) goto ON_FAIL;
945938
PYXMLSEC_DEBUGF("%p", template);

src/tree.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static PyObject* PyXmlSec_TreeAddIds(PyObject* self, PyObject *args, PyObject *k
182182
tmp = PyObject_GetItem(ids, key);
183183
Py_DECREF(key);
184184
if (tmp == NULL) goto ON_FAIL;
185-
list[i] = XSTR(PyString_AsString(tmp));
185+
list[i] = XSTR(PyUnicode_AsUTF8(tmp));
186186
Py_DECREF(tmp);
187187
if (list[i] == NULL) goto ON_FAIL;
188188
}
@@ -230,7 +230,6 @@ static PyMethodDef PyXmlSec_TreeMethods[] = {
230230
{NULL, NULL} /* sentinel */
231231
};
232232

233-
#ifdef PY3K
234233
static PyModuleDef PyXmlSec_TreeModule =
235234
{
236235
PyModuleDef_HEAD_INIT,
@@ -243,16 +242,10 @@ static PyModuleDef PyXmlSec_TreeModule =
243242
NULL, /* m_clear */
244243
NULL, /* m_free */
245244
};
246-
#endif // PY3K
247245

248246

249247
int PyXmlSec_TreeModule_Init(PyObject* package) {
250-
#ifdef PY3K
251248
PyObject* tree = PyModule_Create(&PyXmlSec_TreeModule);
252-
#else
253-
PyObject* tree = Py_InitModule3(STRINGIFY(MODULE_NAME) ".tree", PyXmlSec_TreeMethods, PYXMLSEC_TREE_DOC);
254-
Py_XINCREF(tree);
255-
#endif
256249

257250
if (!tree) goto ON_FAIL;
258251

src/utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ PyObject* PyXmlSec_GetFilePathOrContent(PyObject* file, int* is_content) {
2525
return data;
2626
}
2727
*is_content = 0;
28-
if (!PyString_FSConverter(file, &tmp)) {
28+
if (!PyUnicode_FSConverter(file, &tmp)) {
2929
return NULL;
3030
}
3131
return tmp;
3232
}
3333

3434
int PyXmlSec_SetStringAttr(PyObject* obj, const char* name, const char* value) {
35-
PyObject* tmp = PyString_FromString(value);
35+
PyObject* tmp = PyUnicode_FromString(value);
3636
int r;
3737

3838
if (tmp == NULL) {

0 commit comments

Comments
 (0)