| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include <Python.h> |
| 15 |
#include <string.h> |
| 16 |
#include <stdlib.h> |
| 17 |
|
| 18 |
#include "_namemapper.h" |
| 19 |
|
| 20 |
#ifdef __cplusplus |
| 21 |
extern "C" { |
| 22 |
#endif |
| 23 |
|
| 24 |
|
| 25 |
static PyObject *NotFound; |
| 26 |
static PyObject *TooManyPeriods; |
| 27 |
static PyObject* pprintMod_pformat; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
static void setNotFoundException(char *key, PyObject *namespace) |
| 35 |
{ |
| 36 |
PyObject *exceptionStr = NULL; |
| 37 |
exceptionStr = PyUnicode_FromFormat("cannot find \'%s\'", key); |
| 38 |
PyErr_SetObject(NotFound, exceptionStr); |
| 39 |
Py_XDECREF(exceptionStr); |
| 40 |
} |
| 41 |
|
| 42 |
static int wrapInternalNotFoundException(char *fullName, PyObject *namespace) |
| 43 |
{ |
| 44 |
PyObject *excType, *excValue, *excTraceback, *isAlreadyWrapped = NULL; |
| 45 |
PyObject *newExcValue = NULL; |
| 46 |
if (!ALLOW_WRAPPING_OF_NOTFOUND_EXCEPTIONS) { |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
if (!PyErr_Occurred()) { |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
if (PyErr_GivenExceptionMatches(PyErr_Occurred(), NotFound)) { |
| 55 |
PyErr_Fetch(&excType, &excValue, &excTraceback); |
| 56 |
isAlreadyWrapped = PyObject_CallMethod(excValue, "find", "s", "while searching"); |
| 57 |
|
| 58 |
if (isAlreadyWrapped != NULL) { |
| 59 |
if (PyLong_AsLong(isAlreadyWrapped) == -1) { |
| 60 |
newExcValue = PyUnicode_FromFormat("%U while searching for \'%s\'", |
| 61 |
excValue, fullName); |
| 62 |
} |
| 63 |
Py_DECREF(isAlreadyWrapped); |
| 64 |
} |
| 65 |
else { |
| 66 |
newExcValue = excValue; |
| 67 |
} |
| 68 |
PyErr_Restore(excType, newExcValue, excTraceback); |
| 69 |
return -1; |
| 70 |
} |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
static int isInstanceOrClass(PyObject *nextVal) { |
| 76 |
#ifndef IS_PYTHON3 |
| 77 |
|
| 78 |
if((PyInstance_Check(nextVal)) || (PyClass_Check(nextVal))) { |
| 79 |
return 1; |
| 80 |
} |
| 81 |
#endif |
| 82 |
|
| 83 |
if (!PyObject_HasAttrString(nextVal, "__class__")) { |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if (PyType_Check(nextVal) || PyObject_HasAttrString(nextVal, "mro")) { |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
if (strncmp(nextVal->ob_type->tp_name, "function", 9) == 0) |
| 93 |
return 0; |
| 94 |
|
| 95 |
|
| 96 |
if (PyObject_HasAttrString(nextVal, "__func__") |
| 97 |
|| PyObject_HasAttrString(nextVal, "__code__") |
| 98 |
|| PyObject_HasAttrString(nextVal, "__self__")) { |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
if ((!PyObject_HasAttrString(nextVal, "mro")) && |
| 104 |
PyObject_HasAttrString(nextVal, "__init__")) { |
| 105 |
return 1; |
| 106 |
} |
| 107 |
|
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
static int getNameChunks(char *nameChunks[], char *name, char *nameCopy) |
| 113 |
{ |
| 114 |
char c; |
| 115 |
char *currChunk; |
| 116 |
int currChunkNum = 0; |
| 117 |
|
| 118 |
currChunk = nameCopy; |
| 119 |
while ('\0' != (c = *nameCopy)){ |
| 120 |
if ('.' == c) { |
| 121 |
if (currChunkNum >= (MAXCHUNKS-2)) { |
| 122 |
PyErr_SetString(TooManyPeriods, name); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
*nameCopy ='\0'; |
| 127 |
nameChunks[currChunkNum++] = currChunk; |
| 128 |
nameCopy++; |
| 129 |
currChunk = nameCopy; |
| 130 |
} else |
| 131 |
nameCopy++; |
| 132 |
} |
| 133 |
if (nameCopy > currChunk) { |
| 134 |
nameChunks[currChunkNum++] = currChunk; |
| 135 |
} |
| 136 |
return currChunkNum; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
static int PyNamemapper_hasKey(PyObject *obj, char *key) |
| 141 |
{ |
| 142 |
if (PyMapping_Check(obj) && PyMapping_HasKeyString(obj, key)) { |
| 143 |
return TRUE; |
| 144 |
} else if (PyObject_HasAttrString(obj, key)) { |
| 145 |
return TRUE; |
| 146 |
} |
| 147 |
return FALSE; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
static PyObject *PyNamemapper_valueForKey(PyObject *obj, char *key) |
| 152 |
{ |
| 153 |
PyObject *theValue = NULL; |
| 154 |
|
| 155 |
if (PyMapping_Check(obj) && PyMapping_HasKeyString(obj, key)) { |
| 156 |
theValue = PyMapping_GetItemString(obj, key); |
| 157 |
} else if (PyObject_HasAttrString(obj, key)) { |
| 158 |
theValue = PyObject_GetAttrString(obj, key); |
| 159 |
} else { |
| 160 |
setNotFoundException(key, obj); |
| 161 |
} |
| 162 |
return theValue; |
| 163 |
} |
| 164 |
|
| 165 |
static PyObject *PyNamemapper_valueForName(PyObject *obj, char *nameChunks[], int numChunks, int executeCallables) |
| 166 |
{ |
| 167 |
int i; |
| 168 |
char *currentKey; |
| 169 |
PyObject *currentVal = NULL; |
| 170 |
PyObject *nextVal = NULL; |
| 171 |
|
| 172 |
currentVal = obj; |
| 173 |
for (i=0; i < numChunks;i++) { |
| 174 |
currentKey = nameChunks[i]; |
| 175 |
if (PyErr_CheckSignals()) { |
| 176 |
if (i>0) { |
| 177 |
Py_DECREF(currentVal); |
| 178 |
} |
| 179 |
return NULL; |
| 180 |
} |
| 181 |
|
| 182 |
#if PY_VERSION_HEX >= 0x030d0000 |
| 183 |
|
| 184 |
if (PyMapping_Check(currentVal) && PyMapping_GetOptionalItemString(currentVal, currentKey, &nextVal) |
| 185 |
&& (!PyErr_Occurred()) |
| 186 |
) { |
| 187 |
#else |
| 188 |
if (PyMapping_Check(currentVal) && PyMapping_HasKeyString(currentVal, currentKey)) { |
| 189 |
nextVal = PyMapping_GetItemString(currentVal, currentKey); |
| 190 |
#endif |
| 191 |
|
| 192 |
} else { |
| 193 |
#if PY_VERSION_HEX >= 0x030d0000 |
| 194 |
if ((PyErr_Occurred() != NULL) && |
| 195 |
( |
| 196 |
PyErr_ExceptionMatches(PyExc_IndexError) || |
| 197 |
PyErr_ExceptionMatches(PyExc_TypeError) |
| 198 |
)) { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
PyErr_Clear(); |
| 203 |
} |
| 204 |
#endif |
| 205 |
PyObject *exc; |
| 206 |
nextVal = PyObject_GetAttrString(currentVal, currentKey); |
| 207 |
exc = PyErr_Occurred(); |
| 208 |
|
| 209 |
if (exc != NULL) { |
| 210 |
|
| 211 |
if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 212 |
setNotFoundException(currentKey, currentVal); |
| 213 |
} |
| 214 |
|
| 215 |
if (i > 0) { |
| 216 |
Py_DECREF(currentVal); |
| 217 |
} |
| 218 |
return NULL; |
| 219 |
} |
| 220 |
|
| 221 |
if (nextVal == NULL) { |
| 222 |
setNotFoundException(currentKey, currentVal); |
| 223 |
|
| 224 |
if (i > 0) { |
| 225 |
Py_DECREF(currentVal); |
| 226 |
} |
| 227 |
return NULL; |
| 228 |
} |
| 229 |
} |
| 230 |
if (i > 0) { |
| 231 |
Py_DECREF(currentVal); |
| 232 |
} |
| 233 |
|
| 234 |
if (executeCallables && PyCallable_Check(nextVal) && |
| 235 |
(isInstanceOrClass(nextVal) == 0) ) { |
| 236 |
if (!(currentVal = PyObject_CallObject(nextVal, NULL))) { |
| 237 |
Py_DECREF(nextVal); |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
Py_DECREF(nextVal); |
| 241 |
} else { |
| 242 |
currentVal = nextVal; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
return currentVal; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
static PyObject *namemapper_valueForKey(PyObject *self, PyObject *args) |
| 256 |
{ |
| 257 |
PyObject *obj; |
| 258 |
char *key; |
| 259 |
|
| 260 |
if (!PyArg_ParseTuple(args, "Os", &obj, &key)) { |
| 261 |
return NULL; |
| 262 |
} |
| 263 |
|
| 264 |
return PyNamemapper_valueForKey(obj, key); |
| 265 |
} |
| 266 |
|
| 267 |
static PyObject *namemapper_valueForName(PYARGS) |
| 268 |
{ |
| 269 |
PyObject *obj; |
| 270 |
char *name; |
| 271 |
int executeCallables = 0; |
| 272 |
|
| 273 |
char *nameCopy = NULL; |
| 274 |
char *tmpPntr1 = NULL; |
| 275 |
char *tmpPntr2 = NULL; |
| 276 |
char *nameChunks[MAXCHUNKS]; |
| 277 |
int numChunks; |
| 278 |
|
| 279 |
PyObject *theValue; |
| 280 |
|
| 281 |
static char *kwlist[] = {"obj", "name", "executeCallables", NULL}; |
| 282 |
|
| 283 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os|i", kwlist, &obj, &name, &executeCallables)) { |
| 284 |
return NULL; |
| 285 |
} |
| 286 |
|
| 287 |
createNameCopyAndChunks(); |
| 288 |
|
| 289 |
if (numChunks <= 0) { |
| 290 |
PyErr_SetString(PyExc_ValueError, "Invalid lookup of empty name"); |
| 291 |
theValue = NULL; |
| 292 |
} else { |
| 293 |
theValue = PyNamemapper_valueForName(obj, nameChunks, numChunks, executeCallables); |
| 294 |
if (wrapInternalNotFoundException(name, obj)) { |
| 295 |
theValue = NULL; |
| 296 |
} |
| 297 |
} |
| 298 |
free(nameCopy); |
| 299 |
return theValue; |
| 300 |
} |
| 301 |
|
| 302 |
static PyObject *namemapper_valueFromSearchList(PYARGS) |
| 303 |
{ |
| 304 |
PyObject *searchList; |
| 305 |
char *name; |
| 306 |
int executeCallables = 0; |
| 307 |
|
| 308 |
char *nameCopy = NULL; |
| 309 |
char *tmpPntr1 = NULL; |
| 310 |
char *tmpPntr2 = NULL; |
| 311 |
char *nameChunks[MAXCHUNKS]; |
| 312 |
int numChunks; |
| 313 |
|
| 314 |
PyObject *nameSpace = NULL; |
| 315 |
PyObject *theValue = NULL; |
| 316 |
PyObject *iterator = NULL; |
| 317 |
|
| 318 |
static char *kwlist[] = {"searchList", "name", "executeCallables", NULL}; |
| 319 |
|
| 320 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os|i", kwlist, &searchList, &name, &executeCallables)) { |
| 321 |
return NULL; |
| 322 |
} |
| 323 |
|
| 324 |
createNameCopyAndChunks(); |
| 325 |
|
| 326 |
if (numChunks <= 0) { |
| 327 |
PyErr_SetString(PyExc_ValueError, "Invalid lookup of empty name"); |
| 328 |
goto done; |
| 329 |
} |
| 330 |
|
| 331 |
iterator = PyObject_GetIter(searchList); |
| 332 |
if (iterator == NULL) { |
| 333 |
PyErr_SetString(PyExc_TypeError,"This searchList is not iterable!"); |
| 334 |
goto done; |
| 335 |
} |
| 336 |
|
| 337 |
while ((nameSpace = PyIter_Next(iterator))) { |
| 338 |
checkForNameInNameSpaceAndReturnIfFound(TRUE); |
| 339 |
Py_DECREF(nameSpace); |
| 340 |
if(PyErr_CheckSignals()) { |
| 341 |
theValue = NULL; |
| 342 |
goto done; |
| 343 |
} |
| 344 |
} |
| 345 |
if (PyErr_Occurred()) { |
| 346 |
theValue = NULL; |
| 347 |
goto done; |
| 348 |
} |
| 349 |
|
| 350 |
setNotFoundException(nameChunks[0], searchList); |
| 351 |
|
| 352 |
done: |
| 353 |
Py_XDECREF(iterator); |
| 354 |
free(nameCopy); |
| 355 |
return theValue; |
| 356 |
} |
| 357 |
|
| 358 |
static PyObject *namemapper_valueFromFrameOrSearchList(PyObject *self, PyObject *args, PyObject *keywds) |
| 359 |
{ |
| 360 |
|
| 361 |
char *name; |
| 362 |
int executeCallables = 0; |
| 363 |
PyObject *searchList = NULL; |
| 364 |
|
| 365 |
|
| 366 |
char *nameCopy = NULL; |
| 367 |
char *tmpPntr1 = NULL; |
| 368 |
char *tmpPntr2 = NULL; |
| 369 |
char *nameChunks[MAXCHUNKS]; |
| 370 |
int numChunks; |
| 371 |
|
| 372 |
PyObject *nameSpace = NULL; |
| 373 |
PyObject *theValue = NULL; |
| 374 |
PyObject *excString = NULL; |
| 375 |
PyObject *iterator = NULL; |
| 376 |
|
| 377 |
static char *kwlist[] = {"searchList", "name", "executeCallables", NULL}; |
| 378 |
|
| 379 |
if (!PyArg_ParseTupleAndKeywords(args, keywds, "Os|i", kwlist, &searchList, &name, |
| 380 |
&executeCallables)) { |
| 381 |
return NULL; |
| 382 |
} |
| 383 |
|
| 384 |
createNameCopyAndChunks(); |
| 385 |
|
| 386 |
if (numChunks <= 0) { |
| 387 |
PyErr_SetString(PyExc_ValueError, "Invalid lookup of empty name"); |
| 388 |
goto done; |
| 389 |
} |
| 390 |
|
| 391 |
nameSpace = PyEval_GetLocals(); |
| 392 |
checkForNameInNameSpaceAndReturnIfFound(FALSE); |
| 393 |
|
| 394 |
iterator = PyObject_GetIter(searchList); |
| 395 |
if (iterator == NULL) { |
| 396 |
PyErr_SetString(PyExc_TypeError,"This searchList is not iterable!"); |
| 397 |
goto done; |
| 398 |
} |
| 399 |
while ( (nameSpace = PyIter_Next(iterator)) ) { |
| 400 |
checkForNameInNameSpaceAndReturnIfFound(TRUE); |
| 401 |
Py_DECREF(nameSpace); |
| 402 |
if(PyErr_CheckSignals()) { |
| 403 |
theValue = NULL; |
| 404 |
goto done; |
| 405 |
} |
| 406 |
} |
| 407 |
if (PyErr_Occurred()) { |
| 408 |
theValue = NULL; |
| 409 |
goto done; |
| 410 |
} |
| 411 |
|
| 412 |
nameSpace = PyEval_GetGlobals(); |
| 413 |
checkForNameInNameSpaceAndReturnIfFound(FALSE); |
| 414 |
|
| 415 |
nameSpace = PyEval_GetBuiltins(); |
| 416 |
checkForNameInNameSpaceAndReturnIfFound(FALSE); |
| 417 |
|
| 418 |
excString = Py_BuildValue("s", "[locals()]+searchList+[globals(), __builtins__]"); |
| 419 |
setNotFoundException(nameChunks[0], excString); |
| 420 |
Py_DECREF(excString); |
| 421 |
|
| 422 |
done: |
| 423 |
Py_XDECREF(iterator); |
| 424 |
free(nameCopy); |
| 425 |
return theValue; |
| 426 |
} |
| 427 |
|
| 428 |
static PyObject *namemapper_valueFromFrame(PyObject *self, PyObject *args, PyObject *keywds) |
| 429 |
{ |
| 430 |
|
| 431 |
char *name; |
| 432 |
int executeCallables = 0; |
| 433 |
|
| 434 |
|
| 435 |
char *tmpPntr1 = NULL; |
| 436 |
char *tmpPntr2 = NULL; |
| 437 |
|
| 438 |
char *nameCopy = NULL; |
| 439 |
char *nameChunks[MAXCHUNKS]; |
| 440 |
int numChunks; |
| 441 |
|
| 442 |
PyObject *nameSpace = NULL; |
| 443 |
PyObject *theValue = NULL; |
| 444 |
PyObject *excString = NULL; |
| 445 |
|
| 446 |
static char *kwlist[] = {"name", "executeCallables", NULL}; |
| 447 |
|
| 448 |
if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|i", kwlist, &name, &executeCallables)) { |
| 449 |
return NULL; |
| 450 |
} |
| 451 |
|
| 452 |
createNameCopyAndChunks(); |
| 453 |
|
| 454 |
if (numChunks <= 0) { |
| 455 |
PyErr_SetString(PyExc_ValueError, "Invalid lookup of empty name"); |
| 456 |
goto done; |
| 457 |
} |
| 458 |
|
| 459 |
nameSpace = PyEval_GetLocals(); |
| 460 |
checkForNameInNameSpaceAndReturnIfFound(FALSE); |
| 461 |
|
| 462 |
nameSpace = PyEval_GetGlobals(); |
| 463 |
checkForNameInNameSpaceAndReturnIfFound(FALSE); |
| 464 |
|
| 465 |
nameSpace = PyEval_GetBuiltins(); |
| 466 |
checkForNameInNameSpaceAndReturnIfFound(FALSE); |
| 467 |
|
| 468 |
excString = Py_BuildValue("s", "[locals(), globals(), __builtins__]"); |
| 469 |
setNotFoundException(nameChunks[0], excString); |
| 470 |
Py_DECREF(excString); |
| 471 |
done: |
| 472 |
free(nameCopy); |
| 473 |
return theValue; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
static struct PyMethodDef namemapper_methods[] = { |
| 480 |
{"valueForKey", namemapper_valueForKey, 1}, |
| 481 |
{"valueForName", (PyCFunction)namemapper_valueForName, METH_VARARGS|METH_KEYWORDS}, |
| 482 |
{"valueFromSearchList", (PyCFunction)namemapper_valueFromSearchList, METH_VARARGS|METH_KEYWORDS}, |
| 483 |
{"valueFromFrame", (PyCFunction)namemapper_valueFromFrame, METH_VARARGS|METH_KEYWORDS}, |
| 484 |
{"valueFromFrameOrSearchList", (PyCFunction)namemapper_valueFromFrameOrSearchList, METH_VARARGS|METH_KEYWORDS}, |
| 485 |
{NULL, NULL} |
| 486 |
}; |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
#ifdef IS_PYTHON3 |
| 493 |
static struct PyModuleDef namemappermodule = { |
| 494 |
PyModuleDef_HEAD_INIT, |
| 495 |
"_namemapper", |
| 496 |
NULL, |
| 497 |
-1, |
| 498 |
namemapper_methods, |
| 499 |
NULL, |
| 500 |
NULL, |
| 501 |
NULL, |
| 502 |
NULL}; |
| 503 |
|
| 504 |
PyMODINIT_FUNC PyInit__namemapper(void) |
| 505 |
{ |
| 506 |
PyObject *m = PyModule_Create(&namemappermodule); |
| 507 |
#else |
| 508 |
DL_EXPORT(void) init_namemapper(void) |
| 509 |
{ |
| 510 |
PyObject *m = Py_InitModule3("_namemapper", namemapper_methods, NULL); |
| 511 |
#endif |
| 512 |
|
| 513 |
PyObject *d, *pprintMod; |
| 514 |
|
| 515 |
|
| 516 |
d = PyModule_GetDict(m); |
| 517 |
NotFound = PyErr_NewException("NameMapper.NotFound",PyExc_LookupError,NULL); |
| 518 |
TooManyPeriods = PyErr_NewException("NameMapper.TooManyPeriodsInName",NULL,NULL); |
| 519 |
PyDict_SetItemString(d, "NotFound", NotFound); |
| 520 |
PyDict_SetItemString(d, "TooManyPeriodsInName", TooManyPeriods); |
| 521 |
pprintMod = PyImport_ImportModule("pprint"); |
| 522 |
if (!pprintMod) { |
| 523 |
#ifdef IS_PYTHON3 |
| 524 |
return NULL; |
| 525 |
#else |
| 526 |
return; |
| 527 |
#endif |
| 528 |
} |
| 529 |
pprintMod_pformat = PyObject_GetAttrString(pprintMod, "pformat"); |
| 530 |
Py_DECREF(pprintMod); |
| 531 |
|
| 532 |
if (PyErr_Occurred()) { |
| 533 |
Py_FatalError("Can't initialize module _namemapper"); |
| 534 |
} |
| 535 |
#ifdef IS_PYTHON3 |
| 536 |
return m; |
| 537 |
#endif |
| 538 |
} |
| 539 |
|
| 540 |
#ifdef __cplusplus |
| 541 |
} |
| 542 |
#endif |