/usr/lib/python3/dist-packages/genshi/_speedups.c is in python3-genshi 0.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | /*
* Copyright (C) 2006-2008 Edgewall Software
* All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://genshi.edgewall.org/wiki/License.
*
* This software consists of voluntary contributions made by many
* individuals. For the exact contribution history, see the revision
* history and logs, available at http://genshi.edgewall.org/log/.
*/
#include <Python.h>
#include <structmember.h>
#if PY_MAJOR_VERSION > 2
# define IS_PY3K
#elif PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
# define PY_SSIZE_T_MAX INT_MAX
# define PY_SSIZE_T_MIN INT_MIN
#endif
/* We only use Unicode Strings in this module */
#ifndef IS_PY3K
# define PyObject_Str PyObject_Unicode
#endif
static PyObject *amp1, *amp2, *lt1, *lt2, *gt1, *gt2, *qt1, *qt2;
static PyObject *stripentities, *striptags;
static void
init_constants(void)
{
PyObject *util = PyImport_ImportModule("genshi.util");
stripentities = PyObject_GetAttrString(util, "stripentities");
striptags = PyObject_GetAttrString(util, "striptags");
Py_DECREF(util);
amp1 = PyUnicode_DecodeASCII("&", 1, NULL);
amp2 = PyUnicode_DecodeASCII("&", 5, NULL);
lt1 = PyUnicode_DecodeASCII("<", 1, NULL);
lt2 = PyUnicode_DecodeASCII("<", 4, NULL);
gt1 = PyUnicode_DecodeASCII(">", 1, NULL);
gt2 = PyUnicode_DecodeASCII(">", 4, NULL);
qt1 = PyUnicode_DecodeASCII("\"", 1, NULL);
qt2 = PyUnicode_DecodeASCII(""", 5, NULL);
}
/* Markup class */
PyTypeObject MarkupType; /* declared later */
PyDoc_STRVAR(Markup__doc__,
"Marks a string as being safe for inclusion in HTML/XML output without\n\
needing to be escaped.");
static PyObject *
escape(PyObject *text, int quotes)
{
PyObject *args, *ret;
PyUnicodeObject *in, *out;
Py_UNICODE *inp, *outp;
int len, inn, outn;
if (PyObject_TypeCheck(text, &MarkupType)) {
Py_INCREF(text);
return text;
}
if (PyObject_HasAttrString(text, "__html__")) {
ret = PyObject_CallMethod(text, "__html__", NULL);
args = PyTuple_New(1);
if (args == NULL) {
Py_DECREF(ret);
return NULL;
}
PyTuple_SET_ITEM(args, 0, ret);
ret = MarkupType.tp_new(&MarkupType, args, NULL);
Py_DECREF(args);
return ret;
}
in = (PyUnicodeObject *) PyObject_Str(text);
if (in == NULL) {
return NULL;
}
/* First we need to figure out how long the escaped string will be */
len = inn = 0;
inp = in->str;
while (*(inp) || in->length > inp - in->str) {
switch (*inp++) {
case '&': len += 5; inn++; break;
case '"': len += quotes ? 5 : 1; inn += quotes ? 1 : 0; break;
case '<':
case '>': len += 4; inn++; break;
default: len++;
}
}
/* Do we need to escape anything at all? */
if (!inn) {
args = PyTuple_New(1);
if (args == NULL) {
Py_DECREF((PyObject *) in);
return NULL;
}
PyTuple_SET_ITEM(args, 0, (PyObject *) in);
ret = MarkupType.tp_new(&MarkupType, args, NULL);
Py_DECREF(args);
return ret;
}
out = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, len);
if (out == NULL) {
Py_DECREF((PyObject *) in);
return NULL;
}
outn = 0;
inp = in->str;
outp = out->str;
while (*(inp) || in->length > inp - in->str) {
if (outn == inn) {
/* copy rest of string if we have already replaced everything */
Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str));
break;
}
switch (*inp) {
case '&':
Py_UNICODE_COPY(outp, ((PyUnicodeObject *) amp2)->str, 5);
outp += 5;
outn++;
break;
case '"':
if (quotes) {
Py_UNICODE_COPY(outp, ((PyUnicodeObject *) qt2)->str, 5);
outp += 5;
outn++;
} else {
*outp++ = *inp;
}
break;
case '<':
Py_UNICODE_COPY(outp, ((PyUnicodeObject *) lt2)->str, 4);
outp += 4;
outn++;
break;
case '>':
Py_UNICODE_COPY(outp, ((PyUnicodeObject *) gt2)->str, 4);
outp += 4;
outn++;
break;
default:
*outp++ = *inp;
}
inp++;
}
Py_DECREF((PyObject *) in);
args = PyTuple_New(1);
if (args == NULL) {
Py_DECREF((PyObject *) out);
return NULL;
}
PyTuple_SET_ITEM(args, 0, (PyObject *) out);
ret = MarkupType.tp_new(&MarkupType, args, NULL);
Py_DECREF(args);
return ret;
}
PyDoc_STRVAR(escape__doc__,
"Create a Markup instance from a string and escape special characters\n\
it may contain (<, >, & and \").\n\
\n\
>>> escape('\"1 < 2\"')\n\
<Markup u'"1 < 2"'>\n\
\n\
If the `quotes` parameter is set to `False`, the \" character is left\n\
as is. Escaping quotes is generally only required for strings that are\n\
to be used in attribute values.\n\
\n\
>>> escape('\"1 < 2\"', quotes=False)\n\
<Markup u'\"1 < 2\"'>\n\
\n\
:param text: the text to escape\n\
:param quotes: if ``True``, double quote characters are escaped in\n\
addition to the other special characters\n\
:return: the escaped `Markup` string\n\
:rtype: `Markup`\n\
");
static PyObject *
Markup_escape(PyTypeObject* type, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"text", "quotes", 0};
PyObject *text = NULL;
char quotes = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|b", kwlist, &text, "es)) {
return NULL;
}
if (PyObject_Not(text)) {
args = PyTuple_New(0);
if (args == NULL)
return NULL;
text = type->tp_new(type, args, NULL);
Py_DECREF(args);
return text;
}
if (PyObject_TypeCheck(text, type)) {
Py_INCREF(text);
return text;
}
return escape(text, quotes);
}
static PyObject *
Markup_html(PyObject *self)
{
Py_INCREF(self);
return self;
}
PyDoc_STRVAR(join__doc__,
"Return a `Markup` object which is the concatenation of the strings\n\
in the given sequence, where this `Markup` object is the separator\n\
between the joined elements.\n\
\n\
Any element in the sequence that is not a `Markup` instance is\n\
automatically escaped.\n\
\n\
:param seq: the sequence of strings to join\n\
:param escape_quotes: whether double quote characters in the elements\n\
should be escaped\n\
:return: the joined `Markup` object\n\
:rtype: `Markup`\n\
:see: `escape`\n\
");
static PyObject *
Markup_join(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"seq", "escape_quotes", 0};
PyObject *seq = NULL, *seq2, *tmp, *tmp2;
char quotes = 1;
Py_ssize_t n;
int i;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|b", kwlist, &seq, "es)) {
return NULL;
}
if (!PySequence_Check(seq)) {
return NULL;
}
n = PySequence_Size(seq);
if (n < 0) {
return NULL;
}
seq2 = PyTuple_New(n);
if (seq2 == NULL) {
return NULL;
}
for (i = 0; i < n; i++) {
tmp = PySequence_GetItem(seq, i);
if (tmp == NULL) {
Py_DECREF(seq2);
return NULL;
}
tmp2 = escape(tmp, quotes);
if (tmp2 == NULL) {
Py_DECREF(seq2);
return NULL;
}
PyTuple_SET_ITEM(seq2, i, tmp2);
Py_DECREF(tmp);
}
tmp = PyUnicode_Join(self, seq2);
Py_DECREF(seq2);
if (tmp == NULL)
return NULL;
args = PyTuple_New(1);
if (args == NULL) {
Py_DECREF(tmp);
return NULL;
}
PyTuple_SET_ITEM(args, 0, tmp);
tmp = MarkupType.tp_new(&MarkupType, args, NULL);
Py_DECREF(args);
return tmp;
}
static PyObject *
Markup_add(PyObject *self, PyObject *other)
{
PyObject *tmp, *tmp2, *args, *ret;
if (PyObject_TypeCheck(self, &MarkupType)) {
tmp = escape(other, 1);
if (tmp == NULL)
return NULL;
tmp2 = PyUnicode_Concat(self, tmp);
} else { // __radd__
tmp = escape(self, 1);
if (tmp == NULL)
return NULL;
tmp2 = PyUnicode_Concat(tmp, other);
}
Py_DECREF(tmp);
if (tmp2 == NULL)
return NULL;
args = PyTuple_New(1);
if (args == NULL) {
Py_DECREF(tmp2);
return NULL;
}
PyTuple_SET_ITEM(args, 0, tmp2);
ret = MarkupType.tp_new(&MarkupType, args, NULL);
Py_DECREF(args);
return ret;
}
static PyObject *
Markup_mod(PyObject *self, PyObject *args)
{
PyObject *tmp, *tmp2, *ret, *args2;
int i;
Py_ssize_t nargs = 0;
PyObject *kwds = NULL;
if (PyDict_Check(args)) {
kwds = args;
}
if (kwds && PyDict_Size(kwds)) {
PyObject *kwcopy, *key, *value;
Py_ssize_t pos = 0;
kwcopy = PyDict_Copy( kwds );
if (kwcopy == NULL) {
return NULL;
}
while (PyDict_Next(kwcopy, &pos, &key, &value)) {
tmp = escape(value, 1);
if (tmp == NULL) {
Py_DECREF(kwcopy);
return NULL;
}
if (PyDict_SetItem(kwcopy, key, tmp) < 0) {
Py_DECREF(tmp);
Py_DECREF(kwcopy);
return NULL;
}
}
tmp = PyUnicode_Format(self, kwcopy);
Py_DECREF(kwcopy);
if (tmp == NULL) {
return NULL;
}
} else if (PyTuple_Check(args)) {
nargs = PyTuple_GET_SIZE(args);
args2 = PyTuple_New(nargs);
if (args2 == NULL) {
return NULL;
}
for (i = 0; i < nargs; i++) {
tmp = escape(PyTuple_GET_ITEM(args, i), 1);
if (tmp == NULL) {
Py_DECREF(args2);
return NULL;
}
PyTuple_SET_ITEM(args2, i, tmp);
}
tmp = PyUnicode_Format(self, args2);
Py_DECREF(args2);
if (tmp == NULL) {
return NULL;
}
} else {
tmp2 = escape(args, 1);
if (tmp2 == NULL) {
return NULL;
}
tmp = PyUnicode_Format(self, tmp2);
Py_DECREF(tmp2);
if (tmp == NULL) {
return NULL;
}
}
args = PyTuple_New(1);
if (args == NULL) {
Py_DECREF(tmp);
return NULL;
}
PyTuple_SET_ITEM(args, 0, tmp);
ret = PyUnicode_Type.tp_new(&MarkupType, args, NULL);
Py_DECREF(args);
return ret;
}
static PyObject *
Markup_mul(PyObject *self, PyObject *num)
{
PyObject *unicode, *result, *args;
if (PyObject_TypeCheck(self, &MarkupType)) {
unicode = PyObject_Str(self);
if (unicode == NULL) return NULL;
result = PyNumber_Multiply(unicode, num);
} else { // __rmul__
unicode = PyObject_Str(num);
if (unicode == NULL) return NULL;
result = PyNumber_Multiply(unicode, self);
}
Py_DECREF(unicode);
if (result == NULL) return NULL;
args = PyTuple_New(1);
if (args == NULL) {
Py_DECREF(result);
return NULL;
}
PyTuple_SET_ITEM(args, 0, result);
result = PyUnicode_Type.tp_new(&MarkupType, args, NULL);
Py_DECREF(args);
return result;
}
static PyObject *
Markup_repr(PyObject *self)
{
PyObject *format, *result, *args;
#ifdef IS_PY3K
format = PyUnicode_FromString("<Markup %r>");
#else
format = PyString_FromString("<Markup %r>");
#endif
if (format == NULL) return NULL;
result = PyObject_Str(self);
if (result == NULL) {
Py_DECREF(format);
return NULL;
}
args = PyTuple_New(1);
if (args == NULL) {
Py_DECREF(format);
Py_DECREF(result);
return NULL;
}
PyTuple_SET_ITEM(args, 0, result);
#ifdef IS_PY3K
result = PyUnicode_Format(format, args);
#else
result = PyString_Format(format, args);
#endif
Py_DECREF(format);
Py_DECREF(args);
return result;
}
PyDoc_STRVAR(unescape__doc__,
"Reverse-escapes &, <, >, and \" and returns a `unicode` object.\n\
\n\
>>> Markup('1 < 2').unescape()\n\
u'1 < 2'\n\
\n\
:return: the unescaped string\n\
:rtype: `unicode`\n\
:see: `genshi.core.unescape`\n\
");
static PyObject *
Markup_unescape(PyObject* self)
{
PyObject *tmp, *tmp2;
tmp = PyUnicode_Replace(self, qt2, qt1, -1);
if (tmp == NULL) return NULL;
tmp2 = PyUnicode_Replace(tmp, gt2, gt1, -1);
Py_DECREF(tmp);
if (tmp2 == NULL) return NULL;
tmp = PyUnicode_Replace(tmp2, lt2, lt1, -1);
Py_DECREF(tmp2);
if (tmp == NULL) return NULL;
tmp2 = PyUnicode_Replace(tmp, amp2, amp1, -1);
Py_DECREF(tmp);
return tmp2;
}
PyDoc_STRVAR(stripentities__doc__,
"Return a copy of the text with any character or numeric entities\n\
replaced by the equivalent UTF-8 characters.\n\
\n\
If the `keepxmlentities` parameter is provided and evaluates to `True`,\n\
the core XML entities (``&``, ``'``, ``>``, ``<`` and\n\
``"``) are not stripped.\n\
\n\
:return: a `Markup` instance with entities removed\n\
:rtype: `Markup`\n\
:see: `genshi.util.stripentities`\n\
");
static PyObject *
Markup_stripentities(PyObject* self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"keepxmlentities", 0};
PyObject *result, *args2;
char keepxml = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|b", kwlist, &keepxml)) {
return NULL;
}
if (stripentities == NULL) return NULL;
result = PyObject_CallFunction(stripentities, "Ob", self, keepxml);
if (result == NULL) return NULL;
args2 = PyTuple_New(1);
if (args2 == NULL) {
Py_DECREF(result);
return NULL;
}
PyTuple_SET_ITEM(args2, 0, result);
result = MarkupType.tp_new(&MarkupType, args2, NULL);
Py_DECREF(args2);
return result;
}
PyDoc_STRVAR(striptags__doc__,
"""Return a copy of the text with all XML/HTML tags removed.\n\
\n\
:return: a `Markup` instance with all tags removed\n\
:rtype: `Markup`\n\
:see: `genshi.util.striptags`\n\
");
static PyObject *
Markup_striptags(PyObject* self)
{
PyObject *result, *args;
if (striptags == NULL) return NULL;
result = PyObject_CallFunction(striptags, "O", self);
if (result == NULL) return NULL;
args = PyTuple_New(1);
if (args == NULL) {
Py_DECREF(result);
return NULL;
}
PyTuple_SET_ITEM(args, 0, result);
result = MarkupType.tp_new(&MarkupType, args, NULL);
Py_DECREF(args);
return result;
}
typedef struct {
PyUnicodeObject HEAD;
} MarkupObject;
static PyMethodDef Markup_methods[] = {
{"__html__", (PyCFunction) Markup_html, METH_NOARGS, NULL},
{"escape", (PyCFunction) Markup_escape,
METH_VARARGS|METH_CLASS|METH_KEYWORDS, escape__doc__},
{"join", (PyCFunction)Markup_join, METH_VARARGS|METH_KEYWORDS, join__doc__},
{"unescape", (PyCFunction)Markup_unescape, METH_NOARGS, unescape__doc__},
{"stripentities", (PyCFunction) Markup_stripentities,
METH_VARARGS|METH_KEYWORDS, stripentities__doc__},
{"striptags", (PyCFunction) Markup_striptags, METH_NOARGS,
striptags__doc__},
{NULL} /* Sentinel */
};
static PyNumberMethods Markup_as_number = {
Markup_add, /*nb_add*/
0, /*nb_subtract*/
Markup_mul, /*nb_multiply*/
#ifndef IS_PY3K
0, /*nb_divide*/
#endif
Markup_mod, /*nb_remainder*/
};
PyTypeObject MarkupType = {
#ifdef IS_PY3K
PyVarObject_HEAD_INIT(NULL, 0)
#else
PyObject_HEAD_INIT(NULL)
0,
#endif
"genshi._speedups.Markup",
sizeof(MarkupObject),
0,
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
#ifdef IS_PY3K
0, /*tp_reserved*/
#else
0, /*tp_compare*/
#endif
Markup_repr, /*tp_repr*/
&Markup_as_number, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
#ifdef IS_PY3K
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /*tp_flags*/
#elif defined(Py_TPFLAGS_UNICODE_SUBCLASS)
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES | Py_TPFLAGS_UNICODE_SUBCLASS, /*tp_flags*/
#else
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
#endif
Markup__doc__,/*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
/* Attribute descriptor and subclassing stuff */
Markup_methods,/*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
0, /*tp_init*/
0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
0, /*tp_new*/
0, /*tp_free Low-level free-memory routine */
0, /*tp_is_gc For PyObject_IS_GC */
0, /*tp_bases*/
0, /*tp_mro method resolution order */
0, /*tp_cache*/
0, /*tp_subclasses*/
0 /*tp_weaklist*/
};
#ifdef IS_PY3K
struct PyModuleDef module_def = {
PyModuleDef_HEAD_INIT, /*m_base*/
"_speedups", /*m_name*/
NULL, /*m_doc*/
-1, /*m_size*/
NULL, /*m_methods*/
NULL, /*m_reload*/
NULL, /*m_traverse*/
NULL, /*m_clear*/
NULL /*m_free*/
};
PyObject *
PyInit__speedups(void)
#else
PyMODINIT_FUNC
init_speedups(void)
#endif
{
PyObject *module;
/* Workaround for quirk in Visual Studio, see
<http://www.python.it/faq/faq-3.html#3.24> */
MarkupType.tp_base = &PyUnicode_Type;
if (PyType_Ready(&MarkupType) < 0)
#ifdef IS_PY3K
return NULL;
#else
return;
#endif
init_constants();
#ifdef IS_PY3K
module = PyModule_Create(&module_def);
#else
module = Py_InitModule("_speedups", NULL);
#endif
Py_INCREF(&MarkupType);
PyModule_AddObject(module, "Markup", (PyObject *) &MarkupType);
#ifdef IS_PY3K
return module;
#endif
}
|