bpo-34762: Fix contextvars C API to use PyObject* pointer types. (GH-… · python/cpython@2ec872b (original) (raw)

`@@ -18,6 +18,28 @@ module _contextvars

`

18

18

`/[clinic end generated code: output=da39a3ee5e6b4b0d input=a0955718c8b8cea6]/

`

19

19

``

20

20

``

``

21

`+

#define ENSURE_Context(o, err_ret) \

`

``

22

`+

if (!PyContext_CheckExact(o)) { \

`

``

23

`+

PyErr_SetString(PyExc_TypeError, \

`

``

24

`+

"an instance of Context was expected"); \

`

``

25

`+

return err_ret; \

`

``

26

`+

}

`

``

27

+

``

28

`+

#define ENSURE_ContextVar(o, err_ret) \

`

``

29

`+

if (!PyContextVar_CheckExact(o)) { \

`

``

30

`+

PyErr_SetString(PyExc_TypeError, \

`

``

31

`+

"an instance of ContextVar was expected"); \

`

``

32

`+

return err_ret; \

`

``

33

`+

}

`

``

34

+

``

35

`+

#define ENSURE_ContextToken(o, err_ret) \

`

``

36

`+

if (!PyContextToken_CheckExact(o)) { \

`

``

37

`+

PyErr_SetString(PyExc_TypeError, \

`

``

38

`+

"an instance of Token was expected"); \

`

``

39

`+

return err_ret; \

`

``

40

`+

}

`

``

41

+

``

42

+

21

43

`/////////////////////////// Context API

`

22

44

``

23

45

``

`@@ -50,35 +72,40 @@ _PyContext_NewHamtForTests(void)

`

50

72

`}

`

51

73

``

52

74

``

53

``

`-

PyContext *

`

``

75

`+

PyObject *

`

54

76

`PyContext_New(void)

`

55

77

`{

`

56

``

`-

return context_new_empty();

`

``

78

`+

return (PyObject *)context_new_empty();

`

57

79

`}

`

58

80

``

59

81

``

60

``

`-

PyContext *

`

61

``

`-

PyContext_Copy(PyContext * ctx)

`

``

82

`+

PyObject *

`

``

83

`+

PyContext_Copy(PyObject * octx)

`

62

84

`{

`

63

``

`-

return context_new_from_vars(ctx->ctx_vars);

`

``

85

`+

ENSURE_Context(octx, NULL)

`

``

86

`+

PyContext *ctx = (PyContext *)octx;

`

``

87

`+

return (PyObject *)context_new_from_vars(ctx->ctx_vars);

`

64

88

`}

`

65

89

``

66

90

``

67

``

`-

PyContext *

`

``

91

`+

PyObject *

`

68

92

`PyContext_CopyCurrent(void)

`

69

93

`{

`

70

94

`PyContext *ctx = context_get();

`

71

95

`if (ctx == NULL) {

`

72

96

`return NULL;

`

73

97

` }

`

74

98

``

75

``

`-

return context_new_from_vars(ctx->ctx_vars);

`

``

99

`+

return (PyObject *)context_new_from_vars(ctx->ctx_vars);

`

76

100

`}

`

77

101

``

78

102

``

79

103

`int

`

80

``

`-

PyContext_Enter(PyContext *ctx)

`

``

104

`+

PyContext_Enter(PyObject *octx)

`

81

105

`{

`

``

106

`+

ENSURE_Context(octx, -1)

`

``

107

`+

PyContext *ctx = (PyContext *)octx;

`

``

108

+

82

109

`if (ctx->ctx_entered) {

`

83

110

`PyErr_Format(PyExc_RuntimeError,

`

84

111

`"cannot enter context: %R is already entered", ctx);

`

`@@ -100,8 +127,11 @@ PyContext_Enter(PyContext *ctx)

`

100

127

``

101

128

``

102

129

`int

`

103

``

`-

PyContext_Exit(PyContext *ctx)

`

``

130

`+

PyContext_Exit(PyObject *octx)

`

104

131

`{

`

``

132

`+

ENSURE_Context(octx, -1)

`

``

133

`+

PyContext *ctx = (PyContext *)octx;

`

``

134

+

105

135

`if (!ctx->ctx_entered) {

`

106

136

`PyErr_Format(PyExc_RuntimeError,

`

107

137

`"cannot exit context: %R has not been entered", ctx);

`

`@@ -129,7 +159,7 @@ PyContext_Exit(PyContext *ctx)

`

129

159

`}

`

130

160

``

131

161

``

132

``

`-

PyContextVar *

`

``

162

`+

PyObject *

`

133

163

`PyContextVar_New(const char *name, PyObject *def)

`

134

164

`{

`

135

165

`PyObject *pyname = PyUnicode_FromString(name);

`

`@@ -138,14 +168,15 @@ PyContextVar_New(const char *name, PyObject *def)

`

138

168

` }

`

139

169

`PyContextVar *var = contextvar_new(pyname, def);

`

140

170

`Py_DECREF(pyname);

`

141

``

`-

return var;

`

``

171

`+

return (PyObject *)var;

`

142

172

`}

`

143

173

``

144

174

``

145

175

`int

`

146

``

`-

PyContextVar_Get(PyContextVar *var, PyObject *def, PyObject **val)

`

``

176

`+

PyContextVar_Get(PyObject *ovar, PyObject *def, PyObject **val)

`

147

177

`{

`

148

``

`-

assert(PyContextVar_CheckExact(var));

`

``

178

`+

ENSURE_ContextVar(ovar, -1)

`

``

179

`+

PyContextVar *var = (PyContextVar *)ovar;

`

149

180

``

150

181

`PyThreadState *ts = PyThreadState_GET();

`

151

182

`assert(ts != NULL);

`

`@@ -204,9 +235,12 @@ PyContextVar_Get(PyContextVar *var, PyObject *def, PyObject **val)

`

204

235

`}

`

205

236

``

206

237

``

207

``

`-

PyContextToken *

`

208

``

`-

PyContextVar_Set(PyContextVar *var, PyObject *val)

`

``

238

`+

PyObject *

`

``

239

`+

PyContextVar_Set(PyObject *ovar, PyObject *val)

`

209

240

`{

`

``

241

`+

ENSURE_ContextVar(ovar, NULL)

`

``

242

`+

PyContextVar *var = (PyContextVar *)ovar;

`

``

243

+

210

244

`if (!PyContextVar_CheckExact(var)) {

`

211

245

`PyErr_SetString(

`

212

246

`PyExc_TypeError, "an instance of ContextVar was expected");

`

`@@ -233,13 +267,18 @@ PyContextVar_Set(PyContextVar *var, PyObject *val)

`

233

267

`return NULL;

`

234

268

` }

`

235

269

``

236

``

`-

return tok;

`

``

270

`+

return (PyObject *)tok;

`

237

271

`}

`

238

272

``

239

273

``

240

274

`int

`

241

``

`-

PyContextVar_Reset(PyContextVar *var, PyContextToken *tok)

`

``

275

`+

PyContextVar_Reset(PyObject *ovar, PyObject *otok)

`

242

276

`{

`

``

277

`+

ENSURE_ContextVar(ovar, -1)

`

``

278

`+

ENSURE_ContextToken(otok, -1)

`

``

279

`+

PyContextVar *var = (PyContextVar *)ovar;

`

``

280

`+

PyContextToken *tok = (PyContextToken *)otok;

`

``

281

+

243

282

`if (tok->tok_used) {

`

244

283

`PyErr_Format(PyExc_RuntimeError,

`

245

284

`"%R has already been used once", tok);

`

`@@ -376,7 +415,7 @@ context_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)

`

376

415

`PyExc_TypeError, "Context() does not accept any arguments");

`

377

416

`return NULL;

`

378

417

` }

`

379

``

`-

return (PyObject *)PyContext_New();

`

``

418

`+

return PyContext_New();

`

380

419

`}

`

381

420

``

382

421

`static int

`

`@@ -587,14 +626,14 @@ context_run(PyContext *self, PyObject *const *args,

`

587

626

`return NULL;

`

588

627

` }

`

589

628

``

590

``

`-

if (PyContext_Enter(self)) {

`

``

629

`+

if (PyContext_Enter((PyObject *)self)) {

`

591

630

`return NULL;

`

592

631

` }

`

593

632

``

594

633

`PyObject *call_result = _PyObject_FastCallKeywords(

`

595

634

`args[0], args + 1, nargs - 1, kwnames);

`

596

635

``

597

``

`-

if (PyContext_Exit(self)) {

`

``

636

`+

if (PyContext_Exit((PyObject *)self)) {

`

598

637

`return NULL;

`

599

638

` }

`

600

639

``

`@@ -908,7 +947,7 @@ _contextvars_ContextVar_get_impl(PyContextVar *self, PyObject *default_value)

`

908

947

` }

`

909

948

``

910

949

`PyObject *val;

`

911

``

`-

if (PyContextVar_Get(self, default_value, &val) < 0) {

`

``

950

`+

if (PyContextVar_Get((PyObject *)self, default_value, &val) < 0) {

`

912

951

`return NULL;

`

913

952

` }

`

914

953

``

`@@ -937,7 +976,7 @@ static PyObject *

`

937

976

`_contextvars_ContextVar_set(PyContextVar *self, PyObject *value)

`

938

977

`/[clinic end generated code: output=446ed5e820d6d60b input=c0a6887154227453]/

`

939

978

`{

`

940

``

`-

return (PyObject *)PyContextVar_Set(self, value);

`

``

979

`+

return PyContextVar_Set((PyObject *)self, value);

`

941

980

`}

`

942

981

``

943

982

`/*[clinic input]

`

`@@ -961,7 +1000,7 @@ _contextvars_ContextVar_reset(PyContextVar *self, PyObject *token)

`

961

1000

`return NULL;

`

962

1001

` }

`

963

1002

``

964

``

`-

if (PyContextVar_Reset(self, (PyContextToken *)token)) {

`

``

1003

`+

if (PyContextVar_Reset((PyObject *)self, token)) {

`

965

1004

`return NULL;

`

966

1005

` }

`

967

1006

``