repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
data_stack_dealloc
static void data_stack_dealloc(SRE_STATE* state) { if (state->data_stack) { PyMem_FREE(state->data_stack); state->data_stack = NULL; } state->data_stack_size = state->data_stack_base = 0; }
/* helpers */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_sre.c#L256-L264
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
SRE_AT
LOCAL(int) SRE_AT(SRE_STATE* state, SRE_CHAR* ptr, SRE_CODE at) { /* check if pointer is at given position */ Py_ssize_t thisp, thatp; switch (at) { case SRE_AT_BEGINNING: case SRE_AT_BEGINNING_STRING: return ((void*) ptr == state->beginning); case SRE_AT_BEGINNING_LINE: return ((void*) ptr == state->beginning || SRE_IS_LINEBREAK((int) ptr[-1])); case SRE_AT_END: return (((void*) (ptr+1) == state->end && SRE_IS_LINEBREAK((int) ptr[0])) || ((void*) ptr == state->end)); case SRE_AT_END_LINE: return ((void*) ptr == state->end || SRE_IS_LINEBREAK((int) ptr[0])); case SRE_AT_END_STRING: return ((void*) ptr == state->end); case SRE_AT_BOUNDARY: if (state->beginning == state->end) return 0; thatp = ((void*) ptr > state->beginning) ? SRE_IS_WORD((int) ptr[-1]) : 0; thisp = ((void*) ptr < state->end) ? SRE_IS_WORD((int) ptr[0]) : 0; return thisp != thatp; case SRE_AT_NON_BOUNDARY: if (state->beginning == state->end) return 0; thatp = ((void*) ptr > state->beginning) ? SRE_IS_WORD((int) ptr[-1]) : 0; thisp = ((void*) ptr < state->end) ? SRE_IS_WORD((int) ptr[0]) : 0; return thisp == thatp; case SRE_AT_LOC_BOUNDARY: if (state->beginning == state->end) return 0; thatp = ((void*) ptr > state->beginning) ? SRE_LOC_IS_WORD((int) ptr[-1]) : 0; thisp = ((void*) ptr < state->end) ? SRE_LOC_IS_WORD((int) ptr[0]) : 0; return thisp != thatp; case SRE_AT_LOC_NON_BOUNDARY: if (state->beginning == state->end) return 0; thatp = ((void*) ptr > state->beginning) ? SRE_LOC_IS_WORD((int) ptr[-1]) : 0; thisp = ((void*) ptr < state->end) ? SRE_LOC_IS_WORD((int) ptr[0]) : 0; return thisp == thatp; #if defined(HAVE_UNICODE) case SRE_AT_UNI_BOUNDARY: if (state->beginning == state->end) return 0; thatp = ((void*) ptr > state->beginning) ? SRE_UNI_IS_WORD((int) ptr[-1]) : 0; thisp = ((void*) ptr < state->end) ? SRE_UNI_IS_WORD((int) ptr[0]) : 0; return thisp != thatp; case SRE_AT_UNI_NON_BOUNDARY: if (state->beginning == state->end) return 0; thatp = ((void*) ptr > state->beginning) ? SRE_UNI_IS_WORD((int) ptr[-1]) : 0; thisp = ((void*) ptr < state->end) ? SRE_UNI_IS_WORD((int) ptr[0]) : 0; return thisp == thatp; #endif } return 0; }
/* SRE_RECURSIVE */ /* -------------------------------------------------------------------- */ /* String matching engine */ /* the following section is compiled twice, with different character settings */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_sre.c#L336-L424
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
SRE_INFO
LOCAL(int) SRE_INFO(SRE_STATE* state, SRE_CODE* pattern) { /* check if an SRE_OP_INFO block matches at the current position. returns the number of SRE_CODE objects to skip if successful, 0 if no match */ SRE_CHAR* end = state->end; SRE_CHAR* ptr = state->ptr; Py_ssize_t i; /* check minimal length */ if (pattern[3] && (end - ptr) < pattern[3]) return 0; /* check known prefix */ if (pattern[2] & SRE_INFO_PREFIX && pattern[5] > 1) { /* <length> <skip> <prefix data> <overlap data> */ for (i = 0; i < pattern[5]; i++) if ((SRE_CODE) ptr[i] != pattern[7 + i]) return 0; return pattern[0] + 2 * pattern[6]; } return pattern[0]; }
/* not used in this release */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_sre.c#L606-L630
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
SRE_MATCH
LOCAL(Py_ssize_t) SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern) { SRE_CHAR* end = (SRE_CHAR *)state->end; Py_ssize_t alloc_pos, ctx_pos = -1; Py_ssize_t i, ret = 0; Py_ssize_t jump; unsigned int sigcount=0; SRE_MATCH_CONTEXT* ctx; SRE_MATCH_CONTEXT* nextctx; TRACE(("|%p|%p|ENTER\n", pattern, state->ptr)); DATA_ALLOC(SRE_MATCH_CONTEXT, ctx); ctx->last_ctx_pos = -1; ctx->jump = JUMP_NONE; ctx->pattern = pattern; ctx_pos = alloc_pos; entrance: ctx->ptr = (SRE_CHAR *)state->ptr; if (ctx->pattern[0] == SRE_OP_INFO) { /* optimization info block */ /* <INFO> <1=skip> <2=flags> <3=min> ... */ if (ctx->pattern[3] && (end - ctx->ptr) < ctx->pattern[3]) { TRACE(("reject (got %" PY_FORMAT_SIZE_T "d chars, " "need %" PY_FORMAT_SIZE_T "d)\n", (end - ctx->ptr), (Py_ssize_t) ctx->pattern[3])); RETURN_FAILURE; } ctx->pattern += ctx->pattern[1] + 1; } for (;;) { ++sigcount; if ((0 == (sigcount & 0xfff)) && PyErr_CheckSignals()) RETURN_ERROR(SRE_ERROR_INTERRUPTED); switch (*ctx->pattern++) { case SRE_OP_MARK: /* set mark */ /* <MARK> <gid> */ TRACE(("|%p|%p|MARK %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); i = ctx->pattern[0]; if (i & 1) state->lastindex = i/2 + 1; if (i > state->lastmark) { /* state->lastmark is the highest valid index in the state->mark array. If it is increased by more than 1, the intervening marks must be set to NULL to signal that these marks have not been encountered. */ Py_ssize_t j = state->lastmark + 1; while (j < i) state->mark[j++] = NULL; state->lastmark = i; } state->mark[i] = ctx->ptr; ctx->pattern++; break; case SRE_OP_LITERAL: /* match literal string */ /* <LITERAL> <code> */ TRACE(("|%p|%p|LITERAL %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] != ctx->pattern[0]) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; break; case SRE_OP_NOT_LITERAL: /* match anything that is not literal character */ /* <NOT_LITERAL> <code> */ TRACE(("|%p|%p|NOT_LITERAL %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] == ctx->pattern[0]) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; break; case SRE_OP_SUCCESS: /* end of pattern */ TRACE(("|%p|%p|SUCCESS\n", ctx->pattern, ctx->ptr)); state->ptr = ctx->ptr; RETURN_SUCCESS; case SRE_OP_AT: /* match at given position */ /* <AT> <code> */ TRACE(("|%p|%p|AT %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); if (!SRE_AT(state, ctx->ptr, *ctx->pattern)) RETURN_FAILURE; ctx->pattern++; break; case SRE_OP_CATEGORY: /* match at given category */ /* <CATEGORY> <code> */ TRACE(("|%p|%p|CATEGORY %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); if (ctx->ptr >= end || !sre_category(ctx->pattern[0], ctx->ptr[0])) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; break; case SRE_OP_ANY: /* match anything (except a newline) */ /* <ANY> */ TRACE(("|%p|%p|ANY\n", ctx->pattern, ctx->ptr)); if (ctx->ptr >= end || SRE_IS_LINEBREAK(ctx->ptr[0])) RETURN_FAILURE; ctx->ptr++; break; case SRE_OP_ANY_ALL: /* match anything */ /* <ANY_ALL> */ TRACE(("|%p|%p|ANY_ALL\n", ctx->pattern, ctx->ptr)); if (ctx->ptr >= end) RETURN_FAILURE; ctx->ptr++; break; case SRE_OP_IN: /* match set member (or non_member) */ /* <IN> <skip> <set> */ TRACE(("|%p|%p|IN\n", ctx->pattern, ctx->ptr)); if (ctx->ptr >= end || !SRE_CHARSET(ctx->pattern + 1, *ctx->ptr)) RETURN_FAILURE; ctx->pattern += ctx->pattern[0]; ctx->ptr++; break; case SRE_OP_LITERAL_IGNORE: TRACE(("|%p|%p|LITERAL_IGNORE %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); if (ctx->ptr >= end || state->lower(*ctx->ptr) != state->lower(*ctx->pattern)) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; break; case SRE_OP_NOT_LITERAL_IGNORE: TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); if (ctx->ptr >= end || state->lower(*ctx->ptr) == state->lower(*ctx->pattern)) RETURN_FAILURE; ctx->pattern++; ctx->ptr++; break; case SRE_OP_IN_IGNORE: TRACE(("|%p|%p|IN_IGNORE\n", ctx->pattern, ctx->ptr)); if (ctx->ptr >= end || !SRE_CHARSET(ctx->pattern+1, (SRE_CODE)state->lower(*ctx->ptr))) RETURN_FAILURE; ctx->pattern += ctx->pattern[0]; ctx->ptr++; break; case SRE_OP_JUMP: case SRE_OP_INFO: /* jump forward */ /* <JUMP> <offset> */ TRACE(("|%p|%p|JUMP %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); ctx->pattern += ctx->pattern[0]; break; case SRE_OP_BRANCH: /* alternation */ /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */ TRACE(("|%p|%p|BRANCH\n", ctx->pattern, ctx->ptr)); LASTMARK_SAVE(); ctx->u.rep = state->repeat; if (ctx->u.rep) MARK_PUSH(ctx->lastmark); for (; ctx->pattern[0]; ctx->pattern += ctx->pattern[0]) { if (ctx->pattern[1] == SRE_OP_LITERAL && (ctx->ptr >= end || (SRE_CODE) *ctx->ptr != ctx->pattern[2])) continue; if (ctx->pattern[1] == SRE_OP_IN && (ctx->ptr >= end || !SRE_CHARSET(ctx->pattern + 3, (SRE_CODE) *ctx->ptr))) continue; state->ptr = ctx->ptr; DO_JUMP(JUMP_BRANCH, jump_branch, ctx->pattern+1); if (ret) { if (ctx->u.rep) MARK_POP_DISCARD(ctx->lastmark); RETURN_ON_ERROR(ret); RETURN_SUCCESS; } if (ctx->u.rep) MARK_POP_KEEP(ctx->lastmark); LASTMARK_RESTORE(); } if (ctx->u.rep) MARK_POP_DISCARD(ctx->lastmark); RETURN_FAILURE; case SRE_OP_REPEAT_ONE: /* match repeated sequence (maximizing regexp) */ /* this operator only works if the repeated item is exactly one character wide, and we're not already collecting backtracking points. for other cases, use the MAX_REPEAT operator */ /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */ TRACE(("|%p|%p|REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr, ctx->pattern[1], ctx->pattern[2])); if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr) RETURN_FAILURE; /* cannot match */ state->ptr = ctx->ptr; ret = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[2]); RETURN_ON_ERROR(ret); DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos); ctx->count = ret; ctx->ptr += ctx->count; /* when we arrive here, count contains the number of matches, and ctx->ptr points to the tail of the target string. check if the rest of the pattern matches, and backtrack if not. */ if (ctx->count < (Py_ssize_t) ctx->pattern[1]) RETURN_FAILURE; if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) { /* tail is empty. we're finished */ state->ptr = ctx->ptr; RETURN_SUCCESS; } LASTMARK_SAVE(); if (ctx->pattern[ctx->pattern[0]] == SRE_OP_LITERAL) { /* tail starts with a literal. skip positions where the rest of the pattern cannot possibly match */ ctx->u.chr = ctx->pattern[ctx->pattern[0]+1]; for (;;) { while (ctx->count >= (Py_ssize_t) ctx->pattern[1] && (ctx->ptr >= end || *ctx->ptr != ctx->u.chr)) { ctx->ptr--; ctx->count--; } if (ctx->count < (Py_ssize_t) ctx->pattern[1]) break; state->ptr = ctx->ptr; DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1, ctx->pattern+ctx->pattern[0]); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; } LASTMARK_RESTORE(); ctx->ptr--; ctx->count--; } } else { /* general case */ while (ctx->count >= (Py_ssize_t) ctx->pattern[1]) { state->ptr = ctx->ptr; DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2, ctx->pattern+ctx->pattern[0]); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; } ctx->ptr--; ctx->count--; LASTMARK_RESTORE(); } } RETURN_FAILURE; case SRE_OP_MIN_REPEAT_ONE: /* match repeated sequence (minimizing regexp) */ /* this operator only works if the repeated item is exactly one character wide, and we're not already collecting backtracking points. for other cases, use the MIN_REPEAT operator */ /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */ TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr, ctx->pattern[1], ctx->pattern[2])); if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr) RETURN_FAILURE; /* cannot match */ state->ptr = ctx->ptr; if (ctx->pattern[1] == 0) ctx->count = 0; else { /* count using pattern min as the maximum */ ret = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[1]); RETURN_ON_ERROR(ret); DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos); if (ret < (Py_ssize_t) ctx->pattern[1]) /* didn't match minimum number of times */ RETURN_FAILURE; /* advance past minimum matches of repeat */ ctx->count = ret; ctx->ptr += ctx->count; } if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) { /* tail is empty. we're finished */ state->ptr = ctx->ptr; RETURN_SUCCESS; } else { /* general case */ LASTMARK_SAVE(); while ((Py_ssize_t)ctx->pattern[2] == SRE_MAXREPEAT || ctx->count <= (Py_ssize_t)ctx->pattern[2]) { state->ptr = ctx->ptr; DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one, ctx->pattern+ctx->pattern[0]); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; } state->ptr = ctx->ptr; ret = SRE_COUNT(state, ctx->pattern+3, 1); RETURN_ON_ERROR(ret); DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos); if (ret == 0) break; assert(ret == 1); ctx->ptr++; ctx->count++; LASTMARK_RESTORE(); } } RETURN_FAILURE; case SRE_OP_REPEAT: /* create repeat context. all the hard work is done by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */ /* <REPEAT> <skip> <1=min> <2=max> item <UNTIL> tail */ TRACE(("|%p|%p|REPEAT %d %d\n", ctx->pattern, ctx->ptr, ctx->pattern[1], ctx->pattern[2])); /* install new repeat context */ ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC(sizeof(*ctx->u.rep)); if (!ctx->u.rep) { PyErr_NoMemory(); RETURN_FAILURE; } ctx->u.rep->count = -1; ctx->u.rep->pattern = ctx->pattern; ctx->u.rep->prev = state->repeat; ctx->u.rep->last_ptr = NULL; state->repeat = ctx->u.rep; state->ptr = ctx->ptr; DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]); state->repeat = ctx->u.rep->prev; PyObject_FREE(ctx->u.rep); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; } RETURN_FAILURE; case SRE_OP_MAX_UNTIL: /* maximizing repeat */ /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */ /* FIXME: we probably need to deal with zero-width matches in here... */ ctx->u.rep = state->repeat; if (!ctx->u.rep) RETURN_ERROR(SRE_ERROR_STATE); state->ptr = ctx->ptr; ctx->count = ctx->u.rep->count+1; TRACE(("|%p|%p|MAX_UNTIL %" PY_FORMAT_SIZE_T "d\n", ctx->pattern, ctx->ptr, ctx->count)); if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { /* not enough matches */ ctx->u.rep->count = ctx->count; DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1, ctx->u.rep->pattern+3); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; } ctx->u.rep->count = ctx->count-1; state->ptr = ctx->ptr; RETURN_FAILURE; } if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] || ctx->u.rep->pattern[2] == SRE_MAXREPEAT) && state->ptr != ctx->u.rep->last_ptr) { /* we may have enough matches, but if we can match another item, do so */ ctx->u.rep->count = ctx->count; LASTMARK_SAVE(); MARK_PUSH(ctx->lastmark); /* zero-width match protection */ DATA_PUSH(&ctx->u.rep->last_ptr); ctx->u.rep->last_ptr = state->ptr; DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2, ctx->u.rep->pattern+3); DATA_POP(&ctx->u.rep->last_ptr); if (ret) { MARK_POP_DISCARD(ctx->lastmark); RETURN_ON_ERROR(ret); RETURN_SUCCESS; } MARK_POP(ctx->lastmark); LASTMARK_RESTORE(); ctx->u.rep->count = ctx->count-1; state->ptr = ctx->ptr; } /* cannot match more repeated items here. make sure the tail matches */ state->repeat = ctx->u.rep->prev; DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, ctx->pattern); RETURN_ON_SUCCESS(ret); state->repeat = ctx->u.rep; state->ptr = ctx->ptr; RETURN_FAILURE; case SRE_OP_MIN_UNTIL: /* minimizing repeat */ /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */ ctx->u.rep = state->repeat; if (!ctx->u.rep) RETURN_ERROR(SRE_ERROR_STATE); state->ptr = ctx->ptr; ctx->count = ctx->u.rep->count+1; TRACE(("|%p|%p|MIN_UNTIL %" PY_FORMAT_SIZE_T "d %p\n", ctx->pattern, ctx->ptr, ctx->count, ctx->u.rep->pattern)); if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { /* not enough matches */ ctx->u.rep->count = ctx->count; DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1, ctx->u.rep->pattern+3); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; } ctx->u.rep->count = ctx->count-1; state->ptr = ctx->ptr; RETURN_FAILURE; } LASTMARK_SAVE(); /* see if the tail matches */ state->repeat = ctx->u.rep->prev; DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, ctx->pattern); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; } state->repeat = ctx->u.rep; state->ptr = ctx->ptr; LASTMARK_RESTORE(); if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2] && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) || state->ptr == ctx->u.rep->last_ptr) RETURN_FAILURE; ctx->u.rep->count = ctx->count; /* zero-width match protection */ DATA_PUSH(&ctx->u.rep->last_ptr); ctx->u.rep->last_ptr = state->ptr; DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3, ctx->u.rep->pattern+3); DATA_POP(&ctx->u.rep->last_ptr); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; } ctx->u.rep->count = ctx->count-1; state->ptr = ctx->ptr; RETURN_FAILURE; case SRE_OP_GROUPREF: /* match backreference */ TRACE(("|%p|%p|GROUPREF %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); i = ctx->pattern[0]; { Py_ssize_t groupref = i+i; if (groupref >= state->lastmark) { RETURN_FAILURE; } else { SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; if (!p || !e || e < p) RETURN_FAILURE; while (p < e) { if (ctx->ptr >= end || *ctx->ptr != *p) RETURN_FAILURE; p++; ctx->ptr++; } } } ctx->pattern++; break; case SRE_OP_GROUPREF_IGNORE: /* match backreference */ TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); i = ctx->pattern[0]; { Py_ssize_t groupref = i+i; if (groupref >= state->lastmark) { RETURN_FAILURE; } else { SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; if (!p || !e || e < p) RETURN_FAILURE; while (p < e) { if (ctx->ptr >= end || state->lower(*ctx->ptr) != state->lower(*p)) RETURN_FAILURE; p++; ctx->ptr++; } } } ctx->pattern++; break; case SRE_OP_GROUPREF_EXISTS: TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", ctx->pattern, ctx->ptr, ctx->pattern[0])); /* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */ i = ctx->pattern[0]; { Py_ssize_t groupref = i+i; if (groupref >= state->lastmark) { ctx->pattern += ctx->pattern[1]; break; } else { SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; if (!p || !e || e < p) { ctx->pattern += ctx->pattern[1]; break; } } } ctx->pattern += 2; break; case SRE_OP_ASSERT: /* assert subpattern */ /* <ASSERT> <skip> <back> <pattern> */ TRACE(("|%p|%p|ASSERT %d\n", ctx->pattern, ctx->ptr, ctx->pattern[1])); state->ptr = ctx->ptr - ctx->pattern[1]; if (state->ptr < state->beginning) RETURN_FAILURE; DO_JUMP(JUMP_ASSERT, jump_assert, ctx->pattern+2); RETURN_ON_FAILURE(ret); ctx->pattern += ctx->pattern[0]; break; case SRE_OP_ASSERT_NOT: /* assert not subpattern */ /* <ASSERT_NOT> <skip> <back> <pattern> */ TRACE(("|%p|%p|ASSERT_NOT %d\n", ctx->pattern, ctx->ptr, ctx->pattern[1])); state->ptr = ctx->ptr - ctx->pattern[1]; if (state->ptr >= state->beginning) { DO_JUMP(JUMP_ASSERT_NOT, jump_assert_not, ctx->pattern+2); if (ret) { RETURN_ON_ERROR(ret); RETURN_FAILURE; } } ctx->pattern += ctx->pattern[0]; break; case SRE_OP_FAILURE: /* immediate failure */ TRACE(("|%p|%p|FAILURE\n", ctx->pattern, ctx->ptr)); RETURN_FAILURE; default: TRACE(("|%p|%p|UNKNOWN %d\n", ctx->pattern, ctx->ptr, ctx->pattern[-1])); RETURN_ERROR(SRE_ERROR_ILLEGAL); } } exit: ctx_pos = ctx->last_ctx_pos; jump = ctx->jump; DATA_POP_DISCARD(ctx); if (ctx_pos == -1) return ret; DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos); switch (jump) { case JUMP_MAX_UNTIL_2: TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", ctx->pattern, ctx->ptr)); goto jump_max_until_2; case JUMP_MAX_UNTIL_3: TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", ctx->pattern, ctx->ptr)); goto jump_max_until_3; case JUMP_MIN_UNTIL_2: TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", ctx->pattern, ctx->ptr)); goto jump_min_until_2; case JUMP_MIN_UNTIL_3: TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", ctx->pattern, ctx->ptr)); goto jump_min_until_3; case JUMP_BRANCH: TRACE(("|%p|%p|JUMP_BRANCH\n", ctx->pattern, ctx->ptr)); goto jump_branch; case JUMP_MAX_UNTIL_1: TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", ctx->pattern, ctx->ptr)); goto jump_max_until_1; case JUMP_MIN_UNTIL_1: TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", ctx->pattern, ctx->ptr)); goto jump_min_until_1; case JUMP_REPEAT: TRACE(("|%p|%p|JUMP_REPEAT\n", ctx->pattern, ctx->ptr)); goto jump_repeat; case JUMP_REPEAT_ONE_1: TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", ctx->pattern, ctx->ptr)); goto jump_repeat_one_1; case JUMP_REPEAT_ONE_2: TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", ctx->pattern, ctx->ptr)); goto jump_repeat_one_2; case JUMP_MIN_REPEAT_ONE: TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", ctx->pattern, ctx->ptr)); goto jump_min_repeat_one; case JUMP_ASSERT: TRACE(("|%p|%p|JUMP_ASSERT\n", ctx->pattern, ctx->ptr)); goto jump_assert; case JUMP_ASSERT_NOT: TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", ctx->pattern, ctx->ptr)); goto jump_assert_not; case JUMP_NONE: TRACE(("|%p|%p|RETURN %" PY_FORMAT_SIZE_T "d\n", ctx->pattern, ctx->ptr, ret)); break; } return ret; /* should never get here */ }
/* check if string matches the given pattern. returns <0 for error, 0 for failure, and 1 for success */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_sre.c#L811-L1498
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
if
PySSL_END_ALLOW_THREADS if (self->ctx == NULL) { errstr = ERRSTR("Invalid SSL protocol variant specified."); goto fail; }
/* Set up context */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_ssl.c#L317-L322
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
check_socket_and_wait_for_timeout
static int check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) { fd_set fds; struct timeval tv; int rc; /* Nothing to do unless we're in timeout mode (not non-blocking) */ if (s->sock_timeout < 0.0) return SOCKET_IS_BLOCKING; else if (s->sock_timeout == 0.0) return SOCKET_IS_NONBLOCKING; /* Guard against closed socket */ if (s->sock_fd < 0) return SOCKET_HAS_BEEN_CLOSED; /* Prefer poll, if available, since you can poll() any fd * which can't be done with select(). */ #ifdef HAVE_POLL { struct pollfd pollfd; int timeout; pollfd.fd = s->sock_fd; pollfd.events = writing ? POLLOUT : POLLIN; /* s->sock_timeout is in seconds, timeout in ms */ timeout = (int)(s->sock_timeout * 1000 + 0.5); PySSL_BEGIN_ALLOW_THREADS rc = poll(&pollfd, 1, timeout); PySSL_END_ALLOW_THREADS goto normal_return; } #endif /* Guard against socket too large for select*/ if (!_PyIsSelectable_fd(s->sock_fd)) return SOCKET_TOO_LARGE_FOR_SELECT; /* Construct the arguments to select */ tv.tv_sec = (int)s->sock_timeout; tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); FD_ZERO(&fds); FD_SET(s->sock_fd, &fds); /* See if the socket is ready */ PySSL_BEGIN_ALLOW_THREADS if (writing) rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv); else rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv); PySSL_END_ALLOW_THREADS #ifdef HAVE_POLL normal_return: #endif /* Return SOCKET_TIMED_OUT on timeout, SOCKET_OPERATION_OK otherwise (when we are able to write or when there's something to read) */ return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; }
/* If the socket has a timeout, do a select()/poll() on the socket. The argument writing indicates the direction. Returns one of the possibilities in the timeout_state enum (above). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_ssl.c#L1181-L1242
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
_ssl_threadid_callback
static void _ssl_threadid_callback(CRYPTO_THREADID *id) { CRYPTO_THREADID_set_numeric(id, (unsigned long)PyThread_get_thread_ident()); }
/* use new CRYPTO_THREADID API. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_ssl.c#L1661-L1666
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
_ssl_thread_id_function
static unsigned long _ssl_thread_id_function (void) { return PyThread_get_thread_ident(); }
/* deprecated CRYPTO_set_id_callback() API. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_ssl.c#L1669-L1672
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
get_long
static int get_long(PyObject *v, long *p) { long x; v = get_pylong(v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsLong(v); Py_DECREF(v); if (x == (long)-1 && PyErr_Occurred()) return -1; *p = x; return 0; }
/* Helper to convert a Python object to a C long. Sets an exception (struct.error for an inconvertible type, OverflowError for out-of-range values) and returns -1 on error. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_struct.c#L194-L209
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
get_ulong
static int get_ulong(PyObject *v, unsigned long *p) { unsigned long x; v = get_pylong(v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsUnsignedLong(v); Py_DECREF(v); if (x == (unsigned long)-1 && PyErr_Occurred()) return -1; *p = x; return 0; }
/* Same, but handling unsigned long */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_struct.c#L213-L228
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
get_longlong
static int get_longlong(PyObject *v, PY_LONG_LONG *p) { PY_LONG_LONG x; v = get_pylong(v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsLongLong(v); Py_DECREF(v); if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) return -1; *p = x; return 0; }
/* Same, but handling native long long. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_struct.c#L234-L249
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
get_ulonglong
static int get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p) { unsigned PY_LONG_LONG x; v = get_pylong(v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsUnsignedLongLong(v); Py_DECREF(v); if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) return -1; *p = x; return 0; }
/* Same, but handling native unsigned long long. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_struct.c#L253-L268
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
_range_error
static int _range_error(const formatdef *f, int is_unsigned) { /* ulargest is the largest unsigned value with f->size bytes. * Note that the simpler: * ((size_t)1 << (f->size * 8)) - 1 * doesn't work when f->size == sizeof(size_t) because C doesn't * define what happens when a left shift count is >= the number of * bits in the integer being shifted; e.g., on some boxes it doesn't * shift at all when they're equal. */ const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8); assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T); if (is_unsigned) PyErr_Format(StructError, "'%c' format requires 0 <= number <= %zu", f->format, ulargest); else { const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1); PyErr_Format(StructError, "'%c' format requires %zd <= number <= %zd", f->format, ~ largest, largest); } return -1; }
/* Helper to format the range error exceptions */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_struct.c#L299-L326
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
align
static Py_ssize_t align(Py_ssize_t size, char c, const formatdef *e) { Py_ssize_t extra; if (e->format == c) { if (e->alignment && size > 0) { extra = (e->alignment - 1) - (size - 1) % (e->alignment); if (extra > PY_SSIZE_T_MAX - size) return -1; size += extra; } } return size; }
/* Align a size according to a format code. Return -1 on overflow. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_struct.c#L1199-L1213
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
prepare_s
static int prepare_s(PyStructObject *self) { const formatdef *f; const formatdef *e; formatcode *codes; const char *s; const char *fmt; char c; Py_ssize_t size, len, num, itemsize; fmt = PyString_AS_STRING(self->s_format); f = whichtable((char **)&fmt); s = fmt; size = 0; len = 0; while ((c = *s++) != '\0') { if (isspace(Py_CHARMASK(c))) continue; if ('0' <= c && c <= '9') { num = c - '0'; while ('0' <= (c = *s++) && c <= '9') { /* overflow-safe version of if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */ if (num >= PY_SSIZE_T_MAX / 10 && ( num > PY_SSIZE_T_MAX / 10 || (c - '0') > PY_SSIZE_T_MAX % 10)) goto overflow; num = num*10 + (c - '0'); } if (c == '\0') break; } else num = 1; e = getentry(c, f); if (e == NULL) return -1; switch (c) { case 's': /* fall through */ case 'p': len++; break; case 'x': break; default: len += num; break; } itemsize = e->size; size = align(size, c, e); if (size == -1) goto overflow; /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */ if (num > (PY_SSIZE_T_MAX - size) / itemsize) goto overflow; size += num * itemsize; } /* check for overflow */ if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) { PyErr_NoMemory(); return -1; } self->s_size = size; self->s_len = len; codes = PyMem_MALLOC((len + 1) * sizeof(formatcode)); if (codes == NULL) { PyErr_NoMemory(); return -1; } /* Free any s_codes value left over from a previous initialization. */ if (self->s_codes != NULL) PyMem_FREE(self->s_codes); self->s_codes = codes; s = fmt; size = 0; while ((c = *s++) != '\0') { if (isspace(Py_CHARMASK(c))) continue; if ('0' <= c && c <= '9') { num = c - '0'; while ('0' <= (c = *s++) && c <= '9') num = num*10 + (c - '0'); if (c == '\0') break; } else num = 1; e = getentry(c, f); size = align(size, c, e); if (c == 's' || c == 'p') { codes->offset = size; codes->size = num; codes->fmtdef = e; codes++; size += num; } else if (c == 'x') { size += num; } else { while (--num >= 0) { codes->offset = size; codes->size = e->size; codes->fmtdef = e; codes++; size += e->size; } } } codes->fmtdef = NULL; codes->offset = size; codes->size = 0; return 0; overflow: PyErr_SetString(StructError, "total struct size too long"); return -1; }
/* calculate the size of a format string */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_struct.c#L1218-L1343
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
s_pack_internal
static int s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf) { formatcode *code; /* XXX(nnorwitz): why does i need to be a local? can we use the offset parameter or do we need the wider width? */ Py_ssize_t i; memset(buf, '\0', soself->s_size); i = offset; for (code = soself->s_codes; code->fmtdef != NULL; code++) { Py_ssize_t n; PyObject *v = PyTuple_GET_ITEM(args, i++); const formatdef *e = code->fmtdef; char *res = buf + code->offset; if (e->format == 's') { if (!PyString_Check(v)) { PyErr_SetString(StructError, "argument for 's' must " "be a string"); return -1; } n = PyString_GET_SIZE(v); if (n > code->size) n = code->size; if (n > 0) memcpy(res, PyString_AS_STRING(v), n); } else if (e->format == 'p') { if (!PyString_Check(v)) { PyErr_SetString(StructError, "argument for 'p' must " "be a string"); return -1; } n = PyString_GET_SIZE(v); if (n > (code->size - 1)) n = code->size - 1; if (n > 0) memcpy(res + 1, PyString_AS_STRING(v), n); if (n > 255) n = 255; *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); } else if (e->pack(res, v, e) < 0) { if (strchr(integer_codes, e->format) != NULL && PyErr_ExceptionMatches(PyExc_OverflowError)) PyErr_Format(StructError, "integer out of range for " "'%c' format code", e->format); return -1; } } /* Success */ return 0; }
/* * Guts of the pack function. * * Takes a struct object, a tuple of arguments, and offset in that tuple of * argument for where to start processing the arguments for packing, and a * character buffer for writing the packed string. The caller must insure * that the buffer may contain the required length for packing the arguments. * 0 is returned on success, 1 is returned if there is an error. * */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_struct.c#L1553-L1608
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
broken_buffer_getbuffer
static int broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags) { PyErr_SetString( TestError, "test_broken_memoryview: expected error in bf_getbuffer"); return -1; }
/* Issue #7385: Check that memoryview() does not crash * when bf_getbuffer returns an error */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_testcapimodule.c#L290-L297
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
_make_call_from_thread
static void _make_call_from_thread(void *callable) { _make_call(callable); PyThread_release_lock(thread_done); }
/* Same thing, but releases `thread_done` when it returns. This variant * should be called only from threads spawned by test_thread_state(). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_testcapimodule.c#L1328-L1333
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
_pending_callback
static int _pending_callback(void *arg) { /* we assume the argument is callable object to which we own a reference */ PyObject *callable = (PyObject *)arg; PyObject *r = PyObject_CallObject(callable, NULL); Py_DECREF(callable); Py_XDECREF(r); return r != NULL ? 0 : -1; }
/* test Py_AddPendingCalls using threads */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_testcapimodule.c#L1387-L1395
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
Sleep
static void Sleep(int milli) { /* XXX Too bad if you don't have select(). */ struct timeval t; t.tv_sec = milli/1000; t.tv_usec = (milli%1000) * 1000; select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t); }
/* Millisecond sleep() for Unix platforms. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_tkinter.c#L310-L318
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
WaitForMainloop
static int WaitForMainloop(TkappObject* self) { int i; for (i = 0; i < 10; i++) { if (self->dispatching) return 1; Py_BEGIN_ALLOW_THREADS Sleep(100); Py_END_ALLOW_THREADS } if (self->dispatching) return 1; PyErr_SetString(PyExc_RuntimeError, "main thread is not in main loop"); return 0; }
/* MS_WINDOWS */ /* Wait up to 1s for the mainloop to come up. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_tkinter.c#L323-L338
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
Tkapp_CallProc
static int Tkapp_CallProc(Tkapp_CallEvent *e, int flags) { Tcl_Obj *objStore[ARGSZ]; Tcl_Obj **objv; int objc; int i; ENTER_PYTHON objv = Tkapp_CallArgs(e->args, objStore, &objc); if (!objv) { PyErr_Fetch(e->exc_type, e->exc_value, e->exc_tb); *(e->res) = NULL; } LEAVE_PYTHON if (!objv) goto done; i = Tcl_EvalObjv(e->self->interp, objc, objv, e->flags); ENTER_PYTHON if (i == TCL_ERROR) { *(e->res) = NULL; *(e->exc_type) = NULL; *(e->exc_tb) = NULL; *(e->exc_value) = PyObject_CallFunction( Tkinter_TclError, "s", Tcl_GetStringResult(e->self->interp)); } else { *(e->res) = Tkapp_CallResult(e->self); } LEAVE_PYTHON Tkapp_CallDeallocArgs(objv, objStore, objc); done: /* Wake up calling thread. */ Tcl_MutexLock(&call_mutex); Tcl_ConditionNotify(e->done); Tcl_MutexUnlock(&call_mutex); return 1; }
/* Tkapp_CallProc is the event procedure that is executed in the context of the Tcl interpreter thread. Initially, it holds the Tcl lock, and doesn't hold the Python lock. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_tkinter.c#L1322-L1360
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
TimerHandler
static void TimerHandler(ClientData clientData) { TkttObject *v = (TkttObject *)clientData; PyObject *func = v->func; PyObject *res; if (func == NULL) return; v->func = NULL; ENTER_PYTHON res = PyEval_CallObject(func, NULL); Py_DECREF(func); Py_DECREF(v); /* See Tktt_New() */ if (res == NULL) { errorInCmd = 1; PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); } else Py_DECREF(res); LEAVE_PYTHON }
/** Timer Handler **/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_tkinter.c#L2629-L2655
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
Tkapp_Dealloc
static void Tkapp_Dealloc(PyObject *self) { /*CHECK_TCL_APPARTMENT;*/ ENTER_TCL Tcl_DeleteInterp(Tkapp_Interp(self)); LEAVE_TCL PyObject_Del(self); DisableEventHook(); }
/**** Tkapp Type Methods ****/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_tkinter.c#L2959-L2968
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
ins_long
static void ins_long(PyObject *d, char *name, long val) { PyObject *v = PyInt_FromLong(val); if (v) { PyDict_SetItemString(d, name, v); Py_DECREF(v); } }
/* all errors will be checked in one fell swoop in init_tkinter() */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/_tkinter.c#L3294-L3302
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
array_dealloc
static void array_dealloc(arrayobject *op) { if (op->weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *) op); if (op->ob_item != NULL) PyMem_DEL(op->ob_item); Py_TYPE(op)->tp_free((PyObject *)op); }
/* Methods */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/arraymodule.c#L494-L502
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
st_14linear2ulaw
static unsigned char st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */ { PyInt16 mask; PyInt16 seg; unsigned char uval; /* The original sox code does this in the calling function, not here */ pcm_val = pcm_val >> 2; /* u-law inverts all bits */ /* Get the sign and the magnitude of the value. */ if (pcm_val < 0) { pcm_val = -pcm_val; mask = 0x7F; } else { mask = 0xFF; } if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */ pcm_val += (BIAS >> 2); /* Convert the scaled magnitude to segment number. */ seg = search(pcm_val, seg_uend, 8); /* * Combine the sign, segment, quantization bits; * and complement the code word. */ if (seg >= 8) /* out of range, return maximum value. */ return (unsigned char) (0x7F ^ mask); else { uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF); return (uval ^ mask); } }
/* * linear2ulaw() accepts a 14-bit signed integer and encodes it as u-law data * stored in a unsigned char. This function should only be called with * the data shifted such that it only contains information in the lower * 14-bits. * * In order to simplify the encoding process, the original linear magnitude * is biased by adding 33 which shifts the encoding range from (0 - 8158) to * (33 - 8191). The result can be seen in the following encoding table: * * Biased Linear Input Code Compressed Code * ------------------------ --------------- * 00000001wxyza 000wxyz * 0000001wxyzab 001wxyz * 000001wxyzabc 010wxyz * 00001wxyzabcd 011wxyz * 0001wxyzabcde 100wxyz * 001wxyzabcdef 101wxyz * 01wxyzabcdefg 110wxyz * 1wxyzabcdefgh 111wxyz * * Each biased linear code has a leading 1 which identifies the segment * number. The value of the segment number is equal to 7 minus the number * of leading 0's. The quantization interval is directly available as the * four bits wxyz. * The trailing bits (a - h) are ignored. * * Ordinarily the complement of the resulting code word is used for * transmission, and so the code word is complemented before it is returned. * * For further information see John C. Bellamy's Digital Telephony, 1982, * John Wiley & Sons, pps 98-111 and 472-476. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/audioop.c#L154-L189
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
st_linear2alaw
static unsigned char st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */ { PyInt16 mask; short seg; unsigned char aval; /* The original sox code does this in the calling function, not here */ pcm_val = pcm_val >> 3; /* A-law using even bit inversion */ if (pcm_val >= 0) { mask = 0xD5; /* sign (7th) bit = 1 */ } else { mask = 0x55; /* sign bit = 0 */ pcm_val = -pcm_val - 1; } /* Convert the scaled magnitude to segment number. */ seg = search(pcm_val, seg_aend, 8); /* Combine the sign, segment, and quantization bits. */ if (seg >= 8) /* out of range, return maximum value. */ return (unsigned char) (0x7F ^ mask); else { aval = (unsigned char) seg << SEG_SHIFT; if (seg < 2) aval |= (pcm_val >> 1) & QUANT_MASK; else aval |= (pcm_val >> seg) & QUANT_MASK; return (aval ^ mask); } }
/* * linear2alaw() accepts an 13-bit signed integer and encodes it as A-law data * stored in a unsigned char. This function should only be called with * the data shifted such that it only contains information in the lower * 13-bits. * * Linear Input Code Compressed Code * ------------------------ --------------- * 0000000wxyza 000wxyz * 0000001wxyza 001wxyz * 000001wxyzab 010wxyz * 00001wxyzabc 011wxyz * 0001wxyzabcd 100wxyz * 001wxyzabcde 101wxyz * 01wxyzabcdef 110wxyz * 1wxyzabcdefg 111wxyz * * For further information see John C. Bellamy's Digital Telephony, 1982, * John Wiley & Sons, pps 98-111 and 472-476. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/audioop.c#L251-L284
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
check_iterbuffered
static int check_iterbuffered(BZ2FileObject *f) { if (f->f_buf != NULL && (f->f_bufend - f->f_bufptr) > 0 && f->f_buf[0] != '\0') { PyErr_SetString(PyExc_ValueError, "Mixing iteration and read methods would lose data"); return -1; } return 0; }
/* ===================================================================== */ /* Utility functions. */ /* Refuse regular I/O if there's data in the iteration-buffer. * Mixing them would cause data to arrive out of order, as the read* * methods don't use the iteration buffer. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/bz2module.c#L135-L146
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
Util_NewBufferSize
static size_t Util_NewBufferSize(size_t currentsize) { /* Expand the buffer by an amount proportional to the current size, giving us amortized linear-time behavior. Use a less-than-double growth factor to avoid excessive allocation. */ return currentsize + (currentsize >> 3) + 6; }
/* This is a hacked version of Python's fileobject.c:new_buffersize(). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/bz2module.c#L213-L220
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
Util_UnivNewlineRead
size_t Util_UnivNewlineRead(int *bzerror, BZFILE *stream, char* buf, size_t n, BZ2FileObject *f) { char *dst = buf; int newlinetypes, skipnextlf; assert(buf != NULL); assert(stream != NULL); if (!f->f_univ_newline) return BZ2_bzRead(bzerror, stream, buf, n); newlinetypes = f->f_newlinetypes; skipnextlf = f->f_skipnextlf; /* Invariant: n is the number of bytes remaining to be filled * in the buffer. */ while (n) { size_t nread; int shortread; char *src = dst; nread = BZ2_bzRead(bzerror, stream, dst, n); assert(nread <= n); n -= nread; /* assuming 1 byte out for each in; will adjust */ shortread = n != 0; /* true iff EOF or error */ while (nread--) { char c = *src++; if (c == '\r') { /* Save as LF and set flag to skip next LF. */ *dst++ = '\n'; skipnextlf = 1; } else if (skipnextlf && c == '\n') { /* Skip LF, and remember we saw CR LF. */ skipnextlf = 0; newlinetypes |= NEWLINE_CRLF; ++n; } else { /* Normal char to be stored in buffer. Also * update the newlinetypes flag if either this * is an LF or the previous char was a CR. */ if (c == '\n') newlinetypes |= NEWLINE_LF; else if (skipnextlf) newlinetypes |= NEWLINE_CR; *dst++ = c; skipnextlf = 0; } } if (shortread) { /* If this is EOF, update type flags. */ if (skipnextlf && *bzerror == BZ_STREAM_END) newlinetypes |= NEWLINE_CR; break; } } f->f_newlinetypes = newlinetypes; f->f_skipnextlf = skipnextlf; return dst - buf; }
/* This is a hacked version of Python's * fileobject.c:Py_UniversalNewlineFread(). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/bz2module.c#L333-L397
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
Util_DropReadAhead
static void Util_DropReadAhead(BZ2FileObject *f) { if (f->f_buf != NULL) { PyMem_Free(f->f_buf); f->f_buf = NULL; } }
/* This is a hacked version of Python's fileobject.c:drop_readahead(). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/bz2module.c#L400-L407
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
Util_ReadAhead
static int Util_ReadAhead(BZ2FileObject *f, int bufsize) { int chunksize; int bzerror; if (f->f_buf != NULL) { if((f->f_bufend - f->f_bufptr) >= 1) return 0; else Util_DropReadAhead(f); } if (f->mode == MODE_READ_EOF) { f->f_bufptr = f->f_buf; f->f_bufend = f->f_buf; return 0; } if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) { PyErr_NoMemory(); return -1; } Py_BEGIN_ALLOW_THREADS chunksize = Util_UnivNewlineRead(&bzerror, f->fp, f->f_buf, bufsize, f); Py_END_ALLOW_THREADS f->pos += chunksize; if (bzerror == BZ_STREAM_END) { f->size = f->pos; f->mode = MODE_READ_EOF; } else if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); Util_DropReadAhead(f); return -1; } f->f_bufptr = f->f_buf; f->f_bufend = f->f_buf + chunksize; return 0; }
/* This is a hacked version of Python's fileobject.c:readahead(). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/bz2module.c#L410-L447
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
BZ2File_init
static int BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = {"filename", "mode", "buffering", "compresslevel", 0}; PyObject *name; char *mode = "r"; int buffering = -1; int compresslevel = 9; int bzerror; int mode_char = 0; self->size = -1; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|sii:BZ2File", kwlist, &name, &mode, &buffering, &compresslevel)) return -1; if (compresslevel < 1 || compresslevel > 9) { PyErr_SetString(PyExc_ValueError, "compresslevel must be between 1 and 9"); return -1; } for (;;) { int error = 0; switch (*mode) { case 'r': case 'w': if (mode_char) error = 1; mode_char = *mode; break; case 'b': break; case 'U': #ifdef __VMS self->f_univ_newline = 0; #else self->f_univ_newline = 1; #endif break; default: error = 1; break; } if (error) { PyErr_Format(PyExc_ValueError, "invalid mode char %c", *mode); return -1; } mode++; if (*mode == '\0') break; } if (mode_char == 0) { mode_char = 'r'; } mode = (mode_char == 'r') ? "rb" : "wb"; self->file = PyObject_CallFunction((PyObject*)&PyFile_Type, "(Osi)", name, mode, buffering); if (self->file == NULL) return -1; /* From now on, we have stuff to dealloc, so jump to error label * instead of returning */ #ifdef WITH_THREAD self->lock = PyThread_allocate_lock(); if (!self->lock) { PyErr_SetString(PyExc_MemoryError, "unable to allocate lock"); goto error; } #endif if (mode_char == 'r') self->fp = BZ2_bzReadOpen(&bzerror, PyFile_AsFile(self->file), 0, 0, NULL, 0); else self->fp = BZ2_bzWriteOpen(&bzerror, PyFile_AsFile(self->file), compresslevel, 0, 0); if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); goto error; } PyFile_IncUseCount((PyFileObject *)self->file); self->mode = (mode_char == 'r') ? MODE_READ : MODE_WRITE; return 0; error: Py_CLEAR(self->file); #ifdef WITH_THREAD if (self->lock) { PyThread_free_lock(self->lock); self->lock = NULL; } #endif return -1; }
/* ===================================================================== */ /* Slot definitions for BZ2File_Type. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/bz2module.c#L1357-L1467
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
BZ2Comp_init
static int BZ2Comp_init(BZ2CompObject *self, PyObject *args, PyObject *kwargs) { int compresslevel = 9; int bzerror; static char *kwlist[] = {"compresslevel", 0}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:BZ2Compressor", kwlist, &compresslevel)) return -1; if (compresslevel < 1 || compresslevel > 9) { PyErr_SetString(PyExc_ValueError, "compresslevel must be between 1 and 9"); goto error; } #ifdef WITH_THREAD self->lock = PyThread_allocate_lock(); if (!self->lock) { PyErr_SetString(PyExc_MemoryError, "unable to allocate lock"); goto error; } #endif memset(&self->bzs, 0, sizeof(bz_stream)); bzerror = BZ2_bzCompressInit(&self->bzs, compresslevel, 0, 0); if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); goto error; } self->running = 1; return 0; error: #ifdef WITH_THREAD if (self->lock) { PyThread_free_lock(self->lock); self->lock = NULL; } #endif return -1; }
/* ===================================================================== */ /* Slot definitions for BZ2Comp_Type. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/bz2module.c#L1774-L1817
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
BZ2Decomp_init
static int BZ2Decomp_init(BZ2DecompObject *self, PyObject *args, PyObject *kwargs) { int bzerror; if (!PyArg_ParseTuple(args, ":BZ2Decompressor")) return -1; #ifdef WITH_THREAD self->lock = PyThread_allocate_lock(); if (!self->lock) { PyErr_SetString(PyExc_MemoryError, "unable to allocate lock"); goto error; } #endif self->unused_data = PyString_FromString(""); if (!self->unused_data) goto error; memset(&self->bzs, 0, sizeof(bz_stream)); bzerror = BZ2_bzDecompressInit(&self->bzs, 0, 0); if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); goto error; } self->running = 1; return 0; error: #ifdef WITH_THREAD if (self->lock) { PyThread_free_lock(self->lock); self->lock = NULL; } #endif Py_CLEAR(self->unused_data); return -1; }
/* ===================================================================== */ /* Slot definitions for BZ2Decomp_Type. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/bz2module.c#L2012-L2052
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
Pdata_clear
static int Pdata_clear(Pdata *self, Py_ssize_t clearto) { Py_ssize_t i; PyObject **p; if (clearto < 0) return stackUnderflow(); if (clearto >= self->length) return 0; for (i = self->length, p = self->data + clearto; --i >= clearto; p++) { Py_CLEAR(*p); } self->length = clearto; return 0; }
/* Retain only the initial clearto items. If clearto >= the current * number of items, this is a (non-erroneous) NOP. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L195-L212
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
store_tuple_elements
static int store_tuple_elements(Picklerobject *self, PyObject *t, int len) { Py_ssize_t i; int res = -1; /* guilty until proved innocent */ assert(PyTuple_Size(t) == len); for (i = 0; i < len; i++) { PyObject *element = PyTuple_GET_ITEM(t, i); if (element == NULL) goto finally; if (save(self, element, 0) < 0) goto finally; } res = 0; finally: return res; }
/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L1479-L1499
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
save_tuple
static int save_tuple(Picklerobject *self, PyObject *args) { PyObject *py_tuple_id = NULL; Py_ssize_t len, i; int res = -1; static char tuple = TUPLE; static char pop = POP; static char pop_mark = POP_MARK; static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; if ((len = PyTuple_Size(args)) < 0) goto finally; if (len == 0) { char c_str[2]; if (self->proto) { c_str[0] = EMPTY_TUPLE; len = 1; } else { c_str[0] = MARK; c_str[1] = TUPLE; len = 2; } if (self->write_func(self, c_str, len) >= 0) res = 0; /* Don't memoize an empty tuple. */ goto finally; } /* A non-empty tuple. */ /* id(tuple) isn't in the memo now. If it shows up there after * saving the tuple elements, the tuple must be recursive, in * which case we'll pop everything we put on the stack, and fetch * its value from the memo. */ py_tuple_id = PyLong_FromVoidPtr(args); if (py_tuple_id == NULL) goto finally; if (len <= 3 && self->proto >= 2) { /* Use TUPLE{1,2,3} opcodes. */ if (store_tuple_elements(self, args, len) < 0) goto finally; if (PyDict_GetItem(self->memo, py_tuple_id)) { /* pop the len elements */ for (i = 0; i < len; ++i) if (self->write_func(self, &pop, 1) < 0) goto finally; /* fetch from memo */ if (get(self, py_tuple_id) < 0) goto finally; res = 0; goto finally; } /* Not recursive. */ if (self->write_func(self, len2opcode + len, 1) < 0) goto finally; goto memoize; } /* proto < 2 and len > 0, or proto >= 2 and len > 3. * Generate MARK elt1 elt2 ... TUPLE */ if (self->write_func(self, &MARKv, 1) < 0) goto finally; if (store_tuple_elements(self, args, len) < 0) goto finally; if (PyDict_GetItem(self->memo, py_tuple_id)) { /* pop the stack stuff we pushed */ if (self->bin) { if (self->write_func(self, &pop_mark, 1) < 0) goto finally; } else { /* Note that we pop one more than len, to remove * the MARK too. */ for (i = 0; i <= len; i++) if (self->write_func(self, &pop, 1) < 0) goto finally; } /* fetch from memo */ if (get(self, py_tuple_id) >= 0) res = 0; goto finally; } /* Not recursive. */ if (self->write_func(self, &tuple, 1) < 0) goto finally; memoize: if (put(self, args) >= 0) res = 0; finally: Py_XDECREF(py_tuple_id); return res; }
/* Tuples are ubiquitous in the pickle protocols, so many techniques are * used across protocols to minimize the space needed to pickle them. * Tuples are also the only builtin immutable type that can be recursive * (a tuple can be reached from itself), and that requires some subtle * magic so that it works in all cases. IOW, this is a long routine. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L1507-L1612
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
batch_list
static int batch_list(Picklerobject *self, PyObject *iter) { PyObject *obj = NULL; PyObject *firstitem = NULL; int i, n; static char append = APPEND; static char appends = APPENDS; assert(iter != NULL); if (self->proto == 0) { /* APPENDS isn't available; do one at a time. */ for (;;) { obj = PyIter_Next(iter); if (obj == NULL) { if (PyErr_Occurred()) return -1; break; } i = save(self, obj, 0); Py_DECREF(obj); if (i < 0) return -1; if (self->write_func(self, &append, 1) < 0) return -1; } return 0; } /* proto > 0: write in batches of BATCHSIZE. */ do { /* Get first item */ firstitem = PyIter_Next(iter); if (firstitem == NULL) { if (PyErr_Occurred()) goto BatchFailed; /* nothing more to add */ break; } /* Try to get a second item */ obj = PyIter_Next(iter); if (obj == NULL) { if (PyErr_Occurred()) goto BatchFailed; /* Only one item to write */ if (save(self, firstitem, 0) < 0) goto BatchFailed; if (self->write_func(self, &append, 1) < 0) goto BatchFailed; Py_CLEAR(firstitem); break; } /* More than one item to write */ /* Pump out MARK, items, APPENDS. */ if (self->write_func(self, &MARKv, 1) < 0) goto BatchFailed; if (save(self, firstitem, 0) < 0) goto BatchFailed; Py_CLEAR(firstitem); n = 1; /* Fetch and save up to BATCHSIZE items */ while (obj) { if (save(self, obj, 0) < 0) goto BatchFailed; Py_CLEAR(obj); n += 1; if (n == BATCHSIZE) break; obj = PyIter_Next(iter); if (obj == NULL) { if (PyErr_Occurred()) goto BatchFailed; break; } } if (self->write_func(self, &appends, 1) < 0) goto BatchFailed; } while (n == BATCHSIZE); return 0; BatchFailed: Py_XDECREF(firstitem); Py_XDECREF(obj); return -1; }
/* iter is an iterator giving items, and we batch up chunks of * MARK item item ... item APPENDS * opcode sequences. Calling code should have arranged to first create an * empty list, or list-like object, for the APPENDS to operate on. * Returns 0 on success, <0 on error. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L1620-L1717
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
batch_dict
static int batch_dict(Picklerobject *self, PyObject *iter) { PyObject *p = NULL; PyObject *firstitem = NULL; int i, n; static char setitem = SETITEM; static char setitems = SETITEMS; assert(iter != NULL); if (self->proto == 0) { /* SETITEMS isn't available; do one at a time. */ for (;;) { p = PyIter_Next(iter); if (p == NULL) { if (PyErr_Occurred()) return -1; break; } if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { PyErr_SetString(PyExc_TypeError, "dict items " "iterator must return 2-tuples"); return -1; } i = save(self, PyTuple_GET_ITEM(p, 0), 0); if (i >= 0) i = save(self, PyTuple_GET_ITEM(p, 1), 0); Py_DECREF(p); if (i < 0) return -1; if (self->write_func(self, &setitem, 1) < 0) return -1; } return 0; } /* proto > 0: write in batches of BATCHSIZE. */ do { /* Get first item */ firstitem = PyIter_Next(iter); if (firstitem == NULL) { if (PyErr_Occurred()) goto BatchFailed; /* nothing more to add */ break; } if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { PyErr_SetString(PyExc_TypeError, "dict items " "iterator must return 2-tuples"); goto BatchFailed; } /* Try to get a second item */ p = PyIter_Next(iter); if (p == NULL) { if (PyErr_Occurred()) goto BatchFailed; /* Only one item to write */ if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) goto BatchFailed; if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) goto BatchFailed; if (self->write_func(self, &setitem, 1) < 0) goto BatchFailed; Py_CLEAR(firstitem); break; } /* More than one item to write */ /* Pump out MARK, items, SETITEMS. */ if (self->write_func(self, &MARKv, 1) < 0) goto BatchFailed; if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) goto BatchFailed; if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) goto BatchFailed; Py_CLEAR(firstitem); n = 1; /* Fetch and save up to BATCHSIZE items */ while (p) { if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) { PyErr_SetString(PyExc_TypeError, "dict items " "iterator must return 2-tuples"); goto BatchFailed; } if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0) goto BatchFailed; if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0) goto BatchFailed; Py_CLEAR(p); n += 1; if (n == BATCHSIZE) break; p = PyIter_Next(iter); if (p == NULL) { if (PyErr_Occurred()) goto BatchFailed; break; } } if (self->write_func(self, &setitems, 1) < 0) goto BatchFailed; } while (n == BATCHSIZE); return 0; BatchFailed: Py_XDECREF(firstitem); Py_XDECREF(p); return -1; }
/* iter is an iterator giving (key, value) pairs, and we batch up chunks of * MARK key value ... key value SETITEMS * opcode sequences. Calling code should have arranged to first create an * empty dict, or dict-like object, for the SETITEMS to operate on. * Returns 0 on success, <0 on error. * * This is very much like batch_list(). The difference between saving * elements directly, and picking apart two-tuples, is so long-winded at * the C level, though, that attempts to combine these routines were too * ugly to bear. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L1788-L1908
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
batch_dict_exact
static int batch_dict_exact(Picklerobject *self, PyObject *obj) { PyObject *key = NULL, *value = NULL; int i; Py_ssize_t dict_size, ppos = 0; static char setitem = SETITEM; static char setitems = SETITEMS; assert(obj != NULL); assert(self->proto > 0); dict_size = PyDict_Size(obj); /* Special-case len(d) == 1 to save space. */ if (dict_size == 1) { PyDict_Next(obj, &ppos, &key, &value); if (save(self, key, 0) < 0) return -1; if (save(self, value, 0) < 0) return -1; if (self->write_func(self, &setitem, 1) < 0) return -1; return 0; } /* Write in batches of BATCHSIZE. */ do { i = 0; if (self->write_func(self, &MARKv, 1) < 0) return -1; while (PyDict_Next(obj, &ppos, &key, &value)) { if (save(self, key, 0) < 0) return -1; if (save(self, value, 0) < 0) return -1; if (++i == BATCHSIZE) break; } if (self->write_func(self, &setitems, 1) < 0) return -1; if (PyDict_Size(obj) != dict_size) { PyErr_Format( PyExc_RuntimeError, "dictionary changed size during iteration"); return -1; } } while (i == BATCHSIZE); return 0; }
/* This is a variant of batch_dict() above that specializes for dicts, with no * support for dict subclasses. Like batch_dict(), we batch up chunks of * MARK key value ... key value SETITEMS * opcode sequences. Calling code should have arranged to first create an * empty dict, or dict-like object, for the SETITEMS to operate on. * Returns 0 on success, -1 on error. * * Note that this currently doesn't work for protocol 0. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L1919-L1970
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
save_reduce
static int save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob) { PyObject *callable; PyObject *argtup; PyObject *state = NULL; PyObject *listitems = Py_None; PyObject *dictitems = Py_None; Py_ssize_t size; int use_newobj = self->proto >= 2; static char reduce = REDUCE; static char build = BUILD; static char newobj = NEWOBJ; size = PyTuple_Size(args); if (size < 2 || size > 5) { cPickle_ErrFormat(PicklingError, "tuple returned by " "%s must contain 2 through 5 elements", "O", fn); return -1; } if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5, &callable, &argtup, &state, &listitems, &dictitems)) return -1; if (!PyTuple_Check(argtup)) { cPickle_ErrFormat(PicklingError, "Second element of " "tuple returned by %s must be a tuple", "O", fn); return -1; } if (state == Py_None) state = NULL; if (listitems == Py_None) listitems = NULL; else if (!PyIter_Check(listitems)) { cPickle_ErrFormat(PicklingError, "Fourth element of " "tuple returned by %s must be an iterator, not %s", "Os", fn, Py_TYPE(listitems)->tp_name); return -1; } if (dictitems == Py_None) dictitems = NULL; else if (!PyIter_Check(dictitems)) { cPickle_ErrFormat(PicklingError, "Fifth element of " "tuple returned by %s must be an iterator, not %s", "Os", fn, Py_TYPE(dictitems)->tp_name); return -1; } /* Protocol 2 special case: if callable's name is __newobj__, use * NEWOBJ. This consumes a lot of code. */ if (use_newobj) { PyObject *temp = PyObject_GetAttr(callable, __name___str); if (temp == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError)) PyErr_Clear(); else return -1; use_newobj = 0; } else { use_newobj = PyString_Check(temp) && strcmp(PyString_AS_STRING(temp), "__newobj__") == 0; Py_DECREF(temp); } } if (use_newobj) { PyObject *cls; PyObject *newargtup; Py_ssize_t n, i; /* Sanity checks. */ n = PyTuple_Size(argtup); if (n < 1) { PyErr_SetString(PicklingError, "__newobj__ arglist " "is empty"); return -1; } cls = PyTuple_GET_ITEM(argtup, 0); if (! PyObject_HasAttrString(cls, "__new__")) { PyErr_SetString(PicklingError, "args[0] from " "__newobj__ args has no __new__"); return -1; } /* XXX How could ob be NULL? */ if (ob != NULL) { PyObject *ob_dot_class; ob_dot_class = PyObject_GetAttr(ob, __class___str); if (ob_dot_class == NULL) { if (PyErr_ExceptionMatches( PyExc_AttributeError)) PyErr_Clear(); else return -1; } i = ob_dot_class != cls; /* true iff a problem */ Py_XDECREF(ob_dot_class); if (i) { PyErr_SetString(PicklingError, "args[0] from " "__newobj__ args has the wrong class"); return -1; } } /* Save the class and its __new__ arguments. */ if (save(self, cls, 0) < 0) return -1; newargtup = PyTuple_New(n-1); /* argtup[1:] */ if (newargtup == NULL) return -1; for (i = 1; i < n; ++i) { PyObject *temp = PyTuple_GET_ITEM(argtup, i); Py_INCREF(temp); PyTuple_SET_ITEM(newargtup, i-1, temp); } i = save(self, newargtup, 0); Py_DECREF(newargtup); if (i < 0) return -1; /* Add NEWOBJ opcode. */ if (self->write_func(self, &newobj, 1) < 0) return -1; } else { /* Not using NEWOBJ. */ if (save(self, callable, 0) < 0 || save(self, argtup, 0) < 0 || self->write_func(self, &reduce, 1) < 0) return -1; } /* Memoize. */ /* XXX How can ob be NULL? */ if (ob != NULL) { if (state && !PyDict_Check(state)) { if (put2(self, ob) < 0) return -1; } else if (put(self, ob) < 0) return -1; } if (listitems && batch_list(self, listitems) < 0) return -1; if (dictitems && batch_dict(self, dictitems) < 0) return -1; if (state) { if (save(self, state, 0) < 0 || self->write_func(self, &build, 1) < 0) return -1; } return 0; }
/* We're saving ob, and args is the 2-thru-5 tuple returned by the * appropriate __reduce__ method for ob. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L2383-L2558
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
calc_binint
static long calc_binint(char *s, int x) { unsigned char c; int i; long l; for (i = 0, l = 0L; i < x; i++) { c = (unsigned char)s[i]; l |= (long)c << (i * 8); } #if SIZEOF_LONG > 4 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) * is signed, so on a box with longs bigger than 4 bytes we need * to extend a BININT's sign bit to the full width. */ if (x == 4 && l & (1L << 31)) l |= (~0L) << 32; #endif return l; }
/* s contains x bytes of a little-endian integer. Return its value as a * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian * int, but when x is 4 it's a signed one. This is an historical source * of x-platform bugs. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L3434-L3454
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
load_counted_long
static int load_counted_long(Unpicklerobject *self, int size) { Py_ssize_t i; char *nbytes; unsigned char *pdata; PyObject *along; assert(size == 1 || size == 4); i = self->read_func(self, &nbytes, size); if (i < 0) return -1; size = calc_binint(nbytes, size); if (size < 0) { /* Corrupt or hostile pickle -- we never write one like * this. */ PyErr_SetString(UnpicklingError, "LONG pickle has negative " "byte count"); return -1; } if (size == 0) along = PyLong_FromLong(0L); else { /* Read the raw little-endian bytes & convert. */ i = self->read_func(self, (char **)&pdata, size); if (i < 0) return -1; along = _PyLong_FromByteArray(pdata, (size_t)size, 1 /* little endian */, 1 /* signed */); } if (along == NULL) return -1; PDATA_PUSH(self->stack, along, -1); return 0; }
/* 'size' bytes contain the # of bytes of little-endian 256's-complement * data following. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L3536-L3571
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
load_extension
static int load_extension(Unpicklerobject *self, int nbytes) { char *codebytes; /* the nbytes bytes after the opcode */ long code; /* calc_binint returns long */ PyObject *py_code; /* code as a Python int */ PyObject *obj; /* the object to push */ PyObject *pair; /* (module_name, class_name) */ PyObject *module_name, *class_name; assert(nbytes == 1 || nbytes == 2 || nbytes == 4); if (self->read_func(self, &codebytes, nbytes) < 0) return -1; code = calc_binint(codebytes, nbytes); if (code <= 0) { /* note that 0 is forbidden */ /* Corrupt or hostile pickle. */ PyErr_SetString(UnpicklingError, "EXT specifies code <= 0"); return -1; } /* Look for the code in the cache. */ py_code = PyInt_FromLong(code); if (py_code == NULL) return -1; obj = PyDict_GetItem(extension_cache, py_code); if (obj != NULL) { /* Bingo. */ Py_DECREF(py_code); PDATA_APPEND(self->stack, obj, -1); return 0; } /* Look up the (module_name, class_name) pair. */ pair = PyDict_GetItem(inverted_registry, py_code); if (pair == NULL) { Py_DECREF(py_code); PyErr_Format(PyExc_ValueError, "unregistered extension " "code %ld", code); return -1; } /* Since the extension registry is manipulable via Python code, * confirm that pair is really a 2-tuple of strings. */ if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { Py_DECREF(py_code); PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " "isn't a 2-tuple of strings", code); return -1; } /* Load the object. */ obj = find_class(module_name, class_name, self->find_class); if (obj == NULL) { Py_DECREF(py_code); return -1; } /* Cache code -> obj. */ code = PyDict_SetItem(extension_cache, py_code, obj); Py_DECREF(py_code); if (code < 0) { Py_DECREF(obj); return -1; } PDATA_PUSH(self->stack, obj, -1); return 0; }
/* Push an object from the extension registry (EXT[124]). nbytes is * the number of bytes following the opcode, holding the index (code) value. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L4285-L4349
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
load_proto
static int load_proto(Unpicklerobject *self) { int i; char *protobyte; i = self->read_func(self, &protobyte, 1); if (i < 0) return -1; i = calc_binint(protobyte, 1); /* No point checking for < 0, since calc_binint returns an unsigned * int when chewing on 1 byte. */ assert(i >= 0); if (i <= HIGHEST_PROTOCOL) return 0; PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); return -1; }
/* Just raises an error if we don't know the protocol specified. PROTO * is the first opcode for protocols >= 2. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L4686-L4706
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
noload_obj
static int noload_obj(Unpicklerobject *self) { Py_ssize_t i; if ((i = marker(self)) < 0) return -1; return Pdata_clear(self->stack, i+1); }
/* No-load functions to support noload, which is used to find persistent references. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cPickle.c#L5016-L5023
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
PyArg_GetObject
int PyArg_GetObject(register PyObject *args, int nargs, int i, PyObject **p_arg) { if (nargs != 1) { if (args == NULL || !PyTuple_Check(args) || nargs != PyTuple_Size(args) || i < 0 || i >= nargs) { return PyErr_BadArgument(); } else { args = PyTuple_GetItem(args, i); } } if (args == NULL) { return PyErr_BadArgument(); } *p_arg = args; return 1; }
/* Functions to extract arguments. These needs to know the total number of arguments supplied, since the argument list is a tuple only of there is more than one argument. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cgensupport.c#L13-L31
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
PyArg_GetLongArray
int PyArg_GetLongArray(PyObject *args, int nargs, int i, int n, long *p_arg) { PyObject *v, *w; if (!PyArg_GetObject(args, nargs, i, &v)) return 0; if (PyTuple_Check(v)) { if (PyTuple_Size(v) != n) { return PyErr_BadArgument(); } for (i = 0; i < n; i++) { w = PyTuple_GetItem(v, i); if (!PyInt_Check(w)) { return PyErr_BadArgument(); } p_arg[i] = PyInt_AsLong(w); } return 1; } else if (PyList_Check(v)) { if (PyList_Size(v) != n) { return PyErr_BadArgument(); } for (i = 0; i < n; i++) { w = PyList_GetItem(v, i); if (!PyInt_Check(w)) { return PyErr_BadArgument(); } p_arg[i] = PyInt_AsLong(w); } return 1; } else { return PyErr_BadArgument(); } }
/* XXX The following four are too similar. Should share more code. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/cgensupport.c#L172-L207
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
cl_ErrorHandler
static void cl_ErrorHandler(CL_Handle handle, int code, const char *fmt, ...) { va_list ap; char errbuf[BUFSIZ]; /* hopefully big enough */ char *p; if (PyErr_Occurred()) /* don't change existing error */ return; error_handler_called = 1; va_start(ap, fmt); vsprintf(errbuf, fmt, ap); va_end(ap); p = &errbuf[strlen(errbuf) - 1]; /* swat the line feed */ if (*p == '\n') *p = 0; PyErr_SetString(ClError, errbuf); }
/******************************************************************** Utility routines. ********************************************************************/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/clmodule.c#L38-L55
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
param_type_is_float
static int param_type_is_float(clobject *self, int param) { int bufferlength; if (self->ob_paramtypes == NULL) { error_handler_called = 0; bufferlength = clQueryParams(self->ob_compressorHdl, 0, 0); if (error_handler_called) return -1; self->ob_paramtypes = PyMem_NEW(int, bufferlength); if (self->ob_paramtypes == NULL) return -1; self->ob_nparams = bufferlength / 2; (void) clQueryParams(self->ob_compressorHdl, self->ob_paramtypes, bufferlength); if (error_handler_called) { PyMem_DEL(self->ob_paramtypes); self->ob_paramtypes = NULL; return -1; } } if (param < 0 || param >= self->ob_nparams) return -1; if (self->ob_paramtypes[param*2 + 1] == CL_FLOATING_ENUM_VALUE || self->ob_paramtypes[param*2 + 1] == CL_FLOATING_RANGE_VALUE) return 1; else return 0; }
/* * This assumes that params are always in the range 0 to some maximum. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/clmodule.c#L60-L93
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
divmod
static int divmod(int x, int y, int *r) { int quo; assert(y > 0); quo = x / y; *r = x - quo * y; if (*r < 0) { --quo; *r += y; } assert(0 <= *r && *r < y); return quo; }
/* Compute Python divmod(x, y), returning the quotient and storing the * remainder into *r. The quotient is the floor of x/y, and that's * the real point of this. C will probably truncate instead (C99 * requires truncation; C89 left it implementation-defined). * Simplification: we *require* that y > 0 here. That's appropriate * for all the uses made of it. This simplifies the code and makes * the overflow case impossible (divmod(LONG_MIN, -1) is the only * overflow case). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L128-L142
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
round_to_long
static long round_to_long(double x) { if (x >= 0.0) x = floor(x + 0.5); else x = ceil(x - 0.5); return (long)x; }
/* Round a double to the nearest long. |x| must be small enough to fit * in a C long; this is not checked. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L147-L155
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
is_leap
static int is_leap(int year) { /* Cast year to unsigned. The result is the same either way, but * C can generate faster code for unsigned mod than for signed * mod (especially for % 4 -- a good compiler should just grab * the last 2 bits when the LHS is unsigned). */ const unsigned int ayear = (unsigned int)year; return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); }
/* year -> 1 if leap year, else 0. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L176-L186
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
days_in_month
static int days_in_month(int year, int month) { assert(month >= 1); assert(month <= 12); if (month == 2 && is_leap(year)) return 29; else return _days_in_month[month]; }
/* year, month -> number of days in that month in that year */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L189-L198
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
days_before_month
static int days_before_month(int year, int month) { int days; assert(month >= 1); assert(month <= 12); days = _days_before_month[month]; if (month > 2 && is_leap(year)) ++days; return days; }
/* year, month -> number of days in year preceeding first day of month */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L201-L212
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
days_before_year
static int days_before_year(int year) { int y = year - 1; /* This is incorrect if year <= 0; we really want the floor * here. But so long as MINYEAR is 1, the smallest year this * can see is 0 (this can happen in some normalization endcases), * so we'll just special-case that. */ assert (year >= 0); if (y >= 0) return y*365 + y/4 - y/100 + y/400; else { assert(y == -1); return -366; } }
/* year -> number of days before January 1st of year. Remember that we * start with year 1, so days_before_year(1) == 0. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L217-L233
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
ord_to_ymd
static void ord_to_ymd(int ordinal, int *year, int *month, int *day) { int n, n1, n4, n100, n400, leapyear, preceding; /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of * leap years repeats exactly every 400 years. The basic strategy is * to find the closest 400-year boundary at or before ordinal, then * work with the offset from that boundary to ordinal. Life is much * clearer if we subtract 1 from ordinal first -- then the values * of ordinal at 400-year boundaries are exactly those divisible * by DI400Y: * * D M Y n n-1 * -- --- ---- ---------- ---------------- * 31 Dec -400 -DI400Y -DI400Y -1 * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary * ... * 30 Dec 000 -1 -2 * 31 Dec 000 0 -1 * 1 Jan 001 1 0 400-year boundary * 2 Jan 001 2 1 * 3 Jan 001 3 2 * ... * 31 Dec 400 DI400Y DI400Y -1 * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary */ assert(ordinal >= 1); --ordinal; n400 = ordinal / DI400Y; n = ordinal % DI400Y; *year = n400 * 400 + 1; /* Now n is the (non-negative) offset, in days, from January 1 of * year, to the desired date. Now compute how many 100-year cycles * precede n. * Note that it's possible for n100 to equal 4! In that case 4 full * 100-year cycles precede the desired day, which implies the * desired day is December 31 at the end of a 400-year cycle. */ n100 = n / DI100Y; n = n % DI100Y; /* Now compute how many 4-year cycles precede it. */ n4 = n / DI4Y; n = n % DI4Y; /* And now how many single years. Again n1 can be 4, and again * meaning that the desired day is December 31 at the end of the * 4-year cycle. */ n1 = n / 365; n = n % 365; *year += n100 * 100 + n4 * 4 + n1; if (n1 == 4 || n100 == 4) { assert(n == 0); *year -= 1; *month = 12; *day = 31; return; } /* Now the year is correct, and n is the offset from January 1. We * find the month via an estimate that's either exact or one too * large. */ leapyear = n1 == 3 && (n4 != 24 || n100 == 3); assert(leapyear == is_leap(*year)); *month = (n + 50) >> 5; preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); if (preceding > n) { /* estimate is too large */ *month -= 1; preceding -= days_in_month(*year, *month); } n -= preceding; assert(0 <= n); assert(n < days_in_month(*year, *month)); *day = n + 1; }
/* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L243-L324
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
ymd_to_ord
static int ymd_to_ord(int year, int month, int day) { return days_before_year(year) + days_before_month(year, month) + day; }
/* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L327-L331
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
weekday
static int weekday(int year, int month, int day) { return (ymd_to_ord(year, month, day) + 6) % 7; }
/* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L334-L338
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
iso_week1_monday
static int iso_week1_monday(int year) { int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ int first_weekday = (first_day + 6) % 7; /* ordinal of closest Monday at or before 1/1 */ int week1_monday = first_day - first_weekday; if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ week1_monday += 7; return week1_monday; }
/* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the * first calendar week containing a Thursday. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L343-L355
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
check_delta_day_range
static int check_delta_day_range(int days) { if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) return 0; PyErr_Format(PyExc_OverflowError, "days=%d; must have magnitude <= %d", days, MAX_DELTA_DAYS); return -1; }
/* --------------------------------------------------------------------------- * Range checkers. */ /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. * If not, raise OverflowError and return -1. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L364-L373
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
check_date_args
static int check_date_args(int year, int month, int day) { if (year < MINYEAR || year > MAXYEAR) { PyErr_SetString(PyExc_ValueError, "year is out of range"); return -1; } if (month < 1 || month > 12) { PyErr_SetString(PyExc_ValueError, "month must be in 1..12"); return -1; } if (day < 1 || day > days_in_month(year, month)) { PyErr_SetString(PyExc_ValueError, "day is out of range for month"); return -1; } return 0; }
/* Check that date arguments are in range. Return 0 if they are. If they * aren't, raise ValueError and return -1. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L378-L398
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
check_time_args
static int check_time_args(int h, int m, int s, int us) { if (h < 0 || h > 23) { PyErr_SetString(PyExc_ValueError, "hour must be in 0..23"); return -1; } if (m < 0 || m > 59) { PyErr_SetString(PyExc_ValueError, "minute must be in 0..59"); return -1; } if (s < 0 || s > 59) { PyErr_SetString(PyExc_ValueError, "second must be in 0..59"); return -1; } if (us < 0 || us > 999999) { PyErr_SetString(PyExc_ValueError, "microsecond must be in 0..999999"); return -1; } return 0; }
/* Check that time arguments are in range. Return 0 if they are. If they * aren't, raise ValueError and return -1. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L403-L427
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
normalize_pair
static void normalize_pair(int *hi, int *lo, int factor) { assert(factor > 0); assert(lo != hi); if (*lo < 0 || *lo >= factor) { const int num_hi = divmod(*lo, factor, lo); const int new_hi = *hi + num_hi; assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); *hi = new_hi; } assert(0 <= *lo && *lo < factor); }
/* --------------------------------------------------------------------------- * Normalization utilities. */ /* One step of a mixed-radix conversion. A "hi" unit is equivalent to * factor "lo" units. factor must be > 0. If *lo is less than 0, or * at least factor, enough of *lo is converted into "hi" units so that * 0 <= *lo < factor. The input values must be such that int overflow * is impossible. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L439-L451
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
normalize_d_s_us
static void normalize_d_s_us(int *d, int *s, int *us) { if (*us < 0 || *us >= 1000000) { normalize_pair(s, us, 1000000); /* |s| can't be bigger than about * |original s| + |original us|/1000000 now. */ } if (*s < 0 || *s >= 24*3600) { normalize_pair(d, s, 24*3600); /* |d| can't be bigger than about * |original d| + * (|original s| + |original us|/1000000) / (24*3600) now. */ } assert(0 <= *s && *s < 24*3600); assert(0 <= *us && *us < 1000000); }
/* Fiddle days (d), seconds (s), and microseconds (us) so that * 0 <= *s < 24*3600 * 0 <= *us < 1000000 * The input values must be such that the internals don't overflow. * The way this routine is used, we don't get close. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L459-L478
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
normalize_y_m_d
static int normalize_y_m_d(int *y, int *m, int *d) { int dim; /* # of days in month */ /* This gets muddy: the proper range for day can't be determined * without knowing the correct month and year, but if day is, e.g., * plus or minus a million, the current month and year values make * no sense (and may also be out of bounds themselves). * Saying 12 months == 1 year should be non-controversial. */ if (*m < 1 || *m > 12) { --*m; normalize_pair(y, m, 12); ++*m; /* |y| can't be bigger than about * |original y| + |original m|/12 now. */ } assert(1 <= *m && *m <= 12); /* Now only day can be out of bounds (year may also be out of bounds * for a datetime object, but we don't care about that here). * If day is out of bounds, what to do is arguable, but at least the * method here is principled and explainable. */ dim = days_in_month(*y, *m); if (*d < 1 || *d > dim) { /* Move day-1 days from the first of the month. First try to * get off cheap if we're only one day out of range * (adjustments for timezone alone can't be worse than that). */ if (*d == 0) { --*m; if (*m > 0) *d = days_in_month(*y, *m); else { --*y; *m = 12; *d = 31; } } else if (*d == dim + 1) { /* move forward a day */ ++*m; *d = 1; if (*m > 12) { *m = 1; ++*y; } } else { int ordinal = ymd_to_ord(*y, *m, 1) + *d - 1; if (ordinal < 1 || ordinal > MAXORDINAL) { goto error; } else { ord_to_ymd(ordinal, y, m, d); return 0; } } } assert(*m > 0); assert(*d > 0); if (MINYEAR <= *y && *y <= MAXYEAR) return 0; error: PyErr_SetString(PyExc_OverflowError, "date value out of range"); return -1; }
/* Fiddle years (y), months (m), and days (d) so that * 1 <= *m <= 12 * 1 <= *d <= days_in_month(*y, *m) * The input values must be such that the internals don't overflow. * The way this routine is used, we don't get close. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L486-L557
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
normalize_date
static int normalize_date(int *year, int *month, int *day) { return normalize_y_m_d(year, month, day); }
/* Fiddle out-of-bounds months and days so that the result makes some kind * of sense. The parameters are both inputs and outputs. Returns < 0 on * failure, where failure means the adjusted year is out of bounds. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L563-L567
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
normalize_datetime
static int normalize_datetime(int *year, int *month, int *day, int *hour, int *minute, int *second, int *microsecond) { normalize_pair(second, microsecond, 1000000); normalize_pair(minute, second, 60); normalize_pair(hour, minute, 60); normalize_pair(day, hour, 24); return normalize_date(year, month, day); }
/* Force all the datetime fields into range. The parameters are both * inputs and outputs. Returns < 0 on error. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L572-L582
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
set_date_fields
static void set_date_fields(PyDateTime_Date *self, int y, int m, int d) { self->hashcode = -1; SET_YEAR(self, y); SET_MONTH(self, m); SET_DAY(self, d); }
/* --------------------------------------------------------------------------- * Helpers for setting object fields. These work on pointers to the * appropriate base class. */ /* For date and datetime. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L641-L648
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
check_tzinfo_subclass
static int check_tzinfo_subclass(PyObject *p) { if (p == Py_None || PyTZInfo_Check(p)) return 0; PyErr_Format(PyExc_TypeError, "tzinfo argument must be None or of a tzinfo subclass, " "not type '%s'", Py_TYPE(p)->tp_name); return -1; }
/* --------------------------------------------------------------------------- * tzinfo helpers. */ /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not * raise TypeError and return -1. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L764-L774
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
call_utc_tzinfo_method
static int call_utc_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg, int *none) { PyObject *u; int result = -1; assert(tzinfo != NULL); assert(PyTZInfo_Check(tzinfo)); assert(tzinfoarg != NULL); *none = 0; u = call_tzinfo_method(tzinfo, name, tzinfoarg); if (u == NULL) return -1; else if (u == Py_None) { result = 0; *none = 1; } else if (PyDelta_Check(u)) { const int days = GET_TD_DAYS(u); if (days < -1 || days > 0) result = 24*60; /* trigger ValueError below */ else { /* next line can't overflow because we know days * is -1 or 0 now */ int ss = days * 24 * 3600 + GET_TD_SECONDS(u); result = divmod(ss, 60, &ss); if (ss || GET_TD_MICROSECONDS(u)) { PyErr_Format(PyExc_ValueError, "tzinfo.%s() must return a " "whole number of minutes", name); result = -1; } } } else { PyErr_Format(PyExc_TypeError, "tzinfo.%s() must return None or " "timedelta, not '%s'", name, Py_TYPE(u)->tp_name); } Py_DECREF(u); if (result < -1439 || result > 1439) { PyErr_Format(PyExc_ValueError, "tzinfo.%s() returned %d; must be in " "-1439 .. 1439", name, result); result = -1; } return result; }
/* Call getattr(tzinfo, name)(tzinfoarg), and extract an int from the * result. tzinfo must be an instance of the tzinfo class. If the method * returns None, this returns 0 and sets *none to 1. If the method doesn't * return None or timedelta, TypeError is raised and this returns -1. If it * returnsa timedelta and the value is out of range or isn't a whole number * of minutes, ValueError is raised and this returns -1. * Else *none is set to 0 and the integer method result is returned. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L820-L875
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
call_utcoffset
static int call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg, int *none) { return call_utc_tzinfo_method(tzinfo, "utcoffset", tzinfoarg, none); }
/* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the * result. tzinfo must be an instance of the tzinfo class. If utcoffset() * returns None, call_utcoffset returns 0 and sets *none to 1. If uctoffset() * doesn't return None or timedelta, TypeError is raised and this returns -1. * If utcoffset() returns an invalid timedelta (out of range, or not a whole * # of minutes), ValueError is raised and this returns -1. Else *none is * set to 0 and the offset is returned (as int # of minutes east of UTC). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L885-L889
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
call_dst
static int call_dst(PyObject *tzinfo, PyObject *tzinfoarg, int *none) { return call_utc_tzinfo_method(tzinfo, "dst", tzinfoarg, none); }
/* Call tzinfo.dst(tzinfoarg), and extract an integer from the * result. tzinfo must be an instance of the tzinfo class. If dst() * returns None, call_dst returns 0 and sets *none to 1. If dst() & doesn't return None or timedelta, TypeError is raised and this * returns -1. If dst() returns an invalid timedelta for a UTC offset, * ValueError is raised and this returns -1. Else *none is set to 0 and * the offset is returned (as an int # of minutes east of UTC). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L926-L930
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
classify_utcoffset
static naivety classify_utcoffset(PyObject *op, PyObject *tzinfoarg, int *offset) { int none; PyObject *tzinfo; assert(tzinfoarg != NULL); *offset = 0; tzinfo = get_tzinfo_member(op); /* NULL means no tzinfo, not error */ if (tzinfo == Py_None) return OFFSET_NAIVE; if (tzinfo == NULL) { /* note that a datetime passes the PyDate_Check test */ return (PyTime_Check(op) || PyDate_Check(op)) ? OFFSET_NAIVE : OFFSET_UNKNOWN; } *offset = call_utcoffset(tzinfo, tzinfoarg, &none); if (*offset == -1 && PyErr_Occurred()) return OFFSET_ERROR; return none ? OFFSET_NAIVE : OFFSET_AWARE; }
/* Classify an object as to whether it's naive or offset-aware. See * the "naivety" typedef for details. If the type is aware, *offset is set * to minutes east of UTC (as returned by the tzinfo.utcoffset() method). * If the type is offset-naive (or unknown, or error), *offset is set to 0. * tzinfoarg is the argument to pass to the tzinfo.utcoffset() method. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L990-L1010
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
classify_two_utcoffsets
static int classify_two_utcoffsets(PyObject *o1, int *offset1, naivety *n1, PyObject *tzinfoarg1, PyObject *o2, int *offset2, naivety *n2, PyObject *tzinfoarg2) { if (get_tzinfo_member(o1) == get_tzinfo_member(o2)) { *offset1 = *offset2 = 0; *n1 = *n2 = OFFSET_NAIVE; } else { *n1 = classify_utcoffset(o1, tzinfoarg1, offset1); if (*n1 == OFFSET_ERROR) return -1; *n2 = classify_utcoffset(o2, tzinfoarg2, offset2); if (*n2 == OFFSET_ERROR) return -1; } return 0; }
/* Classify two objects as to whether they're naive or offset-aware. * This isn't quite the same as calling classify_utcoffset() twice: for * binary operations (comparison and subtraction), we generally want to * ignore the tzinfo members if they're identical. This is by design, * so that results match "naive" expectations when mixing objects from a * single timezone. So in that case, this sets both offsets to 0 and * both naiveties to OFFSET_NAIVE. * The function returns 0 if everything's OK, and -1 on error. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L1021-L1040
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
format_utcoffset
static int format_utcoffset(char *buf, size_t buflen, const char *sep, PyObject *tzinfo, PyObject *tzinfoarg) { int offset; int hours; int minutes; char sign; int none; assert(buflen >= 1); offset = call_utcoffset(tzinfo, tzinfoarg, &none); if (offset == -1 && PyErr_Occurred()) return -1; if (none) { *buf = '\0'; return 0; } sign = '+'; if (offset < 0) { sign = '-'; offset = - offset; } hours = divmod(offset, 60, &minutes); PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); return 0; }
/* Add an hours & minutes UTC offset string to buf. buf has no more than * buflen bytes remaining. The UTC offset is gotten by calling * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into * *buf, and that's all. Else the returned value is checked for sanity (an * integer in range), and if that's OK it's converted to an hours & minutes * string of the form * sign HH sep MM * Returns 0 if everything is OK. If the return value from utcoffset() is * bogus, an appropriate exception is set and -1 is returned. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L1111-L1138
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
time_dealloc
static void time_dealloc(PyDateTime_Time *self) { if (HASTZINFO(self)) { Py_XDECREF(self->tzinfo); } Py_TYPE(self)->tp_free((PyObject *)self); }
/* * Destructor. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L3203-L3210
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
datetime_dealloc
static void datetime_dealloc(PyDateTime_DateTime *self) { if (HASTZINFO(self)) { Py_XDECREF(self->tzinfo); } Py_TYPE(self)->tp_free((PyObject *)self); }
/* * Destructor. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/datetimemodule.c#L4030-L4037
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
dbm_dealloc
static void dbm_dealloc(register dbmobject *dp) { if ( dp->di_dbm ) dbm_close(dp->di_dbm); PyObject_Del(dp); }
/* Methods */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/dbmmodule.c#L68-L74
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
insint
static void insint(PyObject *d, char *name, int value) { PyObject *v = PyInt_FromLong((long) value); if (!v || PyDict_SetItemString(d, name, v)) PyErr_Clear(); Py_XDECREF(v); }
/* From socketmodule.c * Convenience routine to export an integer value. * * Errors are silently ignored, for better or for worse... */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/dlmodule.c#L226-L234
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
_inscode
static void _inscode(PyObject *d, PyObject *de, char *name, int code) { PyObject *u = PyString_FromString(name); PyObject *v = PyInt_FromLong((long) code); /* Don't bother checking for errors; they'll be caught at the end * of the module initialization function by the caller of * initerrno(). */ if (u && v) { /* insert in modules dict */ PyDict_SetItem(d, u, v); /* insert in errorcode dict */ PyDict_SetItem(de, v, u); } Py_XDECREF(u); Py_XDECREF(v); }
/* Helper function doing the dictionary inserting */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/errnomodule.c#L21-L39
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
ins
static int ins(PyObject* d, char* symbol, long value) { PyObject* v = PyInt_FromLong(value); if (!v || PyDict_SetItemString(d, symbol, v) < 0) return -1; Py_DECREF(v); return 0; }
/* Module initialisation */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/fcntlmodule.c#L425-L434
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
knowgeneric
static void knowgeneric(genericobject *g) { int i, n; /* Create the list if it doesn't already exist */ if (allgenerics == NULL) { allgenerics = PyList_New(0); if (allgenerics == NULL) { PyErr_Clear(); return; /* Too bad, live without allgenerics... */ } } if (nfreeslots > 0) { /* Search the list for reusable slots (NULL items) */ /* XXX This can be made faster! */ n = PyList_Size(allgenerics); for (i = 0; i < n; i++) { if (PyList_GetItem(allgenerics, i) == NULL) { Py_INCREF(g); PyList_SetItem(allgenerics, i, (PyObject *)g); nfreeslots--; return; } } /* Strange... no free slots found... */ nfreeslots = 0; } /* No free entries, append new item to the end */ PyList_Append(allgenerics, (PyObject *)g); }
/* Add an object to the list of known objects */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/flmodule.c#L43-L72
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
forgetgeneric
static void forgetgeneric(genericobject *g) { int i, n; Py_CLEAR(g->ob_callback); Py_CLEAR(g->ob_callback_arg); if (allgenerics == NULL) return; /* No objects known yet */ n = PyList_Size(allgenerics); for (i = 0; i < n; i++) { if (g == (genericobject *)PyList_GetItem(allgenerics, i)) { PyList_SetItem(allgenerics, i, (PyObject *)NULL); nfreeslots++; break; } } }
/* Remove an object from the list of known objects */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/flmodule.c#L95-L112
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
releaseobjects
static void releaseobjects(FL_FORM *form) { int i, n; genericobject *g; if (allgenerics == NULL) return; /* No objects known yet */ n = PyList_Size(allgenerics); for (i = 0; i < n; i++) { g = (genericobject *)PyList_GetItem(allgenerics, i); if (g != NULL && g->ob_generic->form == form) { fl_delete_object(g->ob_generic); /* The object is now unreachable for do_forms and check_forms, so delete it from the list of known objects */ Py_CLEAR(g->ob_callback); Py_CLEAR(g->ob_callback_arg); PyList_SetItem(allgenerics, i, (PyObject *)NULL); nfreeslots++; } } }
/* Called when a form is about to be freed -- remove all the objects that we know about from it. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/flmodule.c#L117-L139
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
initfuture_builtins
PyMODINIT_FUNC initfuture_builtins(void) { PyObject *m, *itertools, *iter_func; char *it_funcs[] = {"imap", "ifilter", "izip", NULL}; char **cur_func; m = Py_InitModule3("future_builtins", module_functions, module_doc); if (m == NULL) return; itertools = PyImport_ImportModuleNoBlock("itertools"); if (itertools == NULL) return; /* If anything in the following loop fails, we fall through. */ for (cur_func = it_funcs; *cur_func; ++cur_func){ iter_func = PyObject_GetAttrString(itertools, *cur_func); if (iter_func == NULL || PyModule_AddObject(m, *cur_func+1, iter_func) < 0) break; } Py_DECREF(itertools); /* any other initialization needed */ }
/* Initialize this module. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/future_builtins.c#L81-L105
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gc_list_init
static void gc_list_init(PyGC_Head *list) { list->gc.gc_prev = list; list->gc.gc_next = list; }
/*** list functions ***/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L220-L225
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gc_list_append
static void gc_list_append(PyGC_Head *node, PyGC_Head *list) { node->gc.gc_next = list; node->gc.gc_prev = list->gc.gc_prev; node->gc.gc_prev->gc.gc_next = node; list->gc.gc_prev = node; }
/* This became unused after gc_list_move() was introduced. */ /* Append `node` to `list`. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L236-L243
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gc_list_remove
static void gc_list_remove(PyGC_Head *node) { node->gc.gc_prev->gc.gc_next = node->gc.gc_next; node->gc.gc_next->gc.gc_prev = node->gc.gc_prev; node->gc.gc_next = NULL; /* object is not currently tracked */ }
/* Remove `node` from the gc list it's currently in. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L247-L253
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gc_list_move
static void gc_list_move(PyGC_Head *node, PyGC_Head *list) { PyGC_Head *new_prev; PyGC_Head *current_prev = node->gc.gc_prev; PyGC_Head *current_next = node->gc.gc_next; /* Unlink from current list. */ current_prev->gc.gc_next = current_next; current_next->gc.gc_prev = current_prev; /* Relink at end of new list. */ new_prev = node->gc.gc_prev = list->gc.gc_prev; new_prev->gc.gc_next = list->gc.gc_prev = node; node->gc.gc_next = list; }
/* Move `node` from the gc list it's currently in (which is not explicitly * named here) to the end of `list`. This is semantically the same as * gc_list_remove(node) followed by gc_list_append(node, list). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L259-L272
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gc_list_merge
static void gc_list_merge(PyGC_Head *from, PyGC_Head *to) { PyGC_Head *tail; assert(from != to); if (!gc_list_is_empty(from)) { tail = to->gc.gc_prev; tail->gc.gc_next = from->gc.gc_next; tail->gc.gc_next->gc.gc_prev = tail; to->gc.gc_prev = from->gc.gc_prev; to->gc.gc_prev->gc.gc_next = to; } gc_list_init(from); }
/* append list `from` onto list `to`; `from` becomes an empty list */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L275-L288
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
append_objects
static int append_objects(PyObject *py_list, PyGC_Head *gc_list) { PyGC_Head *gc; for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) { PyObject *op = FROM_GC(gc); if (op != py_list) { if (PyList_Append(py_list, op)) { return -1; /* exception */ } } } return 0; }
/* Append objects in a GC list to a Python list. * Return 0 if all OK, < 0 if error (out of memory for list). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L304-L317
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
update_refs
static void update_refs(PyGC_Head *containers) { PyGC_Head *gc = containers->gc.gc_next; for (; gc != containers; gc = gc->gc.gc_next) { assert(gc->gc.gc_refs == GC_REACHABLE); gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc)); /* Python's cyclic gc should never see an incoming refcount * of 0: if something decref'ed to 0, it should have been * deallocated immediately at that time. * Possible cause (if the assert triggers): a tp_dealloc * routine left a gc-aware object tracked during its teardown * phase, and did something-- or allowed something to happen -- * that called back into Python. gc can trigger then, and may * see the still-tracked dying object. Before this assert * was added, such mistakes went on to allow gc to try to * delete the object again. In a debug build, that caused * a mysterious segfault, when _Py_ForgetReference tried * to remove the object from the doubly-linked list of all * objects a second time. In a release build, an actual * double deallocation occurred, which leads to corruption * of the allocator's internal bookkeeping pointers. That's * so serious that maybe this should be a release-build * check instead of an assert? */ assert(gc->gc.gc_refs != 0); } }
/*** end of list stuff ***/ /* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 for all objects * in containers, and is GC_REACHABLE for all tracked gc objects not in * containers. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L326-L353
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
visit_decref
static int visit_decref(PyObject *op, void *data) { assert(op != NULL); if (PyObject_IS_GC(op)) { PyGC_Head *gc = AS_GC(op); /* We're only interested in gc_refs for objects in the * generation being collected, which can be recognized * because only they have positive gc_refs. */ assert(gc->gc.gc_refs != 0); /* else refcount was too small */ if (gc->gc.gc_refs > 0) gc->gc.gc_refs--; } return 0; }
/* A traversal callback for subtract_refs. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L356-L371
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
subtract_refs
static void subtract_refs(PyGC_Head *containers) { traverseproc traverse; PyGC_Head *gc = containers->gc.gc_next; for (; gc != containers; gc=gc->gc.gc_next) { traverse = Py_TYPE(FROM_GC(gc))->tp_traverse; (void) traverse(FROM_GC(gc), (visitproc)visit_decref, NULL); } }
/* Subtract internal references from gc_refs. After this, gc_refs is >= 0 * for all objects in containers, and is GC_REACHABLE for all tracked gc * objects not in containers. The ones with gc_refs > 0 are directly * reachable from outside containers, and so can't be collected. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L378-L389
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
visit_reachable
static int visit_reachable(PyObject *op, PyGC_Head *reachable) { if (PyObject_IS_GC(op)) { PyGC_Head *gc = AS_GC(op); const Py_ssize_t gc_refs = gc->gc.gc_refs; if (gc_refs == 0) { /* This is in move_unreachable's 'young' list, but * the traversal hasn't yet gotten to it. All * we need to do is tell move_unreachable that it's * reachable. */ gc->gc.gc_refs = 1; } else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) { /* This had gc_refs = 0 when move_unreachable got * to it, but turns out it's reachable after all. * Move it back to move_unreachable's 'young' list, * and move_unreachable will eventually get to it * again. */ gc_list_move(gc, reachable); gc->gc.gc_refs = 1; } /* Else there's nothing to do. * If gc_refs > 0, it must be in move_unreachable's 'young' * list, and move_unreachable will eventually get to it. * If gc_refs == GC_REACHABLE, it's either in some other * generation so we don't care about it, or move_unreachable * already dealt with it. * If gc_refs == GC_UNTRACKED, it must be ignored. */ else { assert(gc_refs > 0 || gc_refs == GC_REACHABLE || gc_refs == GC_UNTRACKED); } } return 0; }
/* A traversal callback for move_unreachable. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L392-L432
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
move_unreachable
static void move_unreachable(PyGC_Head *young, PyGC_Head *unreachable) { PyGC_Head *gc = young->gc.gc_next; /* Invariants: all objects "to the left" of us in young have gc_refs * = GC_REACHABLE, and are indeed reachable (directly or indirectly) * from outside the young list as it was at entry. All other objects * from the original young "to the left" of us are in unreachable now, * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the * left of us in 'young' now have been scanned, and no objects here * or to the right have been scanned yet. */ while (gc != young) { PyGC_Head *next; if (gc->gc.gc_refs) { /* gc is definitely reachable from outside the * original 'young'. Mark it as such, and traverse * its pointers to find any other objects that may * be directly reachable from it. Note that the * call to tp_traverse may append objects to young, * so we have to wait until it returns to determine * the next object to visit. */ PyObject *op = FROM_GC(gc); traverseproc traverse = Py_TYPE(op)->tp_traverse; assert(gc->gc.gc_refs > 0); gc->gc.gc_refs = GC_REACHABLE; (void) traverse(op, (visitproc)visit_reachable, (void *)young); next = gc->gc.gc_next; if (PyTuple_CheckExact(op)) { _PyTuple_MaybeUntrack(op); } } else { /* This *may* be unreachable. To make progress, * assume it is. gc isn't directly reachable from * any object we've already traversed, but may be * reachable from an object we haven't gotten to yet. * visit_reachable will eventually move gc back into * young if that's so, and we'll see it again. */ next = gc->gc.gc_next; gc_list_move(gc, unreachable); gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE; } gc = next; } }
/* Move the unreachable objects from young to unreachable. After this, * all objects in young have gc_refs = GC_REACHABLE, and all objects in * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE. * All objects in young after this are directly or indirectly reachable * from outside the original young; and all objects in unreachable are * not. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L442-L494
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
has_finalizer
static int has_finalizer(PyObject *op) { if (PyInstance_Check(op)) { assert(delstr != NULL); return _PyInstance_Lookup(op, delstr) != NULL; } else if (PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE)) return op->ob_type->tp_del != NULL; else if (PyGen_CheckExact(op)) return PyGen_NeedsFinalizing((PyGenObject *)op); else return 0; }
/* Return true if object has a finalization method. * CAUTION: An instance of an old-style class has to be checked for a *__del__ method, and earlier versions of this used to call PyObject_HasAttr, * which in turn could call the class's __getattr__ hook (if any). That * could invoke arbitrary Python code, mutating the object graph in arbitrary * ways, and that was the source of some excruciatingly subtle bugs. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/python/Modules/gcmodule.c#L503-L516
4389085c8ce35cff887a4cc18fc47d1133d89ffb