1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
from .support import HPyTest
class TestLong(HPyTest):
def unsigned_long_bits(self):
""" Return the number of bits in an unsigned long. """
import struct
unsigned_long_bytes = len(struct.pack('l', 0))
return 8 * unsigned_long_bytes
def magic_int(self, v):
""" Return an instance of a class that implements __int__
and returns value v.
"""
class MagicInt(object):
def __int__(self):
return v
return MagicInt()
def magic_index(self, v):
""" Return an instance of a class that implements __index__
and returns value v.
"""
class MagicIndex(object):
def __index__(self):
return v
return MagicIndex()
def python_supports_magic_index(self):
""" Return True if the Python version is 3.8 or later and thus
should support calling __index__ in the various HPyLong_As...
methods.
"""
import sys
vi = sys.version_info
return (vi.major > 3 or (vi.major == 3 and vi.minor >= 8))
def test_Long_FromLong(self):
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
long a = 500;
return HPyLong_FromLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == 500
def test_Long_AsLong(self):
import pytest
import sys
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
long a = HPyLong_AsLong(ctx, arg);
if (a == -1 && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromLong(ctx, a * 2);
}
@EXPORT(f)
@INIT
""")
assert mod.f(45) == 90
with pytest.raises(TypeError):
mod.f("this is not a number")
if sys.version_info < (3, 10):
assert mod.f(self.magic_int(2)) == 4
if self.python_supports_magic_index():
assert mod.f(self.magic_index(2)) == 4
def test_Long_FromUnsignedLong(self):
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
unsigned long a = 500;
return HPyLong_FromUnsignedLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == 500
def test_Long_AsUnsignedLong(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
unsigned long a = HPyLong_AsUnsignedLong(ctx, arg);
if ((a == (unsigned long) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromUnsignedLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(45) == 45
with pytest.raises(OverflowError):
mod.f(-91)
with pytest.raises(TypeError):
mod.f("this is not a number")
with pytest.raises(TypeError):
mod.f(self.magic_int(2))
with pytest.raises(TypeError):
mod.f(self.magic_index(2))
def test_Long_AsUnsignedLongMask(self):
import pytest
import sys
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
unsigned long a = HPyLong_AsUnsignedLongMask(ctx, arg);
if ((a == (unsigned long) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromUnsignedLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(45) == 45
assert mod.f(-1) == 2**self.unsigned_long_bits() - 1
with pytest.raises(TypeError):
mod.f("this is not a number")
if sys.version_info < (3, 10):
assert mod.f(self.magic_int(2)) == 2
if self.python_supports_magic_index():
assert mod.f(self.magic_index(2)) == 2
def test_Long_FromLongLong(self):
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
// take a value which doesn't fit in 32 bit
long long val = 2147483648;
return HPyLong_FromLongLong(ctx, val);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == 2147483648
def test_Long_AsLongLong(self):
import pytest
import sys
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
long long a = HPyLong_AsLongLong(ctx, arg);
if ((a == (long long) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromLongLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(2147483648) == 2147483648
assert mod.f(-2147483648) == -2147483648
with pytest.raises(TypeError):
mod.f("this is not a number")
if sys.version_info < (3, 10):
assert mod.f(self.magic_int(2)) == 2
if self.python_supports_magic_index():
assert mod.f(self.magic_index(2)) == 2
def test_Long_FromUnsignedLongLong(self):
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
// take a value which doesn't fit in unsigned 32 bit
unsigned long long val = 4294967296;
return HPyLong_FromUnsignedLongLong(ctx, val);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == 4294967296
def test_Long_AsUnsignedLongLong(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
unsigned long long a = HPyLong_AsUnsignedLongLong(ctx, arg);
if ((a == (unsigned long long) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromUnsignedLongLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(4294967296) == 4294967296
with pytest.raises(OverflowError):
mod.f(-4294967296)
with pytest.raises(TypeError):
mod.f("this is not a number")
with pytest.raises(TypeError):
mod.f(self.magic_int(2))
with pytest.raises(TypeError):
mod.f(self.magic_index(2))
def test_Long_AsUnsignedLongLongMask(self):
import pytest
import sys
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
unsigned long long a = HPyLong_AsUnsignedLongLongMask(ctx, arg);
if ((a == (unsigned long long) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromUnsignedLongLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(45) == 45
assert mod.f(-1) == 2**64 - 1
with pytest.raises(TypeError):
mod.f("this is not a number")
if sys.version_info < (3, 10):
assert mod.f(self.magic_int(2)) == 2
if self.python_supports_magic_index():
assert mod.f(self.magic_index(2)) == 2
def test_Long_FromSize_t(self):
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
// take a value which doesn't fit in 32 bit
size_t a = 2147483648;
return HPyLong_FromSize_t(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == 2147483648
def test_Long_AsSize_t(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
size_t a = HPyLong_AsSize_t(ctx, arg);
if ((a == (size_t) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromSize_t(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(2147483648) == 2147483648
with pytest.raises(OverflowError):
mod.f(-2147483648)
with pytest.raises(TypeError):
mod.f("this is not a number")
with pytest.raises(TypeError):
mod.f(self.magic_int(2))
with pytest.raises(TypeError):
mod.f(self.magic_index(2))
def test_Long_FromSsize_t(self):
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
HPy_ssize_t a = -42;
return HPyLong_FromSsize_t(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == -42
def test_Long_AsSsize_t(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
HPy_ssize_t a = HPyLong_AsSsize_t(ctx, arg);
if ((a == (HPy_ssize_t) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromSsize_t(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(41) == 41
assert mod.f(-41) == -41
with pytest.raises(TypeError):
mod.f("this is not a number")
with pytest.raises(TypeError):
mod.f(self.magic_int(2))
with pytest.raises(TypeError):
mod.f(self.magic_index(2))
def test_Long_AsVoidPtr(self):
mod = self.make_module("""
HPyDef_METH(f, "is_null", f_impl, HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy val)
{
void* ptr = HPyLong_AsVoidPtr(ctx, val);
if (!ptr) {
return HPy_Dup(ctx, ctx->h_True);
} else {
return HPy_Dup(ctx, ctx->h_False);
}
}
@EXPORT(f)
@INIT
""")
assert mod.is_null(0) == True
assert mod.is_null(10) == False
def test_Long_AsDouble(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
double a = HPyLong_AsDouble(ctx, arg);
if (a == -1.0 && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyFloat_FromDouble(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(45) == 45.0
with pytest.raises(TypeError):
mod.f("this is not a number")
|