Skip to content

Commit 78df489

Browse files
committed
Fix Python 3.7 support.
Closes #144, #147.
1 parent 5309f8d commit 78df489

16 files changed

+97
-35
lines changed

python3-sys/src/ceval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub unsafe fn PyEval_CallObject(callable: *mut PyObject, arg: *mut PyObject) ->
1919
format: *const c_char, ...)
2020
-> *mut PyObject;
2121
pub fn PyEval_CallMethod(obj: *mut PyObject,
22-
methodname: *const c_char,
22+
name: *const c_char,
2323
format: *const c_char, ...)
2424
-> *mut PyObject;
2525
pub fn PyEval_GetBuiltins() -> *mut PyObject;
@@ -51,7 +51,7 @@ pub unsafe fn PyEval_CallObject(callable: *mut PyObject, arg: *mut PyObject) ->
5151
pub fn PyEval_RestoreThread(arg1: *mut PyThreadState) -> ();
5252
}
5353

54-
#[cfg(py_sys_config = "WITH_THREAD")]
54+
#[cfg(any(Py_3_7, py_sys_config = "WITH_THREAD"))]
5555
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
5656
pub fn PyEval_ThreadsInitialized() -> c_int;
5757
pub fn PyEval_InitThreads() -> ();

python3-sys/src/descrobject.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ pub type setter =
1616
#[repr(C)]
1717
#[derive(Copy)]
1818
pub struct PyGetSetDef {
19+
#[cfg(not(Py_3_7))]
1920
pub name: *mut c_char,
21+
#[cfg(Py_3_7)]
22+
pub name: *const c_char,
2023
pub get: Option<getter>,
2124
pub set: Option<setter>,
25+
#[cfg(not(Py_3_7))]
2226
pub doc: *mut c_char,
27+
#[cfg(Py_3_7)]
28+
pub doc: *const c_char,
2329
pub closure: *mut c_void,
2430
}
2531

python3-sys/src/eval.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use object::PyObject;
44
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
55
pub fn PyEval_EvalCode(arg1: *mut PyObject, arg2: *mut PyObject,
66
arg3: *mut PyObject) -> *mut PyObject;
7-
pub fn PyEval_EvalCodeEx(co: *mut PyObject, globals: *mut PyObject,
8-
locals: *mut PyObject, args: *mut *mut PyObject,
9-
argc: c_int, kwds: *mut *mut PyObject,
10-
kwdc: c_int, defs: *mut *mut PyObject,
11-
defc: c_int, kwdefs: *mut PyObject,
12-
closure: *mut PyObject) -> *mut PyObject;
7+
pub fn PyEval_EvalCodeEx(co: *mut PyObject,
8+
globals: *mut PyObject, locals: *mut PyObject,
9+
args: *const *mut PyObject, argc: c_int,
10+
kwds: *const *mut PyObject, kwdc: c_int,
11+
defs: *const *mut PyObject, defc: c_int,
12+
kwdefs: *mut PyObject, closure: *mut PyObject) -> *mut PyObject;
1313
}

python3-sys/src/fileobject.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ pub const PY_STDIOTEXTMODE : &'static str = "b";
2121
#[cfg(Py_3_6)]
2222
pub static mut Py_FileSystemDefaultEncodeErrors: *const c_char;
2323
pub static mut Py_HasFileSystemDefaultEncoding: c_int;
24+
#[cfg(Py_3_7)]
25+
pub static mut Py_UTF8Mode: c_int;
2426
}
2527

python3-sys/src/frameobject.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ pub struct PyFrameObject {
2727
pub f_stacktop: *mut *mut PyObject,
2828
pub f_trace: *mut PyObject, /* Trace function */
2929

30+
#[cfg(not(Py_3_7))]
3031
pub f_exc_type: *mut PyObject,
32+
#[cfg(not(Py_3_7))]
3133
pub f_exc_value: *mut PyObject,
34+
#[cfg(not(Py_3_7))]
3235
pub f_exc_traceback: *mut PyObject,
3336

37+
#[cfg(Py_3_7)]
38+
pub f_trace_lines: c_char,
39+
#[cfg(Py_3_7)]
40+
pub f_trace_opcodes: c_char,
41+
3442
#[cfg(not(Py_3_4))]
3543
pub f_tstate: *mut PyThreadState,
3644

python3-sys/src/import.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use object::PyObject;
2323
cpathname: *mut PyObject)
2424
-> *mut PyObject;
2525
pub fn PyImport_GetModuleDict() -> *mut PyObject;
26+
#[cfg(Py_3_7)]
27+
pub fn PyImport_GetModule(name: *mut PyObject) -> *mut PyObject;
2628
pub fn PyImport_AddModuleObject(name: *mut PyObject) -> *mut PyObject;
2729
pub fn PyImport_AddModule(name: *const c_char) -> *mut PyObject;
2830
pub fn PyImport_ImportModule(name: *const c_char)
@@ -61,6 +63,8 @@ pub unsafe fn PyImport_ImportModuleEx(name: *const c_char,
6163
pub fn PyImport_ImportFrozenModule(name: *const c_char)
6264
-> c_int;
6365

66+
// pub static mut PyNullImporter_Type: PyTypeObject; -- does not actually exist in shared library
67+
6468
pub fn PyImport_AppendInittab(name: *const c_char,
6569
initfunc: Option<extern "C" fn() -> *mut PyObject>)
6670
-> c_int;

python3-sys/src/intrcheck.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
use libc::c_int;
22

33
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
4+
#[cfg(all(unix, Py_3_7))]
5+
pub fn PyOS_BeforeFork() -> ();
6+
#[cfg(all(unix, Py_3_7))]
7+
pub fn PyOS_AfterFork_Parent() -> ();
8+
#[cfg(all(unix, Py_3_7))]
9+
pub fn PyOS_AfterFork_Child() -> ();
10+
411
pub fn PyOS_InterruptOccurred() -> c_int;
512
pub fn PyOS_InitInterrupts() -> ();
613
pub fn PyOS_AfterFork() -> ();

python3-sys/src/methodobject.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,33 @@ pub type PyCFunction =
1616
(slf: *mut PyObject, args: *mut PyObject)
1717
-> *mut PyObject;
1818

19-
#[cfg(all(Py_3_6, not(Py_LIMITED_API)))]
19+
#[cfg(all(Py_3_6, not(Py_LIMITED_API), not(Py_3_7)))]
2020
pub type _PyCFunctionFast =
2121
unsafe extern "C" fn
2222
(slf: *mut PyObject, args: *mut *mut PyObject,
2323
nargs: ::pyport::Py_ssize_t, kwnames: *mut PyObject)
2424
-> *mut PyObject;
2525

26+
#[cfg(all(Py_3_7, not(Py_LIMITED_API)))]
27+
pub type _PyCFunctionFast =
28+
unsafe extern "C" fn(
29+
slf: *mut PyObject,
30+
args: *const *mut PyObject, nargs: ::pyport::Py_ssize_t
31+
) -> *mut PyObject;
32+
2633
pub type PyCFunctionWithKeywords =
2734
unsafe extern "C" fn
2835
(slf: *mut PyObject, args: *mut PyObject,
2936
kwds: *mut PyObject) -> *mut PyObject;
37+
38+
#[cfg(all(Py_3_7, not(Py_LIMITED_API)))]
39+
pub type _PyCFunctionFastWithKeywords =
40+
unsafe extern "C" fn(
41+
slf: *mut PyObject,
42+
args: *const *mut PyObject, nargs: ::pyport::Py_ssize_t,
43+
kwnames: *mut PyObject
44+
) -> *mut PyObject;
45+
3046
pub type PyNoArgsFunction =
3147
unsafe extern "C" fn(slf: *mut PyObject)
3248
-> *mut PyObject;

python3-sys/src/objectabstract.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_
1414
}
1515

1616
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
17-
pub fn PyObject_Call(callable_object: *mut PyObject, args: *mut PyObject,
18-
kw: *mut PyObject) -> *mut PyObject;
19-
pub fn PyObject_CallObject(callable_object: *mut PyObject,
17+
pub fn PyObject_Call(callable: *mut PyObject, args: *mut PyObject,
18+
kwargs: *mut PyObject) -> *mut PyObject;
19+
pub fn PyObject_CallObject(callable: *mut PyObject,
2020
args: *mut PyObject) -> *mut PyObject;
21-
pub fn PyObject_CallFunction(callable_object: *mut PyObject,
21+
pub fn PyObject_CallFunction(callable: *mut PyObject,
2222
format: *const c_char, ...)
2323
-> *mut PyObject;
24-
pub fn PyObject_CallMethod(o: *mut PyObject,
25-
method: *const c_char,
24+
pub fn PyObject_CallMethod(obj: *mut PyObject,
25+
name: *const c_char,
2626
format: *const c_char, ...)
2727
-> *mut PyObject;
28-
28+
2929

3030
pub fn PyObject_CallFunctionObjArgs(callable: *mut PyObject, ...)
3131
-> *mut PyObject;
32-
pub fn PyObject_CallMethodObjArgs(o: *mut PyObject,
33-
method: *mut PyObject, ...)
32+
pub fn PyObject_CallMethodObjArgs(obj: *mut PyObject,
33+
name: *mut PyObject, ...)
3434
-> *mut PyObject;
3535
pub fn PyObject_Type(o: *mut PyObject) -> *mut PyObject;
3636
pub fn PyObject_Size(o: *mut PyObject) -> Py_ssize_t;

python3-sys/src/pydebug.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use libc::c_int;
1010
pub static mut Py_OptimizeFlag: c_int;
1111
pub static mut Py_NoSiteFlag: c_int;
1212
pub static mut Py_BytesWarningFlag: c_int;
13+
#[cfg(not(Py_3_7))]
1314
pub static mut Py_UseClassExceptionsFlag: c_int;
1415
pub static mut Py_FrozenFlag: c_int;
1516
pub static mut Py_IgnoreEnvironmentFlag: c_int;
@@ -19,6 +20,8 @@ use libc::c_int;
1920
pub static mut Py_HashRandomizationFlag: c_int;
2021
#[cfg(Py_3_4)]
2122
pub static mut Py_IsolatedFlag: c_int;
23+
#[cfg(all(Py_3_7, windows))]
24+
pub static mut Py_LegacyWindowsFSEncodingFlag: c_int;
2225
#[cfg(all(Py_3_6, windows))]
2326
pub static mut Py_LegacyWindowsStdioFlag: c_int;
2427
}

python3-sys/src/pyerrors.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub unsafe fn PyExceptionInstance_Class(x: *mut PyObject) -> *mut PyObject {
5555
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
5656
pub static mut PyExc_BaseException: *mut PyObject;
5757
pub static mut PyExc_Exception: *mut PyObject;
58+
#[cfg(Py_3_5)] pub static mut PyExc_StopAsyncIteration: *mut PyObject;
5859
pub static mut PyExc_StopIteration: *mut PyObject;
5960
pub static mut PyExc_GeneratorExit: *mut PyObject;
6061
pub static mut PyExc_ArithmeticError: *mut PyObject;
@@ -66,15 +67,15 @@ pub unsafe fn PyExceptionInstance_Class(x: *mut PyObject) -> *mut PyObject {
6667
pub static mut PyExc_FloatingPointError: *mut PyObject;
6768
pub static mut PyExc_OSError: *mut PyObject;
6869
pub static mut PyExc_ImportError: *mut PyObject;
69-
#[cfg(Py_3_6)]
70-
pub static mut PyExc_ModuleNotFoundError: *mut PyObject;
70+
#[cfg(Py_3_6)] pub static mut PyExc_ModuleNotFoundError: *mut PyObject;
7171
pub static mut PyExc_IndexError: *mut PyObject;
7272
pub static mut PyExc_KeyError: *mut PyObject;
7373
pub static mut PyExc_KeyboardInterrupt: *mut PyObject;
7474
pub static mut PyExc_MemoryError: *mut PyObject;
7575
pub static mut PyExc_NameError: *mut PyObject;
7676
pub static mut PyExc_OverflowError: *mut PyObject;
7777
pub static mut PyExc_RuntimeError: *mut PyObject;
78+
#[cfg(Py_3_5)] pub static mut PyExc_RecursionError: *mut PyObject;
7879
pub static mut PyExc_NotImplementedError: *mut PyObject;
7980
pub static mut PyExc_SyntaxError: *mut PyObject;
8081
pub static mut PyExc_IndentationError: *mut PyObject;
@@ -104,11 +105,15 @@ pub unsafe fn PyExceptionInstance_Class(x: *mut PyObject) -> *mut PyObject {
104105
pub static mut PyExc_NotADirectoryError: *mut PyObject;
105106
pub static mut PyExc_PermissionError: *mut PyObject;
106107
pub static mut PyExc_ProcessLookupError: *mut PyObject;
108+
109+
// Compatibility aliases
107110
pub static mut PyExc_TimeoutError: *mut PyObject;
108111
pub static mut PyExc_EnvironmentError: *mut PyObject;
109112
pub static mut PyExc_IOError: *mut PyObject;
110113
#[cfg(windows)] pub static mut PyExc_WindowsError: *mut PyObject;
111-
pub static mut PyExc_RecursionErrorInst: *mut PyObject;
114+
#[cfg(not(Py_3_7))] pub static mut PyExc_RecursionErrorInst: *mut PyObject;
115+
116+
// Predefined warning categories
112117
pub static mut PyExc_Warning: *mut PyObject;
113118
pub static mut PyExc_UserWarning: *mut PyObject;
114119
pub static mut PyExc_DeprecationWarning: *mut PyObject;

python3-sys/src/pystate.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use libc::{c_int, c_long};
1+
use libc;
22
use object::PyObject;
33
use moduleobject::PyModuleDef;
44

55
#[cfg(Py_3_6)]
6-
pub const MAX_CO_EXTRA_USERS: c_int = 255;
6+
pub const MAX_CO_EXTRA_USERS: libc::c_int = 255;
77

88
pub enum PyInterpreterState { }
99
pub enum PyThreadState { }
@@ -12,8 +12,8 @@ pub enum PyThreadState { }
1212
pub fn PyInterpreterState_New() -> *mut PyInterpreterState;
1313
pub fn PyInterpreterState_Clear(arg1: *mut PyInterpreterState) -> ();
1414
pub fn PyInterpreterState_Delete(arg1: *mut PyInterpreterState) -> ();
15-
//fn _PyState_AddModule(arg1: *mut PyObject,
16-
// arg2: *mut PyModuleDef) -> c_int;
15+
#[cfg(Py_3_7)]
16+
pub fn PyInterpreterState_GetID(arg1: *mut PyInterpreterState) -> i64;
1717
pub fn PyState_FindModule(arg1: *mut PyModuleDef) -> *mut PyObject;
1818
pub fn PyThreadState_New(arg1: *mut PyInterpreterState)
1919
-> *mut PyThreadState;
@@ -22,13 +22,17 @@ pub enum PyThreadState { }
2222
//fn _PyThreadState_Init(arg1: *mut PyThreadState) -> ();
2323
pub fn PyThreadState_Clear(arg1: *mut PyThreadState) -> ();
2424
pub fn PyThreadState_Delete(arg1: *mut PyThreadState) -> ();
25-
#[cfg(py_sys_config="WITH_THREAD")]
25+
#[cfg(any(Py_3_7, py_sys_config="WITH_THREAD"))]
2626
pub fn PyThreadState_DeleteCurrent() -> ();
2727
pub fn PyThreadState_Get() -> *mut PyThreadState;
2828
pub fn PyThreadState_Swap(arg1: *mut PyThreadState) -> *mut PyThreadState;
2929
pub fn PyThreadState_GetDict() -> *mut PyObject;
30-
pub fn PyThreadState_SetAsyncExc(arg1: c_long,
31-
arg2: *mut PyObject) -> c_int;
30+
#[cfg(not(Py_3_7))]
31+
pub fn PyThreadState_SetAsyncExc(arg1: libc::c_long,
32+
arg2: *mut PyObject) -> libc::c_int;
33+
#[cfg(Py_3_7)]
34+
pub fn PyThreadState_SetAsyncExc(arg1: libc::c_ulong,
35+
arg2: *mut PyObject) -> libc::c_int;
3236
}
3337

3438
#[repr(C)]
@@ -38,6 +42,7 @@ pub enum PyGILState_STATE {
3842
PyGILState_UNLOCKED
3943
}
4044

45+
#[cfg(any(Py_3_7, py_sys_config="WITH_THREAD"))]
4146
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
4247
pub fn PyGILState_Ensure() -> PyGILState_STATE;
4348
pub fn PyGILState_Release(arg1: PyGILState_STATE) -> ();

python3-sys/src/pythonrun.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use pystate::PyThreadState;
66
use pyarena::PyArena;
77

88
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" { // TODO: these moved to pylifecycle.h
9-
pub fn Py_SetProgramName(arg1: *mut wchar_t) -> ();
9+
pub fn Py_SetProgramName(arg1: *const wchar_t) -> ();
1010
pub fn Py_GetProgramName() -> *mut wchar_t;
11-
pub fn Py_SetPythonHome(arg1: *mut wchar_t) -> ();
11+
pub fn Py_SetPythonHome(arg1: *const wchar_t) -> ();
1212
pub fn Py_GetPythonHome() -> *mut wchar_t;
1313
pub fn Py_Initialize() -> ();
1414
pub fn Py_InitializeEx(arg1: c_int) -> ();
@@ -18,7 +18,7 @@ use pyarena::PyArena;
1818
pub fn Py_EndInterpreter(arg1: *mut PyThreadState) -> ();
1919
}
2020

21-
// Note: PyCompilerFlags was moved to compile.h in PYthon 3.7;
21+
// Note: PyCompilerFlags was moved to compile.h in Python 3.7;
2222
// We still have our version in pythonrun.rs
2323
#[repr(C)]
2424
#[derive(Copy, Clone)]

python3-sys/src/sliceobject.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ pub unsafe fn PySlice_Check(op: *mut PyObject) -> c_int {
3333
slicelength: *mut Py_ssize_t)
3434
-> c_int;
3535

36-
#[cfg(Py_3_7)] // available since 3.6.1, but we don't have a cfg for the point releases
36+
#[cfg(Py_3_7)] // available since 3.5.4/3.6.1, but we don't have a cfg for the point releases
3737
pub fn PySlice_Unpack(slice: *mut PyObject,
3838
start: *mut Py_ssize_t, stop: *mut Py_ssize_t, step: *mut Py_ssize_t) -> c_int;
39-
#[cfg(Py_3_7)] // available since 3.6.1, but we don't have a cfg for the point releases
39+
#[cfg(Py_3_7)] // available since 3.5.4/3.6.1, but we don't have a cfg for the point releases
4040
pub fn PySlice_AdjustIndices(length: Py_ssize_t,
4141
start: *mut Py_ssize_t, stop: *mut Py_ssize_t,
4242
step: Py_ssize_t) -> Py_ssize_t;

python3-sys/src/tupleobject.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub unsafe fn PyTuple_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObjec
4747
#[inline(always)]
4848
#[cfg(not(Py_LIMITED_API))]
4949
pub unsafe fn PyTuple_GET_SIZE(op: *mut PyObject) -> Py_ssize_t {
50+
debug_assert!(PyTuple_Check(op) != 0);
5051
Py_SIZE(op)
5152
}
5253

src/pythonrun.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ pub fn prepare_freethreaded_python() {
5454
// as we can't make the existing Python main thread acquire the GIL.
5555
assert!(ffi::PyEval_ThreadsInitialized() != 0);
5656
} else {
57-
// If Python isn't initialized yet, we expect that Python threading isn't initialized either.
58-
assert!(ffi::PyEval_ThreadsInitialized() == 0);
57+
#[cfg(feature="python27-sys")] {
58+
// If Python isn't initialized yet, we expect that Python threading isn't initialized either.
59+
assert!(ffi::PyEval_ThreadsInitialized() == 0);
60+
// Note: starting with Python 3.2 it's no longer possible to initialize threading
61+
// without initializing Python; and in Python 3.7 PyEval_ThreadsInitialized() started
62+
// misbehaving when Python was not initialized yet.
63+
}
5964
// Initialize Python.
6065
// We use Py_InitializeEx() with initsigs=0 to disable Python signal handling.
6166
// Signal handling depends on the notion of a 'main thread', which doesn't exist in this case.

0 commit comments

Comments
 (0)