Skip to content

Commit

Permalink
Enable Cpython's test_int.py and fix related bugs
Browse files Browse the repository at this point in the history
- Skip tests that use subinterpreters
- Fix errors in string to int conversions
- Allow int() to be called on any class that implements the buffer protocol, not just memoryview
- Use the existing invalid() function to generate new int error messages
  • Loading branch information
austin-schick committed Jun 9, 2024
1 parent 6b8e3a1 commit fbd2c36
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 78 deletions.
4 changes: 3 additions & 1 deletion www/src/Lib/_testcapi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

CHAR_MAX = 127

CHAR_MIN = -128
Expand Down Expand Up @@ -192,7 +194,7 @@ def raise_memoryerror(*args,**kw):
pass

def run_in_subinterp(*args,**kw):
pass
raise unittest.SkipTest("Brython does not support subinterpreters")

def set_exc_info(*args,**kw):
pass
Expand Down
18 changes: 10 additions & 8 deletions www/src/brython.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ var pylist=['VFS_import','__future__','_aio','_codecs','_codecs_jp','_collection
for(var i=0;i < pylist.length;i++){$B.stdlib[pylist[i]]=['py']}
var js=['_ajax','_ast','_base64','_binascii','_io_classes','_json','_jsre','_locale','_multiprocessing','_posixsubprocess','_profile','_random','_sre','_sre_utils','_string','_strptime','_svg','_symtable','_tokenize','_webcomponent','_webworker','_zlib_utils','aes','array','builtins','dis','encoding_cp932','hashlib','hmac-md5','hmac-ripemd160','hmac-sha1','hmac-sha224','hmac-sha256','hmac-sha3','hmac-sha384','hmac-sha512','html_parser','marshal','math','md5','modulefinder','pbkdf2','posix','pyexpat','python_re','rabbit','rabbit-legacy','rc4','ripemd160','sha1','sha224','sha256','sha3','sha384','sha512','tripledes','unicodedata','xml_helpers','xml_parser']
for(var i=0;i < js.length;i++){$B.stdlib[js[i]]=['js']}
var pkglist=['browser','browser.widgets','collections','concurrent','concurrent.futures','email','email.mime','encodings','html','http','importlib','importlib.metadata','importlib.resources','json','logging','multiprocessing','multiprocessing.dummy','pyexpat_utils','site-packages.foobar','site-packages.simpleaio','site-packages.ui','test','test.encoded_modules','test.leakers','test.namespace_pkgs.not_a_namespace_pkg.foo','test.support','test.test_email','test.test_importlib','test.test_importlib.builtin','test.test_importlib.extension','test.test_importlib.frozen','test.test_importlib.import_','test.test_importlib.source','test.test_json','test.tracedmodules','unittest','unittest.test','unittest.test.testmock','urllib']
var pkglist=['browser','browser.widgets','collections','concurrent','concurrent.futures','email','email.mime','encodings','html','http','importlib','importlib.metadata','importlib.resources','json','logging','multiprocessing','multiprocessing.dummy','site-packages.foobar','site-packages.simpleaio','site-packages.ui','test','test.encoded_modules','test.leakers','test.namespace_pkgs.not_a_namespace_pkg.foo','test.support','test.test_email','test.test_importlib','test.test_importlib.builtin','test.test_importlib.extension','test.test_importlib.frozen','test.test_importlib.import_','test.test_importlib.source','test.test_json','test.tracedmodules','unittest','unittest.test','unittest.test.testmock','urllib']
for(var i=0;i < pkglist.length;i++){$B.stdlib[pkglist[i]]=['py',true]}
$B.stdlib_module_names=Object.keys($B.stdlib)})(__BRYTHON__)
;
Expand All @@ -180,9 +180,9 @@ $B.unicode_bidi_whitespace=[9,10,11,12,13,28,29,30,31,32,133,5760,8192,8193,8194
;
__BRYTHON__.implementation=[3,12,3,'dev',0]
__BRYTHON__.version_info=[3,12,0,'final',0]
__BRYTHON__.compiled_date="2024-06-06 09:32:21.891837"
__BRYTHON__.timestamp=1717659141891
__BRYTHON__.builtin_module_names=["_ajax","_ast","_base64","_binascii","_io_classes","_json","_jsre","_locale","_multiprocessing","_posixsubprocess","_profile","_random","_sre","_sre_utils","_string","_strptime","_svg","_symtable","_tokenize","_webcomponent","_webworker","_zlib_utils","_zlib_utils1","_zlib_utils_kozh","array","builtins","dis","encoding_cp932","encoding_cp932_v2","hashlib","html_parser","marshal","math","modulefinder","posix","pyexpat","python_re","python_re_new","unicodedata","xml_helpers","xml_parser"]
__BRYTHON__.compiled_date="2024-06-09 01:47:29.595940"
__BRYTHON__.timestamp=1717912049595
__BRYTHON__.builtin_module_names=["_ajax","_ast","_base64","_binascii","_io_classes","_json","_jsre","_locale","_multiprocessing","_posixsubprocess","_profile","_random","_sre","_sre_utils","_string","_strptime","_svg","_symtable","_tokenize","_webcomponent","_webworker","_zlib_utils","array","builtins","dis","encoding_cp932","hashlib","html_parser","marshal","math","modulefinder","posix","pyexpat","python_re","unicodedata","xml_helpers","xml_parser"]
;

(function($B){const tokens=['ENDMARKER','NAME','NUMBER','STRING','NEWLINE','INDENT','DEDENT','LPAR','RPAR','LSQB','RSQB','COLON','COMMA','SEMI','PLUS','MINUS','STAR','SLASH','VBAR','AMPER','LESS','GREATER','EQUAL','DOT','PERCENT','LBRACE','RBRACE','EQEQUAL','NOTEQUAL','LESSEQUAL','GREATEREQUAL','TILDE','CIRCUMFLEX','LEFTSHIFT','RIGHTSHIFT','DOUBLESTAR','PLUSEQUAL','MINEQUAL','STAREQUAL','SLASHEQUAL','PERCENTEQUAL','AMPEREQUAL','VBAREQUAL','CIRCUMFLEXEQUAL','LEFTSHIFTEQUAL','RIGHTSHIFTEQUAL','DOUBLESTAREQUAL','DOUBLESLASH','DOUBLESLASHEQUAL','AT','ATEQUAL','RARROW','ELLIPSIS','COLONEQUAL','EXCLAMATION','OP','AWAIT','ASYNC','TYPE_IGNORE','TYPE_COMMENT','SOFT_KEYWORD','FSTRING_START','FSTRING_MIDDLE','FSTRING_END','COMMENT','NL',
Expand Down Expand Up @@ -7558,7 +7558,8 @@ if(value===missing ||value===undefined){if(base !==missing){throw _b_.TypeError.
return 0}
if($B.$isinstance(value,[_b_.bytes,_b_.bytearray])){
value=$B.$getattr(value,'decode')('latin-1')}else if(explicit_base && ! $B.$isinstance(value,_b_.str)){throw _b_.TypeError.$factory(
"int() can't convert non-string with explicit base")}else if($B.$isinstance(value,_b_.memoryview)){value=$B.$getattr(_b_.memoryview.tobytes(value),'decode')('latin-1')}
"int() can't convert non-string with explicit base")}else{let klass=$B.get_class(value)
if(klass.$buffer_protocol){value=$B.$getattr(klass.tobytes(value),'decode')('latin-1')}}
if(! $B.$isinstance(value,_b_.str)){if(base !==missing){throw _b_.TypeError.$factory(
"int() can't convert non-string with explicit base")}else{
for(let special_method of['__int__','__index__','__trunc__']){let num_value=$B.$getattr($B.get_class(value),special_method,_b_.None)
Expand All @@ -7581,8 +7582,6 @@ return int_value(res)}}}
throw _b_.TypeError.$factory(
"int() argument must be a string, a bytes-like object "+
`or a real number, not '${$B.class_name(value)}'`)}}
if(value.length==0){throw _b_.ValueError.$factory(
`invalid literal for int() with base 10: ${_b_.repr(value)}`)}
base=base===missing ? 10:$B.PyNumber_Index(base)
if(!(base >=2 && base <=36)){
if(base !=0){throw _b_.ValueError.$factory("invalid base")}}
Expand All @@ -7594,6 +7593,7 @@ var _value=value.trim(),
sign=''
if(_value.startsWith('+')||_value.startsWith('-')){sign=_value[0]
_value=_value.substr(1)}
if(_value.length==0){invalid(base)}
if(_value.length==2 && base==0 &&
(_value=="0b" ||_value=="0o" ||_value=="0x")){throw _b_.ValueError.$factory("invalid value")}
if(_value.endsWith('_')){invalid(base)}
Expand Down Expand Up @@ -7621,7 +7621,9 @@ if(digit < base){res=$B.rich_op('__mul__',res,base)
res=$B.rich_op('__add__',res,digit)}else{invalid(base)}}
return res}else{_value=_value.replace(/_/g,"")}
if(base==2){res=BigInt('0b'+_value)}else if(base==8){res=BigInt('0o'+_value)}else if(base==16){res=BigInt('0x'+_value)}else{if($B.int_max_str_digits !=0 &&
_value.length > $B.int_max_str_digits){throw _b_.ValueError.$factory("Exceeds the limit "+
_value.length > $B.int_max_str_digits &&
((base &(base-1))!==0)
){throw _b_.ValueError.$factory("Exceeds the limit "+
`(${$B.int_max_str_digits}) for integer string conversion: `+
`value has ${value.length} digits; use `+
"sys.set_int_max_str_digits() to increase the limit.")}
Expand Down
6 changes: 3 additions & 3 deletions www/src/brython_stdlib.js

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions www/src/py_int.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,11 @@ int.$factory = function(){
}else if(explicit_base && ! $B.$isinstance(value, _b_.str)){
throw _b_.TypeError.$factory(
"int() can't convert non-string with explicit base")
}else if($B.$isinstance(value, _b_.memoryview)){
value = $B.$getattr(_b_.memoryview.tobytes(value), 'decode')('latin-1')
}else{
let klass = $B.get_class(value)
if (klass.$buffer_protocol) {
value = $B.$getattr(klass.tobytes(value), 'decode')('latin-1')
}
}

if(! $B.$isinstance(value, _b_.str)){
Expand Down Expand Up @@ -758,10 +761,6 @@ int.$factory = function(){
}
}

if(value.length == 0){
throw _b_.ValueError.$factory(
`invalid literal for int() with base 10: ${_b_.repr(value)}`)
}
base = base === missing ? 10: $B.PyNumber_Index(base)

if(! (base >=2 && base <= 36)){
Expand All @@ -788,6 +787,10 @@ int.$factory = function(){
_value = _value.substr(1)
}

if (_value.length == 0) {
invalid(base)
}

if(_value.length == 2 && base == 0 &&
(_value == "0b" || _value == "0o" || _value == "0x")){
throw _b_.ValueError.$factory("invalid value")
Expand Down Expand Up @@ -880,8 +883,9 @@ int.$factory = function(){
res = BigInt('0x' + _value)
}else{
if($B.int_max_str_digits != 0 &&
_value.length > $B.int_max_str_digits){
throw _b_.ValueError.$factory("Exceeds the limit " +
_value.length > $B.int_max_str_digits &&
((base & (base - 1)) !== 0) // base is not a power of 2
){ throw _b_.ValueError.$factory("Exceeds the limit " +
`(${$B.int_max_str_digits}) for integer string conversion: ` +
`value has ${value.length} digits; use ` +
"sys.set_int_max_str_digits() to increase the limit.")
Expand Down
24 changes: 12 additions & 12 deletions www/src/stdlib_paths.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 41 additions & 45 deletions www/src/version_info.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,41 @@
__BRYTHON__.implementation = [3, 12, 3, 'dev', 0]
__BRYTHON__.version_info = [3, 12, 0, 'final', 0]
__BRYTHON__.compiled_date = "2024-06-06 09:32:21.891837"
__BRYTHON__.timestamp = 1717659141891
__BRYTHON__.builtin_module_names = ["_ajax",
"_ast",
"_base64",
"_binascii",
"_io_classes",
"_json",
"_jsre",
"_locale",
"_multiprocessing",
"_posixsubprocess",
"_profile",
"_random",
"_sre",
"_sre_utils",
"_string",
"_strptime",
"_svg",
"_symtable",
"_tokenize",
"_webcomponent",
"_webworker",
"_zlib_utils",
"_zlib_utils1",
"_zlib_utils_kozh",
"array",
"builtins",
"dis",
"encoding_cp932",
"encoding_cp932_v2",
"hashlib",
"html_parser",
"marshal",
"math",
"modulefinder",
"posix",
"pyexpat",
"python_re",
"python_re_new",
"unicodedata",
"xml_helpers",
"xml_parser"]
__BRYTHON__.implementation = [3, 12, 3, 'dev', 0]
__BRYTHON__.version_info = [3, 12, 0, 'final', 0]
__BRYTHON__.compiled_date = "2024-06-09 01:47:29.595940"
__BRYTHON__.timestamp = 1717912049595
__BRYTHON__.builtin_module_names = ["_ajax",
"_ast",
"_base64",
"_binascii",
"_io_classes",
"_json",
"_jsre",
"_locale",
"_multiprocessing",
"_posixsubprocess",
"_profile",
"_random",
"_sre",
"_sre_utils",
"_string",
"_strptime",
"_svg",
"_symtable",
"_tokenize",
"_webcomponent",
"_webworker",
"_zlib_utils",
"array",
"builtins",
"dis",
"encoding_cp932",
"hashlib",
"html_parser",
"marshal",
"math",
"modulefinder",
"posix",
"pyexpat",
"python_re",
"unicodedata",
"xml_helpers",
"xml_parser"]
2 changes: 1 addition & 1 deletion www/tests/run_tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@

qunit_add_tests(test_utils.load_brython_test_cases('.'))

for test_name in ('test.test_syntax', 'test.test_bool'):
for test_name in ('test.test_syntax', 'test.test_int', 'test.test_bool'):
suite = defaultTestLoader.loadTestsFromName(test_name)
qunit_add_tests(suite, default_caption = 'CPython')

Expand Down

0 comments on commit fbd2c36

Please sign in to comment.