Example #1
0
static PyObject*
th_brk_(PyObject *self, PyObject *args) 
{
    PyObject *result = NULL;
    Py_UNICODE *s1;
    int s1_len;

    if (!PyArg_ParseTuple(args, "u#", &s1, &s1_len)) {
        PyErr_SetString(PyExc_TypeError, "parameter must be unicode");
        return NULL;
    }

    if(s1_len == 0) {
        PyErr_SetString(PyExc_ValueError, "parameter must not be empty string");
        return NULL;
    }

    PyObject *txt_cp874 = PyUnicode_Encode(s1, s1_len, "CP874", NULL);
    if(txt_cp874 == NULL) {
        return NULL;
    }

    Py_ssize_t len = PyString_Size(txt_cp874);
    char *c_txt_cp874 = PyString_AsString(txt_cp874);
    int *pos = (int *)malloc(sizeof(int) * (s1_len + 1)); 
    int n = th_brk((unsigned char *)c_txt_cp874, pos, len);
    int i, s = 0;
    char *buffer;
    result = PyList_New(0);
    for(i = 0; i < n; i++) {
        PyObject *tok;
        PyObject *tok_cp874 = PySequence_GetSlice(txt_cp874, s, pos[i]);
        Py_ssize_t tok_len;
        PyString_AsStringAndSize(tok_cp874, &buffer, &tok_len);
        tok = PyUnicode_Decode(buffer, tok_len, "CP874", NULL);
        s = pos[i];
        PyList_Append(result, tok); 
        Py_XDECREF(tok_cp874);
        Py_XDECREF(tok);
    } 
    if(s < len) {
        PyObject *tok_cp874 = PySequence_GetSlice(txt_cp874, s, len);
        Py_ssize_t tok_len;
        PyObject *tok;
        PyString_AsStringAndSize(tok_cp874, &buffer, &tok_len);
        tok = PyUnicode_Decode(buffer, tok_len, "CP874", NULL);
        PyList_Append(result, tok);
        Py_XDECREF(tok_cp874);
        Py_XDECREF(tok);
    } 
    Py_XDECREF(txt_cp874);

    free(pos); 
    return result;
}
Example #2
0
static PyObject *createDecodedString(hirlite_RliteObject *self, const char *str, size_t len) {
    PyObject *obj;

    if (self->encoding == NULL) {
        obj = PyBytes_FromStringAndSize(str, len);
    } else {
        obj = PyUnicode_Decode(str, len, self->encoding, NULL);
        if (obj == NULL) {
            if (PyErr_ExceptionMatches(PyExc_ValueError)) {
                /* Ignore encoding and simply return plain string. */
                obj = PyBytes_FromStringAndSize(str, len);
            } else {
                assert(PyErr_ExceptionMatches(PyExc_LookupError));

                /* Return Py_None as placeholder to let the error bubble up and
                 * be used when a full reply in Reader#gets(). */
                obj = Py_None;
                Py_INCREF(obj);
            }

            PyErr_Clear();
        }
    }

    assert(obj != NULL);
    return obj;
}
Example #3
0
/* -interp, +buf */
static inline PyObject *CReader_readStr(CReader_Buffer *buffer, char unicode)
 {
  char start[3];
  char startlen = 1;
  size_t len = 0;
  PyObject *res;

  if (!buffer->type->extend(buffer, 2)) return NULL;
  start[0] = start[1] = start[2] = buffer->buf[buffer->pos];
  if (!buffer->type->advance(buffer, 1)) return NULL;
  if (buffer->type->contains(buffer, start, 2, 0))
   {
    startlen = 3;
    if (!buffer->type->advance(buffer, 2)) return NULL;
   }
  if ((len = CReader_readStr_read(buffer, start, startlen, NULL)) == -1) return NULL;
  {
   char buf[len + 1];

   if (CReader_readStr_read(buffer, start, startlen, buf) == -1) return NULL;
   buf[len] = '\0';

   CReader_Buffer_lockInterpreter(buffer);   
   if (unicode)
    res = PyUnicode_Decode(buf, len, "utf", "strict");
   else
    res = PyString_Decode(buf, len, "ascii", "strict");
   CReader_Buffer_releaseInterpreter(buffer);
   return res;
  }
 }
Example #4
0
static PyObject *
cal_JsonToIcal(PyObject *self, PyObject *args)
{
    JsonObject *a;
    BongoCalObject *cal;
    icalcomponent *comp;
    
    if (!PyArg_ParseTuple(args, "O", &a)) {
        PyErr_SetString(PyExc_TypeError, "JsonToIcal() takes 2 arguments");
        return NULL;
    }
    
    cal = BongoCalObjectNew(a->obj);
    BongoCalObjectResolveSystemTimezones(cal);
    BongoCalObjectFree(cal, FALSE);

    comp = BongoCalJsonToIcal(a->obj);
    
    if (comp) {
        PyObject *ret;
        char *data = icalcomponent_as_ical_string(comp);
        ret = PyUnicode_Decode(data, strlen(data), "utf-8", "strict");
        icalcomponent_free(comp);
        return ret;
    } else {
        PyErr_SetString(PyExc_ValueError, "Couldn't convert json object");
        return NULL;
    }
}
Example #5
0
/*
 * Get service description.
 */
PyObject *
psutil_winservice_query_descr(PyObject *self, PyObject *args) {
    ENUM_SERVICE_STATUS_PROCESS *lpService = NULL;
    BOOL ok;
    DWORD bytesNeeded = 0;
    DWORD resumeHandle = 0;
    DWORD dwBytes = 0;
    SC_HANDLE hService = NULL;
    SERVICE_DESCRIPTION *scd = NULL;
    char *service_name;
    PyObject *py_retstr = NULL;

    if (!PyArg_ParseTuple(args, "s", &service_name))
        return NULL;
    hService = psutil_get_service_handler(
        service_name, SC_MANAGER_ENUMERATE_SERVICE, SERVICE_QUERY_CONFIG);
    if (hService == NULL)
        goto error;

    // This first call to QueryServiceConfig2() is necessary in order
    // to get the right size.
    bytesNeeded = 0;
    QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, NULL, 0,
                        &bytesNeeded);
    if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
        PyErr_SetFromWindowsErr(0);
        goto error;
    }

    scd = (SERVICE_DESCRIPTION *)malloc(bytesNeeded);
    ok = QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION,
                             (LPBYTE)scd, bytesNeeded, &bytesNeeded);
    if (ok == 0) {
        PyErr_SetFromWindowsErr(0);
        goto error;
    }

    if (scd->lpDescription == NULL) {
        py_retstr = Py_BuildValue("s", "");
    }
    else {
        py_retstr = PyUnicode_Decode(
            scd->lpDescription,
            _tcslen(scd->lpDescription),
            Py_FileSystemDefaultEncoding,
            "replace");
    }
    if (!py_retstr)
        goto error;

    free(scd);
    return py_retstr;

error:
    if (hService != NULL)
        CloseServiceHandle(hService);
    if (lpService != NULL)
        free(lpService);
    return NULL;
}
Example #6
0
/* Convert a Vim line into a Python string.
 * All internal newlines are replaced by null characters.
 *
 * On errors, the Python exception data is set, and NULL is returned.
 */
static PyObject *
LineToString(const char *str)
{
	PyObject *result;
	Py_ssize_t len = strlen(str);
	char *tmp,*p;

	tmp = (char *)alloc((unsigned)(len+1));
	p = tmp;
	if (p == NULL) {
		PyErr_NoMemory();
		return NULL;
	}

	while (*str) {
		if (*str == '\n')
			*p = '\0';
		else
			*p = *str;

		++p;
		++str;
	}
	*p = '\0';

	result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);

	vim_free(tmp);
	return result;
}
/**
  Convert a string MySQL value to Python str or bytes.

  Convert, and decode if needed, a string MySQL value to
  Python str or bytes.

  @param    data        string to be converted
  @param    length      length of data
  @param    flags       field flags
  @param    charset     character used for decoding
  @param    use_unicode return Unicode

  @return   Converted string
    @retval PyUnicode   if not BINARY_FLAG
    @retval PyBytes     Python v3 if not use_unicode
    @retval PyString    Python v2 if not use_unicode
    @retval NULL    Exception
 */
PyObject*
mytopy_string(const char *data, const unsigned long length,
              const unsigned long flags, const char *charset,
              unsigned int use_unicode)
{
    if (!charset || !data) {
        printf("\n==> here ");
        if (charset) {
            printf(" charset:%s", charset);
        }
        if (data) {
            printf(" data:'%s'", data);
        }
        printf("\n");
        return NULL;
    }

    if (!(flags & BINARY_FLAG) && use_unicode && strcmp(charset, "binary") != 0)
    {
        return PyUnicode_Decode(data, length, charset, NULL);
    }
    else
    {
#ifndef PY3
        return PyStringFromStringAndSize(data, length);
#else
        return PyBytes_FromStringAndSize(data, length);
#endif
    }
}
Example #8
0
static PyObject *
UnicodeResultProcessor_process(UnicodeResultProcessor *self, PyObject *value)
{
    const char *encoding, *errors;
    char *str;
    Py_ssize_t len;

    if (value == Py_None)
        Py_RETURN_NONE;

#if PY_MAJOR_VERSION >= 3
    if (PyBytes_AsStringAndSize(value, &str, &len))
        return NULL;

    encoding = PyBytes_AS_STRING(self->encoding);
    errors = PyBytes_AS_STRING(self->errors);
#else
    if (PyString_AsStringAndSize(value, &str, &len))
        return NULL;

    encoding = PyString_AS_STRING(self->encoding);
    errors = PyString_AS_STRING(self->errors);
#endif

    return PyUnicode_Decode(str, len, encoding, errors);
}
Example #9
0
static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) {
    while (t->p) {
        #if PY_MAJOR_VERSION < 3
        if (t->is_unicode) {
            *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL);
        } else if (t->intern) {
            *t->p = PyString_InternFromString(t->s);
        } else {
            *t->p = PyString_FromStringAndSize(t->s, t->n - 1);
        }
        #else  /* Python 3+ has unicode identifiers */
        if (t->is_unicode | t->is_str) {
            if (t->intern) {
                *t->p = PyUnicode_InternFromString(t->s);
            } else if (t->encoding) {
                *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL);
            } else {
                *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1);
            }
        } else {
            *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1);
        }
        #endif
        if (!*t->p)
            return -1;
        ++t;
    }
    return 0;
}
/* Converts a target string of LENGTH bytes in the target's charset to a
   Python Unicode string. If LENGTH is -1, convert until a null byte is found.

   Returns NULL on error, with a python exception set.  */
PyObject *
target_string_to_unicode (const gdb_byte *str, int length)
{
  if (length == -1)
    length = strlen (str);

  return PyUnicode_Decode (str, length, target_charset (), NULL);
}
Example #11
0
static int
tok_stdin_decode(struct tok_state *tok, char **inp)
{
	PyObject *enc, *sysstdin, *decoded, *utf8;
	const char *encoding;
	char *converted;

	if (PySys_GetFile((char *)"stdin", NULL) != stdin)
		return 0;
	sysstdin = PySys_GetObject("stdin");
	if (sysstdin == NULL || !PyFile_Check(sysstdin))
		return 0;

	enc = ((PyFileObject *)sysstdin)->f_encoding;
	if (enc == NULL || !PyString_Check(enc))
		return 0;
	Py_INCREF(enc);

	encoding = PyString_AsString(enc);
	decoded = PyUnicode_Decode(*inp, strlen(*inp), 0, encoding, NULL);
	if (decoded == NULL)
		goto error_clear;

	utf8 = PyUnicode_AsEncodedString(decoded, "utf-8", NULL);
	Py_DECREF(decoded);
	if (utf8 == NULL)
		goto error_clear;

	assert(PyString_Check(utf8));
	converted = new_string(PyString_AS_STRING(utf8),
			       PyString_GET_SIZE(utf8));
	Py_DECREF(utf8);
	if (converted == NULL)
		goto error_nomem;

	PyMem_FREE(*inp);
	*inp = converted;
	if (tok->encoding != NULL)
		PyMem_FREE(tok->encoding);
	tok->encoding = new_string(encoding, strlen(encoding));
	if (tok->encoding == NULL)
		goto error_nomem;

	Py_DECREF(enc);
	return 0;

error_nomem:
	Py_DECREF(enc);
	tok->done = E_NOMEM;
	return -1;

error_clear:
	/* Fallback to iso-8859-1: for backward compatibility */
	Py_DECREF(enc);
	PyErr_Clear();
	return 0;
}
static PyObject *
typecast_UNICODE_cast(const char *s, Py_ssize_t len, PyObject *curs)
{
    char *enc;

    if (s == NULL) {Py_INCREF(Py_None); return Py_None;}

    enc = ((cursorObject*)curs)->conn->codec;
    return PyUnicode_Decode(s, len, enc, NULL);
}
Example #13
0
static PyObject *
translate_into_utf8(const char* str, const char* enc) {
	PyObject *utf8;
	PyObject* buf = PyUnicode_Decode(str, strlen(str), 0, enc, NULL);
	if (buf == NULL)
		return NULL;
	utf8 = PyUnicode_AsUTF8String(buf);
	Py_DECREF(buf);
	return utf8;
}
Example #14
0
/* Return a new "string" from a char* from the database.
 *
 * On Py2 just get a string, on Py3 decode it in the connection codec.
 *
 * Use a fallback if the connection is NULL.
 */
PyObject *
conn_text_from_chars(connectionObject *self, const char *str)
{
#if PY_MAJOR_VERSION < 3
        return PyString_FromString(str);
#else
        const char *codec = self ? self->codec : "ascii";
        return PyUnicode_Decode(str, strlen(str), codec, "replace");
#endif
}
Example #15
0
static void
sets(PyObject *v, int i, const char* val)
{
  if (val) {
      PyObject *o = PyUnicode_Decode(val, strlen(val),
                                     Py_FileSystemDefaultEncoding,
                                     "surrogateescape");
      PyStructSequence_SET_ITEM(v, i, o);
  } else {
      PyStructSequence_SET_ITEM(v, i, Py_None);
      Py_INCREF(Py_None);
  }
}
Example #16
0
//-----------------------------------------------------------------------------
// StringVar_GetValue()
//   Returns the value stored at the given array position.
//-----------------------------------------------------------------------------
static PyObject *StringVar_GetValue(
    udt_StringVar *var,                 // variable to determine value for
    unsigned pos)                       // array position
{
    char *data;

    data = var->data + pos * var->bufferSize;
    if (var->type == &vt_Binary)
        return PyBytes_FromStringAndSize(data, var->actualLength[pos]);
    if (var->type == &vt_FixedNationalChar
            || var->type == &vt_NationalCharString)
        return PyUnicode_Decode(data, var->actualLength[pos],
                var->environment->nencoding, NULL);
    return cxString_FromEncodedString(data, var->actualLength[pos],
            var->environment->encoding);
}
Example #17
0
static PyObject *py_named_pipe_auth_req_info4_get_client_addr(PyObject *obj, void *closure)
{
	struct named_pipe_auth_req_info4 *object = (struct named_pipe_auth_req_info4 *)py_talloc_get_ptr(obj);
	PyObject *py_client_addr;
	if (object->client_addr == NULL) {
		py_client_addr = Py_None;
		Py_INCREF(py_client_addr);
	} else {
		if (object->client_addr == NULL) {
			py_client_addr = Py_None;
			Py_INCREF(py_client_addr);
		} else {
			py_client_addr = PyUnicode_Decode(object->client_addr, strlen(object->client_addr), "utf-8", "ignore");
		}
	}
	return py_client_addr;
}
Example #18
0
static PyObject *py_dssetup_DsRolePrimaryDomInfoBasic_get_domain(PyObject *obj, void *closure)
{
	struct dssetup_DsRolePrimaryDomInfoBasic *object = (struct dssetup_DsRolePrimaryDomInfoBasic *)pytalloc_get_ptr(obj);
	PyObject *py_domain;
	if (object->domain == NULL) {
		py_domain = Py_None;
		Py_INCREF(py_domain);
	} else {
		if (object->domain == NULL) {
			py_domain = Py_None;
			Py_INCREF(py_domain);
		} else {
			py_domain = PyUnicode_Decode(object->domain, strlen(object->domain), "utf-8", "ignore");
		}
	}
	return py_domain;
}
Example #19
0
static PyObject *
typecast_UNICODE_cast(const char *s, Py_ssize_t len, PyObject *curs)
{
    PyObject *enc;

    if (s == NULL) {Py_INCREF(Py_None); return Py_None;}

    enc = PyDict_GetItemString(psycoEncodings,
                               ((cursorObject*)curs)->conn->encoding);
    if (enc) {
        return PyUnicode_Decode(s, len, PyString_AsString(enc), NULL);
    }
    else {
       PyErr_Format(InterfaceError,
                    "can't decode into unicode string from %s",
                    ((cursorObject*)curs)->conn->encoding);
       return NULL;
    }
}
Example #20
0
/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
   of an error.
*/
static PyObject *
test_u_code(PyObject *self)
{
	PyObject *tuple, *obj;
	Py_UNICODE *value;
	int len;

	/* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
	/* Just use the macro and check that it compiles */
	int x = Py_UNICODE_ISSPACE(25);

        tuple = PyTuple_New(1);
        if (tuple == NULL)
        	return NULL;

        obj = PyUnicode_Decode("test", strlen("test"),
			       "ascii", NULL);
        if (obj == NULL)
        	return NULL;

        PyTuple_SET_ITEM(tuple, 0, obj);

        value = 0;
        if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
        	return NULL;
        if (value != PyUnicode_AS_UNICODE(obj))
        	return raiseTestError("test_u_code",
			"u code returned wrong value for u'test'");
        value = 0;
        if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
        	return NULL;
        if (value != PyUnicode_AS_UNICODE(obj) ||
	    len != PyUnicode_GET_SIZE(obj))
        	return raiseTestError("test_u_code",
			"u# code returned wrong values for u'test'");

	Py_DECREF(tuple);
	Py_INCREF(Py_None);
	return Py_None;
}
Example #21
0
static PyObject *
psyco_lobj_read(lobjectObject *self, PyObject *args)
{
    PyObject *res;
    int where, end;
    Py_ssize_t size = -1;
    char *buffer;

    if (!PyArg_ParseTuple(args, "|" CONV_CODE_PY_SSIZE_T,  &size)) return NULL;

    EXC_IF_LOBJ_CLOSED(self);
    EXC_IF_LOBJ_LEVEL0(self);
    EXC_IF_LOBJ_UNMARKED(self);

    if (size < 0) {
        if ((where = lobject_tell(self)) < 0) return NULL;
        if ((end = lobject_seek(self, 0, SEEK_END)) < 0) return NULL;
        if (lobject_seek(self, where, SEEK_SET) < 0) return NULL;
        size = end - where;
    }

    if ((buffer = PyMem_Malloc(size)) == NULL) {
        PyErr_NoMemory();
        return NULL;
    }
    if ((size = lobject_read(self, buffer, size)) < 0) {
        PyMem_Free(buffer);
        return NULL;
    }

    if (self->mode & LOBJECT_BINARY) {
        res = Bytes_FromStringAndSize(buffer, size);
    } else {
        res = PyUnicode_Decode(buffer, size, self->conn->codec, NULL);
    }
    PyMem_Free(buffer);

    return res;
}
Example #22
0
/*
 * Find attribute. An X509Name object has the following attributes:
 * countryName (alias C), stateOrProvince (alias ST), locality (alias L),
 * organization (alias O), organizationalUnit (alias OU), commonName (alias
 * CN) and more...
 *
 * Arguments: self - The X509Name object
 *            name - The attribute name
 * Returns:   A Python object for the attribute, or NULL if something went
 *            wrong
 */
static PyObject *
crypto_X509Name_getattro(crypto_X509NameObj *self, PyObject *nameobj)
{
    int nid, len;
    char *utf8string;
    char *name;
#ifdef PY3
    name = PyBytes_AsString(PyUnicode_AsASCIIString(nameobj));
#else
    name = PyBytes_AsString(nameobj);
#endif

    if ((nid = OBJ_txt2nid(name)) == NID_undef) {
        /*
         * This is a bit weird.  OBJ_txt2nid indicated failure, but it seems
         * a lower level function, a2d_ASN1_OBJECT, also feels the need to
         * push something onto the error queue.  If we don't clean that up
         * now, someone else will bump into it later and be quite confused. 
         * See lp#314814.
         */
        flush_error_queue();
        return PyObject_GenericGetAttr((PyObject*)self, nameobj);
    }

    len = get_name_by_nid(self->x509_name, nid, &utf8string);
    if (len < 0)
        return NULL;
    else if (len == 0)
    {
        Py_INCREF(Py_None);
        return Py_None;
    }
    else {
	    PyObject* result = PyUnicode_Decode(utf8string, len, "utf-8", NULL);
	    OPENSSL_free(utf8string);
	    return result;
    }
}
Example #23
0
static PyObject *
suggest(Dictionary *self, PyObject *args) {
	char *word = NULL, **slist = NULL;
	int i, num_slist;
	PyObject *ans, *temp;

	if (!PyArg_ParseTuple(args, "es", self->encoding, &word)) return NULL;

	num_slist = self->handle->suggest(&slist, word);
	ans = PyTuple_New(num_slist);
    if (ans == NULL) PyErr_NoMemory();
    else {
        for (i = 0; i < num_slist; i++) {
            temp = PyUnicode_Decode(slist[i], strlen(slist[i]), self->encoding, "strict");
            if (temp == NULL) { Py_DECREF(ans); ans = NULL; break; }
            PyTuple_SET_ITEM(ans, i, temp);
        }
    }

    if (slist != NULL) self->handle->free_list(&slist, num_slist);
    PyMem_Free(word);
	return ans;
}
Example #24
0
/* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
   of an error.
*/
static PyObject *
test_u_code(PyObject *self)
{
	PyObject *tuple, *obj;
	Py_UNICODE *value;
	int len;

        tuple = PyTuple_New(1);
        if (tuple == NULL)
        	return NULL;

        obj = PyUnicode_Decode("test", strlen("test"),
			       "ascii", NULL);
        if (obj == NULL)
        	return NULL;

        PyTuple_SET_ITEM(tuple, 0, obj);

        value = 0;
        if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
        	return NULL;
        if (value != PyUnicode_AS_UNICODE(obj))
        	return raiseTestError("test_u_code",
			"u code returned wrong value for u'test'");
        value = 0;
        if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
        	return NULL;
        if (value != PyUnicode_AS_UNICODE(obj) ||
	    len != PyUnicode_GET_SIZE(obj))
        	return raiseTestError("test_u_code",
			"u# code returned wrong values for u'test'");

	Py_DECREF(tuple);
	Py_INCREF(Py_None);
	return Py_None;
}
Example #25
0
/*
   read_directory(archive) -> files dict (new reference)

   Given a path to a Zip archive, build a dict, mapping file names
   (local to the archive, using SEP as a separator) to toc entries.

   A toc_entry is a tuple:

   (__file__,      # value to use for __file__, available for all files,
                   # encoded to the filesystem encoding
    compress,      # compression kind; 0 for uncompressed
    data_size,     # size of compressed data on disk
    file_size,     # size of decompressed data
    file_offset,   # offset of file header from start of archive
    time,          # mod time of file (in dos format)
    date,          # mod data of file (in dos format)
    crc,           # crc checksum of the data
   )

   Directories can be recognized by the trailing SEP in the name,
   data_size and file_offset are 0.
*/
static PyObject *
read_directory(PyObject *archive)
{
    PyObject *files = NULL;
    FILE *fp;
    unsigned short flags, compress, time, date, name_size;
    unsigned int crc, data_size, file_size, header_size, header_offset;
    unsigned long file_offset, header_position;
    unsigned long arc_offset;  /* Absolute offset to start of the zip-archive. */
    unsigned int count, i;
    unsigned char buffer[46];
    char name[MAXPATHLEN + 5];
    PyObject *nameobj = NULL;
    PyObject *path;
    const char *charset;
    int bootstrap;
    const char *errmsg = NULL;

    fp = _Py_fopen_obj(archive, "rb");
    if (fp == NULL) {
        if (PyErr_ExceptionMatches(PyExc_OSError)) {
            _PyErr_FormatFromCause(ZipImportError,
                                   "can't open Zip file: %R", archive);
        }
        return NULL;
    }

    if (fseek(fp, -22, SEEK_END) == -1) {
        goto file_error;
    }
    header_position = (unsigned long)ftell(fp);
    if (header_position == (unsigned long)-1) {
        goto file_error;
    }
    assert(header_position <= (unsigned long)LONG_MAX);
    if (fread(buffer, 1, 22, fp) != 22) {
        goto file_error;
    }
    if (get_uint32(buffer) != 0x06054B50u) {
        /* Bad: End of Central Dir signature */
        errmsg = "not a Zip file";
        goto invalid_header;
    }

    header_size = get_uint32(buffer + 12);
    header_offset = get_uint32(buffer + 16);
    if (header_position < header_size) {
        errmsg = "bad central directory size";
        goto invalid_header;
    }
    if (header_position < header_offset) {
        errmsg = "bad central directory offset";
        goto invalid_header;
    }
    if (header_position - header_size < header_offset) {
        errmsg = "bad central directory size or offset";
        goto invalid_header;
    }
    header_position -= header_size;
    arc_offset = header_position - header_offset;

    files = PyDict_New();
    if (files == NULL) {
        goto error;
    }
    /* Start of Central Directory */
    count = 0;
    if (fseek(fp, (long)header_position, 0) == -1) {
        goto file_error;
    }
    for (;;) {
        PyObject *t;
        size_t n;
        int err;

        n = fread(buffer, 1, 46, fp);
        if (n < 4) {
            goto eof_error;
        }
        /* Start of file header */
        if (get_uint32(buffer) != 0x02014B50u) {
            break;              /* Bad: Central Dir File Header */
        }
        if (n != 46) {
            goto eof_error;
        }
        flags = get_uint16(buffer + 8);
        compress = get_uint16(buffer + 10);
        time = get_uint16(buffer + 12);
        date = get_uint16(buffer + 14);
        crc = get_uint32(buffer + 16);
        data_size = get_uint32(buffer + 20);
        file_size = get_uint32(buffer + 24);
        name_size = get_uint16(buffer + 28);
        header_size = (unsigned int)name_size +
           get_uint16(buffer + 30) /* extra field */ +
           get_uint16(buffer + 32) /* comment */;

        file_offset = get_uint32(buffer + 42);
        if (file_offset > header_offset) {
            errmsg = "bad local header offset";
            goto invalid_header;
        }
        file_offset += arc_offset;

        if (name_size > MAXPATHLEN) {
            name_size = MAXPATHLEN;
        }
        if (fread(name, 1, name_size, fp) != name_size) {
            goto file_error;
        }
        name[name_size] = '\0';  /* Add terminating null byte */
#if SEP != '/'
        for (i = 0; i < name_size; i++) {
            if (name[i] == '/') {
                name[i] = SEP;
            }
        }
#endif
        /* Skip the rest of the header.
         * On Windows, calling fseek to skip over the fields we don't use is
         * slower than reading the data because fseek flushes stdio's
         * internal buffers.  See issue #8745. */
        assert(header_size <= 3*0xFFFFu);
        for (i = name_size; i < header_size; i++) {
            if (getc(fp) == EOF) {
                goto file_error;
            }
        }

        bootstrap = 0;
        if (flags & 0x0800) {
            charset = "utf-8";
        }
        else if (!PyThreadState_GET()->interp->codecs_initialized) {
            /* During bootstrap, we may need to load the encodings
               package from a ZIP file. But the cp437 encoding is implemented
               in Python in the encodings package.

               Break out of this dependency by assuming that the path to
               the encodings module is ASCII-only. */
            charset = "ascii";
            bootstrap = 1;
        }
        else {
            charset = "cp437";
        }
        nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
        if (nameobj == NULL) {
            if (bootstrap) {
                PyErr_Format(PyExc_NotImplementedError,
                    "bootstrap issue: python%i%i.zip contains non-ASCII "
                    "filenames without the unicode flag",
                    PY_MAJOR_VERSION, PY_MINOR_VERSION);
            }
            goto error;
        }
        if (PyUnicode_READY(nameobj) == -1) {
            goto error;
        }
        path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
        if (path == NULL) {
            goto error;
        }
        t = Py_BuildValue("NHIIkHHI", path, compress, data_size,
                          file_size, file_offset, time, date, crc);
        if (t == NULL) {
            goto error;
        }
        err = PyDict_SetItem(files, nameobj, t);
        Py_CLEAR(nameobj);
        Py_DECREF(t);
        if (err != 0) {
            goto error;
        }
        count++;
    }
    fclose(fp);
    if (Py_VerboseFlag) {
        PySys_FormatStderr("# zipimport: found %u names in %R\n",
                           count, archive);
    }
    return files;

eof_error:
    set_file_error(archive, !ferror(fp));
    goto error;

file_error:
    PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
    goto error;

invalid_header:
    assert(errmsg != NULL);
    PyErr_Format(ZipImportError, "%s: %R", errmsg, archive);
    goto error;

error:
    fclose(fp);
    Py_XDECREF(files);
    Py_XDECREF(nameobj);
    return NULL;
}
Example #26
0
/*
   read_directory(archive) -> files dict (new reference)

   Given a path to a Zip archive, build a dict, mapping file names
   (local to the archive, using SEP as a separator) to toc entries.

   A toc_entry is a tuple:

   (__file__,      # value to use for __file__, available for all files,
                   # encoded to the filesystem encoding
    compress,      # compression kind; 0 for uncompressed
    data_size,     # size of compressed data on disk
    file_size,     # size of decompressed data
    file_offset,   # offset of file header from start of archive
    time,          # mod time of file (in dos format)
    date,          # mod data of file (in dos format)
    crc,           # crc checksum of the data
   )

   Directories can be recognized by the trailing SEP in the name,
   data_size and file_offset are 0.
*/
static PyObject *
read_directory(PyObject *archive)
{
    PyObject *files = NULL;
    FILE *fp;
    unsigned short flags;
    short compress, time, date, name_size;
    long crc, data_size, file_size, header_size;
    Py_ssize_t file_offset, header_position, header_offset;
    long l, count;
    Py_ssize_t i;
    char name[MAXPATHLEN + 5];
    PyObject *nameobj = NULL;
    char *p, endof_central_dir[22];
    Py_ssize_t arc_offset;  /* Absolute offset to start of the zip-archive. */
    PyObject *path;
    const char *charset;
    int bootstrap;

    fp = _Py_fopen(archive, "rb");
    if (fp == NULL) {
        if (!PyErr_Occurred())
            PyErr_Format(ZipImportError, "can't open Zip file: %R", archive);
        return NULL;
    }
    fseek(fp, -22, SEEK_END);
    header_position = ftell(fp);
    if (fread(endof_central_dir, 1, 22, fp) != 22) {
        fclose(fp);
        PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
        return NULL;
    }
    if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
        /* Bad: End of Central Dir signature */
        fclose(fp);
        PyErr_Format(ZipImportError, "not a Zip file: %R", archive);
        return NULL;
    }

    header_size = get_long((unsigned char *)endof_central_dir + 12);
    header_offset = get_long((unsigned char *)endof_central_dir + 16);
    arc_offset = header_position - header_offset - header_size;
    header_offset += arc_offset;

    files = PyDict_New();
    if (files == NULL)
        goto error;

    /* Start of Central Directory */
    count = 0;
    for (;;) {
        PyObject *t;
        int err;

        fseek(fp, header_offset, 0);  /* Start of file header */
        l = PyMarshal_ReadLongFromFile(fp);
        if (l != 0x02014B50)
            break;              /* Bad: Central Dir File Header */
        fseek(fp, header_offset + 8, 0);
        flags = (unsigned short)PyMarshal_ReadShortFromFile(fp);
        compress = PyMarshal_ReadShortFromFile(fp);
        time = PyMarshal_ReadShortFromFile(fp);
        date = PyMarshal_ReadShortFromFile(fp);
        crc = PyMarshal_ReadLongFromFile(fp);
        data_size = PyMarshal_ReadLongFromFile(fp);
        file_size = PyMarshal_ReadLongFromFile(fp);
        name_size = PyMarshal_ReadShortFromFile(fp);
        header_size = 46 + name_size +
           PyMarshal_ReadShortFromFile(fp) +
           PyMarshal_ReadShortFromFile(fp);
        fseek(fp, header_offset + 42, 0);
        file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
        if (name_size > MAXPATHLEN)
            name_size = MAXPATHLEN;

        p = name;
        for (i = 0; i < (Py_ssize_t)name_size; i++) {
            *p = (char)getc(fp);
            if (*p == '/')
                *p = SEP;
            p++;
        }
        *p = 0;         /* Add terminating null byte */
        header_offset += header_size;

        bootstrap = 0;
        if (flags & 0x0800)
            charset = "utf-8";
        else if (!PyThreadState_GET()->interp->codecs_initialized) {
            /* During bootstrap, we may need to load the encodings
               package from a ZIP file. But the cp437 encoding is implemented
               in Python in the encodings package.

               Break out of this dependency by assuming that the path to
               the encodings module is ASCII-only. */
            charset = "ascii";
            bootstrap = 1;
        }
        else
            charset = "cp437";
        nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
        if (PyUnicode_READY(nameobj) == -1)
            goto error;
        if (nameobj == NULL) {
            if (bootstrap)
                PyErr_Format(PyExc_NotImplementedError,
                    "bootstrap issue: python%i%i.zip contains non-ASCII "
                    "filenames without the unicode flag",
                    PY_MAJOR_VERSION, PY_MINOR_VERSION);
            goto error;
        }
        path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
        if (path == NULL)
            goto error;
        t = Py_BuildValue("Nhllnhhl", path, compress, data_size,
                          file_size, file_offset, time, date, crc);
        if (t == NULL)
            goto error;
        err = PyDict_SetItem(files, nameobj, t);
        Py_CLEAR(nameobj);
        Py_DECREF(t);
        if (err != 0)
            goto error;
        count++;
    }
    fclose(fp);
    if (Py_VerboseFlag)
        PySys_FormatStderr("# zipimport: found %ld names in %R\n",
                           count, archive);
    return files;
error:
    fclose(fp);
    Py_XDECREF(files);
    Py_XDECREF(nameobj);
    return NULL;
}
Example #27
0
static PyObject *DecodeString (UMTypeInfo *ti, char *value, size_t cbValue)
{
  //FIXME: This code must be endiness aware of system isn't little endian

  switch (ti->charset)
  {
  case MCS_big5_chinese_ci://1,
  case MCS_big5_bin://84,
  case MCS_dec8_swedish_ci://3,
  case MCS_dec8_bin://69,
  case MCS_cp850_general_ci://4,
  case MCS_cp850_bin://80,
  case MCS_hp8_english_ci://6,
  case MCS_hp8_bin://72,
  case MCS_koi8r_general_ci://7,
  case MCS_koi8r_bin://74,
    break;

  case MCS_latin1_german1_ci://5,
  case MCS_latin1_swedish_ci://8,
  case MCS_latin1_danish_ci://15,
  case MCS_latin1_german2_ci://31,
  case MCS_latin1_bin://47,
  case MCS_latin1_general_ci://48,
  case MCS_latin1_general_cs://49,
  case MCS_latin1_spanish_ci://94,
    return PyUnicode_DecodeLatin1 (value, cbValue, NULL);

  case MCS_latin2_czech_cs:// 2,
  case MCS_latin2_general_ci://9,
  case MCS_latin2_hungarian_ci://21,
  case MCS_latin2_croatian_ci://27,
  case MCS_latin2_bin://77,
  case MCS_swe7_swedish_ci://10,
  case MCS_swe7_bin://82,
    break;

  case MCS_ascii_general_ci://11,
  case MCS_ascii_bin://65,
    return PyUnicode_DecodeASCII(value, cbValue, NULL);

  case MCS_ujis_japanese_ci://12,
  case MCS_ujis_bin://91,
  case MCS_sjis_japanese_ci://13,
  case MCS_sjis_bin://88,
  case MCS_hebrew_general_ci://16,
  case MCS_hebrew_bin://71,
  case MCS_tis620_thai_ci://18,
  case MCS_tis620_bin://89,
  case MCS_euckr_korean_ci://19,
  case MCS_euckr_bin://85,
  case MCS_koi8u_general_ci://22,
  case MCS_koi8u_bin://75,
  case MCS_gb2312_chinese_ci://24,
  case MCS_gb2312_bin://86,
  case MCS_greek_general_ci://25,
  case MCS_greek_bin://70,
    break;

  case MCS_cp1250_general_ci://26,
  case MCS_cp1250_czech_cs://34,
  case MCS_cp1250_croatian_ci://44,
  case MCS_cp1250_bin://66,
  case MCS_cp1250_polish_ci://99,
    return PyUnicode_Decode(value, cbValue, "cp1250", NULL);

  case MCS_gbk_chinese_ci://28,
  case MCS_gbk_bin://87,
  case MCS_latin5_turkish_ci://30,
  case MCS_latin5_bin://78,
  case MCS_armscii8_general_ci://32,
  case MCS_armscii8_bin://64,
    break;

  case MCS_utf8_general_ci://33,
  case MCS_utf8_bin://83,
  case MCS_utf8_unicode_ci://192,
  case MCS_utf8_icelandic_ci://193,
  case MCS_utf8_latvian_ci://194,
  case MCS_utf8_romanian_ci://195,
  case MCS_utf8_slovenian_ci://196,
  case MCS_utf8_polish_ci://197,
  case MCS_utf8_estonian_ci://198,
  case MCS_utf8_spanish_ci://199,
  case MCS_utf8_swedish_ci://200,
  case MCS_utf8_turkish_ci://201,
  case MCS_utf8_czech_ci://202,
  case MCS_utf8_danish_ci://203,
  case MCS_utf8_lithuanian_ci://204,
  case MCS_utf8_slovak_ci://205,
  case MCS_utf8_spanish2_ci://206,
  case MCS_utf8_roman_ci://207,
  case MCS_utf8_persian_ci://208,
  case MCS_utf8_esperanto_ci://209,
  case MCS_utf8_hungarian_ci://210,
  case MCS_utf8_sinhala_ci://211,
    return PyUnicode_DecodeUTF8 (value, cbValue, NULL);

  case MCS_ucs2_general_ci://35,
  case MCS_ucs2_bin://90,
  case MCS_ucs2_unicode_ci://128,
  case MCS_ucs2_icelandic_ci://129,
  case MCS_ucs2_latvian_ci://130,
  case MCS_ucs2_romanian_ci://131,
  case MCS_ucs2_slovenian_ci://132,
  case MCS_ucs2_polish_ci://133,
  case MCS_ucs2_estonian_ci://134,
  case MCS_ucs2_spanish_ci://135,
  case MCS_ucs2_swedish_ci://136,
  case MCS_ucs2_turkish_ci://137,
  case MCS_ucs2_czech_ci://138,
  case MCS_ucs2_danish_ci://139,
  case MCS_ucs2_lithuanian_ci://140,
  case MCS_ucs2_slovak_ci://141,
  case MCS_ucs2_spanish2_ci://142,
  case MCS_ucs2_roman_ci://143,
  case MCS_ucs2_persian_ci://144,
  case MCS_ucs2_esperanto_ci://145,
  case MCS_ucs2_hungarian_ci://146,
  case MCS_ucs2_sinhala_ci://147,
    break;

  case MCS_cp866_general_ci://36,
  case MCS_cp866_bin://68,
  case MCS_keybcs2_general_ci://37,
  case MCS_keybcs2_bin://73,
  case MCS_macce_general_ci://38,
  case MCS_macce_bin://43,
  case MCS_macroman_general_ci://39,
  case MCS_macroman_bin://53,
  case MCS_cp852_general_ci://40,
  case MCS_cp852_bin://81,
  case MCS_latin7_estonian_cs://20,
  case MCS_latin7_general_ci://41,
  case MCS_latin7_general_cs://42,
  case MCS_latin7_bin://79,
  case MCS_utf8mb4_general_ci://45,
  case MCS_utf8mb4_bin://46,
  case MCS_utf8mb4_unicode_ci://224,
  case MCS_utf8mb4_icelandic_ci://225,
  case MCS_utf8mb4_latvian_ci://226,
  case MCS_utf8mb4_romanian_ci://227,
  case MCS_utf8mb4_slovenian_ci://228,
  case MCS_utf8mb4_polish_ci://229,
  case MCS_utf8mb4_estonian_ci://230,
  case MCS_utf8mb4_spanish_ci://231,
  case MCS_utf8mb4_swedish_ci://232,
  case MCS_utf8mb4_turkish_ci://233,
  case MCS_utf8mb4_czech_ci://234,
  case MCS_utf8mb4_danish_ci://235,
  case MCS_utf8mb4_lithuanian_ci://236,
  case MCS_utf8mb4_slovak_ci://237,
  case MCS_utf8mb4_spanish2_ci://238,
  case MCS_utf8mb4_roman_ci://239,
  case MCS_utf8mb4_persian_ci://240,
  case MCS_utf8mb4_esperanto_ci://241,
  case MCS_utf8mb4_hungarian_ci://242,
  case MCS_utf8mb4_sinhala_ci://243,
  case MCS_cp1251_bulgarian_ci://14,
  case MCS_cp1251_ukrainian_ci://23,
  case MCS_cp1251_bin://50,
  case MCS_cp1251_general_ci://51,
  case MCS_cp1251_general_cs://52,
    break;

  case MCS_utf16_general_ci://54,
  case MCS_utf16_bin://55,
  case MCS_utf16_unicode_ci://101,
  case MCS_utf16_icelandic_ci://102,
  case MCS_utf16_latvian_ci://103,
  case MCS_utf16_romanian_ci://104,
  case MCS_utf16_slovenian_ci://105,
  case MCS_utf16_polish_ci://106,
  case MCS_utf16_estonian_ci://107,
  case MCS_utf16_spanish_ci://108,
  case MCS_utf16_swedish_ci://109,
  case MCS_utf16_turkish_ci://110,
  case MCS_utf16_czech_ci://111,
  case MCS_utf16_danish_ci://112,
  case MCS_utf16_lithuanian_ci://113,
  case MCS_utf16_slovak_ci://114,
  case MCS_utf16_spanish2_ci://115,
  case MCS_utf16_roman_ci://116,
  case MCS_utf16_persian_ci://117,
  case MCS_utf16_esperanto_ci://118,
  case MCS_utf16_hungarian_ci://119,
  case MCS_utf16_sinhala_ci://120,
    //return PyUnicode_DecodeUTF16(value, cbValue / 2, NULL, NULL);
    break;

  case MCS_cp1256_general_ci://57,
  case MCS_cp1256_bin://67,
    break;

  case MCS_cp1257_lithuanian_ci://29,
  case MCS_cp1257_bin://58,
  case MCS_cp1257_general_ci://59,
    break;

  case MCS_utf32_general_ci://60,
  case MCS_utf32_bin://61,
  case MCS_utf32_unicode_ci://160,
  case MCS_utf32_icelandic_ci://161,
  case MCS_utf32_latvian_ci://162,
  case MCS_utf32_romanian_ci://163,
  case MCS_utf32_slovenian_ci://164,
  case MCS_utf32_polish_ci://165,
  case MCS_utf32_estonian_ci://166,
  case MCS_utf32_spanish_ci://167,
  case MCS_utf32_swedish_ci://168,
  case MCS_utf32_turkish_ci://169,
  case MCS_utf32_czech_ci://170,
  case MCS_utf32_danish_ci://171,
  case MCS_utf32_lithuanian_ci://172,
  case MCS_utf32_slovak_ci://173,
  case MCS_utf32_spanish2_ci://174,
  case MCS_utf32_roman_ci://175,
  case MCS_utf32_persian_ci://176,
  case MCS_utf32_esperanto_ci://177,
  case MCS_utf32_hungarian_ci://178,
  case MCS_utf32_sinhala_ci://179,
    //return PyUnicode_DecodeUTF32 (value, cbValue / 4, NULL, NULL);
    break;

  case MCS_geostd8_general_ci://92,
  case MCS_geostd8_bin://93,
  case MCS_cp932_japanese_ci://95,
  case MCS_cp932_bin://96,
  case MCS_eucjpms_japanese_ci://97,
  case MCS_eucjpms_bin://98,
    break;

  case MCS_binary:
    return PyString_FromStringAndSize(value, cbValue);

  default:
    break;
  }

  return PyErr_Format (PyExc_ValueError, "Unsupported character set %d when decoding string", (int) ti->charset);
}
Example #28
0
static void
PyInit_timezone(PyObject *m) {
    /* This code moved from PyInit_time wholesale to allow calling it from
    time_tzset. In the future, some parts of it can be moved back
    (for platforms that don't HAVE_WORKING_TZSET, when we know what they
    are), and the extraneous calls to tzset(3) should be removed.
    I haven't done this yet, as I don't want to change this code as
    little as possible when introducing the time.tzset and time.tzsetwall
    methods. This should simply be a method of doing the following once,
    at the top of this function and removing the call to tzset() from
    time_tzset():

        #ifdef HAVE_TZSET
        tzset()
        #endif

    And I'm lazy and hate C so nyer.
     */
#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
    PyObject *otz0, *otz1;
    tzset();
#ifdef PYOS_OS2
    PyModule_AddIntConstant(m, "timezone", _timezone);
#else /* !PYOS_OS2 */
    PyModule_AddIntConstant(m, "timezone", timezone);
#endif /* PYOS_OS2 */
#ifdef HAVE_ALTZONE
    PyModule_AddIntConstant(m, "altzone", altzone);
#else
#ifdef PYOS_OS2
    PyModule_AddIntConstant(m, "altzone", _timezone-3600);
#else /* !PYOS_OS2 */
    PyModule_AddIntConstant(m, "altzone", timezone-3600);
#endif /* PYOS_OS2 */
#endif
    PyModule_AddIntConstant(m, "daylight", daylight);
    otz0 = PyUnicode_Decode(tzname[0], strlen(tzname[0]), TZNAME_ENCODING, NULL);
    otz1 = PyUnicode_Decode(tzname[1], strlen(tzname[1]), TZNAME_ENCODING, NULL);
    PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
#ifdef HAVE_STRUCT_TM_TM_ZONE
    {
#define YEAR ((time_t)((365 * 24 + 6) * 3600))
        time_t t;
        struct tm *p;
        long janzone, julyzone;
        char janname[10], julyname[10];
        t = (time((time_t *)0) / YEAR) * YEAR;
        p = localtime(&t);
        janzone = -p->tm_gmtoff;
        strncpy(janname, p->tm_zone ? p->tm_zone : "   ", 9);
        janname[9] = '\0';
        t += YEAR/2;
        p = localtime(&t);
        julyzone = -p->tm_gmtoff;
        strncpy(julyname, p->tm_zone ? p->tm_zone : "   ", 9);
        julyname[9] = '\0';

        if( janzone < julyzone ) {
            /* DST is reversed in the southern hemisphere */
            PyModule_AddIntConstant(m, "timezone", julyzone);
            PyModule_AddIntConstant(m, "altzone", janzone);
            PyModule_AddIntConstant(m, "daylight",
                                    janzone != julyzone);
            PyModule_AddObject(m, "tzname",
                               Py_BuildValue("(zz)",
                                             julyname, janname));
        } else {
            PyModule_AddIntConstant(m, "timezone", janzone);
            PyModule_AddIntConstant(m, "altzone", julyzone);
            PyModule_AddIntConstant(m, "daylight",
                                    janzone != julyzone);
            PyModule_AddObject(m, "tzname",
                               Py_BuildValue("(zz)",
                                             janname, julyname));
        }
    }
#else
#endif /* HAVE_STRUCT_TM_TM_ZONE */
#ifdef __CYGWIN__
    tzset();
    PyModule_AddIntConstant(m, "timezone", _timezone);
    PyModule_AddIntConstant(m, "altzone", _timezone-3600);
    PyModule_AddIntConstant(m, "daylight", _daylight);
    PyModule_AddObject(m, "tzname",
                       Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
#endif /* __CYGWIN__ */
#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
}
Example #29
0
static PyObject *
time_strftime(PyObject *self, PyObject *args)
{
    PyObject *tup = NULL;
    struct tm buf;
    const time_char *fmt;
#ifdef HAVE_WCSFTIME
    wchar_t *format;
#else
    PyObject *format;
#endif
    PyObject *format_arg;
    size_t fmtlen, buflen;
    time_char *outbuf = NULL;
    size_t i;
    PyObject *ret = NULL;

    memset((void *) &buf, '\0', sizeof(buf));

    /* Will always expect a unicode string to be passed as format.
       Given that there's no str type anymore in py3k this seems safe.
    */
    if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup))
        return NULL;

    if (tup == NULL) {
        time_t tt = time(NULL);
        buf = *localtime(&tt);
    }
    else if (!gettmarg(tup, &buf) || !checktm(&buf))
        return NULL;

#if defined(_MSC_VER) || defined(sun)
    if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) {
        PyErr_Format(PyExc_ValueError,
                     "strftime() requires year in [1; 9999]",
                     buf.tm_year + 1900);
        return NULL;
    }
#endif

    /* Normalize tm_isdst just in case someone foolishly implements %Z
       based on the assumption that tm_isdst falls within the range of
       [-1, 1] */
    if (buf.tm_isdst < -1)
        buf.tm_isdst = -1;
    else if (buf.tm_isdst > 1)
        buf.tm_isdst = 1;

#ifdef HAVE_WCSFTIME
    format = PyUnicode_AsWideCharString(format_arg, NULL);
    if (format == NULL)
        return NULL;
    fmt = format;
#else
    /* Convert the unicode string to an ascii one */
    format = PyUnicode_AsEncodedString(format_arg, TZNAME_ENCODING, NULL);
    if (format == NULL)
        return NULL;
    fmt = PyBytes_AS_STRING(format);
#endif

#if defined(MS_WINDOWS) && defined(HAVE_WCSFTIME)
    /* check that the format string contains only valid directives */
    for(outbuf = wcschr(fmt, L'%');
        outbuf != NULL;
        outbuf = wcschr(outbuf+2, L'%'))
    {
        if (outbuf[1]=='#')
            ++outbuf; /* not documented by python, */
        if (outbuf[1]=='\0' ||
            !wcschr(L"aAbBcdfHIjmMpSUwWxXyYzZ%", outbuf[1]))
        {
            PyErr_SetString(PyExc_ValueError, "Invalid format string");
            return 0;
        }
    }
#endif

    fmtlen = time_strlen(fmt);

    /* I hate these functions that presume you know how big the output
     * will be ahead of time...
     */
    for (i = 1024; ; i += i) {
        outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
        if (outbuf == NULL) {
            PyErr_NoMemory();
            break;
        }
        buflen = format_time(outbuf, i, fmt, &buf);
        if (buflen > 0 || i >= 256 * fmtlen) {
            /* If the buffer is 256 times as long as the format,
               it's probably not failing for lack of room!
               More likely, the format yields an empty result,
               e.g. an empty format, or %Z when the timezone
               is unknown. */
#ifdef HAVE_WCSFTIME
            ret = PyUnicode_FromWideChar(outbuf, buflen);
#else
            ret = PyUnicode_Decode(outbuf, buflen,
                                   TZNAME_ENCODING, NULL);
#endif
            PyMem_Free(outbuf);
            break;
        }
        PyMem_Free(outbuf);
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
        /* VisualStudio .NET 2005 does this properly */
        if (buflen == 0 && errno == EINVAL) {
            PyErr_SetString(PyExc_ValueError, "Invalid format string");
            break;
        }
#endif
    }
#ifdef HAVE_WCSFTIME
    PyMem_Free(format);
#else
    Py_DECREF(format);
#endif
    return ret;
}
Example #30
0
/*
 * External interface
 */
static void
DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
{
#if defined(MACOS) && !defined(MACOS_X_UNIX)
	GrafPtr		oldPort;
#endif
#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
	char		*saved_locale;
#endif
	PyObject		*cmdstr;
	PyObject		*cmdbytes;
	PyGILState_STATE	pygilstate;

#if defined(MACOS) && !defined(MACOS_X_UNIX)
	GetPort(&oldPort);
	/* Check if the Python library is available */
	if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
		goto theend;
#endif
	if (Python3_Init())
		goto theend;

	init_range(arg);

	Python_Release_Vim();	    /* leave vim */

#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
	/* Python only works properly when the LC_NUMERIC locale is "C". */
	saved_locale = setlocale(LC_NUMERIC, NULL);
	if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
		saved_locale = NULL;
	else {
		/* Need to make a copy, value may change when setting new locale. */
		saved_locale = (char *)vim_strsave((char_u *)saved_locale);
		(void)setlocale(LC_NUMERIC, "C");
	}
#endif

	pygilstate = PyGILState_Ensure();

	/* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
	 * SyntaxError (unicode error). */
	cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
							  (char *)ENC_OPT, CODEC_ERROR_HANDLER);
	cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
	Py_XDECREF(cmdstr);

	run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
	Py_XDECREF(cmdbytes);

	PyGILState_Release(pygilstate);

#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
	if (saved_locale != NULL) {
		(void)setlocale(LC_NUMERIC, saved_locale);
		vim_free(saved_locale);
	}
#endif

	Python_Lock_Vim();		    /* enter vim */
	PythonIO_Flush();
#if defined(MACOS) && !defined(MACOS_X_UNIX)
	SetPort(oldPort);
#endif

theend:
	return;	    /* keeps lint happy */
}