Skip to content

Commit 2dc2007

Browse files
alexmalyshevfacebook-github-bot
authored andcommitted
Separate out Static Python primitive type codes
Summary: Creating a separate library to avoid upcoming dependency cycles between StaticPython/ and Jit/. Reviewed By: DinoV Differential Revision: D78560463 fbshipit-source-id: 00dfb7b27c61816140a393444ff0c0a1eb3f5726
1 parent 488ef6b commit 2dc2007

5 files changed

Lines changed: 69 additions & 49 deletions

File tree

StaticPython/type.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,6 @@ uint64_t _PyClassLoader_Unbox(PyObject* value, int primitive_type) {
167167
return new_val;
168168
}
169169

170-
int _PyClassLoader_GetTypeCode(PyTypeObject* type) {
171-
if (type->tp_cache == NULL) {
172-
return TYPED_OBJECT;
173-
}
174-
175-
return ((_PyType_VTable*)type->tp_cache)->vt_typecode;
176-
}
177-
178170
static PyObject*
179171
classloader_instantiate_generic(PyObject* gtd, PyObject* name, PyObject* path) {
180172
if (!PyType_Check(gtd)) {

StaticPython/type.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ int _PyClassLoader_PrimitiveTypeToStructMemberType(int type);
1515
PyObject* _PyClassLoader_Box(uint64_t value, int primitive_type);
1616
uint64_t _PyClassLoader_Unbox(PyObject* value, int primitive_type);
1717

18-
int _PyClassLoader_GetTypeCode(PyTypeObject* type);
19-
2018
static inline int _PyObject_TypeCheckOptional(
2119
PyObject* val,
2220
PyTypeObject* type,

StaticPython/type_code.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
#include "cinderx/StaticPython/type_code.h"
4+
5+
#include "cinderx/StaticPython/vtable.h"
6+
7+
int _PyClassLoader_GetTypeCode(PyTypeObject* type) {
8+
if (type->tp_cache == NULL) {
9+
return TYPED_OBJECT;
10+
}
11+
12+
return ((_PyType_VTable*)type->tp_cache)->vt_typecode;
13+
}

StaticPython/type_code.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
#pragma once
4+
5+
#include <Python.h>
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
#define TYPED_INT_UNSIGNED 0
12+
#define TYPED_INT_SIGNED 1
13+
14+
#define TYPED_INT_8BIT 0
15+
#define TYPED_INT_16BIT 1
16+
#define TYPED_INT_32BIT 2
17+
#define TYPED_INT_64BIT 3
18+
19+
#define TYPED_INT8 (TYPED_INT_8BIT << 1 | TYPED_INT_SIGNED)
20+
#define TYPED_INT16 (TYPED_INT_16BIT << 1 | TYPED_INT_SIGNED)
21+
#define TYPED_INT32 (TYPED_INT_32BIT << 1 | TYPED_INT_SIGNED)
22+
#define TYPED_INT64 (TYPED_INT_64BIT << 1 | TYPED_INT_SIGNED)
23+
24+
#define TYPED_UINT8 (TYPED_INT_8BIT << 1 | TYPED_INT_UNSIGNED)
25+
#define TYPED_UINT16 (TYPED_INT_16BIT << 1 | TYPED_INT_UNSIGNED)
26+
#define TYPED_UINT32 (TYPED_INT_32BIT << 1 | TYPED_INT_UNSIGNED)
27+
#define TYPED_UINT64 (TYPED_INT_64BIT << 1 | TYPED_INT_UNSIGNED)
28+
29+
// Gets one of TYPED_INT_8BIT, TYPED_INT_16BIT, etc.. from TYPED_INT8,
30+
// TYPED_UINT8, etc... also TYPED_SIZE(TYPED_BOOL) == TYPED_INT_8BIT
31+
#define TYPED_SIZE(typed_int) ((typed_int >> 1) & 3)
32+
33+
#define TYPED_OBJECT 0x08
34+
#define TYPED_DOUBLE 0x09
35+
#define TYPED_SINGLE 0x0A
36+
#define TYPED_CHAR 0x0B
37+
// must be even: TYPED_BOOL & TYPED_INT_SIGNED should be false
38+
#define TYPED_BOOL 0x0C
39+
#define TYPED_VOID 0x0D
40+
#define TYPED_STRING 0x0E
41+
#define TYPED_ERROR 0x0F
42+
43+
#define TYPED_ARRAY 0x80
44+
45+
#define _Py_IS_TYPED_ARRAY(x) (x & TYPED_ARRAY)
46+
#define _Py_IS_TYPED_ARRAY_SIGNED(x) (x & (TYPED_INT_SIGNED << 4))
47+
48+
#define Ci_METH_TYPED 0x0400
49+
50+
int _PyClassLoader_GetTypeCode(PyTypeObject* type);
51+
52+
#ifdef __cplusplus
53+
}
54+
#endif

StaticPython/typed_method_def.h

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,12 @@
44
#include <Python.h>
55

66
#include "cinderx/StaticPython/generic_type.h"
7+
#include "cinderx/StaticPython/type_code.h"
8+
79
#ifdef __cplusplus
810
extern "C" {
911
#endif
1012

11-
#define TYPED_INT_UNSIGNED 0
12-
#define TYPED_INT_SIGNED 1
13-
14-
#define TYPED_INT_8BIT 0
15-
#define TYPED_INT_16BIT 1
16-
#define TYPED_INT_32BIT 2
17-
#define TYPED_INT_64BIT 3
18-
19-
#define TYPED_INT8 (TYPED_INT_8BIT << 1 | TYPED_INT_SIGNED)
20-
#define TYPED_INT16 (TYPED_INT_16BIT << 1 | TYPED_INT_SIGNED)
21-
#define TYPED_INT32 (TYPED_INT_32BIT << 1 | TYPED_INT_SIGNED)
22-
#define TYPED_INT64 (TYPED_INT_64BIT << 1 | TYPED_INT_SIGNED)
23-
24-
#define TYPED_UINT8 (TYPED_INT_8BIT << 1 | TYPED_INT_UNSIGNED)
25-
#define TYPED_UINT16 (TYPED_INT_16BIT << 1 | TYPED_INT_UNSIGNED)
26-
#define TYPED_UINT32 (TYPED_INT_32BIT << 1 | TYPED_INT_UNSIGNED)
27-
#define TYPED_UINT64 (TYPED_INT_64BIT << 1 | TYPED_INT_UNSIGNED)
28-
29-
// Gets one of TYPED_INT_8BIT, TYPED_INT_16BIT, etc.. from TYPED_INT8,
30-
// TYPED_UINT8, etc... also TYPED_SIZE(TYPED_BOOL) == TYPED_INT_8BIT
31-
#define TYPED_SIZE(typed_int) ((typed_int >> 1) & 3)
32-
33-
#define TYPED_OBJECT 0x08
34-
#define TYPED_DOUBLE 0x09
35-
#define TYPED_SINGLE 0x0A
36-
#define TYPED_CHAR 0x0B
37-
// must be even: TYPED_BOOL & TYPED_INT_SIGNED should be false
38-
#define TYPED_BOOL 0x0C
39-
#define TYPED_VOID 0x0D
40-
#define TYPED_STRING 0x0E
41-
#define TYPED_ERROR 0x0F
42-
43-
#define TYPED_ARRAY 0x80
44-
45-
#define _Py_IS_TYPED_ARRAY(x) (x & TYPED_ARRAY)
46-
#define _Py_IS_TYPED_ARRAY_SIGNED(x) (x & (TYPED_INT_SIGNED << 4))
47-
48-
#define Ci_METH_TYPED 0x0400
49-
5013
/* Flag marks this as optional */
5114
#define Ci_Py_SIG_OPTIONAL 0x01
5215
/* Flag marks this a type param, high bits are type index */

0 commit comments

Comments
 (0)