Commit 08ddbdcb authored by André Anjos's avatar André Anjos 💬
Browse files

Merge branch '7-what-is-the-use-of-pybobio_filenameconverter' into 'master'

Resolve "What is the use of PyBobIo_FilenameConverter"

Closes #7

See merge request !21
parents 387e215d 3cd2d16e
Pipeline #11712 passed with stages
in 9 minutes and 59 seconds
......@@ -67,34 +67,35 @@ static void PyBobIoFile_Delete (PyBobIoFileObject* o) {
}
int PyBobIo_FilenameConverter (PyObject* o, PyObject** b) {
#if PY_VERSION_HEX >= 0x03020000
if (!PyUnicode_FSConverter(o, b)) return 0;
#else
int PyBobIo_FilenameConverter (PyObject* o, const char** b) {
#if PY_VERSION_HEX >= 0x03000000
if (PyUnicode_Check(o)) {
*b = PyUnicode_AsEncodedString(o, Py_FileSystemDefaultEncoding, "strict");
*b = PyUnicode_AsUTF8(o);
} else {
PyObject* temp = PyObject_Bytes(o);
auto temp_ = make_safe(temp);
*b = PyBytes_AsString(temp);
}
else {
#if PY_VERSION_HEX >= 0x03000000
*b = PyObject_Bytes(o);
#else
*b = PyObject_Str(o);
#endif
if (PyUnicode_Check(o)) {
PyObject* temp = PyUnicode_AsEncodedString(o, Py_FileSystemDefaultEncoding, "strict");
auto temp_ = make_safe(temp);
*b = PyString_AsString(temp);
} else {
*b = PyString_AsString(o);
}
if (!b) return 0;
#endif
return 1;
return b != 0;
}
/* The __init__(self) method */
static int PyBobIoFile_init(PyBobIoFileObject* self, PyObject *args, PyObject* kwds) {
const char* c_filename = 0;
BOB_TRY
/* Parses input arguments in a single shot */
static char** kwlist = s_file.kwlist();
PyObject* filename = 0;
char* pretend_extension = 0;
const char* filename;
const char* pretend_extension = 0;
#if PY_VERSION_HEX >= 0x03000000
# define MODE_CHAR "C"
......@@ -109,28 +110,20 @@ BOB_TRY
#undef MODE_CHAR
auto filename_ = make_safe(filename);
if (mode != 'r' && mode != 'w' && mode != 'a') {
PyErr_Format(PyExc_ValueError, "file open mode string should have 1 element and be either 'r' (read), 'w' (write) or 'a' (append)");
return -1;
}
#if PY_VERSION_HEX >= 0x03000000
c_filename = PyBytes_AS_STRING(filename);
#else
c_filename = PyString_AS_STRING(filename);
#endif
if (pretend_extension) {
self->f = bob::io::base::open(c_filename, mode, pretend_extension);
self->f = bob::io::base::open(filename, mode, pretend_extension);
}
else {
self->f = bob::io::base::open(c_filename, mode);
self->f = bob::io::base::open(filename, mode);
}
return 0; ///< SUCCESS
BOB_CATCH_MEMBER(c_filename, -1);
BOB_CATCH_MEMBER("constructor", -1);
}
static PyObject* PyBobIoFile_repr(PyBobIoFileObject* self) {
......
......@@ -137,15 +137,13 @@ BOB_TRY
char mode = 'r';
#endif
PyObject* filename = 0;
const char* filename;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|" MODE_CHAR, kwlist1,
&PyBobIo_FilenameConverter, &filename, &mode))
return -1;
#undef MODE_CHAR
auto filename_ = make_safe(filename);
if (mode != 'r' && mode != 'w' && mode != 'a' && mode != 'x') {
PyErr_Format(PyExc_ValueError, "file open mode string should have 1 element and be either 'r' (read), 'w' (write), 'a' (append), 'x' (exclusive)");
return -1;
......@@ -153,13 +151,7 @@ BOB_TRY
bob::io::base::HDF5File::mode_t mode_mode = mode_from_char(mode);
if (PyErr_Occurred()) return -1;
#if PY_VERSION_HEX >= 0x03000000
const char* c_filename = PyBytes_AS_STRING(filename);
#else
const char* c_filename = PyString_AS_STRING(filename);
#endif
self->f.reset(new bob::io::base::HDF5File(c_filename, mode_mode));
self->f.reset(new bob::io::base::HDF5File(filename, mode_mode));
return 0; ///< SUCCESS
BOB_CATCH_MEMBER("hdf5 constructor", -1)
}
......
......@@ -92,7 +92,7 @@ typedef struct {
#define PyBobIo_TypeInfoAsTuple_PROTO (const bob::io::base::array::typeinfo& ti)
#define PyBobIo_FilenameConverter_RET int
#define PyBobIo_FilenameConverter_PROTO (PyObject* o, PyObject** b)
#define PyBobIo_FilenameConverter_PROTO (PyObject* o, const char** b)
/*****************
* HDF5 bindings *
......
......@@ -9,7 +9,7 @@
#define BOB_IO_BASE_CONFIG_H
/* Macros that define versions and important names */
#define BOB_IO_BASE_API_VERSION 0x0200
#define BOB_IO_BASE_API_VERSION 0x0201
#ifdef BOB_IMPORT_VERSION
......
......@@ -530,3 +530,8 @@ def test_python_interfaces():
finally:
os.unlink(tmpname)
def unicode_test():
filename = test_utils.datafile(u"Φîłèñäϻæ.hdf5", __name__)
hdf5 = HDF5File(filename)
assert numpy.allclose(hdf5["Test"], range(10))
......@@ -69,24 +69,27 @@ Generic Functions
described above in case of success.
.. cpp:function:: int PyBobIo_FilenameConverter (PyObject* o, PyObject** b)
.. cpp:function:: int PyBobIo_FilenameConverter (PyObject* o, const char** b)
This function is meant to be used with :c:func:`PyArg_ParseTupleAndKeywords`
family of functions in the Python C-API. It converts an arbitrary input
object into a ``PyStringObject`` (in Python2.x) and into a ``PyBytesObject``
(in Python3.x). If the input object is of type ``PyUnicodeObject``, which is
the default in Python3.x, the unicode code is properly decoded using
:c:func:`PyUnicode_AsEncodedString` with ``encoding`` set to
``Py_FileSystemDefaultEncoding`` and ``errors`` set to ``"strict"``. On
versions of Python >= 3.2, this is just an alias for
:c:func:`PyUnicode_FSConverter`, which does a similar job.
Objects which are not ``PyUnicodeObject`` are coerced into a bytes/string
object using :c:func:`PyObject_Bytes` (on Python3.x) and
:c:func:`PyObject_Str` (on Python 2.x).
object into a ``const char*`` If the input object is of type
``PyUnicodeObject`` (which is the default in Python3.x) the unicode code is
properly decoded using :c:func:`PyUnicode_AsEncodedString` with ``encoding``
set to ``Py_FileSystemDefaultEncoding`` and ``errors`` set to ``"strict"``.
Objects which are not ``PyUnicodeObject`` are first coerced into a
bytes/string before converting to the ``const char*`` object using
:c:func:`PyObject_Bytes` (on Python3.x) and :c:func:`PyObject_Str`
(on Python 2.x).
Returns 0 if an error is detected, 1 on success.
.. note::
Since version 2.2, this function converts into ``const char*`` directly.
Before version 2.2, it was returning either ``PyBytesObject`` (Python 3)
or ``PyStringObject`` (Python 2).
Bob File Support
----------------
......
2.1.1b0
\ No newline at end of file
2.2.0b0
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment