Skip to content

Commit 1203eb7

Browse files
committed
stop using c++
1 parent 6af994d commit 1203eb7

File tree

5 files changed

+84
-84
lines changed

5 files changed

+84
-84
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pyupgrade:
2222

2323
.PHONY: cython
2424
cython:
25-
cython --cplus msgpack/_cmsgpack.pyx
25+
cython msgpack/_cmsgpack.pyx
2626

2727
.PHONY: test
2828
test: cython

Diff for: msgpack/_unpacker.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ cdef extern from "unpack.h":
3535
PyObject* timestamp_t
3636
PyObject *giga;
3737
PyObject *utc;
38-
char *unicode_errors
38+
const char *unicode_errors
3939
Py_ssize_t max_str_len
4040
Py_ssize_t max_bin_len
4141
Py_ssize_t max_array_len

Diff for: msgpack/unpack_container_header.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
static inline int unpack_container_header(unpack_context* ctx, const char* data, Py_ssize_t len, Py_ssize_t* off)
2+
{
3+
assert(len >= *off);
4+
uint32_t size;
5+
const unsigned char *const p = (unsigned char*)data + *off;
6+
7+
#define inc_offset(inc) \
8+
if (len - *off < inc) \
9+
return 0; \
10+
*off += inc;
11+
12+
switch (*p) {
13+
case var_offset:
14+
inc_offset(3);
15+
size = _msgpack_load16(uint16_t, p + 1);
16+
break;
17+
case var_offset + 1:
18+
inc_offset(5);
19+
size = _msgpack_load32(uint32_t, p + 1);
20+
break;
21+
#ifdef USE_CASE_RANGE
22+
case fixed_offset + 0x0 ... fixed_offset + 0xf:
23+
#else
24+
case fixed_offset + 0x0:
25+
case fixed_offset + 0x1:
26+
case fixed_offset + 0x2:
27+
case fixed_offset + 0x3:
28+
case fixed_offset + 0x4:
29+
case fixed_offset + 0x5:
30+
case fixed_offset + 0x6:
31+
case fixed_offset + 0x7:
32+
case fixed_offset + 0x8:
33+
case fixed_offset + 0x9:
34+
case fixed_offset + 0xa:
35+
case fixed_offset + 0xb:
36+
case fixed_offset + 0xc:
37+
case fixed_offset + 0xd:
38+
case fixed_offset + 0xe:
39+
case fixed_offset + 0xf:
40+
#endif
41+
++*off;
42+
size = ((unsigned int)*p) & 0x0f;
43+
break;
44+
default:
45+
PyErr_SetString(PyExc_ValueError, "Unexpected type header on stream");
46+
return -1;
47+
}
48+
unpack_callback_uint32(&ctx->user, size, &ctx->stack[0].obj);
49+
return 1;
50+
}
51+

Diff for: msgpack/unpack_template.h

+22-63
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ static inline void unpack_clear(unpack_context *ctx)
7575
Py_CLEAR(ctx->stack[0].obj);
7676
}
7777

78-
template <bool construct>
79-
static inline int unpack_execute(unpack_context* ctx, const char* data, Py_ssize_t len, Py_ssize_t* off)
78+
static inline int unpack_execute(bool construct, unpack_context* ctx, const char* data, Py_ssize_t len, Py_ssize_t* off)
8079
{
8180
assert(len >= *off);
8281

@@ -386,6 +385,7 @@ static inline int unpack_execute(unpack_context* ctx, const char* data, Py_ssize
386385
#undef construct_cb
387386
}
388387

388+
#undef NEXT_CS
389389
#undef SWITCH_RANGE_BEGIN
390390
#undef SWITCH_RANGE
391391
#undef SWITCH_RANGE_DEFAULT
@@ -397,68 +397,27 @@ static inline int unpack_execute(unpack_context* ctx, const char* data, Py_ssize
397397
#undef again_fixed_trail_if_zero
398398
#undef start_container
399399

400-
template <unsigned int fixed_offset, unsigned int var_offset>
401-
static inline int unpack_container_header(unpack_context* ctx, const char* data, Py_ssize_t len, Py_ssize_t* off)
402-
{
403-
assert(len >= *off);
404-
uint32_t size;
405-
const unsigned char *const p = (unsigned char*)data + *off;
406-
407-
#define inc_offset(inc) \
408-
if (len - *off < inc) \
409-
return 0; \
410-
*off += inc;
411-
412-
switch (*p) {
413-
case var_offset:
414-
inc_offset(3);
415-
size = _msgpack_load16(uint16_t, p + 1);
416-
break;
417-
case var_offset + 1:
418-
inc_offset(5);
419-
size = _msgpack_load32(uint32_t, p + 1);
420-
break;
421-
#ifdef USE_CASE_RANGE
422-
case fixed_offset + 0x0 ... fixed_offset + 0xf:
423-
#else
424-
case fixed_offset + 0x0:
425-
case fixed_offset + 0x1:
426-
case fixed_offset + 0x2:
427-
case fixed_offset + 0x3:
428-
case fixed_offset + 0x4:
429-
case fixed_offset + 0x5:
430-
case fixed_offset + 0x6:
431-
case fixed_offset + 0x7:
432-
case fixed_offset + 0x8:
433-
case fixed_offset + 0x9:
434-
case fixed_offset + 0xa:
435-
case fixed_offset + 0xb:
436-
case fixed_offset + 0xc:
437-
case fixed_offset + 0xd:
438-
case fixed_offset + 0xe:
439-
case fixed_offset + 0xf:
440-
#endif
441-
++*off;
442-
size = ((unsigned int)*p) & 0x0f;
443-
break;
444-
default:
445-
PyErr_SetString(PyExc_ValueError, "Unexpected type header on stream");
446-
return -1;
447-
}
448-
unpack_callback_uint32(&ctx->user, size, &ctx->stack[0].obj);
449-
return 1;
400+
static int unpack_construct(unpack_context *ctx, const char *data, Py_ssize_t len, Py_ssize_t *off) {
401+
return unpack_execute(true, ctx, data, len, off);
402+
}
403+
static int unpack_skip(unpack_context *ctx, const char *data, Py_ssize_t len, Py_ssize_t *off) {
404+
return unpack_execute(false, ctx, data, len, off);
450405
}
451406

452-
#undef SWITCH_RANGE_BEGIN
453-
#undef SWITCH_RANGE
454-
#undef SWITCH_RANGE_DEFAULT
455-
#undef SWITCH_RANGE_END
456-
457-
static const execute_fn unpack_construct = &unpack_execute<true>;
458-
static const execute_fn unpack_skip = &unpack_execute<false>;
459-
static const execute_fn read_array_header = &unpack_container_header<0x90, 0xdc>;
460-
static const execute_fn read_map_header = &unpack_container_header<0x80, 0xde>;
461-
462-
#undef NEXT_CS
407+
#define unpack_container_header read_array_header
408+
#define fixed_offset 0x90
409+
#define var_offset 0xdc
410+
#include "unpack_container_header.h"
411+
#undef unpack_container_header
412+
#undef fixed_offset
413+
#undef var_offset
414+
415+
#define unpack_container_header read_map_header
416+
#define fixed_offset 0x80
417+
#define var_offset 0xde
418+
#include "unpack_container_header.h"
419+
#undef unpack_container_header
420+
#undef fixed_offset
421+
#undef var_offset
463422

464423
/* vim: set ts=4 sw=4 sts=4 expandtab */

Diff for: setup.py

+9-19
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
#!/usr/bin/env python
22
import os
33
import sys
4-
5-
6-
libraries = []
7-
macros = []
8-
9-
if sys.platform == "win32":
10-
libraries.append("ws2_32")
11-
macros = [("__LITTLE_ENDIAN__", "1")]
12-
cflags = os.environ.get("CXXFLAGS")
13-
cxx20flag = "/std:c++20"
14-
if cflags is None:
15-
cflags = cxx20flag
16-
elif cxx20flag not in cflags:
17-
cflags += " " + cxx20flag
18-
os.environ["CXXFLAGS"] = cflags
19-
20-
214
from setuptools import setup, Extension
225
from setuptools.command.build_ext import build_ext
236
from setuptools.command.sdist import sdist
@@ -42,7 +25,7 @@ def cythonize(src):
4225
if not have_cython:
4326
raise Exception("Cython is required for building from checkout")
4427
sys.stderr.write(f"cythonize: {src!r}\n")
45-
cython_compiler.compile([src], cplus=True)
28+
cython_compiler.compile([src])
4629

4730

4831
def ensure_source(src):
@@ -66,12 +49,19 @@ def __init__(self, *args, **kwargs):
6649
sdist.__init__(self, *args, **kwargs)
6750

6851

52+
libraries = []
53+
macros = []
6954
ext_modules = []
55+
56+
if sys.platform == "win32":
57+
libraries.append("ws2_32")
58+
macros = [("__LITTLE_ENDIAN__", "1")]
59+
7060
if not PYPY and not os.environ.get("MSGPACK_PUREPYTHON"):
7161
ext_modules.append(
7262
Extension(
7363
"msgpack._cmsgpack",
74-
sources=["msgpack/_cmsgpack.cpp"],
64+
sources=["msgpack/_cmsgpack.c"],
7565
libraries=libraries,
7666
include_dirs=["."],
7767
define_macros=macros,

0 commit comments

Comments
 (0)