comparison genshi/_speedups.c @ 930:0ec0a695ec96

Merge r1138 from py3k: add python 3 support to _speedups C extension
author hodgestar
date Fri, 18 Mar 2011 09:04:14 +0000
parents d282c6c0133e
children
comparison
equal deleted inserted replaced
929:1a86e0af2ae1 930:0ec0a695ec96
12 */ 12 */
13 13
14 #include <Python.h> 14 #include <Python.h>
15 #include <structmember.h> 15 #include <structmember.h>
16 16
17 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN) 17 #if PY_MAJOR_VERSION > 2
18 typedef int Py_ssize_t; 18 # define IS_PY3K
19 #define PY_SSIZE_T_MAX INT_MAX 19 #elif PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
20 #define PY_SSIZE_T_MIN INT_MIN 20 typedef int Py_ssize_t;
21 # define PY_SSIZE_T_MAX INT_MAX
22 # define PY_SSIZE_T_MIN INT_MIN
23 #endif
24
25 /* We only use Unicode Strings in this module */
26 #ifndef IS_PY3K
27 # define PyObject_Str PyObject_Unicode
21 #endif 28 #endif
22 29
23 static PyObject *amp1, *amp2, *lt1, *lt2, *gt1, *gt2, *qt1, *qt2; 30 static PyObject *amp1, *amp2, *lt1, *lt2, *gt1, *gt2, *qt1, *qt2;
24 static PyObject *stripentities, *striptags; 31 static PyObject *stripentities, *striptags;
25 32
71 PyTuple_SET_ITEM(args, 0, ret); 78 PyTuple_SET_ITEM(args, 0, ret);
72 ret = MarkupType.tp_new(&MarkupType, args, NULL); 79 ret = MarkupType.tp_new(&MarkupType, args, NULL);
73 Py_DECREF(args); 80 Py_DECREF(args);
74 return ret; 81 return ret;
75 } 82 }
76 in = (PyUnicodeObject *) PyObject_Unicode(text); 83 in = (PyUnicodeObject *) PyObject_Str(text);
77 if (in == NULL) { 84 if (in == NULL) {
78 return NULL; 85 return NULL;
79 } 86 }
80 /* First we need to figure out how long the escaped string will be */ 87 /* First we need to figure out how long the escaped string will be */
81 len = inn = 0; 88 len = inn = 0;
388 Markup_mul(PyObject *self, PyObject *num) 395 Markup_mul(PyObject *self, PyObject *num)
389 { 396 {
390 PyObject *unicode, *result, *args; 397 PyObject *unicode, *result, *args;
391 398
392 if (PyObject_TypeCheck(self, &MarkupType)) { 399 if (PyObject_TypeCheck(self, &MarkupType)) {
393 unicode = PyObject_Unicode(self); 400 unicode = PyObject_Str(self);
394 if (unicode == NULL) return NULL; 401 if (unicode == NULL) return NULL;
395 result = PyNumber_Multiply(unicode, num); 402 result = PyNumber_Multiply(unicode, num);
396 } else { // __rmul__ 403 } else { // __rmul__
397 unicode = PyObject_Unicode(num); 404 unicode = PyObject_Str(num);
398 if (unicode == NULL) return NULL; 405 if (unicode == NULL) return NULL;
399 result = PyNumber_Multiply(unicode, self); 406 result = PyNumber_Multiply(unicode, self);
400 } 407 }
401 Py_DECREF(unicode); 408 Py_DECREF(unicode);
402 409
416 static PyObject * 423 static PyObject *
417 Markup_repr(PyObject *self) 424 Markup_repr(PyObject *self)
418 { 425 {
419 PyObject *format, *result, *args; 426 PyObject *format, *result, *args;
420 427
428 #ifdef IS_PY3K
429 format = PyUnicode_FromString("<Markup %r>");
430 #else
421 format = PyString_FromString("<Markup %r>"); 431 format = PyString_FromString("<Markup %r>");
432 #endif
422 if (format == NULL) return NULL; 433 if (format == NULL) return NULL;
423 result = PyObject_Unicode(self); 434 result = PyObject_Str(self);
424 if (result == NULL) { 435 if (result == NULL) {
425 Py_DECREF(format); 436 Py_DECREF(format);
426 return NULL; 437 return NULL;
427 } 438 }
428 args = PyTuple_New(1); 439 args = PyTuple_New(1);
430 Py_DECREF(format); 441 Py_DECREF(format);
431 Py_DECREF(result); 442 Py_DECREF(result);
432 return NULL; 443 return NULL;
433 } 444 }
434 PyTuple_SET_ITEM(args, 0, result); 445 PyTuple_SET_ITEM(args, 0, result);
446 #ifdef IS_PY3K
447 result = PyUnicode_Format(format, args);
448 #else
435 result = PyString_Format(format, args); 449 result = PyString_Format(format, args);
450 #endif
436 Py_DECREF(format); 451 Py_DECREF(format);
437 Py_DECREF(args); 452 Py_DECREF(args);
438 return result; 453 return result;
439 } 454 }
440 455
551 566
552 static PyNumberMethods Markup_as_number = { 567 static PyNumberMethods Markup_as_number = {
553 Markup_add, /*nb_add*/ 568 Markup_add, /*nb_add*/
554 0, /*nb_subtract*/ 569 0, /*nb_subtract*/
555 Markup_mul, /*nb_multiply*/ 570 Markup_mul, /*nb_multiply*/
571 #ifndef IS_PY3K
556 0, /*nb_divide*/ 572 0, /*nb_divide*/
573 #endif
557 Markup_mod, /*nb_remainder*/ 574 Markup_mod, /*nb_remainder*/
558 }; 575 };
559 576
560 PyTypeObject MarkupType = { 577 PyTypeObject MarkupType = {
578 #ifdef IS_PY3K
579 PyVarObject_HEAD_INIT(NULL, 0)
580 #else
561 PyObject_HEAD_INIT(NULL) 581 PyObject_HEAD_INIT(NULL)
562 0, 582 0,
583 #endif
563 "genshi._speedups.Markup", 584 "genshi._speedups.Markup",
564 sizeof(MarkupObject), 585 sizeof(MarkupObject),
565 0, 586 0,
566 0, /*tp_dealloc*/ 587 0, /*tp_dealloc*/
567 0, /*tp_print*/ 588 0, /*tp_print*/
568 0, /*tp_getattr*/ 589 0, /*tp_getattr*/
569 0, /*tp_setattr*/ 590 0, /*tp_setattr*/
591 #ifdef IS_PY3K
592 0, /*tp_reserved*/
593 #else
570 0, /*tp_compare*/ 594 0, /*tp_compare*/
595 #endif
571 Markup_repr, /*tp_repr*/ 596 Markup_repr, /*tp_repr*/
572 &Markup_as_number, /*tp_as_number*/ 597 &Markup_as_number, /*tp_as_number*/
573 0, /*tp_as_sequence*/ 598 0, /*tp_as_sequence*/
574 0, /*tp_as_mapping*/ 599 0, /*tp_as_mapping*/
575 0, /*tp_hash */ 600 0, /*tp_hash */
578 0, /*tp_str*/ 603 0, /*tp_str*/
579 0, /*tp_getattro*/ 604 0, /*tp_getattro*/
580 0, /*tp_setattro*/ 605 0, /*tp_setattro*/
581 0, /*tp_as_buffer*/ 606 0, /*tp_as_buffer*/
582 607
608 #ifdef IS_PY3K
609 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /*tp_flags*/
610 #elif defined(Py_TPFLAGS_UNICODE_SUBCLASS)
611 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES | Py_TPFLAGS_UNICODE_SUBCLASS, /*tp_flags*/
612 #else
583 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ 613 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
614 #endif
615
584 Markup__doc__,/*tp_doc*/ 616 Markup__doc__,/*tp_doc*/
585 617
586 0, /*tp_traverse*/ 618 0, /*tp_traverse*/
587 0, /*tp_clear*/ 619 0, /*tp_clear*/
588 620
614 0, /*tp_cache*/ 646 0, /*tp_cache*/
615 0, /*tp_subclasses*/ 647 0, /*tp_subclasses*/
616 0 /*tp_weaklist*/ 648 0 /*tp_weaklist*/
617 }; 649 };
618 650
651 #ifdef IS_PY3K
652 struct PyModuleDef module_def = {
653 PyModuleDef_HEAD_INIT, /*m_base*/
654 "_speedups", /*m_name*/
655 NULL, /*m_doc*/
656 -1, /*m_size*/
657 NULL, /*m_methods*/
658 NULL, /*m_reload*/
659 NULL, /*m_traverse*/
660 NULL, /*m_clear*/
661 NULL /*m_free*/
662 };
663
664 PyObject *
665 PyInit__speedups(void)
666 #else
619 PyMODINIT_FUNC 667 PyMODINIT_FUNC
620 init_speedups(void) 668 init_speedups(void)
669 #endif
621 { 670 {
622 PyObject *module; 671 PyObject *module;
623 672
624 /* Workaround for quirk in Visual Studio, see 673 /* Workaround for quirk in Visual Studio, see
625 <http://www.python.it/faq/faq-3.html#3.24> */ 674 <http://www.python.it/faq/faq-3.html#3.24> */
626 MarkupType.tp_base = &PyUnicode_Type; 675 MarkupType.tp_base = &PyUnicode_Type;
627 676
628 if (PyType_Ready(&MarkupType) < 0) 677 if (PyType_Ready(&MarkupType) < 0)
678 #ifdef IS_PY3K
679 return NULL;
680 #else
629 return; 681 return;
682 #endif
630 683
631 init_constants(); 684 init_constants();
632 685
686 #ifdef IS_PY3K
687 module = PyModule_Create(&module_def);
688 #else
633 module = Py_InitModule("_speedups", NULL); 689 module = Py_InitModule("_speedups", NULL);
690 #endif
634 Py_INCREF(&MarkupType); 691 Py_INCREF(&MarkupType);
635 PyModule_AddObject(module, "Markup", (PyObject *) &MarkupType); 692 PyModule_AddObject(module, "Markup", (PyObject *) &MarkupType);
636 } 693
694 #ifdef IS_PY3K
695 return module;
696 #endif
697 }
Copyright (C) 2012-2017 Edgewall Software