Skip to content

Commit

Permalink
enums obfuscate __module__
Browse files Browse the repository at this point in the history
Summary:
We lie about the `__module__` to fix a ton of issues:
- fragile tests
- customer-generated code that uses `__module__` to generate import paths.

Reviewed By: yoney

Differential Revision: D62528614

fbshipit-source-id: 6d0e502aa983254774f0f58274bafffd6f9000e7
  • Loading branch information
ahilger authored and facebook-github-bot committed Sep 12, 2024
1 parent 78af7e9 commit 7ef4555
Show file tree
Hide file tree
Showing 72 changed files with 149 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class {{enum:name}}(
{{enum_value:py_name}} = {{enum_value:value}}
{{/enum:values}}

__module__ = _fbthrift__module_name__
{{! prevent accidental treatment as py-deprecated }}
__slots__ = ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class __{{struct:name}}Type(
{{! put EMPTY = 0 arm last to overwrite any other arms}}
EMPTY = 0

__module__ = _fbthrift__module_name__
{{! prevent accidental treatment as py-deprecated }}
__slots__ = ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ from thrift.python.types import EnumMeta as __EnumMeta
import thrift.py3.types
import {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.thrift_metadata

_fbthrift__module_name__ = "{{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.types"

{{> types/enum_py }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import module.thrift_metadata

_fbthrift__module_name__ = "module.types"



Expand All @@ -21,6 +22,7 @@ class MyEnum(
MyValue2 = 1
DOMAIN = 2

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import test.fixtures.enumstrict.module.thrift_metadata

_fbthrift__module_name__ = "test.fixtures.enumstrict.module.types"



Expand All @@ -18,6 +19,7 @@ class EmptyEnum(
metaclass=__EnumMeta,
):

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -51,6 +53,7 @@ class MyEnum(
ONE = 1
TWO = 2

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -85,6 +88,7 @@ class MyUseIntrinsicDefaultEnum(
ONE = 1
TWO = 2

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -136,6 +140,7 @@ class MyBigEnum(
EIGHTEEN = 18
NINETEEN = 19

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import module.thrift_metadata

_fbthrift__module_name__ = "module.types"



Expand All @@ -20,6 +21,7 @@ class MyEnum(
MyValue1 = 0
MyValue2 = 1

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import test.fixtures.basic.module.thrift_metadata

_fbthrift__module_name__ = "test.fixtures.basic.module.types"



Expand All @@ -20,6 +21,7 @@ class MyEnum(
MyValue1 = 0
MyValue2 = 1

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -53,6 +55,7 @@ class HackEnum(
Value1 = 0
Value2 = 1

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -90,6 +93,7 @@ class __MyUnionType(
floatSet = 4
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()


Expand All @@ -100,5 +104,6 @@ class __UnionToBeRenamedType(
reserved_field = 1
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import module.thrift_metadata

_fbthrift__module_name__ = "module.types"



Expand All @@ -26,6 +27,7 @@ class __ComplexUnionType(
stringRef = 14
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()


Expand All @@ -37,6 +39,7 @@ class __ListUnionType(
stringListValue = 3
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()


Expand All @@ -48,6 +51,7 @@ class __DataUnionType(
stringData = 2
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()


Expand All @@ -59,6 +63,7 @@ class __ValUnionType(
v2 = 2
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()


Expand All @@ -70,6 +75,7 @@ class __VirtualComplexUnionType(
thingTwo = 2
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()


Expand All @@ -80,5 +86,6 @@ class __NonCopyableUnionType(
s = 1
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import module.thrift_metadata

_fbthrift__module_name__ = "module.types"



Expand All @@ -18,6 +19,7 @@ class EmptyEnum(
metaclass=__EnumMeta,
):

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -53,6 +55,7 @@ class City(
SEA = 2
LON = 3

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -88,6 +91,7 @@ class Company(
OCULUS = 2
INSTAGRAM = 3

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -123,6 +127,7 @@ class __union1Type(
d = 2
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()


Expand All @@ -136,5 +141,6 @@ class __union2Type(
u = 4
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import module.thrift_metadata

_fbthrift__module_name__ = "module.types"



Expand All @@ -20,5 +21,6 @@ class __NadaType(
):
EMPTY = 0

__module__ = _fbthrift__module_name__
__slots__ = ()

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import test.fixtures.enums.module.thrift_metadata

_fbthrift__module_name__ = "test.fixtures.enums.module.types"



Expand All @@ -22,6 +23,7 @@ class Metasyntactic(
BAZ = 3
BAX = 4

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -59,6 +61,7 @@ class MyEnum1(
ME1_5 = 5
ME1_6 = 6

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -93,6 +96,7 @@ class MyEnum2(
ME2_1 = 1
ME2_2 = 2

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -130,6 +134,7 @@ class MyEnum3(
ME3_9 = 9
ME3_10 = 10

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -165,6 +170,7 @@ class MyEnum4(
ME4_C = 2147483647
ME4_D = -2147483648

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -199,6 +205,7 @@ class MyBitmaskEnum1(
TWO = 2
FOUR = 4

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -233,6 +240,7 @@ class MyBitmaskEnum2(
TWO = 2
FOUR = 4

__module__ = _fbthrift__module_name__
__slots__ = ()

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import module.thrift_metadata

_fbthrift__module_name__ = "module.types"



Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import includes.thrift_metadata

_fbthrift__module_name__ = "includes.types"



Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import module.thrift_metadata

_fbthrift__module_name__ = "module.types"



Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import transitive.thrift_metadata

_fbthrift__module_name__ = "transitive.types"



Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import includes.thrift_metadata

_fbthrift__module_name__ = "includes.types"



Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import transitive.thrift_metadata

_fbthrift__module_name__ = "transitive.types"



Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import includes.thrift_metadata

_fbthrift__module_name__ = "includes.types"



Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import module.thrift_metadata

_fbthrift__module_name__ = "module.types"



Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import service.thrift_metadata

_fbthrift__module_name__ = "service.types"



Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import thrift.py3.types
import transitive.thrift_metadata

_fbthrift__module_name__ = "transitive.types"



Loading

0 comments on commit 7ef4555

Please sign in to comment.