return d;
}
-/* Convert a bytes object to a char*. Optionally lock the buffer if it is a
- bytes array. */
-
-static char*
-bytes2str(PyObject* o, int lock)
-{
- if(PyBytes_Check(o))
- return PyBytes_AsString(o);
- else if(PyByteArray_Check(o)) {
- if (lock && PyObject_GetBuffer(o, NULL, 0) < 0)
- /* On a bytearray, this should not fail. */
- PyErr_BadInternalCall();
- return PyByteArray_AsString(o);
- } else {
- /* The FS converter should have verified that this
- is either bytes or bytearray. */
- Py_FatalError("bad object passed to bytes2str");
- /* not reached. */
- return "";
- }
-}
-
-/* Release the lock, decref the object. */
-static void
-release_bytes(PyObject* o)
-{
- if (PyByteArray_Check(o))
- o->ob_type->tp_as_buffer->bf_releasebuffer(o, 0);
- Py_DECREF(o);
-}
-
-
/* Set a POSIX-specific error from errno, and return NULL */
static PyObject *
posix_error_with_allocated_filename(PyObject* name)
{
PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
- bytes2str(name, 0));
- release_bytes(name);
+ PyBytes_AsString(name));
+ Py_DECREF(name);
return rc;
}
if (!PyArg_ParseTuple(args, format,
PyUnicode_FSConverter, &opath1))
return NULL;
- path1 = bytes2str(opath1, 1);
+ path1 = PyBytes_AsString(opath1);
Py_BEGIN_ALLOW_THREADS
res = (*func)(path1);
Py_END_ALLOW_THREADS
if (res < 0)
return posix_error_with_allocated_filename(opath1);
- release_bytes(opath1);
+ Py_DECREF(opath1);
Py_INCREF(Py_None);
return Py_None;
}
PyUnicode_FSConverter, &opath2)) {
return NULL;
}
- path1 = bytes2str(opath1, 1);
- path2 = bytes2str(opath2, 1);
+ path1 = PyBytes_AsString(opath1);
+ path2 = PyBytes_AsString(opath2);
Py_BEGIN_ALLOW_THREADS
res = (*func)(path1, path2);
Py_END_ALLOW_THREADS
- release_bytes(opath1);
- release_bytes(opath2);
+ Py_DECREF(opath1);
+ Py_DECREF(opath2);
if (res != 0)
/* XXX how to report both path1 and path2??? */
return posix_error();
if (!PyArg_ParseTuple(args, format,
PyUnicode_FSConverter, &opath))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
res = (*statfunc)(path, &st);
Py_END_ALLOW_THREADS
else
result = _pystat_fromstructstat(&st);
- release_bytes(opath);
+ Py_DECREF(opath);
return result;
}
if (!PyArg_ParseTuple(args, "O&i:access",
PyUnicode_FSConverter, &opath, &mode))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
attr = GetFileAttributesA(path);
Py_END_ALLOW_THREADS
- release_bytes(opath);
+ Py_DECREF(opath);
finish:
if (attr == 0xFFFFFFFF)
/* File does not exist, or cannot read attributes */
if (!PyArg_ParseTuple(args, "O&i:access",
PyUnicode_FSConverter, &opath, &mode))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
res = access(path, mode);
Py_END_ALLOW_THREADS
- release_bytes(opath);
+ Py_DECREF(opath);
return PyBool_FromLong(res == 0);
#endif
}
if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
&opath, &i))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
attr = GetFileAttributesA(path);
if (attr != 0xFFFFFFFF) {
Py_END_ALLOW_THREADS
if (!res) {
win32_error("chmod", path);
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
- release_bytes(opath);
+ Py_DECREF(opath);
Py_INCREF(Py_None);
return Py_None;
#else /* MS_WINDOWS */
if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
&opath, &i))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
res = chmod(path, i);
Py_END_ALLOW_THREADS
if (res < 0)
return posix_error_with_allocated_filename(opath);
- release_bytes(opath);
+ Py_DECREF(opath);
Py_INCREF(Py_None);
return Py_None;
#endif
if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
&opath, &i))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
res = lchmod(path, i);
Py_END_ALLOW_THREADS
if (res < 0)
return posix_error_with_allocated_filename(opath);
- release_bytes(opath);
+ Py_DECREF(opath);
Py_RETURN_NONE;
}
#endif /* HAVE_LCHMOD */
if (!PyArg_ParseTuple(args, "O&k:chflags",
PyUnicode_FSConverter, &opath, &flags))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
res = chflags(path, flags);
Py_END_ALLOW_THREADS
if (res < 0)
return posix_error_with_allocated_filename(opath);
- release_bytes(opath);
+ Py_DECREF(opath);
Py_INCREF(Py_None);
return Py_None;
}
if (!PyArg_ParseTuple(args, "O&k:lchflags",
PyUnicode_FSConverter, &opath, &flags))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
res = lchflags(path, flags);
Py_END_ALLOW_THREADS
if (res < 0)
return posix_error_with_allocated_filename(opath);
- release_bytes(opath);
+ Py_DECREF(opath);
Py_INCREF(Py_None);
return Py_None;
}
PyUnicode_FSConverter, &opath,
&uid, &gid))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
res = chown(path, (uid_t) uid, (gid_t) gid);
Py_END_ALLOW_THREADS
if (res < 0)
return posix_error_with_allocated_filename(opath);
- release_bytes(opath);
+ Py_DECREF(opath);
Py_INCREF(Py_None);
return Py_None;
}
PyUnicode_FSConverter, &opath,
&uid, &gid))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
res = lchown(path, (uid_t) uid, (gid_t) gid);
Py_END_ALLOW_THREADS
if (res < 0)
return posix_error_with_allocated_filename(opath);
- release_bytes(opath);
+ Py_DECREF(opath);
Py_INCREF(Py_None);
return Py_None;
}
if (!PyArg_ParseTuple(args, "O&:listdir",
PyUnicode_FSConverter, &opath))
return NULL;
- if (PyObject_Size(opath)+1 > MAX_PATH) {
+ if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
PyErr_SetString(PyExc_ValueError, "path too long");
Py_DECREF(opath);
return NULL;
}
- strcpy(namebuf, bytes2str(opath, 0));
+ strcpy(namebuf, PyBytes_AsString(opath));
len = PyObject_Size(opath);
if (len > 0) {
char ch = namebuf[len-1];
if (!PyArg_ParseTuple(args, "O&:listdir",
PyUnicode_FSConverter, &oname))
return NULL;
- name = bytes2str(oname);
- len = PyObject_Size(oname);
+ name = PyBytes_AsString(oname);
+ len = PyBytes_GET_SIZE(oname);
if (len >= MAX_PATH) {
- release_bytes(oname);
+ Py_DECREF(oname);
PyErr_SetString(PyExc_ValueError, "path too long");
return NULL;
}
strcpy(namebuf + len, "*.*");
if ((d = PyList_New(0)) == NULL) {
- release_bytes(oname);
+ Py_DECREF(oname);
return NULL;
}
} while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
}
- release_bytes(oname);
+ Py_DECREF(oname);
return d;
#else
PyObject *oname;
}
if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
return NULL;
- name = bytes2str(oname, 1);
+ name = PyBytes_AsString(oname);
if ((dirp = opendir(name)) == NULL) {
return posix_error_with_allocated_filename(oname);
}
if ((d = PyList_New(0)) == NULL) {
closedir(dirp);
- release_bytes(oname);
+ Py_DECREF(oname);
return NULL;
}
for (;;) {
Py_DECREF(v);
}
closedir(dirp);
- release_bytes(oname);
+ Py_DECREF(oname);
return d;
if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
PyUnicode_FSConverter, &opath))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
outbuf, &temp)) {
win32_error("GetFullPathName", path);
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
- release_bytes(opath);
+ Py_DECREF(opath);
if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
return PyUnicode_Decode(outbuf, strlen(outbuf),
Py_FileSystemDefaultEncoding, NULL);
if (!PyArg_ParseTuple(args, "O&|i:mkdir",
PyUnicode_FSConverter, &opath, &mode))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
/* PyUnicode_AS_UNICODE OK without thread lock as
it is a simple dereference. */
Py_END_ALLOW_THREADS
if (!res) {
win32_error("mkdir", path);
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
- release_bytes(opath);
+ Py_DECREF(opath);
Py_INCREF(Py_None);
return Py_None;
#else
if (!PyArg_ParseTuple(args, "O&|i:mkdir",
PyUnicode_FSConverter, &opath, &mode))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
res = mkdir(path);
Py_END_ALLOW_THREADS
if (res < 0)
return posix_error_with_allocated_filename(opath);
- release_bytes(opath);
+ Py_DECREF(opath);
Py_INCREF(Py_None);
return Py_None;
#endif
PyUnicode_FSConverter, &command_obj))
return NULL;
- command = bytes2str(command_obj, 1);
+ command = PyBytes_AsString(command_obj);
Py_BEGIN_ALLOW_THREADS
sts = system(command);
Py_END_ALLOW_THREADS
- release_bytes(command_obj);
+ Py_DECREF(command_obj);
#endif
return PyLong_FromLong(sts);
}
if (!PyArg_ParseTuple(args, "O&O:utime",
PyUnicode_FSConverter, &oapath, &arg))
return NULL;
- apath = bytes2str(oapath, 1);
+ apath = PyBytes_AsString(oapath);
Py_BEGIN_ALLOW_THREADS
hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
NULL, OPEN_EXISTING,
Py_END_ALLOW_THREADS
if (hFile == INVALID_HANDLE_VALUE) {
win32_error("utime", apath);
- release_bytes(oapath);
+ Py_DECREF(oapath);
return NULL;
}
- release_bytes(oapath);
+ Py_DECREF(oapath);
}
if (arg == Py_None) {
if (!PyArg_ParseTuple(args, "O&O:utime",
PyUnicode_FSConverter, &opath, &arg))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
if (arg == Py_None) {
/* optional time values not given */
Py_BEGIN_ALLOW_THREADS
else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
PyErr_SetString(PyExc_TypeError,
"utime() arg 2 must be a tuple (atime, mtime)");
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
else {
if (extract_time(PyTuple_GET_ITEM(arg, 0),
&atime, &ausec) == -1) {
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
if (extract_time(PyTuple_GET_ITEM(arg, 1),
&mtime, &musec) == -1) {
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
ATIME = atime;
if (res < 0) {
return posix_error_with_allocated_filename(opath);
}
- release_bytes(opath);
+ Py_DECREF(opath);
Py_INCREF(Py_None);
return Py_None;
#undef UTIME_ARG
Py_ssize_t size;
if (!PyUnicode_FSConverter(o, &bytes))
return 0;
- size = PyObject_Size(bytes);
+ size = PyBytes_GET_SIZE(bytes);
*out = PyMem_Malloc(size+1);
if (!*out)
return 0;
- /* Don't lock bytes, as we hold the GIL */
- memcpy(*out, bytes2str(bytes, 0), size+1);
+ memcpy(*out, PyBytes_AsString(bytes), size+1);
Py_DECREF(bytes);
return 1;
}
PyUnicode_FSConverter,
&opath, &argv))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
if (PyList_Check(argv)) {
argc = PyList_Size(argv);
getitem = PyList_GetItem;
}
else {
PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
if (argc < 1) {
PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
argvlist = PyMem_NEW(char *, argc+1);
if (argvlist == NULL) {
- release_bytes(opath);
+ Py_DECREF(opath);
return PyErr_NoMemory();
}
for (i = 0; i < argc; i++) {
free_string_array(argvlist, i);
PyErr_SetString(PyExc_TypeError,
"execv() arg 2 must contain only strings");
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
/* If we get here it's definitely an error */
free_string_array(argvlist, argc);
- release_bytes(opath);
+ Py_DECREF(opath);
return posix_error();
}
PyUnicode_FSConverter,
&opath, &argv, &env))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
if (PyList_Check(argv)) {
argc = PyList_Size(argv);
getitem = PyList_GetItem;
Py_XDECREF(vals);
Py_XDECREF(keys);
fail_0:
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
#endif /* HAVE_EXECV */
PyUnicode_FSConverter,
&opath, &argv))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
if (PyList_Check(argv)) {
argc = PyList_Size(argv);
getitem = PyList_GetItem;
else {
PyErr_SetString(PyExc_TypeError,
"spawnv() arg 2 must be a tuple or list");
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
argvlist = PyMem_NEW(char *, argc+1);
if (argvlist == NULL) {
- release_bytes(opath);
+ Py_DECREF(opath);
return PyErr_NoMemory();
}
for (i = 0; i < argc; i++) {
PyErr_SetString(
PyExc_TypeError,
"spawnv() arg 2 must contain only strings");
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
}
#endif
free_string_array(argvlist, argc);
- release_bytes(opath);
+ Py_DECREF(opath);
if (spawnval == -1)
return posix_error();
PyUnicode_FSConverter,
&opath, &argv, &env))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
if (PyList_Check(argv)) {
argc = PyList_Size(argv);
getitem = PyList_GetItem;
Py_XDECREF(vals);
Py_XDECREF(keys);
fail_0:
- release_bytes(opath);
+ Py_DECREF(opath);
return res;
}
PyUnicode_FSConverter,
&opath, &argv))
return NULL;
- path = bytes2str(opath);
+ path = PyBytes_AsString(opath);
if (PyList_Check(argv)) {
argc = PyList_Size(argv);
getitem = PyList_GetItem;
else {
PyErr_SetString(PyExc_TypeError,
"spawnvp() arg 2 must be a tuple or list");
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
argvlist = PyMem_NEW(char *, argc+1);
if (argvlist == NULL) {
- release_bytes(opath);
+ Py_DECREF(opath);
return PyErr_NoMemory();
}
for (i = 0; i < argc; i++) {
PyErr_SetString(
PyExc_TypeError,
"spawnvp() arg 2 must contain only strings");
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
}
Py_END_ALLOW_THREADS
free_string_array(argvlist, argc);
- release_bytes(opath);
+ Py_DECREF(opath);
if (spawnval == -1)
return posix_error();
PyUnicode_FSConverter,
&opath, &argv, &env))
return NULL;
- path = bytes2str(opath);
+ path = PyBytes_AsString(opath);
if (PyList_Check(argv)) {
argc = PyList_Size(argv);
getitem = PyList_GetItem;
Py_XDECREF(vals);
Py_XDECREF(keys);
fail_0:
- release_bytes(opath);
+ Py_DECREF(opath);
return res;
}
#endif /* PYOS_OS2 */
if (!PyArg_ParseTuple(args, "O&:readlink",
PyUnicode_FSConverter, &opath))
return NULL;
- path = bytes2str(opath, 1);
+ path = PyBytes_AsString(opath);
v = PySequence_GetItem(args, 0);
if (v == NULL) {
- release_bytes(opath);
+ Py_DECREF(opath);
return NULL;
}
if (n < 0)
return posix_error_with_allocated_filename(opath);
- release_bytes(opath);
+ Py_DECREF(opath);
v = PyBytes_FromStringAndSize(buf, n);
if (arg_is_unicode) {
PyObject *w;
PyUnicode_FSConverter, &ofile,
&flag, &mode))
return NULL;
- file = bytes2str(ofile, 1);
+ file = PyBytes_AsString(ofile);
Py_BEGIN_ALLOW_THREADS
fd = open(file, flag, mode);
Py_END_ALLOW_THREADS
if (fd < 0)
return posix_error_with_allocated_filename(ofile);
- release_bytes(ofile);
+ Py_DECREF(ofile);
return PyLong_FromLong((long)fd);
}
PyUnicode_FSConverter, &os1,
PyUnicode_FSConverter, &os2))
return NULL;
- s1 = bytes2str(os1, 1);
- s2 = bytes2str(os2, 1);
+ s1 = PyBytes_AsString(os1);
+ s2 = PyBytes_AsString(os2);
#endif
#if defined(PYOS_OS2)
PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
if (putenv(newenv)) {
Py_DECREF(newstr);
- release_bytes(os1);
- release_bytes(os2);
+ Py_DECREF(os1);
+ Py_DECREF(os2);
posix_error();
return NULL;
}
}
#endif
#ifndef MS_WINDOWS
- release_bytes(os1);
- release_bytes(os2);
+ Py_DECREF(os1);
+ Py_DECREF(os2);
#endif
Py_INCREF(Py_None);
return Py_None;
PyUnicode_FSConverter, &ofilepath,
&operation))
return NULL;
- filepath = bytes2str(ofilepath, 1);
+ filepath = PyBytes_AsString(ofilepath);
Py_BEGIN_ALLOW_THREADS
rc = ShellExecute((HWND)0, operation, filepath,
NULL, NULL, SW_SHOWNORMAL);
Py_END_ALLOW_THREADS
if (rc <= (HINSTANCE)32) {
PyObject *errval = win32_error("startfile", filepath);
- release_bytes(ofilepath);
+ Py_DECREF(ofilepath);
return errval;
}
- release_bytes(ofilepath);
+ Py_DECREF(ofilepath);
Py_INCREF(Py_None);
return Py_None;
}