Skip to content

Commit 9cf0fbd

Browse files
authored
PYTHON-2001 Fix warnings emitted by Python 3.8 (#428)
Fix DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats Fix DeprecationWarning: isAlive() is deprecated, use is_alive() instead Fix SyntaxWarning: invalid escape sequence Test Python 3.8 on Travis
1 parent bbf55d6 commit 9cf0fbd

11 files changed

+81
-80
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ python:
66
- 3.5
77
- 3.6
88
- 3.7
9+
- 3.8
910
- pypy
1011
- pypy3.5
1112

bson/_cbsonmodule.c

+13-26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* should be used to speed up BSON encoding and decoding.
2121
*/
2222

23+
#define PY_SSIZE_T_CLEAN
2324
#include "Python.h"
2425
#include "datetime.h"
2526

@@ -1818,13 +1819,8 @@ static PyObject* _cbson_dict_to_bson(PyObject* self, PyObject* args) {
18181819
}
18191820

18201821
/* objectify buffer */
1821-
#if PY_MAJOR_VERSION >= 3
1822-
result = Py_BuildValue("y#", buffer_get_buffer(buffer),
1823-
buffer_get_position(buffer));
1824-
#else
1825-
result = Py_BuildValue("s#", buffer_get_buffer(buffer),
1826-
buffer_get_position(buffer));
1827-
#endif
1822+
result = Py_BuildValue(BYTES_FORMAT_STRING, buffer_get_buffer(buffer),
1823+
(Py_ssize_t)buffer_get_position(buffer));
18281824
destroy_codec_options(&options);
18291825
buffer_free(buffer);
18301826
return result;
@@ -1896,7 +1892,7 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
18961892
if (options->is_raw_bson) {
18971893
value = PyObject_CallFunction(
18981894
options->document_class, BYTES_FORMAT_STRING "O",
1899-
buffer + *position, size, options->options_obj);
1895+
buffer + *position, (Py_ssize_t)size, options->options_obj);
19001896
if (!value) {
19011897
goto invalid;
19021898
}
@@ -2175,11 +2171,8 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
21752171
goto invalid;
21762172
}
21772173
if ((objectid_type = _get_object(state->ObjectId, "bson.objectid", "ObjectId"))) {
2178-
#if PY_MAJOR_VERSION >= 3
2179-
value = PyObject_CallFunction(objectid_type, "y#", buffer + *position, 12);
2180-
#else
2181-
value = PyObject_CallFunction(objectid_type, "s#", buffer + *position, 12);
2182-
#endif
2174+
value = PyObject_CallFunction(objectid_type, BYTES_FORMAT_STRING,
2175+
buffer + *position, (Py_ssize_t)12);
21832176
Py_DECREF(objectid_type);
21842177
}
21852178
*position += 12;
@@ -2365,11 +2358,8 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
23652358
*position += coll_length;
23662359

23672360
if ((objectid_type = _get_object(state->ObjectId, "bson.objectid", "ObjectId"))) {
2368-
#if PY_MAJOR_VERSION >= 3
2369-
id = PyObject_CallFunction(objectid_type, "y#", buffer + *position, 12);
2370-
#else
2371-
id = PyObject_CallFunction(objectid_type, "s#", buffer + *position, 12);
2372-
#endif
2361+
id = PyObject_CallFunction(objectid_type, BYTES_FORMAT_STRING,
2362+
buffer + *position, (Py_ssize_t)12);
23732363
Py_DECREF(objectid_type);
23742364
}
23752365
if (!id) {
@@ -2556,13 +2546,9 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
25562546
"Decimal128"))) {
25572547
value = PyObject_CallMethod(dec128,
25582548
"from_bid",
2559-
#if PY_MAJOR_VERSION >= 3
2560-
"y#",
2561-
#else
2562-
"s#",
2563-
#endif
2549+
BYTES_FORMAT_STRING,
25642550
buffer + *position,
2565-
16);
2551+
(Py_ssize_t)16);
25662552
Py_DECREF(dec128);
25672553
}
25682554
*position += 16;
@@ -2939,7 +2925,7 @@ static PyObject* _cbson_bson_to_dict(PyObject* self, PyObject* args) {
29392925
/* No need to decode fields if using RawBSONDocument */
29402926
if (options.is_raw_bson) {
29412927
result = PyObject_CallFunction(
2942-
options.document_class, BYTES_FORMAT_STRING "O", string, size,
2928+
options.document_class, BYTES_FORMAT_STRING "O", string, (Py_ssize_t)size,
29432929
options_obj);
29442930
}
29452931
else {
@@ -3031,7 +3017,7 @@ static PyObject* _cbson_decode_all(PyObject* self, PyObject* args) {
30313017
/* No need to decode fields if using RawBSONDocument. */
30323018
if (options.is_raw_bson) {
30333019
dict = PyObject_CallFunction(
3034-
options.document_class, BYTES_FORMAT_STRING "O", string, size,
3020+
options.document_class, BYTES_FORMAT_STRING "O", string, (Py_ssize_t)size,
30353021
options_obj);
30363022
} else {
30373023
dict = elements_to_dict(self, string + 4, (unsigned)size - 5, &options);
@@ -3143,6 +3129,7 @@ init_cbson(void)
31433129
_cbson_API[_cbson_buffer_write_int64_INDEX] = (void *) buffer_write_int64;
31443130
_cbson_API[_cbson_buffer_write_int32_at_position_INDEX] =
31453131
(void *) buffer_write_int32_at_position;
3132+
_cbson_API[_cbson_downcast_and_check_INDEX] = (void *) _downcast_and_check;
31463133

31473134
#if PY_VERSION_HEX >= 0x03010000
31483135
/* PyCapsule is new in python 3.1 */

bson/_cbsonmodule.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ typedef struct codec_options_t {
113113
#define _cbson_buffer_write_int32_at_position_RETURN void
114114
#define _cbson_buffer_write_int32_at_position_PROTO (buffer_t buffer, int position, int32_t data)
115115

116+
#define _cbson_downcast_and_check_INDEX 10
117+
#define _cbson_downcast_and_check_RETURN int
118+
#define _cbson_downcast_and_check_PROTO (Py_ssize_t size, uint8_t extra)
119+
116120
/* Total number of C API pointers */
117-
#define _cbson_API_POINTER_COUNT 10
121+
#define _cbson_API_POINTER_COUNT 11
118122

119123
#ifdef _CBSON_MODULE
120124
/* This section is used when compiling _cbsonmodule */
@@ -139,6 +143,8 @@ static _cbson_buffer_write_int64_RETURN buffer_write_int64 _cbson_buffer_write_i
139143

140144
static _cbson_buffer_write_int32_at_position_RETURN buffer_write_int32_at_position _cbson_buffer_write_int32_at_position_PROTO;
141145

146+
static _cbson_downcast_and_check_RETURN _downcast_and_check _cbson_downcast_and_check_PROTO;
147+
142148
#else
143149
/* This section is used in modules that use _cbsonmodule's API */
144150

@@ -164,6 +170,8 @@ static void **_cbson_API;
164170

165171
#define buffer_write_int32_at_position (*(_cbson_buffer_write_int32_at_position_RETURN (*)_cbson_buffer_write_int32_at_position_PROTO) _cbson_API[_cbson_buffer_write_int32_at_position_INDEX])
166172

173+
#define _downcast_and_check (*(_cbson_downcast_and_check_RETURN (*)_cbson_downcast_and_check_PROTO) _cbson_API[_cbson_downcast_and_check_INDEX])
174+
167175
#define _cbson_IMPORT _cbson_API = (void **)PyCapsule_Import("_cbson._C_API", 0)
168176

169177
#endif

bson/time64.c

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ gmtime64_r() is a 64-bit equivalent of gmtime_r().
4444
#endif
4545

4646
/* Including Python.h fixes issues with interpreters built with -std=c99. */
47+
#define PY_SSIZE_T_CLEAN
4748
#include "Python.h"
4849

4950
#include <time.h>

0 commit comments

Comments
 (0)