Skip to content

Commit

Permalink
Include a test case for methods with double-underscore names
Browse files Browse the repository at this point in the history
  • Loading branch information
pelson committed May 23, 2022
1 parent e082164 commit b8e09a6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion jpype/_pykeywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def pysafe(s: str) -> typing.Optional[str]:
Python that is guaranteed to not collide with the Python grammar.
"""
if s.startswith("__") and s.endswith('__'):
if s.startswith("__") and s.endswith("__") and len(s) >= 4:
# Dunder methods should not be considered safe.
# (see system defined names in the Python documentation
# https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers
Expand Down
30 changes: 30 additions & 0 deletions test/harness/jpype/attr/TestKeywords.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* ****************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
See NOTICE file for details.
**************************************************************************** */
package jpype.attr;

public class TestKeywords
{

public String __leading_double_underscore()
{
return "foo";
}

public String __dunder_name__()
{
return "foo";
}
}
2 changes: 0 additions & 2 deletions test/jpypetest/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ def setUp(self):
jpype.startJVM(jvm_path, *args,
convertStrings=self._convertStrings)
self.jpype = jpype.JPackage('jpype')
if sys.version < '3':
self.assertCountEqual = self.assertItemsEqual

def tearDown(self):
pass
Expand Down
10 changes: 10 additions & 0 deletions test/jpypetest/test_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pytest

import jpype
import common


@pytest.mark.parametrize(
Expand All @@ -29,6 +30,7 @@
# Print is no longer a keyword in Python 3, but still protected to
# avoid API breaking in JPype v1.
'print',
'____', # Not allowed.
]
)
def testPySafe__Keywords(identifier):
Expand All @@ -48,7 +50,15 @@ def testPySafe__Keywords(identifier):
'notSpecial__',
'_notSpecial_',
'_not__special_',
'__', '___', # Technically these are fine.
])
def testPySafe__NotKeywords(identifier):
safe = jpype._pykeywords.pysafe(identifier)
assert safe == identifier


class AttributeTestCase(common.JPypeTestCase):
def testPySafe(self):
cls = jpype.JPackage("jpype").attr.TestKeywords
self.assertTrue(hasattr(cls, "__leading_double_underscore"))
self.assertFalse(hasattr(cls, "__dunder_name__"))

0 comments on commit b8e09a6

Please sign in to comment.