@@ -24,20 +24,7 @@ from cpython.method cimport (
2424from cpython.bytes cimport PyBytes_FromFormat
2525
2626# from libc.stdint cimport uintptr_t
27- cdef extern from * :
28- """
29- #if PY_VERSION_HEX < 0x03040000 && defined(_MSC_VER)
30- #ifndef _MSC_STDINT_H_
31- #ifdef _WIN64 // [
32- typedef unsigned __int64 uintptr_t;
33- #else // _WIN64 ][
34- typedef _W64 unsigned int uintptr_t;
35- #endif // _WIN64 ]
36- #endif
37- #else
38- #include <stdint.h>
39- #endif
40- """
27+ cdef extern from " stdint.h" :
4128 ctypedef size_t uintptr_t
4229 cdef const Py_ssize_t PY_SSIZE_T_MAX
4330 cdef const char CHAR_MIN, CHAR_MAX
@@ -51,10 +38,7 @@ from sys import exc_info
5138
5239cdef object Mapping
5340cdef object Sequence
54- try :
55- from collections.abc import Mapping, Sequence
56- except ImportError :
57- from collections import Mapping, Sequence # Py2
41+ from collections.abc import Mapping, Sequence
5842
5943cdef object wraps
6044from functools import wraps
@@ -75,12 +59,6 @@ DEF POBJECT = b"POBJECT" # as used by LunaticPython
7559DEF LUPAOFH = b" LUPA_NUMBER_OVERFLOW_CALLBACK_FUNCTION"
7660DEF PYREFST = b" LUPA_PYTHON_REFERENCES_TABLE"
7761
78- cdef extern from * :
79- """
80- #define IS_PY2 (PY_MAJOR_VERSION == 2)
81- """
82- int IS_PY2
83-
8462cdef enum WrappedObjectFlags:
8563 # flags that determine the behaviour of a wrapped object:
8664 OBJ_AS_INDEX = 1 # prefers the getitem protocol (over getattr)
@@ -165,7 +143,7 @@ def lua_type(obj):
165143 return ' userdata'
166144 else :
167145 lua_type_name = lua.lua_typename(L, ltype)
168- return lua_type_name if IS_PY2 else lua_type_name .decode(' ascii' )
146+ return lua_type_name.decode(' ascii' )
169147 finally :
170148 lua.lua_settop(L, old_top)
171149 unlock_runtime(lua_object._runtime)
@@ -235,7 +213,7 @@ cdef class LuaRuntime:
235213 Normally, it should return the now well-behaved object that can be
236214 converted/wrapped to a Lua type. If the object cannot be precisely
237215 represented in Lua, it should raise an ``OverflowError``.
238-
216+
239217 * ``max_memory``: max memory usage this LuaRuntime can use in bytes.
240218 If max_memory is None, the default lua allocator is used and calls to
241219 ``set_max_memory(limit)`` will fail with a ``LuaMemoryError``.
@@ -656,20 +634,20 @@ cdef class LuaRuntime:
656634 luaL_openlib(L, " python" , py_lib, 0 ) # lib
657635 lua.lua_pushlightuserdata(L, < void * > self ) # lib udata
658636 lua.lua_pushcclosure(L, py_args, 1 ) # lib function
659- lua.lua_setfield(L, - 2 , " args" ) # lib
637+ lua.lua_setfield(L, - 2 , " args" ) # lib
660638
661639 # register our own object metatable
662640 lua.luaL_newmetatable(L, POBJECT) # lib metatbl
663641 luaL_openlib(L, NULL , py_object_lib, 0 )
664- lua.lua_pop(L, 1 ) # lib
642+ lua.lua_pop(L, 1 ) # lib
665643
666644 # create and store the python references table
667645 lua.lua_newtable(L) # lib tbl
668646 lua.lua_createtable(L, 0 , 1 ) # lib tbl metatbl
669647 lua.lua_pushlstring(L, " v" , 1 ) # lib tbl metatbl "v"
670648 lua.lua_setfield(L, - 2 , " __mode" ) # lib tbl metatbl
671649 lua.lua_setmetatable(L, - 2 ) # lib tbl
672- lua.lua_setfield(L, lua.LUA_REGISTRYINDEX, PYREFST) # lib
650+ lua.lua_setfield(L, lua.LUA_REGISTRYINDEX, PYREFST) # lib
673651
674652 # register global names in the module
675653 self .register_py_object(b' Py_None' , b' none' , None )
@@ -818,17 +796,14 @@ cdef tuple unpack_lua_table(LuaRuntime runtime, lua_State* L):
818796 while lua.lua_next(L, - 2 ): # key value
819797 key = py_from_lua(runtime, L, - 2 )
820798 value = py_from_lua(runtime, L, - 1 )
821- if isinstance (key, ( int , long ) ) and not isinstance (key, bool ):
799+ if isinstance (key, int ) and not isinstance (key, bool ):
822800 index = < Py_ssize_t> key
823801 if index < 1 or index > length:
824802 raise IndexError (" table index out of range" )
825803 cpython.ref.Py_INCREF(value)
826804 cpython.tuple.PyTuple_SET_ITEM(args, index- 1 , value)
827805 elif isinstance (key, bytes):
828- if IS_PY2:
829- kwargs[key] = value
830- else :
831- kwargs[(< bytes> key).decode(source_encoding)] = value
806+ kwargs[(< bytes> key).decode(source_encoding)] = value
832807 elif isinstance (key, unicode ):
833808 kwargs[key] = value
834809 else :
@@ -1508,21 +1483,14 @@ cdef object py_from_lua(LuaRuntime runtime, lua_State *L, int n):
15081483 elif lua_type == lua.LUA_TNUMBER:
15091484 if lua.LUA_VERSION_NUM >= 503 :
15101485 if lua.lua_isinteger(L, n):
1511- integer = lua.lua_tointeger(L, n)
1512- if IS_PY2 and (sizeof(lua.lua_Integer) <= sizeof(long ) or LONG_MIN <= integer <= LONG_MAX):
1513- return < long > integer
1514- else :
1515- return integer
1486+ return lua.lua_tointeger(L, n)
15161487 else :
15171488 return lua.lua_tonumber(L, n)
15181489 else :
15191490 number = lua.lua_tonumber(L, n)
15201491 integer = < lua.lua_Integer> number
15211492 if number == integer:
1522- if IS_PY2 and (sizeof(lua.lua_Integer) <= sizeof(long ) or LONG_MIN <= integer <= LONG_MAX):
1523- return < long > integer
1524- else :
1525- return integer
1493+ return integer
15261494 else :
15271495 return number
15281496 elif lua_type == lua.LUA_TSTRING:
@@ -1632,7 +1600,7 @@ cdef int py_to_lua(LuaRuntime runtime, lua_State *L, object o, bint wrap_none=Fa
16321600 elif type (o) is float :
16331601 lua.lua_pushnumber(L, < lua.lua_Number> cpython.float.PyFloat_AS_DOUBLE(o))
16341602 pushed_values_count = 1
1635- elif isinstance (o, ( long , int ) ):
1603+ elif isinstance (o, int ):
16361604 try :
16371605 lua.lua_pushinteger(L, < lua.lua_Integer> o)
16381606 pushed_values_count = 1
@@ -2013,7 +1981,7 @@ cdef void* _lua_alloc_restricted(void* ud, void* ptr, size_t old_size, size_t ne
20131981 return NULL
20141982 elif new_size == old_size:
20151983 return ptr
2016-
1984+
20171985 if memory_status.limit > 0 and new_size > old_size and memory_status.limit <= memory_status.used + new_size - old_size: # reached the limit
20181986 # print("REACHED LIMIT")
20191987 return NULL
@@ -2085,7 +2053,7 @@ cdef int py_object_gc_with_gil(py_object *py_obj, lua_State* L) noexcept with gi
20852053 return 0
20862054 finally :
20872055 py_obj.obj = NULL
2088-
2056+
20892057cdef int py_object_gc(lua_State* L) noexcept nogil:
20902058 if not lua.lua_isuserdata(L, 1 ):
20912059 return 0
0 commit comments