comparison mercurial/mpatch.c @ 11360:2ac98313b26c

mpatch.c: Added preliminary support for py3k. This is done by including the util.h header file, that defines appropriate macros according to the current python version.
author Renato Cunha <renatoc@gmail.com>
date Tue, 15 Jun 2010 19:49:56 -0300
parents 08a0f04b56bd
children 613b8bd2284e
comparison
equal deleted inserted replaced
11359:4eaacccbb2ca 11360:2ac98313b26c
21 */ 21 */
22 22
23 #include <Python.h> 23 #include <Python.h>
24 #include <stdlib.h> 24 #include <stdlib.h>
25 #include <string.h> 25 #include <string.h>
26
27 #include "util.h"
26 28
27 /* Definitions to get compatibility with python 2.4 and earlier which 29 /* Definitions to get compatibility with python 2.4 and earlier which
28 does not have Py_ssize_t. See also PEP 353. 30 does not have Py_ssize_t. See also PEP 353.
29 Note: msvc (8 or earlier) does not have ssize_t, so we use Py_ssize_t. 31 Note: msvc (8 or earlier) does not have ssize_t, so we use Py_ssize_t.
30 */ 32 */
371 outlen = calcsize(inlen, patch); 373 outlen = calcsize(inlen, patch);
372 if (outlen < 0) { 374 if (outlen < 0) {
373 result = NULL; 375 result = NULL;
374 goto cleanup; 376 goto cleanup;
375 } 377 }
376 result = PyString_FromStringAndSize(NULL, outlen); 378 result = PyBytes_FromStringAndSize(NULL, outlen);
377 if (!result) { 379 if (!result) {
378 result = NULL; 380 result = NULL;
379 goto cleanup; 381 goto cleanup;
380 } 382 }
381 out = PyString_AsString(result); 383 out = PyBytes_AsString(result);
382 if (!apply(out, in, inlen, patch)) { 384 if (!apply(out, in, inlen, patch)) {
383 Py_DECREF(result); 385 Py_DECREF(result);
384 result = NULL; 386 result = NULL;
385 } 387 }
386 cleanup: 388 cleanup:
433 {"patches", patches, METH_VARARGS, "apply a series of patches\n"}, 435 {"patches", patches, METH_VARARGS, "apply a series of patches\n"},
434 {"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"}, 436 {"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"},
435 {NULL, NULL} 437 {NULL, NULL}
436 }; 438 };
437 439
440 #ifdef IS_PY3K
441 static struct PyModuleDef mpatch_module = {
442 PyModuleDef_HEAD_INIT,
443 "mpatch",
444 mpatch_doc,
445 -1,
446 methods
447 };
448
449 PyMODINIT_FUNC PyInit_mpatch(void)
450 {
451 PyObject *m;
452
453 m = PyModule_Create(&mpatch_module);
454 if (m == NULL)
455 return NULL;
456
457 mpatch_Error = PyErr_NewException("mpatch.mpatchError", NULL, NULL);
458 Py_INCREF(mpatch_Error);
459 PyModule_AddObject(m, "mpatchError", mpatch_Error);
460
461 return m;
462 }
463 #else
438 PyMODINIT_FUNC 464 PyMODINIT_FUNC
439 initmpatch(void) 465 initmpatch(void)
440 { 466 {
441 Py_InitModule3("mpatch", methods, mpatch_doc); 467 Py_InitModule3("mpatch", methods, mpatch_doc);
442 mpatch_Error = PyErr_NewException("mpatch.mpatchError", NULL, NULL); 468 mpatch_Error = PyErr_NewException("mpatch.mpatchError", NULL, NULL);
443 } 469 }
444 470 #endif