1
- # Copyright (c) 2018, 2022 , Oracle and/or its affiliates. All rights reserved.
1
+ # Copyright (c) 2018, 2023 , Oracle and/or its affiliates. All rights reserved.
2
2
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3
3
#
4
4
# The Universal Permissive License (UPL), Version 1.0
37
37
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38
38
# SOFTWARE.
39
39
40
- import sys
41
40
import re
41
+ import sys
42
42
43
- from . import CPyExtTestCase , CPyExtFunction , unhandled_error_compare , GRAALPYTHON
43
+ from . import CPyExtTestCase , CPyExtFunction , unhandled_error_compare , GRAALPYTHON , CPyExtFunctionOutVars
44
44
45
45
__dir__ = __file__ .rpartition ("/" )[0 ]
46
46
@@ -99,9 +99,10 @@ def _reference_contains(args):
99
99
raise TypeError
100
100
return args [1 ] in args [0 ]
101
101
102
+
102
103
def _reference_compare (args ):
103
104
if not isinstance (args [0 ], str ) or not isinstance (args [1 ], str ):
104
- raise TypeError
105
+ raise TypeError
105
106
106
107
if args [0 ] == args [1 ]:
107
108
return 0
@@ -110,24 +111,29 @@ def _reference_compare(args):
110
111
else :
111
112
return 1
112
113
114
+
113
115
def _reference_as_encoded_string (args ):
114
116
if not isinstance (args [0 ], str ):
115
- raise TypeError
117
+ raise TypeError
116
118
117
119
s = args [0 ]
118
120
encoding = args [1 ]
119
121
errors = args [2 ]
120
122
return s .encode (encoding , errors )
121
123
124
+
122
125
_codecs_module = None
126
+
127
+
123
128
def _reference_as_unicode_escape_string (args ):
124
129
if not isinstance (args [0 ], str ):
125
- raise TypeError
130
+ raise TypeError
126
131
global _codecs_module
127
132
if not _codecs_module :
128
133
import _codecs as _codecs_module
129
134
return _codecs_module .unicode_escape_encode (args [0 ])[0 ]
130
135
136
+
131
137
def _reference_tailmatch (args ):
132
138
if not isinstance (args [0 ], str ) or not isinstance (args [1 ], str ):
133
139
raise TypeError
@@ -141,6 +147,7 @@ def _reference_tailmatch(args):
141
147
return 1 if s [start :end ].endswith (substr ) else 0
142
148
return 1 if s [start :end ].startswith (substr ) else 0
143
149
150
+
144
151
class CustomString (str ):
145
152
pass
146
153
@@ -336,7 +343,7 @@ def compile_module(self, name):
336
343
_reference_fromformat ,
337
344
lambda : (
338
345
("word0: %s; word1: %s; int: %d; long long: %lld" , "hello" , "world" , 1234 , 1234 ),
339
- ("word0: %s; word1: %s; int: %d; long long: %lld" , "hello" , "world" , 1234 , (1 << 44 )+ 123 ),
346
+ ("word0: %s; word1: %s; int: %d; long long: %lld" , "hello" , "world" , 1234 , (1 << 44 ) + 123 ),
340
347
),
341
348
code = "typedef long long longlong_t;" ,
342
349
resultspec = "O" ,
@@ -461,6 +468,20 @@ def compile_module(self, name):
461
468
cmpfunc = unhandled_error_compare
462
469
)
463
470
471
+ test_PyUnicode_AsUTF8AndSize = CPyExtFunctionOutVars (
472
+ lambda args : (s := args [0 ].encode ("utf-8" ), len (s )),
473
+ lambda : (
474
+ ("hello" ,),
475
+ ("hellö" ,),
476
+ ),
477
+ resultspec = "yn" ,
478
+ resulttype = 'const char*' ,
479
+ argspec = 'O' ,
480
+ arguments = ["PyObject* s" ],
481
+ resultvars = ["Py_ssize_t size" ],
482
+ cmpfunc = unhandled_error_compare
483
+ )
484
+
464
485
test_PyUnicode_DecodeUTF32 = CPyExtFunction (
465
486
lambda args : args [1 ],
466
487
lambda : (
@@ -487,7 +508,7 @@ def compile_module(self, name):
487
508
test_PyUnicode_DecodeUTF8Stateful = CPyExtFunction (
488
509
lambda args : args [0 ],
489
510
lambda : (
490
- ("_type_" , ),
511
+ ("_type_" ,),
491
512
),
492
513
code = """PyObject* wrap_PyUnicode_DecodeUTF8Stateful(PyObject* _type_str) {
493
514
_Py_IDENTIFIER(_type_);
@@ -569,7 +590,8 @@ def compile_module(self, name):
569
590
test_PyUnicode_AsUnicode = CPyExtFunction (
570
591
lambda args : True ,
571
592
lambda : (
572
- ("hello" , b'\x68 \x00 \x65 \x00 \x6c \x00 \x6c \x00 \x6f \x00 ' , b"\x68 \x00 \x00 \x00 \x65 \x00 \x00 \x00 \x6c \x00 \x00 \x00 \x6c \x00 \x00 \x00 \x6f \x00 \x00 \x00 " ),
593
+ ("hello" , b'\x68 \x00 \x65 \x00 \x6c \x00 \x6c \x00 \x6f \x00 ' ,
594
+ b"\x68 \x00 \x00 \x00 \x65 \x00 \x00 \x00 \x6c \x00 \x00 \x00 \x6c \x00 \x00 \x00 \x6f \x00 \x00 \x00 " ),
573
595
),
574
596
code = """ PyObject* wrap_PyUnicode_AsUnicode(PyObject* unicodeObj, PyObject* expected_16, PyObject* expected_32) {
575
597
Py_ssize_t n = Py_UNICODE_SIZE == 2 ? PyBytes_Size(expected_16) : PyBytes_Size(expected_32);
@@ -595,7 +617,8 @@ def compile_module(self, name):
595
617
test_PyUnicode_AsUnicodeAndSize = CPyExtFunction (
596
618
lambda args : True ,
597
619
lambda : (
598
- ("hello" , b'\x68 \x00 \x65 \x00 \x6c \x00 \x6c \x00 \x6f \x00 ' , b"\x68 \x00 \x00 \x00 \x65 \x00 \x00 \x00 \x6c \x00 \x00 \x00 \x6c \x00 \x00 \x00 \x6f \x00 \x00 \x00 " ),
620
+ ("hello" , b'\x68 \x00 \x65 \x00 \x6c \x00 \x6c \x00 \x6f \x00 ' ,
621
+ b"\x68 \x00 \x00 \x00 \x65 \x00 \x00 \x00 \x6c \x00 \x00 \x00 \x6c \x00 \x00 \x00 \x6f \x00 \x00 \x00 " ),
599
622
),
600
623
code = """ PyObject* wrap_PyUnicode_AsUnicodeAndSize(PyObject* unicodeObj, PyObject* expected_16, PyObject* expected_32) {
601
624
Py_ssize_t n = Py_UNICODE_SIZE == 2 ? PyBytes_Size(expected_16) : PyBytes_Size(expected_32);
@@ -673,7 +696,7 @@ def compile_module(self, name):
673
696
arguments = ["PyObject* str" , "PyObject* seq" ],
674
697
cmpfunc = unhandled_error_compare
675
698
)
676
-
699
+
677
700
test_PyUnicode_Compare = CPyExtFunction (
678
701
_reference_compare ,
679
702
lambda : (
@@ -738,7 +761,6 @@ def compile_module(self, name):
738
761
cmpfunc = unhandled_error_compare
739
762
)
740
763
741
-
742
764
test_PyUnicode_AsEncodedString = CPyExtFunction (
743
765
_reference_as_encoded_string ,
744
766
lambda : (
@@ -760,7 +782,7 @@ def compile_module(self, name):
760
782
_reference_as_unicode_escape_string ,
761
783
lambda : (
762
784
("abcd" ,),
763
- ("öüä" ,),
785
+ ("öüä" ,),
764
786
(1 ,),
765
787
),
766
788
resultspec = "O" ,
@@ -873,5 +895,3 @@ def compile_module(self, name):
873
895
arguments = ["PyObject* string" , "PyObject* sep" , "Py_ssize_t maxsplit" ],
874
896
cmpfunc = unhandled_error_compare
875
897
)
876
-
877
-
0 commit comments